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.services.metadata;
025:
026: import java.util.UUID;
027: import jeeves.constants.Jeeves;
028: import jeeves.exceptions.MissingParameterEx;
029: import jeeves.interfaces.Service;
030: import jeeves.resources.dbms.Dbms;
031: import jeeves.server.ServiceConfig;
032: import jeeves.server.context.ServiceContext;
033: import jeeves.utils.Util;
034: import jeeves.utils.Xml;
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.DataManager;
039: import org.jdom.Element;
040: import org.jdom.Namespace;
041:
042: //=============================================================================
043:
044: /** Inserts a new metadata to the system (data is validated)
045: */
046:
047: public class Insert implements Service {
048: //--------------------------------------------------------------------------
049: //---
050: //--- Init
051: //---
052: //--------------------------------------------------------------------------
053:
054: private String stylePath;
055:
056: public void init(String appPath, ServiceConfig params)
057: throws Exception {
058: this .stylePath = appPath + Geonet.Path.IMPORT_STYLESHEETS;
059: }
060:
061: //--------------------------------------------------------------------------
062: //---
063: //--- Service
064: //---
065: //--------------------------------------------------------------------------
066:
067: public Element exec(Element params, ServiceContext context)
068: throws Exception {
069: GeonetContext gc = (GeonetContext) context
070: .getHandlerContext(Geonet.CONTEXT_NAME);
071:
072: DataManager dataMan = gc.getDataManager();
073:
074: String data = Util.getParam(params, Params.DATA);
075: String group = Util.getParam(params, Params.GROUP);
076: String schema = Util.getParam(params, Params.SCHEMA);
077: String isTemplate = Util.getParam(params, Params.TEMPLATE, "n");
078: String title = Util.getParam(params, Params.TITLE, "");
079: String category = Util.getParam(params, Params.CATEGORY);
080: String style = Util.getParam(params, Params.STYLESHEET);
081:
082: boolean validate = Util
083: .getParam(params, Params.VALIDATE, "off").equals("on");
084:
085: if (isTemplate.equals("s") && title.length() == 0)
086: throw new MissingParameterEx("title");
087:
088: //-----------------------------------------------------------------------
089: //--- add the DTD to the input xml to perform validation
090:
091: Element xml = Xml.loadString(data, false);
092:
093: // Apply a stylesheet transformation if requested
094: if (!style.equals("_none_"))
095: xml = Xml.transform(xml, stylePath + "/" + style);
096:
097: if (validate)
098: dataMan.validate(schema, xml);
099:
100: //-----------------------------------------------------------------------
101: //--- if the uuid does not exist and is not a template we generate it
102:
103: String uuid;
104: if (isTemplate.equals("n")) {
105: uuid = dataMan.extractUUID(schema, xml);
106: if (uuid.length() == 0)
107: uuid = UUID.randomUUID().toString();
108: } else
109: uuid = UUID.randomUUID().toString();
110:
111: //-----------------------------------------------------------------------
112: //--- insert metadata into the system
113:
114: Dbms dbms = (Dbms) context.getResourceManager().open(
115: Geonet.Res.MAIN_DB);
116:
117: String id = dataMan.insertMetadata(dbms, schema, group, xml,
118: context.getSerialFactory(), gc.getSiteId(), uuid,
119: isTemplate, title, context.getUserSession()
120: .getUserIdAsInt());
121:
122: //--- Insert category if requested
123: if (!"_none_".equals(category))
124: dataMan.setCategory(dbms, id, category);
125:
126: Element response = new Element(Jeeves.Elem.RESPONSE);
127: response.addContent(new Element(Params.ID).setText(id));
128:
129: return response;
130: };
131:
132: //---------------------------------------------------------------------------
133:
134: private void fixNamespace(Element md, Namespace ns) {
135: if (md.getNamespaceURI().equals(ns.getURI()))
136: md.setNamespace(ns);
137:
138: for (Object o : md.getChildren())
139: fixNamespace((Element) o, ns);
140: }
141: }
142:
143: //=============================================================================
|