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.Writer;
022: import java.util.ArrayList;
023: import java.util.Arrays;
024: import java.util.Collection;
025: import java.util.Iterator;
026: import java.util.LinkedList;
027: import java.util.List;
028: import java.util.Map;
029:
030: import de.finix.contelligent.AbstractComponent;
031: import de.finix.contelligent.CallData;
032: import de.finix.contelligent.Component;
033: import de.finix.contelligent.ComponentManager;
034: import de.finix.contelligent.ComponentNotFoundException;
035: import de.finix.contelligent.ComponentPath;
036: import de.finix.contelligent.ExternalRelationSource;
037: import de.finix.contelligent.ModificationVetoException;
038: import de.finix.contelligent.action.ActionResult;
039: import de.finix.contelligent.exception.ContelligentException;
040: import de.finix.contelligent.logging.LoggingService;
041: import de.finix.contelligent.render.MissingParameterException;
042: import de.finix.contelligent.render.ParameterDescription;
043: import de.finix.contelligent.render.Renderable;
044: import de.finix.contelligent.render.Renderer;
045: import de.finix.contelligent.search.SearchResult;
046:
047: public class SearchResultQuery extends AbstractComponent implements
048: Query, Renderable, Renderer, ExternalRelationSource {
049:
050: final static private org.apache.log4j.Logger log = LoggingService
051: .getLogger(SearchResultQuery.class);
052:
053: private ComponentPath searchResultPath;
054:
055: final static String GET_META = "eval_meta";
056:
057: final static String META_SEARCH_REQUEST = "search_string";
058:
059: final static String META_TOTAL_RESULTS = "total_resuts";
060:
061: final private static ParameterDescription[] parameters = new ParameterDescription[] { new ParameterDescription(
062: GET_META, "Render Meta Info", true, true, new String[] {
063: META_SEARCH_REQUEST, META_TOTAL_RESULTS }) };
064:
065: public ComponentPath getSearchResultPath() {
066: return searchResultPath;
067: }
068:
069: public void setSearchResultPath(ComponentPath searchResultPath) {
070: this .searchResultPath = searchResultPath;
071: }
072:
073: public List getQueryResult(CallData callData) {
074: List queryResult = null;
075: List cachedResult = (List) callData.getRequestAttribute(
076: "queryResult", this .getComponentContext().getPath()
077: .toPath());
078: if (cachedResult != null)
079: return cachedResult;
080:
081: synchronized (this ) {
082: List results = new ArrayList();
083: try {
084: ComponentManager manager = callData.getActualManager();
085: Component actionResult = (Component) manager
086: .getComponent(searchResultPath, callData);
087: if (actionResult instanceof ActionResult) {
088: Object searchResult = ((ActionResult) actionResult)
089: .getObject(callData);
090: if (searchResult instanceof SearchResult) {
091: log
092: .debug("Building query result from search result '"
093: + searchResultPath + "'");
094: for (Iterator i = ((SearchResult) searchResult)
095: .iterator(); i.hasNext();) {
096: SearchResult.SearchResultItem item = (SearchResult.SearchResultItem) i
097: .next();
098: SearchQueryResult result = new SearchQueryResult(
099: item);
100: results.add(result);
101: }
102: } else {
103: log
104: .error("The requested component '"
105: + searchResultPath
106: + "' must contain an object of type SearchResult!");
107: }
108: } else {
109: log.error("The requested component '"
110: + searchResultPath
111: + "' must be of type ActionResult!");
112: }
113: callData.setRequestAttribute("queryResult", results,
114: this .getComponentContext().getPath().toPath());
115:
116: queryResult = results;
117: } catch (ComponentNotFoundException e) {
118: log.error("Could not resolve folder with path '"
119: + searchResultPath + "'", e);
120: }
121: }
122: return queryResult;
123: }
124:
125: class SearchQueryResult implements ComponentQueryResult {
126: private final static String SCORE = "score";
127:
128: private final static String TYPE = "type";
129:
130: private SearchResult.SearchResultItem searchResultItem;
131:
132: public SearchQueryResult(
133: SearchResult.SearchResultItem searchResultItem) {
134: this .searchResultItem = searchResultItem;
135: }
136:
137: public ComponentPath getComponentPath() {
138: return new ComponentPath(searchResultItem.getPath());
139: }
140:
141: public Object get(String key) {
142: if (key.equals(SCORE)) {
143: return String.valueOf(searchResultItem.getScore());
144: } else if (key.equals(TYPE)) {
145: return searchResultItem.getType();
146: }
147: return searchResultItem.getFieldValue(key);
148: }
149:
150: public Collection getKeys() {
151: return Arrays.asList(new String[] { SCORE, TYPE });
152: }
153: }
154:
155: /*
156: * (non-Javadoc)
157: *
158: * @see de.finix.contelligent.components.query.Query#getParameterDescription()
159: */
160: public ParameterDescription[] getParameterDescription() {
161: return parameters;
162: }
163:
164: /*
165: * (non-Javadoc)
166: *
167: * @see de.finix.contelligent.render.Renderable#getRenderer()
168: */
169: public Renderer getRenderer() {
170: return this ;
171: }
172:
173: /*
174: * (non-Javadoc)
175: *
176: * @see de.finix.contelligent.render.Renderer#render(java.io.Writer,
177: * java.util.Map, de.finix.contelligent.CallData)
178: */
179: public void render(Writer writer, Map parameterMap,
180: CallData callData) throws IOException,
181: MissingParameterException, ContelligentException {
182:
183: if (parameterMap != null && parameterMap.containsKey(GET_META)) {
184: String value = ((String[]) parameterMap.get(GET_META))[0];
185:
186: if (value == null) {
187: log.error("render() - parameter " + GET_META
188: + " needed to render.");
189: } else if (value.equals(META_SEARCH_REQUEST)) {
190: ComponentManager manager = callData.getActualManager();
191: Component res = manager.getComponent(searchResultPath,
192: callData);
193:
194: if (res instanceof ActionResult) {
195: Object _obj = ((ActionResult) res)
196: .getObject(callData);
197: SearchResult searchResult = (SearchResult) _obj;
198: writer.write(searchResult.getQuery());
199: }
200: } else if (value.equals(META_TOTAL_RESULTS)) {
201: ComponentManager manager = callData.getActualManager();
202: Component res = manager.getComponent(searchResultPath,
203: callData);
204:
205: if (res instanceof ActionResult) {
206: Object _obj = ((ActionResult) res)
207: .getObject(callData);
208: SearchResult searchResult = (SearchResult) _obj;
209: writer.write("" + searchResult.getLength());
210: }
211: } else {
212: log.error("render() - valid parameter " + GET_META
213: + " needed to render ... '" + value
214: + "' is not a valid parameter key!");
215: }
216: }
217:
218: }
219:
220: /*
221: * (non-Javadoc)
222: *
223: * @see de.finix.contelligent.render.Renderer#getSensitiveCategories()
224: */
225: public Collection getSensitiveCategories() {
226: // TODO Auto-generated method stub
227: return null;
228: }
229:
230: /*
231: * (non-Javadoc)
232: *
233: * @see de.finix.contelligent.ExternalRelationSource#relatedPaths()
234: */
235: public List relatedPaths() {
236: LinkedList list = new LinkedList();
237: list.add(getSearchResultPath());
238: return list;
239: }
240:
241: /*
242: * (non-Javadoc)
243: *
244: * @see de.finix.contelligent.ExternalRelationSource#relatedPaths(java.util.List)
245: */
246: public void relatedPaths(List newTargetPaths)
247: throws ModificationVetoException {
248: if (newTargetPaths == null || newTargetPaths.size() == 0
249: || newTargetPaths.size() > 1) {
250: throw new ModificationVetoException(
251: "illegal state - newTargetPaths:'" + newTargetPaths
252: + "'");
253: }
254: setSearchResultPath((ComponentPath) newTargetPaths.get(0));
255: }
256: }
|