001: package org.objectweb.salome_tmf.databaseSQL;
002:
003: import java.sql.PreparedStatement;
004: import java.sql.ResultSet;
005: import java.util.Enumeration;
006: import java.util.Hashtable;
007: import java.util.Vector;
008:
009: import org.objectweb.salome_tmf.api.ApiConstants;
010: import org.objectweb.salome_tmf.api.data.GroupWrapper;
011: import org.objectweb.salome_tmf.api.data.UrlAttachementWrapper;
012: import org.objectweb.salome_tmf.api.data.UserWrapper;
013: import org.objectweb.salome_tmf.api.sql.ISQLGroup;
014: import org.objectweb.salome_tmf.api.sql.ISQLPersonne;
015:
016: public class SQLProjectGForge extends SQLProject {
017:
018: /**
019: * Get a Vector of UserWrapper representing all the Users in the project projectName
020: * And Upate Table Persone with GFORGE user of the project
021: * @param projectName
022: * @throws Exception
023: */
024: public UserWrapper[] getUsersOfProject(String projectName)
025: throws Exception {
026: Vector resultSalome = new Vector();
027:
028: Hashtable userSalome = new Hashtable();
029: Hashtable userGforge = new Hashtable();
030:
031: PreparedStatement prep = SQLEngine
032: .getSQLSelectQuery("selectAllProjectUsers"); //ok
033: prep.setString(1, projectName);
034: ResultSet stmtRes = SQLEngine.runSelectQuery(prep);
035:
036: while (stmtRes.next()) {
037: UserWrapper pUserWrapper = new UserWrapper();
038: pUserWrapper.setIdBDD(stmtRes.getInt("id_personne"));
039: pUserWrapper.setLogin(stmtRes.getString("login_personne"));
040: pUserWrapper.setName(stmtRes.getString("nom_personne"));
041: pUserWrapper
042: .setPrenom(stmtRes.getString("prenom_personne"));
043: pUserWrapper.setDescription(stmtRes
044: .getString("desc_personne"));
045: pUserWrapper.setEmail(stmtRes.getString("email_personne"));
046: pUserWrapper.setTel(stmtRes.getString("tel_personne"));
047: pUserWrapper.setCreateDate(stmtRes
048: .getDate("date_creation_personne"));
049: pUserWrapper.setCreateTime(stmtRes.getTime(
050: "heure_creation_personne").getTime());
051: pUserWrapper.setPassword(stmtRes.getString("mot_de_passe"));
052: resultSalome.add(pUserWrapper);
053: userSalome
054: .put(pUserWrapper.getLogin().trim(), pUserWrapper);
055: }
056:
057: /*prep = SQLEngineGForge.getSQLSelectGForgeQuery("selectAllGForgeUsers");
058: stmtRes = SQLEngineGForge.runSelectQuery(prep);
059: while (stmtRes.next()) {
060: System.out.println(stmtRes);
061: }*/
062:
063: Vector resultGforge = new Vector();
064: prep = SQLEngineGForge
065: .getSQLSelectGForgeQuery("selectAllGForgeProjectUsers");
066: prep.setString(1, projectName);
067: //prep.setString(1,"salome_demo");
068:
069: stmtRes = SQLEngineGForge.runSelectQuery(prep);
070: while (stmtRes.next()) {
071: //System.out.println("Add user GFORGE " + stmtRes);
072: UserWrapper pUserWrapper = new UserWrapper();
073: pUserWrapper.setIdBDD(stmtRes.getInt("user_id"));
074: pUserWrapper.setLogin(stmtRes.getString("user_name"));
075: pUserWrapper.setName(stmtRes.getString("lastname"));
076: pUserWrapper.setPrenom(stmtRes.getString("firstname"));
077: pUserWrapper.setDescription(stmtRes.getString("title"));
078: pUserWrapper.setEmail(stmtRes.getString("email"));
079: pUserWrapper.setTel(stmtRes.getString("phone"));
080: pUserWrapper.setPassword(stmtRes.getString("user_pw"));
081: resultGforge.add(pUserWrapper);
082: userGforge
083: .put(pUserWrapper.getLogin().trim(), pUserWrapper);
084: }
085:
086: //Ajout des Users GForge qui ne sont pas dans Salome
087: SQLEngineGForge.closeGforgeDB();
088:
089: int transNumber = -1;
090: int groupID;
091: ISQLGroup pISQISQLGroup = SQLObjectFactory
092: .getInstanceOfISQLGroup();
093: try {
094: transNumber = SQLEngine.beginTransaction(0,
095: ApiConstants.INSERT_GROUP);
096:
097: int projectID = getProject(projectName).getIdBDD();
098: GroupWrapper[] groupsOfTheProject = getProjectGroups(projectID);
099: GroupWrapper pGForgeGroupWrapper = null;
100: int i = 0;
101: boolean find = false;
102: while (!find && i < groupsOfTheProject.length) {
103: GroupWrapper pGroupWrapper = groupsOfTheProject[i];
104: if (pGroupWrapper.getName().trim().equals("GForge")) {
105: find = true;
106: pGForgeGroupWrapper = pGroupWrapper;
107: }
108: i++;
109: }
110:
111: if (pGForgeGroupWrapper == null) {
112: // Ajout du group GForge
113: groupID = pISQISQLGroup.insert(projectID, "GForge",
114: "Users registred in GForge", 254);
115: } else {
116: groupID = pGForgeGroupWrapper.getIdBDD();
117: }
118: SQLEngine.commitTrans(transNumber);
119: } catch (Exception e) {
120: e.printStackTrace();
121: SQLEngine.rollBackTrans(transNumber);
122:
123: UserWrapper[] uwArray = new UserWrapper[resultSalome.size()];
124: for (int i = 0; i < resultSalome.size(); i++) {
125: uwArray[i] = (UserWrapper) resultSalome.get(i);
126: }
127: return uwArray;
128: }
129:
130: Enumeration loginGforgeEnum = userGforge.keys();
131: ISQLPersonne pISQLPersonne = SQLObjectFactoryGforge
132: .getInstanceOfISQLPersonne();
133: while (loginGforgeEnum.hasMoreElements()) {
134: try {
135: transNumber = SQLEngine.beginTransaction(0,
136: ApiConstants.INSERT_GROUP);
137: String login = (String) loginGforgeEnum.nextElement();
138: UserWrapper pSalomeUserWrapper = (UserWrapper) userSalome
139: .get(login);
140: UserWrapper pGForgeUserWrapper = (UserWrapper) userGforge
141: .get(login);
142: if (pSalomeUserWrapper == null) {
143: int userID = pISQLPersonne.insert(
144: pGForgeUserWrapper.getLogin(),
145: pGForgeUserWrapper.getName(),
146: pGForgeUserWrapper.getPrenom(),
147: pGForgeUserWrapper.getDescription(),
148: pGForgeUserWrapper.getEmail(),
149: pGForgeUserWrapper.getTel(),
150: pGForgeUserWrapper.getPassword(), false);
151:
152: pISQISQLGroup.insertUser(groupID, userID);
153:
154: resultSalome.add(userGforge.get(login));
155: } else {
156: //update password
157: pISQLPersonne.updatePassword(login,
158: pGForgeUserWrapper.getPassword(), false);
159: }
160: SQLEngine.commitTrans(transNumber);
161: } catch (Exception e) {
162: e.printStackTrace();
163: SQLEngine.rollBackTrans(transNumber);
164: }
165: }
166:
167: UserWrapper[] uwArray = new UserWrapper[resultSalome.size()];
168: for (int i = 0; i < resultSalome.size(); i++) {
169: uwArray[i] = (UserWrapper) resultSalome.get(i);
170: }
171: return uwArray;
172: }
173: }
|