001: /*
002: * Copyright 2001-2006 C:1 Financial Services GmbH
003: *
004: * This software is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License Version 2.1, as published by the Free Software Foundation.
007: *
008: * This software is distributed in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
011: * Lesser General Public License for more details.
012: *
013: * You should have received a copy of the GNU Lesser General Public
014: * License along with this library; if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
016: */
017:
018: package de.finix.contelligent.components.query;
019:
020: import java.io.IOException;
021: import java.io.StringWriter;
022: import java.io.Writer;
023: import java.util.Collection;
024: import java.util.Collections;
025: import java.util.Iterator;
026: import java.util.Map;
027:
028: import de.finix.contelligent.AbstractComponent;
029: import de.finix.contelligent.CallData;
030: import de.finix.contelligent.Component;
031: import de.finix.contelligent.ComponentManager;
032: import de.finix.contelligent.ComponentPath;
033: import de.finix.contelligent.content.ContentProvider;
034: import de.finix.contelligent.content.Metadata;
035: import de.finix.contelligent.exception.ContelligentException;
036: import de.finix.contelligent.logging.LoggingService;
037: import de.finix.contelligent.render.DefaultRenderer;
038: import de.finix.contelligent.render.ParameterDescription;
039: import de.finix.contelligent.render.Renderable;
040: import de.finix.contelligent.render.Renderer;
041:
042: public class QueryResultEntry extends AbstractComponent implements
043: Renderable, Renderer {
044: final static private org.apache.log4j.Logger log = LoggingService
045: .getLogger(QueryResultEntry.class);
046:
047: private String key;
048:
049: private boolean querySubcomponent = false;
050:
051: private boolean followLinks = false;
052:
053: private String defaultResult;
054:
055: private ParameterDescription[] parameters = new ParameterDescription[0];
056:
057: public boolean getQuerySubcomponent() {
058: return querySubcomponent;
059: }
060:
061: public void setQuerySubcomponent(boolean querySubcomponent) {
062: this .querySubcomponent = querySubcomponent;
063: }
064:
065: public boolean getFollowLinks() {
066: return this .followLinks;
067: }
068:
069: public void setFollowLinks(boolean followLinks) {
070: this .followLinks = followLinks;
071: }
072:
073: public String getKey() {
074: return key;
075: }
076:
077: public void setKey(String key) {
078: this .key = key;
079: }
080:
081: public String getDefaultResult() {
082: return defaultResult;
083: }
084:
085: public void setDefaultResult(String defaultResult) {
086: this .defaultResult = defaultResult;
087: }
088:
089: public ParameterDescription[] getParameterDescription() {
090: return parameters;
091: }
092:
093: public Collection getSensitiveCategories() {
094: return Collections.EMPTY_SET;
095: }
096:
097: public Renderer getRenderer() {
098: return this ;
099: }
100:
101: public String evaluate(QueryResult result, CallData callData)
102: throws ContelligentException, IOException {
103: String renderedEntry = null;
104: String localKey = getKey();
105: String subKey = null;
106: int index = localKey.indexOf('#');
107: if (index != -1) {
108: subKey = localKey.substring(index + 1);
109: localKey = localKey.substring(0, index);
110: }
111: ComponentManager manager = callData.getActualManager();
112: if (getQuerySubcomponent()
113: && result instanceof ComponentQueryResult) {
114: ComponentPath childPath = ((ComponentQueryResult) result)
115: .getComponentPath();
116: Component child = null;
117: if (localKey.trim().equals(".")
118: || localKey.trim().length() == 0) {
119: child = manager.getComponent(childPath, callData);
120: } else {
121: ComponentPath qPath = new ComponentPath(localKey);
122: if (getFollowLinks()) {
123: // here we have to resolve two components: The base query
124: // result and the subcomponent, since both might be Links
125: child = manager.getComponent(childPath, callData);
126: childPath = child.getComponentContext().getPath();
127: child = manager.getComponent(childPath
128: .append(qPath), callData, true);
129: } else {
130: // the quick way, but without link resolving
131: child = manager.getComponent(childPath
132: .append(qPath), callData);
133: }
134: }
135: if (subKey == null) {
136: if (child instanceof Renderable) {
137: Writer childWriter = new StringWriter();
138: ((Renderable) child).getRenderer().render(
139: childWriter, Collections.EMPTY_MAP,
140: callData);
141: renderedEntry = childWriter.toString();
142: } else if (child instanceof ContentProvider) {
143: renderedEntry = new DefaultRenderer(
144: ((ContentProvider) child).getContent())
145: .getContentAsString(callData);
146: }
147: } else {
148: index = subKey.indexOf('#');
149: if (index != -1) {
150: String dataKey = subKey.substring(index + 1);
151: subKey = subKey.substring(0, index);
152: // The subKey allows us to expand the syntax to other
153: // data types (e.g. properties) later.
154: if (subKey.equals("metadata")) {
155: Metadata metadata = child.getComponentContext()
156: .getMetadata();
157: StringBuilder sb = new StringBuilder();
158: Iterator metaValues = metadata.getValues(
159: dataKey).iterator();
160: while (metaValues.hasNext()) {
161: String value = (String) metaValues.next();
162: if (sb.length() > 0) {
163: sb.append(",");
164: }
165: sb.append(value);
166: }
167: if (sb.length() > 0) {
168: renderedEntry = sb.toString();
169: }
170: }
171: }
172: }
173: } else {
174: renderedEntry = (String) result.get(localKey);
175: }
176:
177: if (renderedEntry == null) {
178: renderedEntry = defaultResult;
179: }
180:
181: return renderedEntry;
182: }
183:
184: public void render(Writer writer, Map parameterMap,
185: CallData callData) throws ContelligentException,
186: IOException {
187: if (callData.getRequestAttribute(key) != null) {
188: writer.write(callData.getRequestAttribute(key).toString());
189: } else {
190: log.warn("Key '" + key + "' not defined!");
191: }
192: }
193: }
|