/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package reportgen.math.reference.field;
import java.util.Map;
import reportgen.prototype.stream.SQLStream;
import reportgen.utils.ReportException;
import java.util.Set;
import org.jdom.Element;
import reportgen.math.MathExpressionOperand;
import reportgen.prototype.UsedReportableType;
import reportgen.prototype.context.group.ContextGroup;
import reportgen.prototype.context.Context;
import reportgen.prototype.entity.QEntity;
import reportgen.prototype.entity.QEntityProperty;
import reportgen.utils.Atom;
/**
*
* @author axe
*/
public class MathExpressionEntityFieldRef extends MathExpressionOperand {
public static final FieldContextGroup GROUP = new FieldContextGroup();
public static final String TAG = "field";
public static final String ATTR_VALUE = "value";
private QEntityProperty property;
public MathExpressionEntityFieldRef(QEntityProperty property, Context context) {
super(context);
this.property = property;
}
public MathExpressionEntityFieldRef(Element element, Context context) throws ReportException {
super(element, context);
String fullname = getStringAttribute(element, ATTR_VALUE);
int aliaspos = fullname.indexOf('.');
Atom atom = new Atom(fullname.substring(0, aliaspos));
QEntity entity = context.getEntity(atom);
String propertyName = fullname.substring(aliaspos+1);
ContextGroup contextGroup = getContextGroup(entity.getContextGroup());
property = context.getProperty(contextGroup, entity, propertyName);
}
@Override
protected void toXML(Element root) {
String mnemonic = property.getEntity().getAtom() + "." + property.getIdentification();
root.setAttribute(ATTR_VALUE, mnemonic);
}
@Override
public MathExpressionEntityFieldRef makeClone() throws ReportException {
return new MathExpressionEntityFieldRef(toXML(), getParentContext());
}
@Override
public FieldContextGroup getContextGroup() {
return getContextGroup(property.getEntity().getContextGroup());
}
public static FieldContextGroup getContextGroup(ContextGroup coreGroup) {
return (FieldContextGroup) GROUP.clone(coreGroup);
}
@Override
protected String getRootTag() {
return TAG;
}
@Override
public Class getCls() {
return getRef().getCls();
}
public QEntityProperty getRef() {
return property;
}
public void setRef(QEntityProperty property) {
this.property = property;
}
@Override
public String toString() {
return property.toString();
}
@Override
public void validate() throws ReportException {
if(property == null) {
throw new ReportException("Не выбрано свойство объекта");
}
}
@Override
public boolean isContain(Object entity) {
if(property == entity
|| property.getEntity() == entity) {
return true;
}
return super.isContain(entity);
}
@Override
public void appendToQuery(SQLStream sql, Map model) {
sql.append(property.getAlias());
}
@Override
public Object getValue(Map constants) throws ReportException {
return constants.get(property);
}
@Override
public void buildUsedSet(UsedReportableType cls, Set set) {
if(cls == UsedReportableType.property) {
set.add(property);
}
}
}