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.oaipmh;
025:
026: import java.util.ArrayList;
027: import java.util.List;
028: import jeeves.constants.Jeeves;
029: import jeeves.resources.dbms.Dbms;
030: import jeeves.server.ServiceConfig;
031: import jeeves.server.context.ServiceContext;
032: import jeeves.utils.Xml;
033: import org.fao.geonet.GeonetContext;
034: import org.fao.geonet.constants.Edit;
035: import org.fao.geonet.constants.Geonet;
036: import org.fao.geonet.kernel.search.MetaSearcher;
037: import org.fao.geonet.kernel.search.SearchManager;
038: import org.fao.geonet.kernel.setting.SettingInfo;
039: import org.fao.oaipmh.exceptions.IdDoesNotExistException;
040: import org.fao.oaipmh.exceptions.OaiPmhException;
041: import org.jdom.Element;
042:
043: //=============================================================================
044:
045: public class Lib {
046: public static final int MAX_RECORDS = 10;
047:
048: public static final String SESSION_OBJECT = "oai-list-records-result";
049:
050: //---------------------------------------------------------------------------
051: //---
052: //--- API methods
053: //---
054: //---------------------------------------------------------------------------
055:
056: public static String getMetadataSchema(ServiceContext context,
057: String uuid) throws Exception {
058: Dbms dbms = (Dbms) context.getResourceManager().open(
059: Geonet.Res.MAIN_DB);
060:
061: String query = "SELECT schemaId FROM Metadata WHERE uuid=?";
062:
063: List list = dbms.select(query, uuid).getChildren();
064:
065: if (list.size() == 0)
066: throw new IdDoesNotExistException(uuid);
067:
068: Element elem = (Element) list.get(0);
069:
070: return elem.getChildText("schemaid");
071: }
072:
073: //---------------------------------------------------------------------------
074:
075: public static String getSchemaUrl(ServiceContext context,
076: String relativePath) {
077: SettingInfo si = new SettingInfo(context);
078:
079: return si.getSiteUrl() + context.getBaseUrl() + "/"
080: + relativePath;
081: }
082:
083: //--------------------------------------------------------------------------
084:
085: public static Element toOaiDC(String schema, Element md,
086: String uuid, String changeDate, String appPath)
087: throws Exception {
088: //--- setup environment
089:
090: Element env = new Element("env");
091:
092: env.addContent(new Element("uuid").setText(uuid));
093: env.addContent(new Element("changeDate").setText(changeDate));
094:
095: //--- setup root element
096:
097: Element root = new Element("root");
098: root.addContent(md);
099: root.addContent(env);
100:
101: //--- do an XSL transformation
102:
103: String styleSheet = appPath + "xml/schemas/" + schema
104: + "/convert/oai-dc.xsl";
105:
106: return Xml.transform(root, styleSheet);
107: }
108:
109: //---------------------------------------------------------------------------
110:
111: public static List<Integer> search(ServiceContext context,
112: Element params) throws Exception {
113: GeonetContext gc = (GeonetContext) context
114: .getHandlerContext(Geonet.CONTEXT_NAME);
115: SearchManager sm = gc.getSearchmanager();
116:
117: MetaSearcher searcher = sm.newSearcher(SearchManager.LUCENE,
118: Geonet.File.SEARCH_LUCENE);
119:
120: context.debug("Searching with params:\n"
121: + Xml.getString(params));
122:
123: searcher.search(context, params, dummyConfig);
124:
125: params.addContent(new Element("fast").setText("true"));
126: params.addContent(new Element("from").setText("1"));
127: params.addContent(new Element("to").setText(searcher.getSize()
128: + ""));
129:
130: context.info("Records found : " + searcher.getSize());
131:
132: Element records = searcher
133: .present(context, params, dummyConfig);
134:
135: records.getChild("summary").detach();
136:
137: List<Integer> result = new ArrayList<Integer>();
138:
139: for (Object o : records.getChildren()) {
140: Element rec = (Element) o;
141: Element info = rec.getChild("info", Edit.NAMESPACE);
142:
143: result.add(Integer.parseInt(info.getChildText("id")));
144: }
145:
146: return result;
147: }
148:
149: //---------------------------------------------------------------------------
150:
151: public static Element toJeevesException(OaiPmhException e) {
152: String msg = e.getMessage();
153: String cls = e.getClass().getSimpleName();
154: String id = e.getCode();
155: Element res = e.getResponse();
156:
157: Element error = new Element(Jeeves.Elem.ERROR).addContent(
158: new Element("message").setText(msg)).addContent(
159: new Element("class").setText(cls));
160:
161: error.setAttribute("id", id);
162:
163: if (res != null) {
164: Element elObj = new Element("object");
165: elObj.addContent(res.detach());
166:
167: error.addContent(elObj);
168: }
169:
170: return error;
171: }
172:
173: //---------------------------------------------------------------------------
174: //---
175: //--- Variables
176: //---
177: //---------------------------------------------------------------------------
178:
179: private static ServiceConfig dummyConfig = new ServiceConfig();
180: }
181:
182: //=============================================================================
|