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.oaipmh.responses;
025:
026: import java.util.ArrayList;
027: import java.util.List;
028: import org.fao.geonet.util.ISODate;
029: import org.fao.oaipmh.OaiPmh;
030: import org.fao.oaipmh.requests.IdentifyRequest;
031: import org.jdom.Element;
032:
033: //=============================================================================
034:
035: public class IdentifyResponse extends AbstractResponse {
036: //---------------------------------------------------------------------------
037: //---
038: //--- Constructor
039: //---
040: //---------------------------------------------------------------------------
041:
042: public IdentifyResponse() {
043: }
044:
045: //---------------------------------------------------------------------------
046:
047: public IdentifyResponse(Element response) {
048: super (response);
049: build(response);
050: }
051:
052: //---------------------------------------------------------------------------
053: //---
054: //--- API methods
055: //---
056: //---------------------------------------------------------------------------
057:
058: public String getRepositoryName() {
059: return reposName;
060: }
061:
062: public String getBaseUrl() {
063: return baseURL;
064: }
065:
066: public ISODate getEarliestDateStamp() {
067: return earlDateStamp;
068: }
069:
070: public DeletedRecord getDeletedRecord() {
071: return deletedRecord;
072: }
073:
074: public Granularity getGranularity() {
075: return granularity;
076: }
077:
078: //---------------------------------------------------------------------------
079:
080: public void setRepositoryName(String name) {
081: reposName = name;
082: }
083:
084: //---------------------------------------------------------------------------
085:
086: public void setBaseUrl(String url) {
087: baseURL = url;
088: }
089:
090: //---------------------------------------------------------------------------
091:
092: public void setEarliestDateStamp(ISODate dateStamp) {
093: earlDateStamp = dateStamp;
094: }
095:
096: //---------------------------------------------------------------------------
097:
098: public void setDeletedRecord(DeletedRecord dr) {
099: deletedRecord = dr;
100: }
101:
102: //---------------------------------------------------------------------------
103:
104: public void setGranularity(Granularity g) {
105: granularity = g;
106: }
107:
108: //---------------------------------------------------------------------------
109:
110: public void clearAdminEmails() {
111: adminEmails.clear();
112: }
113:
114: //---------------------------------------------------------------------------
115:
116: public void addAdminEmail(String email) {
117: adminEmails.add(email);
118: }
119:
120: //---------------------------------------------------------------------------
121:
122: public Element toXml() {
123: Element root = new Element(IdentifyRequest.VERB,
124: OaiPmh.Namespaces.OAI_PMH);
125:
126: add(root, "repositoryName", reposName);
127: add(root, "baseURL", baseURL);
128: add(root, "protocolVersion", "2.0");
129:
130: for (String email : adminEmails)
131: add(root, "adminEmail", email);
132:
133: add(root, "earliestDatestamp", earlDateStamp + "Z");
134: add(root, "deletedRecord", deletedRecord.toString());
135: add(root, "granularity", granularity.toString());
136:
137: for (String compression : compressions)
138: add(root, "compression", compression);
139:
140: for (Element descr : descriptions)
141: root.addContent((Element) descr.clone());
142:
143: return root;
144: }
145:
146: //---------------------------------------------------------------------------
147: //---
148: //--- Private methods
149: //---
150: //---------------------------------------------------------------------------
151:
152: private void build(Element response) {
153: Element ident = response.getChild("Identify",
154: OaiPmh.Namespaces.OAI_PMH);
155:
156: reposName = ident.getChildText("repositoryName",
157: OaiPmh.Namespaces.OAI_PMH);
158: baseURL = ident.getChildText("baseURL",
159: OaiPmh.Namespaces.OAI_PMH);
160:
161: //--- store earliest datestamp
162:
163: String eds = ident.getChildText("earliestDatestamp",
164: OaiPmh.Namespaces.OAI_PMH);
165: earlDateStamp = new ISODate(eds);
166:
167: //--- add admin emails
168:
169: for (Object o : ident.getChildren("adminEmail",
170: OaiPmh.Namespaces.OAI_PMH)) {
171: Element email = (Element) o;
172: adminEmails.add(email.getText());
173: }
174:
175: //--- handle granularity
176:
177: String gran = ident.getChildText("granularity",
178: OaiPmh.Namespaces.OAI_PMH);
179: granularity = Granularity.parse(gran);
180:
181: //--- handle deleted record
182:
183: String delRec = ident.getChildText("deletedRecord",
184: OaiPmh.Namespaces.OAI_PMH);
185: deletedRecord = DeletedRecord.parse(delRec);
186:
187: //--- add compressions
188:
189: for (Object o : ident.getChildren("compression",
190: OaiPmh.Namespaces.OAI_PMH)) {
191: Element comp = (Element) o;
192: compressions.add(comp.getText());
193: }
194:
195: //--- add descriptions
196:
197: for (Object o : ident.getChildren("description",
198: OaiPmh.Namespaces.OAI_PMH))
199: descriptions.add((Element) o);
200: }
201:
202: //---------------------------------------------------------------------------
203: //---
204: //--- Variables
205: //---
206: //---------------------------------------------------------------------------
207:
208: private String reposName;
209: private String baseURL;
210:
211: private ISODate earlDateStamp;
212: private DeletedRecord deletedRecord;
213: private Granularity granularity;
214:
215: private List<String> adminEmails = new ArrayList<String>();
216: private List<String> compressions = new ArrayList<String>();
217: private List<Element> descriptions = new ArrayList<Element>();
218:
219: //---------------------------------------------------------------------------
220: //---
221: //--- Enumerations
222: //---
223: //---------------------------------------------------------------------------
224:
225: //---------------------------------------------------------------------------
226: //--- Granularity
227: //---------------------------------------------------------------------------
228:
229: public enum Granularity {
230: SHORT("YYYY-MM-DD"), LONG("YYYY-MM-DDThh:mm:ssZ");
231:
232: //------------------------------------------------------------------------
233:
234: private Granularity(String type) {
235: this .type = type;
236: }
237:
238: //------------------------------------------------------------------------
239:
240: public String toString() {
241: return type;
242: }
243:
244: //------------------------------------------------------------------------
245:
246: public static Granularity parse(String type) {
247: if (type.equals(SHORT.toString()))
248: return SHORT;
249: if (type.equals(LONG.toString()))
250: return LONG;
251:
252: throw new RuntimeException("Unknown granularity type : "
253: + type);
254: }
255:
256: //------------------------------------------------------------------------
257:
258: private String type;
259: }
260:
261: //---------------------------------------------------------------------------
262: //--- DeletedRecord
263: //---------------------------------------------------------------------------
264:
265: public enum DeletedRecord {
266: NO("no"), PERSISTENT("persistent"), TRANSIENT("transient");
267:
268: //------------------------------------------------------------------------
269:
270: private DeletedRecord(String type) {
271: this .type = type;
272: }
273:
274: //------------------------------------------------------------------------
275:
276: public String toString() {
277: return type;
278: }
279:
280: //------------------------------------------------------------------------
281:
282: public static DeletedRecord parse(String type) {
283: if (type.equals(NO.toString()))
284: return NO;
285: if (type.equals(PERSISTENT.toString()))
286: return PERSISTENT;
287: if (type.equals(TRANSIENT.toString()))
288: return TRANSIENT;
289:
290: throw new RuntimeException("Unknown deleted record type : "
291: + type);
292: }
293:
294: //------------------------------------------------------------------------
295:
296: private String type;
297: }
298: }
299:
300: //=============================================================================
|