001: package org.objectweb.salome_tmf.databaseSQL;
002:
003: import java.sql.Date;
004: import java.sql.PreparedStatement;
005: import java.sql.ResultSet;
006: import java.util.Vector;
007:
008: import org.objectweb.salome_tmf.api.data.FileAttachementWrapper;
009: import org.objectweb.salome_tmf.api.data.LockInfoWrapper;
010: import org.objectweb.salome_tmf.api.sql.ISQLSalomeLock;
011:
012: public class SQLSalomeLock implements ISQLSalomeLock {
013:
014: static boolean tableExist = false;
015:
016: public SQLSalomeLock() {
017: if (!tableExist) {
018: try {
019: PreparedStatement prep;
020: prep = SQLEngine.getStatement("BEGIN");
021: prep.execute();
022:
023: prep = SQLEngine.getSQLCommonQuery("showTable"); //ok
024: ResultSet stmtRes = SQLEngine.runSelectQuery(prep);
025: boolean trouve = false;
026: while (stmtRes.next() && !trouve) {
027: String tableName = stmtRes.getString(1);
028: String nomTable = "SALOME_LOCK";
029: if (tableName.equals(nomTable)
030: || tableName.equals(nomTable.toLowerCase())) {
031: trouve = true;
032: }
033: }
034: if (trouve == false) {
035: prep = SQLEngine
036: .getSQLCommonQuery("createLockTable"); //ok
037: SQLEngine.runAddQuery(prep);
038: }
039: tableExist = true;
040: prep = SQLEngine.getStatement("COMMIT");
041: prep.execute();
042: } catch (Exception e) {
043: try {
044: PreparedStatement prep = SQLEngine
045: .getStatement("ROLLBACK");
046: prep.execute();
047: } catch (Exception e2) {
048:
049: }
050: }
051: }
052: }
053:
054: public LockInfoWrapper isLock(int projectID) throws Exception {
055: LockInfoWrapper pLockInfo = null;
056: try {
057: //transNumber = SQLEngine.beginTransaction(0, ApiConstants.READ_LOCK);
058: //LOCK IN SHARE MODE
059: PreparedStatement prep;
060: prep = SQLEngine.getStatement("BEGIN");
061: prep.execute();
062:
063: prep = SQLEngine.getSQLSelectQuery("selectSalomeLock"); //ok
064: prep.setInt(1, projectID);
065: ResultSet stmtRes = SQLEngine.runSelectQuery(prep);
066: int lock_code = 0;
067: while (stmtRes.next()) {
068: int personne_id = stmtRes.getInt("id_personne");
069: lock_code = lock_code | stmtRes.getInt("lock_code");
070: int action_code = stmtRes.getInt("action_code");
071: int pid = stmtRes.getInt("pid");
072: String info = stmtRes.getString("info");
073: Date date = stmtRes.getDate("lock_date");
074: pLockInfo = new LockInfoWrapper(projectID, personne_id,
075: pid, lock_code, action_code, info, date);
076: }
077:
078: prep = SQLEngine.getStatement("COMMIT");
079: prep.execute();
080: //SQLEngine.commitTrans(transNumber);
081: } catch (Exception e) {
082: PreparedStatement prep = SQLEngine.getStatement("ROLLBACK");
083: prep.execute();
084: //SQLEngine.rollBackTrans(transNumber);
085: throw e;
086: }
087: return pLockInfo;
088: }
089:
090: public LockInfoWrapper[] getAllProjectLocks(int projectID)
091: throws Exception {
092: Vector lockInfoList = new Vector();
093: try {
094: //transNumber = SQLEngine.beginTransaction(0, ApiConstants.READ_LOCK);
095: //LOCK IN SHARE MODE
096: PreparedStatement prep;
097: prep = SQLEngine.getStatement("BEGIN");
098: prep.execute();
099:
100: prep = SQLEngine.getSQLSelectQuery("selectSalomeLock"); //ok
101: prep.setInt(1, projectID);
102: ResultSet stmtRes = SQLEngine.runSelectQuery(prep);
103: while (stmtRes.next()) {
104: int personne_id = stmtRes.getInt("id_personne");
105: int lock_code = stmtRes.getInt("lock_code");
106: int action_code = stmtRes.getInt("action_code");
107: int pid = stmtRes.getInt("pid");
108: String info = stmtRes.getString("info");
109: Date date = stmtRes.getDate("lock_date");
110: LockInfoWrapper pLockInfo = new LockInfoWrapper(
111: projectID, personne_id, pid, lock_code,
112: action_code, info, date);
113: lockInfoList.addElement(pLockInfo);
114: }
115:
116: prep = SQLEngine.getStatement("COMMIT");
117: prep.execute();
118: //SQLEngine.commitTrans(transNumber);
119: } catch (Exception e) {
120: PreparedStatement prep = SQLEngine.getStatement("ROLLBACK");
121: prep.execute();
122: //SQLEngine.rollBackTrans(transNumber);
123: throw e;
124: }
125: LockInfoWrapper[] liwArray = new LockInfoWrapper[lockInfoList
126: .size()];
127: for (int i = 0; i < lockInfoList.size(); i++) {
128: liwArray[i] = (LockInfoWrapper) lockInfoList.get(i);
129: }
130: return liwArray;
131: }
132:
133: public void insert(int projectID, int personneId, int lock_code,
134: int action_code, String info, int pid) throws Exception {
135: try {
136: PreparedStatement prep;
137: //transNumber = SQLEngine.beginTransaction(0, ApiConstants.INSERT_LOCK);
138: prep = SQLEngine.getStatement("BEGIN");
139: prep.execute();
140:
141: prep = SQLEngine.getSQLAddQuery("addSalomeLock");
142: prep.setInt(1, projectID);
143: prep.setInt(2, personneId);
144: prep.setInt(3, pid);
145: prep.setInt(4, lock_code);
146: prep.setInt(5, action_code);
147: prep.setString(6, info);
148: prep.setDate(7, org.objectweb.salome_tmf.api.Util
149: .getCurrentDate());
150: SQLEngine.runAddQuery(prep);
151:
152: prep = SQLEngine.getStatement("COMMIT");
153: prep.execute();
154: } catch (Exception e) {
155: PreparedStatement prep = SQLEngine.getStatement("ROLLBACK");
156: prep.execute();
157: //SQLEngine.rollBackTrans(transNumber);
158: throw e;
159: }
160: }
161:
162: public void delete(int projectID, int pid) throws Exception {
163: //int transNumber = -1;
164: try {
165: PreparedStatement prep;
166: //transNumber = SQLEngine.beginTransaction(0, ApiConstants.DELETE_LOCK);
167: prep = SQLEngine.getStatement("BEGIN");
168: prep.execute();
169:
170: prep = SQLEngine.getSQLDeleteQuery("deleteSalomeLock"); //ok
171: prep.setInt(1, projectID);
172: prep.setInt(2, pid);
173: SQLEngine.runDeleteQuery(prep);
174:
175: prep = SQLEngine.getStatement("COMMIT");
176: prep.execute();
177: } catch (Exception e) {
178: PreparedStatement prep = SQLEngine.getStatement("ROLLBACK");
179: prep.execute();
180: throw e;
181: }
182: }
183:
184: public void delete(int projectID) throws Exception {
185: //int transNumber = -1;
186: try {
187: PreparedStatement prep;
188: //transNumber = SQLEngine.beginTransaction(0, ApiConstants.DELETE_LOCK);
189: prep = SQLEngine.getStatement("BEGIN");
190: prep.execute();
191:
192: prep = SQLEngine.getSQLDeleteQuery("deleteAllSalomeLock"); //ok
193: prep.setInt(1, projectID);
194: SQLEngine.runDeleteQuery(prep);
195:
196: prep = SQLEngine.getStatement("COMMIT");
197: prep.execute();
198: //SQLEngine.commitTrans(transNumber);
199: } catch (Exception e) {
200: PreparedStatement prep = SQLEngine.getStatement("ROLLBACK");
201: prep.execute();
202: //SQLEngine.rollBackTrans(transNumber);
203: throw e;
204: }
205: }
206:
207: }
|