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.guiservices.templates;
025:
026: import java.util.List;
027: import jeeves.constants.Jeeves;
028: import jeeves.interfaces.Service;
029: import jeeves.server.ServiceConfig;
030: import jeeves.server.context.ServiceContext;
031: import jeeves.utils.Xml;
032: import org.fao.geonet.GeonetContext;
033: import org.fao.geonet.constants.Edit;
034: import org.fao.geonet.constants.Geonet;
035: import org.fao.geonet.kernel.search.MetaSearcher;
036: import org.fao.geonet.kernel.search.SearchManager;
037: import org.jdom.Element;
038:
039: //=============================================================================
040:
041: /** A simple service that returns all metadata templates that can be added
042: */
043:
044: public class Get implements Service {
045: private String styleSheet;
046:
047: private String arParams[] = { "extended", "off", "remote", "off",
048: "attrset", "geo", "template", "y", "any", "", };
049:
050: //--------------------------------------------------------------------------
051: //---
052: //--- Init
053: //---
054: //--------------------------------------------------------------------------
055:
056: public void init(String appPath, ServiceConfig params)
057: throws Exception {
058: styleSheet = appPath + Geonet.Path.STYLESHEETS
059: + "/portal-present.xsl";
060: }
061:
062: //--------------------------------------------------------------------------
063: //---
064: //--- API
065: //---
066: //--------------------------------------------------------------------------
067:
068: public Element exec(Element params, ServiceContext context)
069: throws Exception {
070: Element result = search(context).setName(Jeeves.Elem.RESPONSE);
071: Element root = new Element("root");
072:
073: root.addContent(result);
074:
075: List list = Xml.transform(root, styleSheet).getChildren();
076:
077: Element response = new Element("dummy");
078:
079: for (int i = 0; i < list.size(); i++) {
080: Element elem = (Element) list.get(i);
081: Element info = elem.getChild(Edit.RootChild.INFO,
082: Edit.NAMESPACE);
083:
084: if (!elem.getName().equals("metadata"))
085: continue;
086:
087: String id = info.getChildText(Edit.Info.Elem.ID);
088: String template = info
089: .getChildText(Edit.Info.Elem.IS_TEMPLATE);
090:
091: if (template.equals("y"))
092: response.addContent(buildRecord(id, elem
093: .getChildText("title")));
094: }
095:
096: return response;
097: }
098:
099: //--------------------------------------------------------------------------
100: //---
101: //--- Private methods
102: //---
103: //--------------------------------------------------------------------------
104:
105: private Element search(ServiceContext context) throws Exception {
106: GeonetContext gc = (GeonetContext) context
107: .getHandlerContext(Geonet.CONTEXT_NAME);
108:
109: context.info("Creating searcher");
110:
111: Element params = buildParams();
112: ServiceConfig config = new ServiceConfig();
113:
114: SearchManager searchMan = gc.getSearchmanager();
115: MetaSearcher searcher = searchMan.newSearcher(
116: SearchManager.LUCENE, Geonet.File.SEARCH_LUCENE);
117:
118: searcher.search(context, params, config);
119:
120: params.addContent(new Element("from").setText("1"));
121: params.addContent(new Element("to").setText(searcher.getSize()
122: + ""));
123:
124: Element result = searcher.present(context, params, config);
125:
126: searcher.close();
127:
128: return result;
129: }
130:
131: //--------------------------------------------------------------------------
132:
133: private Element buildParams() {
134: Element params = new Element(Jeeves.Elem.REQUEST);
135:
136: for (int i = 0; i < arParams.length / 2; i++)
137: params.addContent(new Element(arParams[i * 2])
138: .setText(arParams[i * 2 + 1]));
139:
140: return params;
141: }
142:
143: //--------------------------------------------------------------------------
144:
145: private Element buildRecord(String id, String name) {
146: Element el = new Element("record");
147:
148: el.addContent(new Element("id").setText(id));
149: el.addContent(new Element("name").setText(name));
150:
151: return el;
152: }
153: }
154:
155: //=============================================================================
|