/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package reportgen.math;
import reportgen.utils.SupportXMLRootImpl;
import reportgen.math.agregate.agregate.AggregateFunction;
import org.jdom.Element;
import reportgen.prototype.context.Context;
import reportgen.utils.ReportException;
/**
* @author axe
*/
public class ResultColumn {
public static final String TYPE = "column";
public static final String ATTR_GROUP = "group";
public static final String ATTR_TITLE = "title";
public static final String ATTR_ORDERPRIORITY = "orderpriority";
private String colTitle;
private AggregateFunction function = AggregateFunction.ASIS;
private int orderPriority = 0;
public ResultColumn() {
}
public ResultColumn(String title, AggregateFunction grFunc, int orderPriority) {
this.colTitle = title;
this.function = grFunc;
this.orderPriority = orderPriority;
}
public ResultColumn(Element element, Context context, Class cls) throws ReportException {
colTitle = SupportXMLRootImpl.getStringAttribute(element, ATTR_TITLE);
if (colTitle.length() == 0) {
throw new ReportException("Пустой заголовок результирующей колонки");
}
String funcName = SupportXMLRootImpl.getStringAttribute(element, ATTR_GROUP, null, false);
if(funcName == null) {
function = AggregateFunction.ASIS;
} else {
function = context.getAggregFunction(funcName, cls);
}
orderPriority = SupportXMLRootImpl.getIntAttribute(element, ATTR_ORDERPRIORITY, 0, false);
}
public void toXML(Element root) {
root.setAttribute(ATTR_TITLE, colTitle);
if(function != AggregateFunction.ASIS) {
//QueryGroupFunction.ASIS - default
root.setAttribute(ATTR_GROUP, function.getMnemonic());
}
if(orderPriority != 0) {
//0 - default
root.setAttribute(ATTR_ORDERPRIORITY, new Integer(orderPriority).toString());
}
}
/**
* Get the value of colTitle
*
* @return the value of colTitle
*/
public String getColTitle() {
return colTitle;
}
/**
* Set the value of colTitle
*
* @param colTitle new value of colTitle
*/
public void setColTitle(String colTitle) {
this.colTitle = colTitle;
}
public AggregateFunction getFunction() {
return function;
}
public void setFunction(AggregateFunction function) {
assert function != null;
this.function = function;
}
public int getOrderPriority() {
return orderPriority;
}
public void setOrderPriority(int orderPriority) {
this.orderPriority = orderPriority;
}
public void validate() throws ReportException {
if(colTitle == null || colTitle.length()==0) {
throw new ReportException("Колонка результата должна иметь уникальный заголовок");
}
}
}