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.csw.common.requests;
025:
026: import java.util.ArrayList;
027: import java.util.HashSet;
028: import org.fao.geonet.csw.common.Csw;
029: import org.fao.geonet.csw.common.Csw.ConstraintLanguage;
030: import org.fao.geonet.csw.common.Csw.ElementSetName;
031: import org.fao.geonet.csw.common.Csw.OutputSchema;
032: import org.fao.geonet.csw.common.Csw.ResultType;
033: import org.fao.geonet.csw.common.Csw.TypeName;
034: import org.fao.geonet.csw.common.util.Xml;
035: import org.jdom.Element;
036:
037: //=============================================================================
038:
039: /** Params:
040: * - resultType (0..1) Can be 'hits', 'results', 'validate'. Default is 'hits'
041: * - outputFormat (0..1) Can be only 'application/xml'
042: * - namespace (0..1) Used for the GET request
043: * - outputSchema (0..1) Can be 'ogccore', 'profile'. Default is 'ogccore'
044: * - startPosition (0..1) Default is 1
045: * - maxRecords (0..1) Default is 10
046: * - TypeNames (1..1) A set of 'dataset', 'datasetcollection', 'service', 'application'
047: * - elementSetName (0..1) Can be 'brief', 'summary', 'full'. Default is 'summary'
048: * - constraintLanguage (1..1) Can be 'CQL_TEXT', 'FILTER'. Must be included
049: * when 'constraint' is specified
050: * - constraintLanguageVersion (1..1) Example '1.0.0'
051: * - constraint (0..1) Query to execute
052: * - distributedSearch (0..1) TRUE|FALSE
053: * - hopCount (0..1) default is 2
054: */
055:
056: public class GetRecordsRequest extends CatalogRequest {
057: private String outputFormat;
058: private String startPosition;
059: private String maxRecords;
060: private String constrLangVersion;
061: private String constraint;
062: private String hopCount;
063: private boolean distribSearch;
064:
065: private ResultType resultType;
066: private OutputSchema outputSchema;
067: private ElementSetName elemSetName;
068: private ConstraintLanguage constrLang;
069:
070: private HashSet<TypeName> hsTypeNames = new HashSet<TypeName>();
071: private ArrayList<String> alSortBy = new ArrayList<String>();
072:
073: //---------------------------------------------------------------------------
074: //---
075: //--- Constructor
076: //---
077: //---------------------------------------------------------------------------
078:
079: public GetRecordsRequest() {
080: }
081:
082: //---------------------------------------------------------------------------
083: //---
084: //--- API methods
085: //---
086: //---------------------------------------------------------------------------
087:
088: public void setResultType(ResultType type) {
089: resultType = type;
090: }
091:
092: //---------------------------------------------------------------------------
093:
094: public void setOutputFormat(String format) {
095: outputFormat = format;
096: }
097:
098: //---------------------------------------------------------------------------
099:
100: public void setOutputSchema(OutputSchema schema) {
101: outputSchema = schema;
102: }
103:
104: //---------------------------------------------------------------------------
105:
106: public void setStartPosition(String start) {
107: startPosition = start;
108: }
109:
110: //---------------------------------------------------------------------------
111:
112: public void setMaxRecords(String num) {
113: maxRecords = num;
114: }
115:
116: //---------------------------------------------------------------------------
117:
118: public void setElementSetName(ElementSetName name) {
119: elemSetName = name;
120: }
121:
122: //---------------------------------------------------------------------------
123:
124: public void addTypeName(TypeName typeName) {
125: hsTypeNames.add(typeName);
126: }
127:
128: //---------------------------------------------------------------------------
129:
130: public void setConstraintLanguage(ConstraintLanguage lang) {
131: constrLang = lang;
132: }
133:
134: //---------------------------------------------------------------------------
135:
136: public void setConstraintLangVersion(String version) {
137: constrLangVersion = version;
138: }
139:
140: //---------------------------------------------------------------------------
141:
142: public void setConstraint(String constr) {
143: constraint = constr;
144: }
145:
146: //---------------------------------------------------------------------------
147:
148: public void setDistributedSearch(boolean yesno) {
149: distribSearch = yesno;
150: }
151:
152: //---------------------------------------------------------------------------
153:
154: public void setHopCount(String count) {
155: hopCount = count;
156: }
157:
158: //---------------------------------------------------------------------------
159:
160: public void addSortBy(String field, boolean ascend) {
161: if (ascend)
162: alSortBy.add(field + ":A");
163: else
164: alSortBy.add(field + ":D");
165: }
166:
167: //---------------------------------------------------------------------------
168: //---
169: //--- Protected methods
170: //---
171: //---------------------------------------------------------------------------
172:
173: protected String getRequestName() {
174: return "GetRecords";
175: }
176:
177: //---------------------------------------------------------------------------
178:
179: protected void setupGetParams() {
180: addParam("request", getRequestName());
181: addParam("service", Csw.SERVICE);
182: addParam("version", Csw.CSW_VERSION);
183:
184: addParam("resultType", resultType);
185: addParam("namespace", Csw.NAMESPACE_CSW.getPrefix() + ":"
186: + Csw.NAMESPACE_CSW.getURI());
187: addParam("outputFormat", outputFormat);
188: addParam("outputSchema", outputSchema, Csw.NAMESPACE_CSW
189: .getPrefix()
190: + ":");
191: addParam("startPosition", startPosition);
192: addParam("maxRecords", maxRecords);
193: addParam("elementSetName", elemSetName);
194: addParam("constraint", constraint);
195: addParam("hopCount", hopCount);
196:
197: if (distribSearch)
198: addParam("distributedSearch", "TRUE");
199:
200: addParam("constraintLanguage", constrLang);
201: addParam("constraint_language_version", constrLangVersion);
202:
203: fill("typeNames", hsTypeNames);
204: fill("sortBy", alSortBy);
205: }
206:
207: //---------------------------------------------------------------------------
208:
209: protected Element getPostParams() {
210: Element params = new Element(getRequestName(),
211: Csw.NAMESPACE_CSW);
212:
213: //--- 'service' and 'version' are common mandatory attributes
214: setAttrib(params, "service", Csw.SERVICE);
215: setAttrib(params, "version", Csw.CSW_VERSION);
216:
217: setAttrib(params, "resultType", resultType);
218: setAttrib(params, "outputFormat", outputFormat);
219: setAttrib(params, "outputSchema", outputSchema,
220: Csw.NAMESPACE_CSW.getPrefix() + ":");
221: setAttrib(params, "startPosition", startPosition);
222: setAttrib(params, "maxRecords", maxRecords);
223:
224: if (distribSearch) {
225: Element ds = new Element("DistributedSearch",
226: Csw.NAMESPACE_CSW);
227: ds.setText("TRUE");
228:
229: if (hopCount != null)
230: ds.setAttribute("hopCount", hopCount);
231:
232: params.addContent(ds);
233: }
234:
235: params.addContent(getQuery());
236:
237: return params;
238: }
239:
240: //---------------------------------------------------------------------------
241:
242: private Element getQuery() {
243: Element query = new Element("Query", Csw.NAMESPACE_CSW);
244:
245: setAttrib(query, "typeNames", hsTypeNames, "");
246: addParam(query, "ElementSetName", elemSetName);
247:
248: //--- handle constraint
249:
250: if (constraint != null && constrLang != null) {
251: Element constr = new Element("Constraint",
252: Csw.NAMESPACE_CSW);
253: query.addContent(constr);
254:
255: if (constrLang == ConstraintLanguage.CQL)
256: addParam(constr, "CqlText", constraint);
257: else
258: addFilter(constr);
259:
260: setAttrib(constr, "version", constrLangVersion);
261: }
262:
263: //--- handle sortby
264:
265: if (alSortBy.size() != 0) {
266: Element sortBy = new Element("SortBy", Csw.NAMESPACE_OGC);
267: query.addContent(sortBy);
268:
269: for (String sortInfo : alSortBy) {
270: String field = sortInfo.substring(0,
271: sortInfo.length() - 2);
272: boolean ascen = sortInfo.endsWith(":A");
273:
274: Element sortProp = new Element("SortProperty",
275: Csw.NAMESPACE_OGC);
276: sortBy.addContent(sortProp);
277:
278: Element propName = new Element("PropertyName",
279: Csw.NAMESPACE_OGC).setText(field);
280: Element sortOrder = new Element("SortOrder",
281: Csw.NAMESPACE_OGC).setText(ascen ? "ASC"
282: : "DESC");
283:
284: sortProp.addContent(propName);
285: sortProp.addContent(sortOrder);
286: }
287: }
288:
289: return query;
290: }
291:
292: //---------------------------------------------------------------------------
293:
294: private void addFilter(Element constr) {
295: try {
296: constr.addContent(Xml.loadString(constraint, false));
297: } catch (Exception e) {
298: e.printStackTrace();
299: }
300: }
301: }
302:
303: //=============================================================================
|