001: package org.objectweb.salome_tmf.data;
002:
003: import java.util.ArrayList;
004:
005: import org.objectweb.salome_tmf.api.Api;
006: import org.objectweb.salome_tmf.api.Permission;
007: import org.objectweb.salome_tmf.api.Util;
008: import org.objectweb.salome_tmf.api.data.GroupWrapper;
009: import org.objectweb.salome_tmf.api.sql.ISQLGroup;
010:
011: public class Group extends SimpleData {
012:
013: static ISQLGroup pISQLGroup = null;
014:
015: private boolean[] testsPermissions;
016: private boolean[] campaignPermissions;
017:
018: private ArrayList userList;
019:
020: /**************************************************************************/
021: /** CONSTRUCTEUR ***/
022: /**************************************************************************/
023:
024: /**
025: * Constructeur du Groupe. le nom et la description sont instanciés avec la
026: * chaîne vide.
027: */
028: public Group(String name, String description) {
029: super (name, description);
030: testsPermissions = new boolean[3];
031: campaignPermissions = new boolean[4];
032: userList = new ArrayList();
033: if (pISQLGroup == null) {
034: pISQLGroup = Api.getISQLObjectFactory().getISQLGroup();
035: }
036: } // Fin du constructeur Group/0
037:
038: public Group(GroupWrapper pGroupWrapper) {
039: super (pGroupWrapper.getName(), pGroupWrapper.getDescription());
040: int permissions = pGroupWrapper.getPermission();
041: testsPermissions = new boolean[3];
042: campaignPermissions = new boolean[4];
043: changeTestPermissionInModel(
044: 0,
045: ((permissions & Permission.ALLOW_CREATE_TEST) == Permission.ALLOW_CREATE_TEST));
046: changeTestPermissionInModel(
047: 1,
048: ((permissions & Permission.ALLOW_UPDATE_TEST) == Permission.ALLOW_UPDATE_TEST));
049: changeTestPermissionInModel(
050: 2,
051: ((permissions & Permission.ALLOW_DELETE_TEST) == Permission.ALLOW_DELETE_TEST));
052: changeCampaignPermissionInModel(
053: 0,
054: ((permissions & Permission.ALLOW_CREATE_CAMP) == Permission.ALLOW_CREATE_CAMP));
055: changeCampaignPermissionInModel(
056: 1,
057: ((permissions & Permission.ALLOW_UPDATE_CAMP) == Permission.ALLOW_UPDATE_CAMP));
058: changeCampaignPermissionInModel(
059: 2,
060: ((permissions & Permission.ALLOW_DELETE_CAMP) == Permission.ALLOW_DELETE_CAMP));
061: changeCampaignPermissionInModel(
062: 3,
063: ((permissions & Permission.ALLOW_EXECUT_CAMP) == Permission.ALLOW_EXECUT_CAMP));
064: userList = new ArrayList();
065: idBdd = pGroupWrapper.getIdBDD();
066: if (pISQLGroup == null) {
067: pISQLGroup = Api.getISQLObjectFactory().getISQLGroup();
068: }
069: }
070:
071: /**
072: * Constructeur d'un groupe. La description est instanciée avec une chaîne
073: * vide, les permissions et la liste des utilisateurs sont créées vides.
074: * @param n le nom du groupe
075: */
076: public Group(String n) {
077: this (n, "");
078: }
079:
080: public void clearCache() {
081: /* NOTHING */
082: }
083:
084: /********************************** DB **********************************/
085:
086: void addInDB(int idProject) throws Exception {
087: if (isInBase()) {
088: throw new Exception("Group " + name + " already in BDD");
089: }
090: idBdd = pISQLGroup.insert(idProject, name, description, 0);
091: }
092:
093: public void updatePermissionInDB(int perm) throws Exception {
094: if (!isInBase()) {
095: throw new Exception("Group " + name + " is not in BDD");
096: }
097: pISQLGroup.updatePermission(idBdd, perm);
098: }
099:
100: /******************************************************************************/
101: /** METHODES PUBLIQUES ***/
102: /******************************************************************************/
103: void delete() throws Exception {
104: if (!isInBase()) {
105: throw new Exception("Group " + name + " is not in BDD");
106: }
107: pISQLGroup.delete(idBdd);
108: }
109:
110: public void updateInDB(String neWname, String newDescription)
111: throws Exception {
112: if (!isInBase()) {
113: throw new Exception("Group " + name + " is not in BDD");
114: }
115: Util.log("[Group->updateInDB] with " + neWname + ", and "
116: + newDescription);
117: pISQLGroup.updateGroup(idBdd, neWname, newDescription);
118: }
119:
120: public void updateInDBAndModel(String name, String description)
121: throws Exception {
122: updateInDB(name, description);
123: updateInModel(name, description);
124: }
125:
126: public void updateInModel(String name, String description) {
127: this .name = name;
128: this .description = description;
129: }
130:
131: public void updateNameInModel(String name) {
132: this .name = name;
133: }
134:
135: /*public void updateDescriptionInModel(String des){
136: description = des;
137: }*/
138:
139: public void updateUserDescForGroupInDB(User pUser,
140: String description) throws Exception {
141: if (!isInBase()) {
142: throw new Exception("Group " + name + " is not in BDD");
143: }
144: if (!pUser.isInBase()) {
145: throw new Exception("User " + pUser.getLoginFromModel()
146: + " is not in BDD");
147: }
148: pISQLGroup.updateUserDescInGroup(idBdd, pUser.getIdBdd(),
149: description);
150: }
151:
152: /**
153: * Retourne la liste des utilisateurs
154: * @return la liste des utilisateurs
155: */
156: public ArrayList getUserListFromModel() {
157: return userList;
158: }
159:
160: /**setNameInModel
161: * Change une permission du groupe
162: * @param name nom de la permission
163: * @param value valeur (vrai/faux)
164: */
165: public void changeTestPermissionInModel(int index, boolean value) {
166: testsPermissions[index] = value;
167: }
168:
169: /**
170: * Change une permission du groupe
171: * @param name nom de la permission
172: * @param value valeur (vrai/faux)
173: */
174: public void changeCampaignPermissionInModel(int index, boolean value) {
175: campaignPermissions[index] = value;
176: }
177:
178: /**
179: * Ajoute un utilisateur au groupe
180: * @param user un utilisateur
181: */
182: public void addUserInModel(User user) {
183: userList.add(user);
184: }
185:
186: /**
187: * Supprime un utilisateur du groupe
188: * @param user un utilisateur
189: */
190: public void removeUserInModel(User user) {
191: userList.remove(user);
192: }
193:
194: public boolean[] getCampaignPermissionsFromModel() {
195: return campaignPermissions;
196: }
197:
198: public boolean[] getTestsPermissionsFromModel() {
199: return testsPermissions;
200: }
201:
202: public boolean containUserInModel(User user) {
203: for (int i = 0; i < userList.size(); i++) {
204: User pUser = (User) userList.get(i);
205: if (pUser.getLoginFromModel().equals(
206: user.getLoginFromModel())) {
207: return true;
208: }
209: }
210: return false;
211: }
212:
213: ////////////////////////////////////////EQUALS //////////////////////////////////
214: public boolean equals(Object o) {
215: Group testedGroup;
216: boolean test = false;
217: if (!(o instanceof Group)) {
218: return false;
219: }
220: testedGroup = (Group) o;
221: if (isInBase() && testedGroup.isInBase()) {
222: test = idBdd == testedGroup.idBdd;
223: } else {
224: test = name.equals(testedGroup.getNameFromModel());
225: }
226: return test;
227: }
228:
229: public boolean existeInBase() throws Exception {
230: if (!isInBase()) {
231: return false;
232: }
233: return pISQLGroup.getID(DataLoader.getCurrentProject()
234: .getIdBdd(), name) == idBdd;
235: }
236: }
|