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.io.File;
027: import java.io.FileInputStream;
028: import java.io.FileOutputStream;
029: import jeeves.constants.Jeeves;
030: import jeeves.exceptions.OperationNotAllowedEx;
031: import jeeves.interfaces.Service;
032: import jeeves.resources.dbms.Dbms;
033: import jeeves.server.ServiceConfig;
034: import jeeves.server.UserSession;
035: import jeeves.server.context.ServiceContext;
036: import jeeves.utils.BinaryFile;
037: import jeeves.utils.Util;
038: import org.fao.geonet.GeonetContext;
039: import org.fao.geonet.constants.Geonet;
040: import org.fao.geonet.constants.Params;
041: import org.fao.geonet.kernel.AccessManager;
042: import org.fao.geonet.kernel.DataManager;
043: import org.fao.geonet.kernel.MdInfo;
044: import org.fao.geonet.kernel.mef.MEFLib;
045: import org.fao.geonet.kernel.search.MetaSearcher;
046: import org.fao.geonet.lib.Lib;
047: import org.jdom.Element;
048:
049: //=============================================================================
050:
051: /** Removes a metadata from the system
052: */
053:
054: public class Delete implements Service {
055: public void init(String appPath, ServiceConfig params)
056: throws Exception {
057: }
058:
059: //--------------------------------------------------------------------------
060: //---
061: //--- Service
062: //---
063: //--------------------------------------------------------------------------
064:
065: public Element exec(Element params, ServiceContext context)
066: throws Exception {
067: GeonetContext gc = (GeonetContext) context
068: .getHandlerContext(Geonet.CONTEXT_NAME);
069: DataManager dataMan = gc.getDataManager();
070: AccessManager accessMan = gc.getAccessManager();
071: UserSession session = context.getUserSession();
072:
073: Dbms dbms = (Dbms) context.getResourceManager().open(
074: Geonet.Res.MAIN_DB);
075:
076: String id = Util.getParam(params, Params.ID);
077:
078: //-----------------------------------------------------------------------
079: //--- check access
080:
081: MdInfo info = dataMan.getMetadataInfo(dbms, id);
082:
083: if (info == null)
084: throw new IllegalArgumentException(
085: "Metadata not found --> " + id);
086:
087: if (!accessMan.canEdit(context, id))
088: throw new OperationNotAllowedEx();
089:
090: //-----------------------------------------------------------------------
091: //--- backup metadata in 'removed' folder
092:
093: if (info.template != MdInfo.Template.SUBTEMPLATE)
094: backupFile(context, id, info.uuid, MEFLib.doExport(context,
095: info.uuid, "full", false));
096:
097: //-----------------------------------------------------------------------
098: //--- delete metadata and return status
099:
100: dataMan.deleteMetadata(dbms, id);
101:
102: Element elResp = new Element(Jeeves.Elem.RESPONSE);
103: elResp.addContent(new Element(Geonet.Elem.ID).setText(id));
104:
105: // invalidate current result set set
106: MetaSearcher searcher = (MetaSearcher) context.getUserSession()
107: .getProperty(Geonet.Session.SEARCH_RESULT);
108:
109: if (searcher != null)
110: searcher.setValid(false);
111:
112: return elResp;
113: }
114:
115: //--------------------------------------------------------------------------
116: //---
117: //--- Private methods
118: //---
119: //--------------------------------------------------------------------------
120:
121: private void backupFile(ServiceContext context, String id,
122: String uuid, String file) {
123: String outDir = Lib.resource.getRemovedDir(context, id);
124: String outFile = outDir + uuid + ".mef";
125:
126: new File(outDir).mkdirs();
127:
128: try {
129: FileInputStream is = new FileInputStream(file);
130: FileOutputStream os = new FileOutputStream(outFile);
131:
132: BinaryFile.copy(is, os, true, true);
133: } catch (Exception e) {
134: context.warning("Cannot backup mef file : "
135: + e.getMessage());
136: e.printStackTrace();
137: }
138:
139: new File(file).delete();
140: }
141: }
142:
143: //=============================================================================
|