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.ownership;
025:
026: import java.sql.SQLException;
027: import java.util.HashSet;
028: import java.util.List;
029: import java.util.Set;
030: import java.util.StringTokenizer;
031: import jeeves.interfaces.Service;
032: import jeeves.resources.dbms.Dbms;
033: import jeeves.server.ServiceConfig;
034: import jeeves.server.context.ServiceContext;
035: import jeeves.utils.Util;
036: import org.fao.geonet.GeonetContext;
037: import org.fao.geonet.constants.Geonet;
038: import org.fao.geonet.kernel.DataManager;
039: import org.jdom.Element;
040:
041: //=============================================================================
042:
043: public class Transfer implements Service {
044: public void init(String appPath, ServiceConfig params)
045: throws Exception {
046: }
047:
048: //--------------------------------------------------------------------------
049: //---
050: //--- Service
051: //---
052: //--------------------------------------------------------------------------
053:
054: public Element exec(Element params, ServiceContext context)
055: throws Exception {
056: int sourceUsr = Util.getParamAsInt(params, "sourceUser");
057: int sourceGrp = Util.getParamAsInt(params, "sourceGroup");
058: int targetUsr = Util.getParamAsInt(params, "targetUser");
059: int targetGrp = Util.getParamAsInt(params, "targetGroup");
060:
061: GeonetContext gc = (GeonetContext) context
062: .getHandlerContext(Geonet.CONTEXT_NAME);
063: DataManager dm = gc.getDataManager();
064:
065: Dbms dbms = (Dbms) context.getResourceManager().open(
066: Geonet.Res.MAIN_DB);
067:
068: //--- transfer privileges (if case)
069:
070: Set<String> sourcePriv = retrievePrivileges(dbms, sourceUsr,
071: sourceGrp);
072: Set<String> targetPriv = retrievePrivileges(dbms, targetUsr,
073: targetGrp);
074:
075: //--- a commit just to release some resources
076:
077: dbms.commit();
078:
079: int privCount = 0;
080:
081: Set<Integer> metadata = new HashSet<Integer>();
082:
083: for (String priv : sourcePriv) {
084: StringTokenizer st = new StringTokenizer(priv, "|");
085:
086: int opId = Integer.parseInt(st.nextToken());
087: int mdId = Integer.parseInt(st.nextToken());
088:
089: dm.unsetOperation(dbms, mdId, sourceGrp, opId);
090:
091: if (!targetPriv.contains(priv))
092: dbms.execute(
093: "INSERT INTO OperationAllowed(metadataId, groupId, operationId) "
094: + "VALUES(?,?,?)", mdId, targetGrp,
095: opId);
096:
097: dbms
098: .execute(
099: "UPDATE Metadata SET owner=?, groupOwner=? WHERE id=?",
100: targetUsr, targetGrp, mdId);
101:
102: metadata.add(mdId);
103: privCount++;
104: }
105:
106: dbms.commit();
107:
108: //--- reindex metadata
109:
110: for (int mdId : metadata)
111: dm.indexMetadata(dbms, Integer.toString(mdId));
112:
113: //--- return summary
114:
115: return new Element("response").addContent(
116: new Element("privileges").setText(privCount + ""))
117: .addContent(
118: new Element("metadata").setText(metadata.size()
119: + ""));
120: }
121:
122: //--------------------------------------------------------------------------
123:
124: private Set<String> retrievePrivileges(Dbms dbms, int userId,
125: int groupId) throws SQLException {
126: String query = "SELECT * " + "FROM OperationAllowed, Metadata "
127: + "WHERE metadataId=id AND owner=? AND groupId=?";
128:
129: List list = dbms.select(query, userId, groupId).getChildren();
130:
131: Set<String> result = new HashSet<String>();
132:
133: for (Object o : list) {
134: Element elem = (Element) o;
135: String opId = elem.getChildText("operationid");
136: String mdId = elem.getChildText("metadataid");
137:
138: result.add(opId + "|" + mdId);
139: }
140:
141: return result;
142: }
143: }
144:
145: //=============================================================================
|