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.Hashtable;
027: import java.util.List;
028: import jeeves.exceptions.BadParameterEx;
029: import jeeves.exceptions.OperationNotAllowedEx;
030: import jeeves.resources.dbms.Dbms;
031: import jeeves.server.UserSession;
032: import jeeves.server.context.ServiceContext;
033: import jeeves.utils.Util;
034: import org.fao.geonet.GeonetContext;
035: import org.fao.geonet.constants.Geonet;
036: import org.fao.geonet.constants.Params;
037: import org.fao.geonet.exceptions.ConcurrentUpdateEx;
038: import org.fao.geonet.kernel.AccessManager;
039: import org.fao.geonet.kernel.DataManager;
040: import org.jdom.Element;
041:
042: //=============================================================================
043:
044: /** Utilities
045: */
046:
047: class EditUtils {
048: //--------------------------------------------------------------------------
049: //---
050: //--- API methods
051: //---
052: //--------------------------------------------------------------------------
053:
054: /** Perform common editor preprocessing tasks
055: */
056:
057: public static void preprocessUpdate(Element params,
058: ServiceContext context) throws Exception {
059: GeonetContext gc = (GeonetContext) context
060: .getHandlerContext(Geonet.CONTEXT_NAME);
061: DataManager dataMan = gc.getDataManager();
062: AccessManager accessMan = gc.getAccessManager();
063: UserSession session = context.getUserSession();
064: Dbms dbms = (Dbms) context.getResourceManager().open(
065: Geonet.Res.MAIN_DB);
066:
067: String id = Util.getParam(params, Params.ID);
068:
069: //-----------------------------------------------------------------------
070: //--- handle current tab
071:
072: Element elCurrTab = params.getChild(Params.CURRTAB);
073:
074: if (elCurrTab != null)
075: session.setProperty(Geonet.Session.METADATA_SHOW, elCurrTab
076: .getText());
077:
078: //-----------------------------------------------------------------------
079: //--- check access
080:
081: if (!dataMan.existsMetadata(dbms, id))
082: throw new BadParameterEx("id", id);
083:
084: if (!accessMan.canEdit(context, id))
085: throw new OperationNotAllowedEx();
086: }
087:
088: //--------------------------------------------------------------------------
089: /** Update metadata content
090: */
091:
092: public static void updateContent(Element params,
093: ServiceContext context, boolean validate) throws Exception {
094: GeonetContext gc = (GeonetContext) context
095: .getHandlerContext(Geonet.CONTEXT_NAME);
096: DataManager dataMan = gc.getDataManager();
097:
098: Dbms dbms = (Dbms) context.getResourceManager().open(
099: Geonet.Res.MAIN_DB);
100:
101: String id = Util.getParam(params, Params.ID);
102: String version = Util.getParam(params, Params.VERSION);
103:
104: //--- build hashtable with changes
105: //--- each change is a couple (pos, value)
106:
107: Hashtable htChanges = new Hashtable(100);
108:
109: List list = params.getChildren();
110:
111: for (int i = 0; i < list.size(); i++) {
112: Element el = (Element) list.get(i);
113:
114: String sPos = el.getName();
115: String sVal = el.getText();
116:
117: if (sPos.startsWith("_"))
118: htChanges.put(sPos.substring(1), sVal);
119: }
120:
121: //-----------------------------------------------------------------------
122: //--- update element and return status
123:
124: if (!dataMan.updateMetadata(context.getUserSession(), dbms, id,
125: version, htChanges, validate))
126: throw new ConcurrentUpdateEx(id);
127: }
128:
129: //--------------------------------------------------------------------------
130:
131: public static void updateContent(Element params,
132: ServiceContext context) throws Exception {
133: updateContent(params, context, false);
134: }
135: }
136:
137: //=============================================================================
|