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.thumbnail;
025:
026: import java.io.File;
027: import jeeves.exceptions.OperationAbortedEx;
028: import jeeves.interfaces.Service;
029: import jeeves.resources.dbms.Dbms;
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.exceptions.ConcurrentUpdateEx;
037: import org.fao.geonet.kernel.DataManager;
038: import org.fao.geonet.lib.Lib;
039: import org.jdom.Element;
040:
041: //=============================================================================
042:
043: public class Unset implements Service {
044: //--------------------------------------------------------------------------
045: //---
046: //--- Init
047: //---
048: //--------------------------------------------------------------------------
049:
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 id = Util.getParam(params, Params.ID);
063: String type = Util.getParam(params, Params.TYPE);
064: String version = Util.getParam(params, Params.VERSION);
065:
066: Lib.resource.checkEditPrivilege(context, id);
067:
068: //-----------------------------------------------------------------------
069: //--- extract thumbnail filename
070:
071: GeonetContext gc = (GeonetContext) context
072: .getHandlerContext(Geonet.CONTEXT_NAME);
073:
074: DataManager dataMan = gc.getDataManager();
075:
076: Dbms dbms = (Dbms) context.getResourceManager().open(
077: Geonet.Res.MAIN_DB);
078:
079: //--- check if the metadata has been modified from last time
080:
081: if (version != null && !dataMan.getVersion(id).equals(version))
082: throw new ConcurrentUpdateEx(id);
083:
084: Element result = dataMan.getThumbnails(dbms, id);
085:
086: if (result == null)
087: throw new OperationAbortedEx("Metadata not found", id);
088:
089: result = result.getChild(type);
090:
091: if (result == null)
092: throw new OperationAbortedEx("Metadata has no thumbnail",
093: id);
094:
095: String file = Lib.resource.getDir(context,
096: Params.Access.PUBLIC, id)
097: + result.getText();
098:
099: //-----------------------------------------------------------------------
100: //--- remove thumbnail
101:
102: dataMan.unsetThumbnail(dbms, id, type.equals("small"));
103:
104: if (!new File(file).delete())
105: context.error("Error while deleting thumbnail : " + file);
106:
107: //-----------------------------------------------------------------------
108:
109: Element response = new Element("a");
110: response.addContent(new Element("id").setText(id));
111: response.addContent(new Element("version").setText(dataMan
112: .getNewVersion(id)));
113:
114: return response;
115: }
116: }
117:
118: //=============================================================================
|