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.List;
027: import java.util.Set;
028: import jeeves.constants.Jeeves;
029: import jeeves.interfaces.Service;
030: import jeeves.resources.dbms.Dbms;
031: import jeeves.server.ServiceConfig;
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.MetadataNotFoundEx;
038: import org.fao.geonet.kernel.AccessManager;
039: import org.fao.geonet.kernel.DataManager;
040: import org.fao.geonet.kernel.MdInfo;
041: import org.fao.geonet.lib.Lib;
042: import org.jdom.Element;
043:
044: //=============================================================================
045:
046: /** Given a metadata id returns all operation allowed on it. Called by the
047: * metadata.admin service
048: */
049:
050: public class GetAdminOper implements Service {
051: //--------------------------------------------------------------------------
052: //---
053: //--- Init
054: //---
055: //--------------------------------------------------------------------------
056:
057: public void init(String appPath, ServiceConfig params)
058: throws Exception {
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: DataManager dm = gc.getDataManager();
072: AccessManager am = gc.getAccessManager();
073:
074: Dbms dbms = (Dbms) context.getResourceManager().open(
075: Geonet.Res.MAIN_DB);
076:
077: String id = Util.getParam(params, Params.ID);
078:
079: //-----------------------------------------------------------------------
080: //--- check access
081:
082: MdInfo info = dm.getMetadataInfo(dbms, id);
083:
084: if (info == null)
085: throw new MetadataNotFoundEx(id);
086:
087: Element owner = new Element("owner").setText(info.owner);
088:
089: //--- get all operations
090:
091: Element elOper = Lib.local.retrieve(dbms, "Operations")
092: .setName(Geonet.Elem.OPERATIONS);
093:
094: //-----------------------------------------------------------------------
095: //--- retrieve groups operations
096:
097: Set<String> userGroups = am.getUserGroups(dbms, context
098: .getUserSession(), context.getIpAddress());
099:
100: Element elGroup = Lib.local.retrieve(dbms, "Groups");
101:
102: List list = elGroup.getChildren();
103:
104: for (int i = 0; i < list.size(); i++) {
105: Element el = (Element) list.get(i);
106:
107: el.setName(Geonet.Elem.GROUP);
108:
109: //--- get all operations that this group can do on given metadata
110:
111: String sGrpId = el.getChildText("id");
112:
113: if (!userGroups.contains(sGrpId)) {
114: el.detach();
115: i--;
116: continue;
117: }
118:
119: int grpId = Integer.parseInt(sGrpId);
120:
121: String query = "SELECT operationId FROM OperationAllowed WHERE metadataId=? AND groupId=?";
122:
123: List listAllow = dbms.select(query, new Integer(id), grpId)
124: .getChildren();
125:
126: //--- now extend the group list adding proper operations
127:
128: List listOper = elOper.getChildren();
129:
130: for (int j = 0; j < listOper.size(); j++) {
131: String operId = ((Element) listOper.get(j))
132: .getChildText("id");
133:
134: Element elGrpOper = new Element(Geonet.Elem.OPER)
135: .addContent(new Element(Geonet.Elem.ID)
136: .setText(operId));
137:
138: boolean bFound = false;
139:
140: for (int k = 0; k < listAllow.size(); k++) {
141: Element elAllow = (Element) listAllow.get(k);
142:
143: if (operId.equals(elAllow
144: .getChildText("operationid"))) {
145: bFound = true;
146: break;
147: }
148: }
149:
150: if (bFound)
151: elGrpOper.addContent(new Element(Geonet.Elem.ON));
152:
153: el.addContent(elGrpOper);
154: }
155: }
156:
157: //-----------------------------------------------------------------------
158: //--- put all together
159:
160: Element elRes = new Element(Jeeves.Elem.RESPONSE).addContent(
161: new Element(Geonet.Elem.ID).setText(id)).addContent(
162: elOper).addContent(elGroup).addContent(owner);
163:
164: return elRes;
165: }
166: }
167:
168: //=============================================================================
|