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.util.List;
027: import java.util.Set;
028: import jeeves.interfaces.Service;
029: import jeeves.resources.dbms.Dbms;
030: import jeeves.server.ServiceConfig;
031: import jeeves.server.UserSession;
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.kernel.AccessManager;
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 Groups 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 id = Util.getParamAsInt(params, "id");
057:
058: GeonetContext gc = (GeonetContext) context
059: .getHandlerContext(Geonet.CONTEXT_NAME);
060: DataManager dm = gc.getDataManager();
061: UserSession us = context.getUserSession();
062: AccessManager am = gc.getAccessManager();
063:
064: Dbms dbms = (Dbms) context.getResourceManager().open(
065: Geonet.Res.MAIN_DB);
066:
067: Set<String> userGroups = am.getVisibleGroups(dbms, id);
068: Set<String> myGroups = am.getUserGroups(dbms, us, null);
069:
070: //--- remove 'Intranet' and 'All' groups
071: myGroups.remove("0");
072: myGroups.remove("1");
073:
074: Element response = new Element("response");
075:
076: for (String groupId : userGroups) {
077: String query = "SELECT count(*) as cnt "
078: + "FROM OperationAllowed, Metadata "
079: + "WHERE metadataId = id AND groupId=? AND owner=?";
080:
081: List list = dbms.select(query, new Integer(groupId), id)
082: .getChildren();
083: String size = ((Element) list.get(0)).getChildText("cnt");
084:
085: if (Integer.parseInt(size) != 0) {
086: List records = Lib.local.retrieveById(dbms, "Groups",
087: groupId).getChildren();
088:
089: if (records.size() != 0) {
090: Element record = (Element) records.get(0);
091: record.detach();
092: record.setName("group");
093:
094: response.addContent(record);
095: }
096: }
097: }
098:
099: dbms.commit();
100:
101: for (String groupId : myGroups) {
102: List records = Lib.local.retrieveById(dbms, "Groups",
103: groupId).getChildren();
104:
105: if (records.size() != 0) {
106: Element record = (Element) records.get(0);
107: record.detach();
108: record.setName("targetGroup");
109: response.addContent(record);
110:
111: String query = "SELECT id, surname, name FROM Users, UserGroups "
112: + "WHERE id=userId AND profile='Editor' AND groupId=?";
113:
114: Element editors = dbms.select(query, new Integer(
115: groupId));
116: dbms.commit();
117:
118: for (Object o : editors.getChildren()) {
119: Element editor = (Element) o;
120: editor = (Element) editor.clone();
121: editor.removeChild("password");
122: editor.setName("editor");
123:
124: record.addContent(editor);
125: }
126: }
127: }
128:
129: return response;
130: }
131: }
132:
133: //=============================================================================
|