001: //=============================================================================
002: //=== Copyright (C) 2001-2007 Food and Agriculture Organization of the
003: //=== United Nations (FAO-UN), United Nations World Food Programme (WFP)
004: //=== and United Nations Environment Programme (UNEP)
005: //===
006: //=== This program is free software; you can redistribute it and/or modify
007: //=== it under the terms of the GNU General Public License as published by
008: //=== the Free Software Foundation; either version 2 of the License, or (at
009: //=== your option) any later version.
010: //===
011: //=== This program is distributed in the hope that it will be useful, but
012: //=== WITHOUT ANY WARRANTY; without even the implied warranty of
013: //=== MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: //=== General Public License for more details.
015: //===
016: //=== You should have received a copy of the GNU General Public License
017: //=== along with this program; if not, write to the Free Software
018: //=== Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
019: //===
020: //=== Contact: Jeroen Ticheler - FAO - Viale delle Terme di Caracalla 2,
021: //=== Rome - Italy. email: geonetwork@osgeo.org
022: //==============================================================================
023:
024: package org.fao.geonet.kernel.csw.services.getrecords;
025:
026: import java.io.File;
027: import java.util.Iterator;
028: import java.util.List;
029: import java.util.Set;
030: import jeeves.resources.dbms.Dbms;
031: import jeeves.server.context.ServiceContext;
032: import jeeves.utils.Util;
033: import jeeves.utils.Xml;
034: import org.fao.geonet.constants.Geonet;
035: import org.fao.geonet.csw.common.Csw;
036: import org.fao.geonet.csw.common.Csw.ElementSetName;
037: import org.fao.geonet.csw.common.Csw.OutputSchema;
038: import org.fao.geonet.csw.common.Csw.ResultType;
039: import org.fao.geonet.csw.common.Csw.TypeName;
040: import org.fao.geonet.csw.common.exceptions.CatalogException;
041: import org.fao.geonet.csw.common.exceptions.NoApplicableCodeEx;
042: import org.jdom.Element;
043:
044: //=============================================================================
045:
046: public class SearchController {
047: //---------------------------------------------------------------------------
048: //---
049: //--- Single public method to perform the general search tasks
050: //---
051: //---------------------------------------------------------------------------
052:
053: public Element search(ServiceContext context, int startPos,
054: int maxRecords, int hopCount, ResultType resultType,
055: OutputSchema outSchema, ElementSetName setName,
056: Set<TypeName> typeNames, Element filterExpr,
057: List<SortField> sortFields, Set<String> elemNames)
058: throws CatalogException {
059: Element results = new Element("SearchResults",
060: Csw.NAMESPACE_CSW);
061:
062: CatalogSearcher searcher = new CatalogSearcher();
063:
064: List<ResultItem> searchResults = searcher.search(context,
065: filterExpr, typeNames, sortFields);
066:
067: int counter = 0;
068:
069: if (resultType == ResultType.RESULTS)
070: for (int i = startPos; (i < startPos + maxRecords)
071: && (i <= searchResults.size()); i++) {
072: counter++;
073:
074: String id = searchResults.get(i - 1).getID();
075: Element md = retrieveMetadata(context, id, setName,
076: outSchema, elemNames);
077:
078: if (md == null)
079: context
080: .warning("SearchController : Metadata not found or invalid schema : "
081: + id);
082: else
083: results.addContent(md);
084: }
085:
086: results.setAttribute("numberOfRecordsMatched", searchResults
087: .size()
088: + "");
089: results.setAttribute("numberOfRecordsReturned", counter + "");
090: results.setAttribute("elementSet", setName.toString());
091:
092: return results;
093: }
094:
095: //---------------------------------------------------------------------------
096:
097: private Element retrieveMetadata(ServiceContext context, String id,
098: ElementSetName setName, OutputSchema outSchema,
099: Set<String> elemNames) throws CatalogException {
100: try {
101: Dbms dbms = (Dbms) context.getResourceManager().open(
102: Geonet.Res.MAIN_DB);
103:
104: //--- get metadata from DB
105:
106: Element res = dbms
107: .select("SELECT schemaId, data FROM Metadata WHERE id="
108: + id);
109: Iterator i = res.getChildren().iterator();
110:
111: dbms.commit();
112:
113: if (!i.hasNext())
114: return null;
115:
116: Element record = (Element) i.next();
117:
118: String schema = record.getChildText("schemaid");
119: String data = record.getChildText("data");
120:
121: //--- skip metadata with wrong schemas
122:
123: if (schema.equals("fgdc-std")
124: || schema.equals("dublin-core")
125: || schema.equals("iso19115"))
126: if (outSchema != OutputSchema.OGC_CORE)
127: return null;
128:
129: Element md = Xml.loadString(data, false);
130:
131: //--- apply stylesheet according to setName and schema
132:
133: String FS = File.separator;
134: String schemaDir = context.getAppPath() + "xml" + FS
135: + "csw" + FS + "schemas" + FS + schema + FS;
136: String prefix = (outSchema == OutputSchema.OGC_CORE) ? "ogc"
137: : "iso";
138: String styleSheet = schemaDir + prefix + "-" + setName
139: + ".xsl";
140:
141: md = Xml.transform(md, styleSheet);
142:
143: //--- needed to detach md from the document
144:
145: md.detach();
146:
147: //--- if the client has specified some ElementNames, then we remove the unwanted children
148:
149: if (elemNames != null)
150: removeElements(md, elemNames);
151:
152: return md;
153: } catch (Exception e) {
154: context.error("Error while getting metadata with id : "
155: + id);
156: context
157: .error(" (C) StackTrace:\n"
158: + Util.getStackTrace(e));
159:
160: throw new NoApplicableCodeEx(
161: "Rised exception while getting metadata :" + e);
162: }
163: }
164:
165: //---------------------------------------------------------------------------
166:
167: private void removeElements(Element md, Set<String> elemNames) {
168: Iterator i = md.getChildren().iterator();
169:
170: while (i.hasNext()) {
171: Element elem = (Element) i.next();
172:
173: if (!FieldMapper.match(elem, elemNames))
174: i.remove();
175: }
176: }
177: }
178:
179: //=============================================================================
|