001: ///////////////////////////////
002: // Makumba, Makumba tag library
003: // Copyright (C) 2000-2003 http://www.makumba.org
004: //
005: // This library is free software; you can redistribute it and/or
006: // modify it under the terms of the GNU Lesser General Public
007: // License as published by the Free Software Foundation; either
008: // version 2.1 of the License, or (at your option) any later version.
009: //
010: // This library is distributed in the hope that it will be useful,
011: // but WITHOUT ANY WARRANTY; without even the implied warranty of
012: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: // Lesser General Public License for more details.
014: //
015: // You should have received a copy of the GNU Lesser General Public
016: // License along with this library; if not, write to the Free Software
017: // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: //
019: // -------------
020: // $Id: ValueComputer.java 1394 2007-07-20 11:37:36Z manuel_gay $
021: // $Name$
022: /////////////////////////////////////
023:
024: package org.makumba.list.engine.valuecomputer;
025:
026: import javax.servlet.jsp.JspException;
027:
028: import org.makumba.FieldDefinition;
029: import org.makumba.LogicException;
030: import org.makumba.analyser.AnalysableTag;
031: import org.makumba.analyser.PageCache;
032: import org.makumba.commons.MultipleKey;
033: import org.makumba.commons.attributes.PageAttributes;
034: import org.makumba.commons.formatters.RecordFormatter;
035: import org.makumba.list.engine.ComposedQuery;
036: import org.makumba.list.engine.QueryExecution;
037: import org.makumba.list.html.RecordViewer;
038: import org.makumba.list.tags.QueryTag;
039: import org.makumba.list.tags.ValueTag;
040:
041: /**
042: * Every ValueTag will build a ValueComputer at page analysis, which it then retrieves and uses at page running
043: *
044: * @author Cristian Bogdan
045: * @version $Id
046: */
047: public class ValueComputer {
048:
049: /**
050: * Determines if 'analyzed' is a queryMak:value or a nonQueryMak:value
051: *
052: * @param analyzed
053: * the analyzed tag
054: * @param parentListKey
055: * the key of the parent list
056: * @param expr
057: * the expression passed in the tag
058: * @param pageCache
059: * the page cache of the page
060: */
061: public static ValueComputer getValueComputerAtAnalysis(
062: AnalysableTag analyzed, MultipleKey parentListKey,
063: String expr, PageCache pageCache) {
064: expr = expr.trim();
065: Object check = QueryTag.getQuery(pageCache, parentListKey)
066: .checkExprSetOrNullable(expr);
067:
068: FieldDefinition set = null;
069: String nullableExpr = null;
070:
071: if (check instanceof String)
072: nullableExpr = (String) check;
073:
074: if (check instanceof FieldDefinition)
075: set = (FieldDefinition) check;
076:
077: if (nullableExpr == null && set == null)
078: return new ValueComputer(analyzed, parentListKey, expr,
079: pageCache);
080:
081: if (set == null)
082: return new NullableValueComputer(analyzed, parentListKey,
083: nullableExpr, expr, pageCache);
084: return new SetValueComputer(analyzed, parentListKey, set, expr,
085: pageCache);
086: }
087:
088: /** The key of the parentList */
089: MultipleKey parentKey;
090:
091: /** The queryProjection index in the currentListData */
092: int projectionIndex;
093:
094: /** The queryProjection expression */
095: String expr;
096:
097: /** The queryProjection type */
098: protected FieldDefinition type;
099:
100: ValueComputer() {
101: }
102:
103: /**
104: * A special ValueComputer made by mak:lists who want to select extra expressions
105: *
106: * @param listKey
107: * the key of the list
108: * @param parentListKey
109: * the key of the parent list
110: * @param expr
111: * the extra expression
112: * @param pageCache
113: * the page cache
114: */
115: ValueComputer(MultipleKey listKey, String expr, PageCache pageCache) {
116: parentKey = listKey;
117: this .expr = expr;
118: QueryTag.getQuery(pageCache, parentKey).checkProjectionInteger(
119: expr);
120:
121: }
122:
123: /**
124: * A nonQueryMak:value value computer
125: *
126: * @param analyzed
127: * the analyzed tag
128: * @param expr
129: * the expression of the tag
130: * @param pageCache
131: * the page cache
132: */
133: ValueComputer(AnalysableTag analyzed, MultipleKey parentListKey,
134: String expr, PageCache pageCache) {
135: this (parentListKey, expr, pageCache);
136: }
137:
138: /**
139: * The key of the query in which this value is a projection.
140: *
141: * @return The key of the parent of the value
142: */
143: MultipleKey getQueryKey() {
144: return parentKey;
145: }
146:
147: /**
148: * Computes the queryProjection index in the currentListData, and the queryProjection type.
149: *
150: * @param pageCache
151: * the page cache
152: */
153: public void doEndAnalyze(PageCache pageCache) {
154: ComposedQuery q = QueryTag.getQuery(pageCache, getQueryKey());
155: projectionIndex = q.checkProjectionInteger(expr).intValue();
156:
157: if (type == null) // if type is not set in the constructor
158: type = q.getResultType()
159: .getFieldDefinition(projectionIndex);
160: }
161:
162: /**
163: * Gets the value of from the QueryExecution based on the projectionIndex
164: *
165: * @param pc
166: * the page context
167: * @return the computed value
168: * @throws LogicException
169: */
170: public Object getValue(javax.servlet.jsp.PageContext pc)
171: throws LogicException {
172: return QueryExecution.getFor(getQueryKey(), pc, null, null)
173: .currentListData().data[projectionIndex];
174: }
175:
176: /**
177: * Formats the value of the queryProjection from the currentListData of the enclosing query. Sets the var and the
178: * printVar values.
179: *
180: * @param running
181: * the tag that is currently running
182: * @param pageCache
183: * the page cache of the current page
184: * @throws JspException
185: * @throws LogicException
186: */
187: public void print(ValueTag running, PageCache pageCache)
188: throws JspException, LogicException {
189: Object o = getValue(running.getPageContext());
190: String s = null;
191: if (running.getPrintVar() != null || running.getVar() == null) {
192: s = ((RecordViewer) pageCache.retrieve(
193: RecordFormatter.FORMATTERS, getQueryKey())).format(
194: projectionIndex, o, running.getParams());
195: }
196:
197: if (running.getVar() != null)
198: PageAttributes.setAttribute(running.getPageContext(),
199: running.getVar(), o);
200: if (running.getPrintVar() != null)
201: running.getPageContext().setAttribute(
202: running.getPrintVar(), s);
203: if (running.getPrintVar() == null && running.getVar() == null) {
204: try {
205: running.getPageContext().getOut().print(s);
206: } catch (Exception e) {
207: throw new JspException(e.toString());
208: }
209: }
210: }
211:
212: public FieldDefinition getType() {
213: return type;
214: }
215: }
|