001: //=============================================================================
002: //=== Copyright (C) 2001-2005 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: //===
020: //=== Contact: Jeroen Ticheler - FAO - Viale delle Terme di Caracalla 2,
021: //=== Rome - Italy. email: GeoNetwork@fao.org
022: //==============================================================================
023:
024: package org.fao.geonet.services.thesaurus;
025:
026: import java.util.ArrayList;
027:
028: import jeeves.constants.Jeeves;
029: import jeeves.interfaces.Service;
030: import jeeves.server.ServiceConfig;
031: import jeeves.server.UserSession;
032: import jeeves.server.context.ServiceContext;
033: import jeeves.utils.Util;
034:
035: import org.fao.geonet.GeonetContext;
036: import org.fao.geonet.constants.Geonet;
037: import org.fao.geonet.constants.Params;
038: import org.fao.geonet.kernel.KeywordBean;
039: import org.fao.geonet.kernel.ThesaurusManager;
040: import org.fao.geonet.kernel.search.KeywordsSearcher;
041: import org.jdom.Element;
042:
043: //=============================================================================
044:
045: /**
046: * For editing : adds a tag to a thesaurus. Access is restricted
047: */
048:
049: public class EditElement implements Service {
050: public void init(String appPath, ServiceConfig params)
051: throws Exception {
052: }
053:
054: // --------------------------------------------------------------------------
055: // ---
056: // --- Service
057: // ---
058: // --------------------------------------------------------------------------
059:
060: public Element exec(Element params, ServiceContext context)
061: throws Exception {
062: String ref = Util.getParam(params, Params.REF);
063: String id = Util.getParam(params, Params.ID, "");
064: String uri = Util.getParam(params, Params.URI, "");
065: String mode = Util.getParam(params, Params.MODE, "");
066: String lang = context.getLanguage();
067:
068: String modeType = "add";
069:
070: Element elResp = new Element(Jeeves.Elem.RESPONSE);
071:
072: if (!id.equals("") || !uri.equals("")) {
073: KeywordBean kb = null;
074:
075: if (!id.equals("")) {
076: UserSession session = context.getUserSession();
077: KeywordsSearcher searcher = (KeywordsSearcher) session
078: .getProperty(Geonet.Session.SEARCH_KEYWORDS_RESULT);
079: kb = searcher.existsResult(id);
080: } else {
081: GeonetContext gc = (GeonetContext) context
082: .getHandlerContext(Geonet.CONTEXT_NAME);
083: ThesaurusManager thesaurusMan = gc
084: .getThesaurusManager();
085: KeywordsSearcher searcher = new KeywordsSearcher(
086: thesaurusMan);
087:
088: kb = searcher.searchById(uri, ref, lang);
089:
090: }
091: // Add info needed by thesaurus.edit
092: elResp.addContent(new Element("prefLab").setText(kb
093: .getValue()));
094: elResp.addContent(new Element("definition").setText(kb
095: .getDefinition()));
096:
097: elResp.addContent(new Element("relCode").setText(kb
098: .getRelativeCode()));
099: elResp.addContent(new Element("nsCode").setText(kb
100: .getNameSpaceCode()));
101: if (kb.getCoordEast() != null)
102: elResp.addContent(new Element("east").setText(kb
103: .getCoordEast()));
104: if (kb.getCoordWest() != null)
105: elResp.addContent(new Element("west").setText(kb
106: .getCoordWest()));
107: if (kb.getCoordSouth() != null)
108: elResp.addContent(new Element("south").setText(kb
109: .getCoordSouth()));
110: if (kb.getCoordNorth() != null)
111: elResp.addContent(new Element("north").setText(kb
112: .getCoordNorth()));
113:
114: modeType = "edit";
115: uri = kb.getRelativeCode();
116: } else {
117: elResp.addContent(new Element("nsCode").setText("#"));
118: }
119:
120: // Only if consult (ie. external thesaurus) search for related concept
121: if (mode.equals("consult")) {
122: ArrayList<String> reqType = new ArrayList<String>();
123: reqType.add("broader");
124: reqType.add("narrower");
125: reqType.add("related");
126:
127: GeonetContext gc = (GeonetContext) context
128: .getHandlerContext(Geonet.CONTEXT_NAME);
129: ThesaurusManager thesaurusMan = gc.getThesaurusManager();
130: KeywordsSearcher searcherBNR = new KeywordsSearcher(
131: thesaurusMan);
132:
133: for (int i = 0; i <= reqType.size() - 1; i++) {
134: searcherBNR.searchBN(uri, ref, reqType.get(i), lang);
135:
136: searcherBNR.sortResults("label");
137: String type;
138:
139: if (reqType.get(i).equals("broader")) // If looking for broader search concept in a narrower element
140: type = "narrower";
141: else if (reqType.get(i).equals("narrower"))
142: type = "broader";
143: else
144: type = "related";
145:
146: Element keywordType = new Element(type);
147: keywordType.addContent(searcherBNR.getResults(params));
148:
149: elResp.addContent(keywordType);
150: }
151:
152: searcherBNR = null;
153: }
154:
155: String thesaType = ref;
156: thesaType = thesaType.substring(thesaType.indexOf('.') + 1,
157: thesaType.length());
158: thesaType = thesaType.substring(0, thesaType.indexOf('.'));
159:
160: elResp.addContent(new Element("thesaType").setText(thesaType));
161: elResp.addContent(new Element("thesaurus").setText(ref));
162: elResp.addContent(new Element("mode").setText(modeType));
163:
164: return elResp;
165: }
166:
167: }
168:
169: // =============================================================================
|