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.metadata;
019:
020: import java.io.IOException;
021: import java.io.StringWriter;
022: import java.util.Iterator;
023: import java.util.List;
024: import java.util.Vector;
025:
026: import de.finix.contelligent.CallData;
027: import de.finix.contelligent.Component;
028: import de.finix.contelligent.ComponentManager;
029: import de.finix.contelligent.ComponentPath;
030: import de.finix.contelligent.components.Folder;
031: import de.finix.contelligent.components.query.DefaultQueryResult;
032: import de.finix.contelligent.components.query.Query;
033: import de.finix.contelligent.core.BasicComponentManager;
034: import de.finix.contelligent.exception.ComponentPersistenceException;
035: import de.finix.contelligent.exception.ContelligentException;
036: import de.finix.contelligent.exception.ContelligentRuntimeException;
037: import de.finix.contelligent.logging.LoggingService;
038: import de.finix.contelligent.render.Renderable;
039: import de.finix.contelligent.render.Renderer;
040:
041: public class MetadataQuery extends Folder implements Query {
042:
043: final static private org.apache.log4j.Logger log = LoggingService
044: .getLogger(MetadataQuery.class);
045:
046: final private static ComponentPath ROOT = new ComponentPath("root");
047:
048: final private static ComponentPath RELATIVE = new ComponentPath(
049: "relative");
050:
051: final private static ComponentPath KEY = new ComponentPath("key");
052:
053: final private static ComponentPath VALUE = new ComponentPath(
054: "value");
055:
056: public List getQueryResult(CallData callData) {
057: BasicComponentManager cm = (BasicComponentManager) callData
058: .getActualManager();
059: ComponentPath root = null;
060: ComponentPath relative = null;
061: String key = null;
062: String value = null;
063: try {
064: root = new ComponentPath(render(callData, ROOT));
065: } catch (ContelligentException ce) {
066: throw new ContelligentRuntimeException(
067: "Unable to read \"root\" component:", ce);
068: }
069: try {
070: relative = new ComponentPath(render(callData, RELATIVE));
071: } catch (ContelligentException ce) {
072: // Ignore, keep relative as null then since this is allowed
073: }
074: try {
075: key = render(callData, KEY);
076: } catch (ContelligentException ce) {
077: throw new ContelligentRuntimeException(
078: "Unable to read \"key\" component:", ce);
079: }
080: try {
081: value = render(callData, VALUE);
082: } catch (ContelligentException ce) {
083: throw new ContelligentRuntimeException(
084: "Unable to read \"value\" component:", ce);
085: }
086: Component rootComp = null;
087: try {
088: rootComp = cm.getComponent(root, callData);
089: } catch (ContelligentException ce) {
090: throw new ContelligentRuntimeException(
091: "Unable to get root component:", ce);
092: }
093: try {
094: Iterator paths = cm.selectMetaContent(rootComp, relative,
095: key, value, "=").iterator();
096: Vector results = new Vector();
097: while (paths.hasNext()) {
098: ComponentPath cp = (ComponentPath) paths.next();
099: results.add(new DefaultQueryResult(cp));
100: }
101: return results;
102: } catch (ComponentPersistenceException cpe) {
103: throw new ContelligentRuntimeException("Query failed: ",
104: cpe);
105: }
106: }
107:
108: protected String render(CallData callData, ComponentPath cp)
109: throws ContelligentException {
110: try {
111: ComponentManager cm = callData.getActualManager();
112: Component comp = cm.getSubcomponent(this , cp, callData);
113: if (comp instanceof Renderable) {
114: Renderer r = ((Renderable) comp).getRenderer();
115: StringWriter writer = new StringWriter();
116: r.render(writer, callData.getParameters(), callData);
117: return writer.toString();
118: } else {
119: throw new ContelligentException(
120: "Component is not renderable: " + cp);
121: }
122: } catch (IOException ioe) {
123: throw new ContelligentException(
124: "An IO Exception has occured: ", ioe);
125: }
126: }
127: }
|