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.resources;
025:
026: import java.io.File;
027: import jeeves.exceptions.ObjectNotFoundEx;
028: import jeeves.exceptions.OperationAbortedEx;
029: import jeeves.interfaces.Service;
030: import jeeves.server.ServiceConfig;
031: import jeeves.server.context.ServiceContext;
032: import jeeves.utils.Util;
033: import org.fao.geonet.GeonetContext;
034: import org.fao.geonet.constants.Geonet;
035: import org.fao.geonet.constants.Params;
036: import org.fao.geonet.kernel.DataManager;
037: import org.fao.geonet.lib.Lib;
038: import org.fao.geonet.services.metadata.Update;
039: import org.jdom.Element;
040:
041: //=============================================================================
042:
043: /** Deletes an uploaded file from the database
044: */
045:
046: public class Remove implements Service {
047: private Element config;
048: private Update update = new Update();
049:
050: //-----------------------------------------------------------------------------
051: //---
052: //--- Init
053: //---
054: //-----------------------------------------------------------------------------
055:
056: public void init(String appPath, ServiceConfig params)
057: throws Exception {
058: update.init(appPath, params);
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 id = Util.getParam(params, Params.ID);
075: String ref = Util.getParam(params, Params.REF);
076: String access = Util.getParam(params, Params.ACCESS);
077:
078: Lib.resource.checkEditPrivilege(context, id);
079:
080: // get online resource name
081: Element metadata = dataMan.getMetadata(context, id, true);
082: Element elem = dataMan.getElementByRef(metadata, ref);
083:
084: if (elem == null)
085: throw new ObjectNotFoundEx("element with ref='" + ref + "'");
086:
087: String fname = elem.getText();
088:
089: // delete online resource
090: File dir = new File(Lib.resource.getDir(context, access, id));
091: File file = new File(dir, fname);
092:
093: if (file.exists() && !file.delete())
094: throw new OperationAbortedEx("unable to delete resource");
095:
096: // update the metadata
097: params.addContent(new Element("_" + ref));
098: Element version = params.getChild("version");
099: version.setText((Integer.parseInt(version.getText()) + 1) + "");
100: return update.exec(params, context);
101: }
102: }
103:
104: //=============================================================================
|