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.user;
025:
026: import java.util.ArrayList;
027: import java.util.Vector;
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.constants.Geonet;
035: import org.fao.geonet.constants.Params;
036: import org.jdom.Element;
037:
038: //=============================================================================
039:
040: /** Update the information of a user
041: */
042:
043: public class Update 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 = params.getChildText(Params.ID);
063: String username = Util.getParam(params, Params.USERNAME);
064: String password = Util.getParam(params, Params.PASSWORD);
065: String surname = Util.getParam(params, Params.SURNAME, "");
066: String name = Util.getParam(params, Params.NAME, "");
067: String profile = Util.getParam(params, Params.PROFILE);
068: String address = Util.getParam(params, Params.ADDRESS, "");
069: String state = Util.getParam(params, Params.STATE, "");
070: String zip = Util.getParam(params, Params.ZIP, "");
071: String country = Util.getParam(params, Params.COUNTRY, "");
072: String email = Util.getParam(params, Params.EMAIL, "");
073: String organ = Util.getParam(params, Params.ORG, "");
074: String kind = Util.getParam(params, Params.KIND, "");
075:
076: if (!context.getProfileManager().exists(profile))
077: throw new Exception("Unkown profile : " + profile);
078:
079: java.util.List listGroups = params.getChildren(Params.GROUPS);
080:
081: if (profile.equals(Geonet.Profile.ADMINISTRATOR))
082: listGroups = new ArrayList();
083:
084: Dbms dbms = (Dbms) context.getResourceManager().open(
085: Geonet.Res.MAIN_DB);
086:
087: if (id == null) // For Adding new user
088: {
089: id = context.getSerialFactory().getSerial(dbms, "Users")
090: + "";
091:
092: String query = "INSERT INTO Users (id, username, password, surname, name, profile, "
093: + "address, state, zip, country, email, organisation, kind) "
094: + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
095:
096: dbms.execute(query, new Integer(id), username, Util
097: .scramble(password), surname, name, profile,
098: address, state, zip, country, email, organ, kind);
099:
100: //--- add groups
101:
102: for (int i = 0; i < listGroups.size(); i++) {
103: String group = ((Element) listGroups.get(i)).getText();
104: addGroup(dbms, id, group);
105: }
106: }
107:
108: else //--- For Update
109: {
110: String query = "UPDATE Users SET username=?, password=?, surname=?, name=?, "
111: + "profile=?, address=?, state=?, zip=?, country=?, email=?,"
112: + "organisation=?, kind=? WHERE id=?";
113:
114: dbms.execute(query, username, Util.scramble(password),
115: surname, name, profile, address, state, zip,
116: country, email, organ, kind, new Integer(id));
117:
118: //--- add groups
119:
120: dbms.execute("DELETE FROM UserGroups WHERE userId=?",
121: new Integer(id));
122:
123: for (int i = 0; i < listGroups.size(); i++) {
124: String group = ((Element) listGroups.get(i)).getText();
125: addGroup(dbms, id, group);
126: }
127: }
128:
129: return new Element(Jeeves.Elem.RESPONSE);
130: }
131:
132: //--------------------------------------------------------------------------
133: //---
134: //--- Private methods
135: //---
136: //--------------------------------------------------------------------------
137:
138: /** Adds a user to a group
139: */
140:
141: private void addGroup(Dbms dbms, String user, String group)
142: throws Exception {
143: dbms
144: .execute(
145: "INSERT INTO UserGroups(userId, groupId) VALUES (?, ?)",
146: new Integer(user), new Integer(group));
147: }
148: }
149:
150: //=============================================================================
|