/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package reportgen.cores.ejb;
import java.util.HashSet;
import reportgen.prototype.entity.QEntityProperty;
import reportgen.prototype.columns.CoreColumnList;
import reportgen.prototype.columns.QueryResultColumn;
import reportgen.prototype.context.Context;
import reportgen.prototype.entity.QSQLBuilder;
import reportgen.prototype.entity.QSQLProcessor;
import reportgen.prototype.inline.InlineCoreSQLList;
import reportgen.utils.ReportException;
import java.util.Set;
import org.jdom.Element;
import reportgen.math.complex.conditions.MathExpressionConditions;
import reportgen.prototype.QCore;
import reportgen.prototype.UsedReportableType;
import reportgen.prototype.inline.InlineCoreJavaList;
import reportgen.prototype.utils.RowCount;
/**
* Объект - запрос
* содержит:
* - ссылку на корневой обект сущность
* - список всех условий выборки
* - список всех полей выборки
* @author axe
*/
class Core extends AbstractCore implements QCore {
private final static String CONDITIONS_POST = "postcond";
//private final InlineCoreJavaList resultInJava;
private final MathExpressionConditions postConditions;
final CoreColumnList columns;
private RowCount rowCount;
public Core(String title, Context context) {
super(title, context);
this.rowCount = new RowCount();
this.columns = new CoreColumnList(this);
this.postConditions = new MathExpressionConditions(getPostProcessContext());
//this.resultInJava = new InlinesConditionList(this, getPostProcessContext());
}
public Core(Element root, Context acontext) throws ReportException {
super(root, acontext);
rowCount = new RowCount(root);
columns = new CoreColumnList(this, root, getResultContext());
Context ctxpp = getPostProcessContext();
//resultInJava = new InlinesConditionList(this, root, ctxpp);
MathExpressionConditions conditions = new MathExpressionConditions(ctxpp);
Element condsPart2 = root.getChild(Core.CONDITIONS_POST);
if(condsPart2 != null) {
Element condElement = condsPart2.getChild(MathExpressionConditions.TAG);
if(condElement != null) {
conditions = new MathExpressionConditions(condElement, ctxpp);
}
}
postConditions = conditions;
}
/**
*
* @return
*/
@Override
protected void toXML(Element xmlRoot) {
super.toXML(xmlRoot);
rowCount.toXML(xmlRoot);
Element colsel = columns.toXML();
if(colsel != null) {
xmlRoot.addContent(colsel);
}
/*
Element inlineEl = resultInJava.toXML();
if(inlineEl != null) {
xmlRoot.addContent(inlineEl);
}*/
if(!postConditions.isEmpty()) {
Element post = new Element(CONDITIONS_POST);
post.addContent(postConditions.toXML());
xmlRoot.addContent(post);
}
}
@Override
public RowCount getRowCount() {
return rowCount;
}
@Override
public void setRowCount(RowCount rowCount) {
this.rowCount = rowCount;
}
@Override
public InlineCoreJavaList getResultInJavaList() {
return null;
}
@Override
public InlineCoreSQLList getResultInlinesList() {
//not supported by this engine
return null;
}
@Override
public CoreColumnList getColumns() {
return columns;
}
@Override
public MathExpressionConditions getPostConditions() {
return postConditions;
}
@Override
public QSQLProcessor getSQLProcessor() {
QSQLBuilder builder = root.getSQLBuilder(getLoadable());
return new SQLProcessor(builder, isDistinct(), getConditions());
}
@Override
protected Set<QEntityProperty> getLoadable() {
HashSet<QEntityProperty> res = new HashSet<QEntityProperty>();
for (QueryResultColumn col: getColumns().getList()) {
col.buildUsedSet(UsedReportableType.property, res);
}
return res;
}
@Override
public void ensureSafeRemove(Object obj2remove) throws ReportException {
super.ensureSafeRemove(obj2remove);
if (obj2remove instanceof QueryResultColumn == false
&& columns.isContain(obj2remove)) {
throw new ReportException("Нельзя удалить этот элемент, ссылка на него содержится в "
+ "результатах выборки");
}
if (postConditions.isContain(obj2remove)) {
throw new ReportException("Нельзя удалить этот элемент, ссылка на него содержится в "
+ "постусловиях выборки");
}
}
@Override
public void buildUsedSet(UsedReportableType cls, Set set) {
super.buildUsedSet(cls, set);
postConditions.buildUsedSet(cls, set);
for(int i=0; i<columns.size(); i++) {
columns.get(i).buildUsedSet(cls, set);
}
/*
for(QCoreInlineSQL core: resultInJava.getList()) {
core.buildUsedSet(cls, set);
}*/
}
/**
*
* @throws reportgen.ren.exception.ReportException
*/
@Override
public void validate() throws ReportException {
super.validate();
columns.validate();
postConditions.validate();
}
@Override
public Context getResultContext() {
return new ContextCoreResult(this, getParentContext());
}
public Context getPostProcessContext() {
return new ContextCore2Stage(this, getParentContext());
}
}