001: /*
002: * SalomeTMF is a Test Management Framework
003: * Copyright (C) 2005 France Telecom R&D
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: *
019: * @author Marche Mikael
020: *
021: * Contact: mikael.marche@rd.francetelecom.com
022: */
023:
024: package org.objectweb.salome_tmf.data;
025:
026: import java.sql.Date;
027:
028: import org.objectweb.salome_tmf.api.Api;
029: import org.objectweb.salome_tmf.api.Util;
030: import org.objectweb.salome_tmf.api.data.UserWrapper;
031: import org.objectweb.salome_tmf.api.sql.ISQLPersonne;
032:
033: public class User extends SimpleData {
034:
035: static ISQLPersonne pISQLPersonne = null;
036:
037: //private String login = name;
038: private String lastName;
039: protected String firstName;
040: protected String email;
041: protected String phoneNumber;
042: protected Date creationDate;
043:
044: public User(String login, String description) {
045: super (login, description);
046: lastName = "";
047: firstName = "";
048: email = "";
049: phoneNumber = "";
050: if (pISQLPersonne == null) {
051: pISQLPersonne = Api.getISQLObjectFactory()
052: .getISQLPersonne();
053: }
054: }
055:
056: public User(UserWrapper pUser) {
057: super (pUser.getLogin(), pUser.getDescription());
058: lastName = pUser.getName();
059: firstName = pUser.getPrenom();
060: email = pUser.getEmail();
061: phoneNumber = pUser.getTel();
062: creationDate = pUser.getCreateDate();
063: idBdd = pUser.getIdBDD();
064: if (pISQLPersonne == null) {
065: pISQLPersonne = Api.getISQLObjectFactory()
066: .getISQLPersonne();
067: }
068: }
069:
070: public User(String login) throws Exception {
071: super (login, "");
072: if (pISQLPersonne == null) {
073: pISQLPersonne = Api.getISQLObjectFactory()
074: .getISQLPersonne();
075: }
076: UserWrapper pUser = pISQLPersonne.getUserByLogin(login);
077: description = pUser.getDescription();
078: lastName = pUser.getName();
079: firstName = pUser.getPrenom();
080: email = pUser.getEmail();
081: phoneNumber = pUser.getTel();
082: creationDate = pUser.getCreateDate();
083: idBdd = pUser.getIdBDD();
084: }
085:
086: public void clearCache() {
087: /* NOTHING */
088: }
089:
090: public Date getCreationDateFromModel() {
091: return creationDate;
092: }
093:
094: public String getEmailFromModel() {
095: return email;
096: }
097:
098: public String getFirstNameFromModel() {
099: return firstName;
100: }
101:
102: public String getLoginFromModel() {
103: return name;
104: }
105:
106: public String getPhoneNumberFromModel() {
107: return phoneNumber;
108: }
109:
110: public String getLastNameFromModel() {
111: return lastName;
112: }
113:
114: public void setLastNameInModel(String string) {
115: lastName = string;
116: }
117:
118: public void setCreationDateInModel(Date date) {
119: creationDate = date;
120: }
121:
122: public void setEmailInModel(String string) {
123: email = string;
124: }
125:
126: public void setFirstNameInModel(String string) {
127: firstName = string;
128: }
129:
130: public void setLoginInModel(String string) {
131: name = string;
132: }
133:
134: public void setPhoneNumberInModel(String string) {
135: phoneNumber = string;
136: }
137:
138: /****************************** DB **********************************/
139: public void addInDB(String pwd) throws Exception {
140: //public void addInDB(String login, String name, String firstName, String desc, String email, String tel, String pwd) throws Exception{
141: if (isInBase()) {
142: throw new Exception("User " + name + " is already in BDD");
143: }
144: int id = pISQLPersonne.insert(name, lastName, firstName,
145: description, email, phoneNumber, pwd, true);
146: setIdBdd(id);
147: }
148:
149: public String getPasswordFromDB() throws Exception {
150: if (!isInBase()) {
151: throw new Exception("User " + name + " is not in BDD");
152: }
153: UserWrapper pUserWrapper = pISQLPersonne.getUserByLogin(name);
154: return pUserWrapper.getPassword();
155: }
156:
157: public String updatePasswordInDB(String newPassword)
158: throws Exception {
159: if (!isInBase()) {
160: throw new Exception("User " + name + " is not in BDD");
161: }
162: return pISQLPersonne.updatePassword(name, newPassword, true);
163: }
164:
165: public void updateInDB(String newLogin, String newName,
166: String newFirstName, String newDesc, String newEmail,
167: String newTel) throws Exception {
168: if (!isInBase()) {
169: throw new Exception("User " + name + " is not in BDD");
170: }
171: pISQLPersonne.update(idBdd, newLogin, newName, newFirstName,
172: newDesc, newEmail, newTel);
173: }
174:
175: public void updateInModel(String newLogin, String newName,
176: String newFirstName, String newDesc, String newEmail,
177: String newTel) {
178: name = newLogin;
179: lastName = newName;
180: firstName = newFirstName;
181: description = newDesc;
182: email = newEmail;
183: phoneNumber = newTel;
184: }
185:
186: public void updateInDBModel(String newLogin, String newName,
187: String newFirstName, String newDesc, String newEmail,
188: String newTel) throws Exception {
189: updateInDB(newLogin, newName, newFirstName, newDesc, newEmail,
190: newTel);
191: updateInModel(newLogin, newName, newFirstName, newDesc,
192: newEmail, newTel);
193: }
194:
195: public void updateInDBAndModel(String newName, String newDesc)
196: throws Exception {
197: throw new Exception("NOT IMPLEMENTED");
198: }
199:
200: void deleteInDB() throws Exception {
201: if (!isInBase()) {
202: throw new Exception("User " + name + " is not in BDD");
203: }
204: //Util.log("[User->deleteInDB] will delete " + name);
205: pISQLPersonne.deleteById(idBdd);
206: }
207:
208: void deleteInProjectInDB(String project) throws Exception {
209: if (!isInBase()) {
210: throw new Exception("User " + name + " is not in BDD");
211: }
212: //Util.log("[User->deleteInDB] will delete " + name);
213: pISQLPersonne.deleteInProject(idBdd, project);
214: }
215:
216: public boolean existeInBase() throws Exception {
217: if (!isInBase()) {
218: return false;
219: }
220: return pISQLPersonne.getID(name) == idBdd;
221: }
222:
223: //////////////////////////////////////// EQUALS //////////////////////////////////
224: public boolean equals(Object o) {
225: User testedUser;
226: boolean test = false;
227: if (!(o instanceof User)) {
228: return false;
229: }
230: testedUser = (User) o;
231: if (isInBase() && testedUser.isInBase()) {
232: test = idBdd == testedUser.idBdd;
233: } else {
234: test = name.equals(testedUser.getNameFromModel());
235: }
236: return test;
237: }
238:
239: public int hashCode() {
240: if (isInBase())
241: return idBdd;
242: else
243: return super.hashCode();
244: }
245: }
|