001: package org.makumba.list.engine.valuecomputer;
002:
003: import java.util.Vector;
004:
005: import javax.servlet.jsp.JspException;
006: import javax.servlet.jsp.PageContext;
007:
008: import org.makumba.FieldDefinition;
009: import org.makumba.LogicException;
010: import org.makumba.analyser.AnalysableTag;
011: import org.makumba.analyser.PageCache;
012: import org.makumba.commons.MultipleKey;
013: import org.makumba.commons.attributes.PageAttributes;
014: import org.makumba.list.engine.ComposedQuery;
015: import org.makumba.list.engine.QueryExecution;
016: import org.makumba.list.tags.QueryTag;
017: import org.makumba.list.tags.ValueTag;
018:
019: /**
020: * The manager of a setValueQuery.
021: *
022: * @author Cristian Bogdan
023: */
024: public class SetValueComputer extends QueryValueComputer {
025: /** If we are in a value tag, the name of the queryProjection that computes the title field, otherwise null */
026: String name = null;
027:
028: /** If we are in a value tag, the index of the queryProjection that computes the title field, otherwise null */
029: int nameIndex;
030:
031: /**
032: * Makes a query that has an extra FROM: the set requested. As projections, add the key of the set type and, if we
033: * are in a value tag, the title field.
034: *
035: * @param analyzed
036: * the tag that is analyzed
037: * @param parentListKey
038: * the key of the parent list
039: * @param set
040: * the FieldDefinition of the set we want to compute a value of
041: * @param setExpr
042: * the expression of the set
043: * @param pageCache
044: * the page cache of the current page
045: */
046: SetValueComputer(AnalysableTag analyzed, MultipleKey parentListKey,
047: FieldDefinition set, String setExpr, PageCache pageCache) {
048: type = set;
049: String label = setExpr.replace('.', '_');
050: String queryProps[] = new String[5];
051: queryProps[ComposedQuery.FROM] = setExpr + " " + label;
052:
053: if (analyzed instanceof ValueTag) {
054: name = label + "."
055: + set.getForeignTable().getTitleFieldName();
056: queryProps[ComposedQuery.ORDERBY] = name;
057: }
058:
059: makeQueryAtAnalysis(parentListKey, set.getName(), queryProps,
060: label, pageCache);
061:
062: if (analyzed instanceof ValueTag)
063: QueryTag.getQuery(pageCache, queryKey)
064: .checkProjectionInteger(name);
065: }
066:
067: /**
068: * Computes nameIndex
069: * @param pageCache
070: * the page cache of the current page
071: */
072: @Override
073: public void doEndAnalyze(PageCache pageCache) {
074: super .doEndAnalyze(pageCache);
075: if (name != null)
076: nameIndex = QueryTag.getQuery(pageCache, queryKey)
077: .checkProjectionInteger(name).intValue();
078: }
079:
080: /**
081: * Goes through the iterationGroupData and returns a vector with the set values. Used only by InputTag
082: *
083: * @param running
084: * the tag that is currently running
085: * @throws LogicException
086: */
087: @Override
088: public Object getValue(PageContext pageContext)
089: throws LogicException {
090: QueryExecution ex = runQuery(pageContext);
091: int n = ex.dataSize();
092: Vector v = new Vector();
093:
094: for (ex.iteration = 0; ex.iteration < n; ex.iteration++)
095: v.addElement(ex.currentListData().data[projectionIndex]);
096: return v;
097: }
098:
099: /**
100: * Goes through the iterationGroupData and prints the set values, comma-separated; also sets var (Vector with the
101: * set values) and printVar
102: *
103: * @param running
104: * the tag that is currently running
105: * @param pageCache
106: * the pageCache of the current page
107: * @throws JspException
108: * @throws LogicException
109: */
110: @Override
111: // FIXME (fred) shouldn't the formatting be in view.html package, instead of here?
112: public void print(ValueTag running, PageCache pageCache)
113: throws JspException, LogicException {
114: QueryExecution ex = runQuery(running.getPageContext());
115: int n = ex.dataSize();
116: Vector v = null;
117:
118: if (running.getVar() != null)
119: v = new Vector();
120:
121: String sep = "";
122: StringBuffer print = new StringBuffer();
123: for (ex.iteration = 0; ex.iteration < n; ex.iteration++) {
124: print.append(sep);
125: sep = ",";
126: if (running.getVar() != null)
127: v
128: .addElement(ex.currentListData().data[projectionIndex]);
129: print.append(ex.currentListData().data[nameIndex]);
130: }
131: String s = print.toString();
132:
133: // replace by 'default' or 'empty' if necessary
134: if (n == 0 && running.getParams().get("default") != null)
135: s = (String) running.getParams().get("default");
136:
137: if (s.length() == 0 && running.getParams().get("empty") != null)
138: s = (String) running.getParams().get("empty");
139:
140: if (running.getVar() != null)
141: PageAttributes.setAttribute(running.getPageContext(),
142: running.getVar(), v);
143: if (running.getPrintVar() != null)
144: running.getPageContext().setAttribute(
145: running.getPrintVar(), s);
146: if (running.getPrintVar() == null && running.getVar() == null) {
147: try {
148: running.getPageContext().getOut().print(s);
149: } catch (Exception e) {
150: throw new JspException(e.toString());
151: }
152: }
153: }
154: }
|