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.main;
025:
026: import java.sql.SQLException;
027: import java.util.ArrayList;
028: import java.util.HashSet;
029: import java.util.Iterator;
030: import java.util.List;
031: import java.util.Set;
032: import jeeves.exceptions.BadParameterEx;
033: import jeeves.interfaces.Service;
034: import jeeves.resources.dbms.Dbms;
035: import jeeves.server.ServiceConfig;
036: import jeeves.server.UserSession;
037: import jeeves.server.context.ServiceContext;
038: import jeeves.utils.Xml;
039: import org.fao.geonet.GeonetContext;
040: import org.fao.geonet.constants.Geonet;
041: import org.fao.geonet.kernel.setting.SettingManager;
042: import org.fao.geonet.lib.Lib;
043: import org.jdom.Element;
044:
045: //=============================================================================
046:
047: public class Info implements Service {
048: private String xslPath;
049:
050: //--------------------------------------------------------------------------
051: //---
052: //--- Init
053: //---
054: //--------------------------------------------------------------------------
055:
056: public void init(String appPath, ServiceConfig config)
057: throws Exception {
058: xslPath = appPath + Geonet.Path.STYLESHEETS + "/xml";
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: SettingManager sm = gc.getSettingManager();
072:
073: Dbms dbms = (Dbms) context.getResourceManager().open(
074: Geonet.Res.MAIN_DB);
075:
076: Element result = new Element("root");
077:
078: for (Iterator i = params.getChildren().iterator(); i.hasNext();) {
079: Element el = (Element) i.next();
080:
081: String name = el.getName();
082: String type = el.getText();
083:
084: if (!name.equals("type"))
085: throw new BadParameterEx(name, type);
086:
087: if (type.equals("site"))
088: result.addContent(gc.getSettingManager().get("system",
089: -1));
090:
091: else if (type.equals("categories"))
092: result.addContent(Lib.local
093: .retrieve(dbms, "Categories"));
094:
095: else if (type.equals("groups"))
096: result.addContent(getGroups(context, dbms));
097:
098: else if (type.equals("operations"))
099: result.addContent(Lib.local
100: .retrieve(dbms, "Operations"));
101:
102: else if (type.equals("regions"))
103: result.addContent(Lib.local.retrieve(dbms, "Regions"));
104:
105: else if (type.equals("sources"))
106: result.addContent(getSources(dbms, sm));
107:
108: else if (type.equals("users"))
109: result.addContent(getUsers(context, dbms));
110:
111: else
112: throw new BadParameterEx("type", type);
113: }
114:
115: result.addContent(getEnv(context));
116:
117: return Xml.transform(result, xslPath + "/info.xsl");
118: }
119:
120: //--------------------------------------------------------------------------
121: //---
122: //--- Private methods
123: //---
124: //--------------------------------------------------------------------------
125:
126: private Element getGroups(ServiceContext context, Dbms dbms)
127: throws SQLException {
128: UserSession session = context.getUserSession();
129:
130: if (!session.isAuthenticated())
131: return Lib.local.retrieve(dbms, "Groups", "id < 2", "id");
132:
133: //--- retrieve user groups
134:
135: if (Geonet.Profile.ADMINISTRATOR.equals(session.getProfile()))
136: return Lib.local.retrieve(dbms, "Groups", null, "id");
137: else {
138: String query = "SELECT groupId as id FROM UserGroups WHERE "
139: + "userId=" + session.getUserId();
140:
141: Set<String> ids = Lib.element.getIds(dbms.select(query));
142: Element groups = Lib.local.retrieve(dbms, "Groups", null,
143: "id");
144:
145: return Lib.element.pruneChildren(groups, ids);
146: }
147: }
148:
149: //--------------------------------------------------------------------------
150:
151: private Element getSources(Dbms dbms, SettingManager sm)
152: throws SQLException {
153: String query = "SELECT * FROM Sources ORDER BY name";
154: Element sources = new Element("sources");
155:
156: String siteId = sm.getValue("system/site/siteId");
157: String siteName = sm.getValue("system/site/name");
158:
159: add(sources, siteId, siteName);
160:
161: for (Object o : dbms.select(query).getChildren()) {
162: Element rec = (Element) o;
163:
164: String uuid = rec.getChildText("uuid");
165: String name = rec.getChildText("name");
166:
167: add(sources, uuid, name);
168: }
169:
170: return sources;
171: }
172:
173: //--------------------------------------------------------------------------
174: //--- Users
175: //--------------------------------------------------------------------------
176:
177: private Element getUsers(ServiceContext context, Dbms dbms)
178: throws SQLException {
179: UserSession us = context.getUserSession();
180: List list = getUsers(context, us, dbms);
181:
182: Element users = new Element("users");
183:
184: for (Object o : list) {
185: Element user = (Element) o;
186:
187: user = (Element) user.clone();
188: user.removeChild("password");
189: user.setName("user");
190:
191: users.addContent(user);
192: }
193:
194: return users;
195: }
196:
197: //--------------------------------------------------------------------------
198:
199: private List getUsers(ServiceContext context, UserSession us,
200: Dbms dbms) throws SQLException {
201: if (!us.isAuthenticated())
202: return new ArrayList<Element>();
203:
204: int id = Integer.parseInt(us.getUserId());
205:
206: if (us.getProfile().equals(Geonet.Profile.ADMINISTRATOR))
207: return dbms.select("SELECT * FROM Users").getChildren();
208:
209: if (!us.getProfile().equals(Geonet.Profile.USER_ADMIN))
210: return dbms.select("SELECT * FROM Users WHERE id=?", id)
211: .getChildren();
212:
213: //--- we have a user admin
214:
215: Set<String> hsMyGroups = getUserGroups(dbms, id);
216:
217: Set profileSet = context.getProfileManager().getProfilesSet(
218: us.getProfile());
219:
220: //--- retrieve all users
221:
222: Element elUsers = dbms
223: .select("SELECT * FROM Users ORDER BY username");
224:
225: //--- now filter them
226:
227: ArrayList<Element> alToRemove = new ArrayList<Element>();
228:
229: for (Object o : elUsers.getChildren()) {
230: Element elRec = (Element) o;
231:
232: String userId = elRec.getChildText("id");
233: String profile = elRec.getChildText("profile");
234:
235: if (!profileSet.contains(profile))
236: alToRemove.add(elRec);
237:
238: else if (!hsMyGroups.containsAll(getUserGroups(dbms,
239: Integer.parseInt(userId))))
240: alToRemove.add(elRec);
241: }
242:
243: //--- remove unwanted users
244:
245: for (int i = 0; i < alToRemove.size(); i++)
246: alToRemove.get(i).detach();
247:
248: //--- return result
249:
250: return elUsers.getChildren();
251: }
252:
253: //--------------------------------------------------------------------------
254:
255: private Set<String> getUserGroups(Dbms dbms, int id)
256: throws SQLException {
257: String query = "SELECT groupId AS id FROM UserGroups WHERE userId=?";
258:
259: List list = dbms.select(query, id).getChildren();
260:
261: HashSet<String> hs = new HashSet<String>();
262:
263: for (int i = 0; i < list.size(); i++) {
264: Element el = (Element) list.get(i);
265: hs.add(el.getChildText("id"));
266: }
267:
268: return hs;
269: }
270:
271: //--------------------------------------------------------------------------
272: //---
273: //--- General purpose methods
274: //---
275: //--------------------------------------------------------------------------
276:
277: private void add(Element sources, String uuid, String name) {
278: Element source = new Element("source").addContent(
279: new Element("uuid").setText(uuid)).addContent(
280: new Element("name").setText(name));
281:
282: sources.addContent(source);
283: }
284:
285: //--------------------------------------------------------------------------
286:
287: private Element getEnv(ServiceContext context) {
288: return new Element("env").addContent(new Element("baseURL")
289: .setText(context.getBaseUrl()));
290: }
291: }
292:
293: //=============================================================================
|