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.util.ArrayList;
023: import java.util.Iterator;
024: import java.util.LinkedList;
025: import java.util.List;
026: import java.util.Set;
027:
028: import de.finix.contelligent.CallData;
029: import de.finix.contelligent.ComponentCreationException;
030: import de.finix.contelligent.ComponentManager;
031: import de.finix.contelligent.ComponentNotFoundException;
032: import de.finix.contelligent.ComponentPath;
033: import de.finix.contelligent.ExternalRelationSource;
034: import de.finix.contelligent.ModificationVetoException;
035: import de.finix.contelligent.RelationsManager;
036: import de.finix.contelligent.components.Folder;
037: import de.finix.contelligent.exception.ComponentPersistenceException;
038: import de.finix.contelligent.exception.ContelligentException;
039: import de.finix.contelligent.logging.LoggingService;
040: import de.finix.contelligent.render.MissingParameterException;
041: import de.finix.contelligent.render.Renderable;
042:
043: public class RelationQuery extends Folder implements Query,
044: ExternalRelationSource {
045:
046: final static private org.apache.log4j.Logger log = LoggingService
047: .getLogger(RelationQuery.class);
048:
049: private ComponentPath componentPath;
050:
051: private ComponentPath resolveOffset;
052:
053: private ComponentPath validSubtree;
054:
055: public ComponentPath getComponentPath() {
056: return componentPath;
057: }
058:
059: public void setComponentPath(ComponentPath componentPath) {
060: this .componentPath = componentPath;
061: }
062:
063: public ComponentPath getResolveOffset() {
064: return resolveOffset;
065: }
066:
067: public void setResolveOffset(ComponentPath resolveOffset) {
068: this .resolveOffset = resolveOffset;
069: }
070:
071: public ComponentPath getValidSubtree() {
072: return validSubtree;
073: }
074:
075: public void setValidSubtree(ComponentPath validSubtreePath) {
076: this .validSubtree = validSubtreePath;
077: }
078:
079: public List getQueryResult(CallData callData) {
080: List cachedResult = (List) callData.getRequestAttribute(
081: "queryResult", this .getComponentContext().getPath()
082: .toPath());
083: if (cachedResult != null)
084: return cachedResult;
085:
086: List results = new ArrayList();
087: try {
088: ComponentManager manager = callData.getActualManager();
089: ComponentPath opPath = componentPath;
090:
091: ComponentPath componentPathComp = this
092: .getComponentContext().getPath().append(
093: "componentPath");
094:
095: if (callData.getActualManager().componentExists(
096: componentPathComp)) {
097: try {
098: Renderable toRender = (Renderable) callData
099: .getActualManager().getComponent(
100: componentPathComp, callData);
101: StringWriter buf = new StringWriter();
102: toRender.getRenderer().render(buf, null, callData);
103: if (buf.toString().length() > 0)
104: opPath = new ComponentPath(buf.toString());
105: } catch (ComponentCreationException e1) {
106: log.error(e1);
107: } catch (ComponentNotFoundException e1) {
108: log.error(e1);
109: } catch (MissingParameterException e) {
110: log.error(e);
111: } catch (IOException e) {
112: log.error(e);
113: } catch (ContelligentException e) {
114: log.error(e);
115: }
116: }
117:
118: // make it absolute, if relative!
119: if (opPath.isRelative()) {
120: opPath = opPath.toAbsolutePath(this
121: .getComponentContext().getPath());
122: }
123:
124: // test if component exists
125: if (!manager.componentExists(opPath)) {
126: log
127: .warn("getQueryResult() - '"
128: + this .getComponentContext().getPath()
129: + "' : '"
130: + opPath
131: + "' does not exist! This should hardly happen! Inspect it!");
132: return results;
133: }
134:
135: RelationsManager relManager = callData.getSystem()
136: .getRelationsManager();
137:
138: Set relatedSet = relManager.getGlobalPathsRelatedTo(opPath,
139: callData.getActualManager().getId());
140: Iterator i = relatedSet.iterator();
141:
142: while (i.hasNext()) {
143: ComponentPath path = (ComponentPath) i.next();
144: ComponentPath result = getResolveOffset()
145: .toAbsolutePath(path);
146: if (result.isSubPathOf(this .validSubtree)) {
147: results.add(new DefaultQueryResult(result));
148: }
149: }
150: } catch (ComponentPersistenceException e) {
151: log.error(
152: "problems with persistence store ... aborting query '"
153: + this .getComponentContext().getPath()
154: + "'", e);
155: }
156: callData.setRequestAttribute("queryResult", results, this
157: .getComponentContext().getPath().toPath());
158: return results;
159: }
160:
161: /*
162: * (non-Javadoc)
163: *
164: * @see de.finix.contelligent.ExternalRelationSource#relatedPaths()
165: */
166: public List relatedPaths() {
167: LinkedList list = new LinkedList();
168: list.add(getComponentPath());
169: return list;
170: }
171:
172: /*
173: * (non-Javadoc)
174: *
175: * @see de.finix.contelligent.ExternalRelationSource#relatedPaths(java.util.List)
176: */
177: public void relatedPaths(List newTargetPaths)
178: throws ModificationVetoException {
179: if (newTargetPaths == null || newTargetPaths.size() == 0
180: || newTargetPaths.size() > 1) {
181: throw new ModificationVetoException(
182: "illegal state - newTargetPaths:'" + newTargetPaths
183: + "'");
184: }
185: setComponentPath((ComponentPath) newTargetPaths.get(0));
186: }
187: }
|