001: package org.objectweb.salome_tmf.databaseSQL;
002:
003: import java.sql.PreparedStatement;
004: import java.sql.ResultSet;
005: import java.util.Hashtable;
006:
007: import org.objectweb.salome_tmf.api.ApiConstants;
008: import org.objectweb.salome_tmf.api.sql.ISQLConfig;
009:
010: public class SQLConfig implements ISQLConfig {
011:
012: public void insertSalomeConf(String key, String value)
013: throws Exception {
014: insertConf(key, value, 0, 0);
015: }
016:
017: public void insertUserConf(String key, String value, int idUser)
018: throws Exception {
019: insertConf(key, value, 0, idUser);
020: }
021:
022: public void insertProjectConf(String key, String value,
023: int idProject) throws Exception {
024: insertConf(key, value, idProject, 0);
025: }
026:
027: synchronized void insertConf(String key, String value,
028: int idProject, int idUser) throws Exception {
029: int transNumber = -1;
030: try {
031: transNumber = SQLEngine.beginTransaction(0,
032: ApiConstants.COMMON_REQ);
033:
034: PreparedStatement prep = SQLEngine
035: .getSQLCommonQuery("insertConfig"); //ok
036: prep.setString(1, key);
037: prep.setString(2, value);
038: prep.setInt(3, idProject);
039: prep.setInt(4, idUser);
040: SQLEngine.runAddQuery(prep);
041:
042: SQLEngine.commitTrans(transNumber);
043: } catch (Exception e) {
044: SQLEngine.rollBackTrans(transNumber);
045: throw e;
046: }
047: }
048:
049: public void updateSalomeConf(String key, String value)
050: throws Exception {
051: updateConf(key, value, 0, 0);
052: }
053:
054: public void updateUserConf(String key, String value, int idUser)
055: throws Exception {
056: updateConf(key, value, 0, idUser);
057: }
058:
059: public void updateProjectConf(String key, String value,
060: int idProject) throws Exception {
061: updateConf(key, value, idProject, 0);
062: }
063:
064: synchronized void updateConf(String key, String value,
065: int idProject, int idUser) throws Exception {
066: int transNumber = -1;
067: try {
068: transNumber = SQLEngine.beginTransaction(0,
069: ApiConstants.COMMON_REQ);
070:
071: PreparedStatement prep = SQLEngine
072: .getSQLCommonQuery("updateConfig"); //ok
073: prep.setString(1, value);
074: prep.setInt(2, idProject);
075: prep.setInt(3, idUser);
076: prep.setString(4, key);
077: prep.setInt(5, idProject);
078: prep.setInt(6, idUser);
079: SQLEngine.runUpdateQuery(prep);
080:
081: SQLEngine.commitTrans(transNumber);
082: } catch (Exception e) {
083: SQLEngine.rollBackTrans(transNumber);
084: throw e;
085: }
086: }
087:
088: public void deleteSalomeConf(String key) throws Exception {
089: deleteConf(key, 0, 0);
090: }
091:
092: public void deleteUserConf(String key, int idUser) throws Exception {
093: deleteConf(key, 0, idUser);
094: }
095:
096: public void deleteProjectConf(String key, int idProject)
097: throws Exception {
098: deleteConf(key, idProject, 0);
099: }
100:
101: synchronized void deleteConf(String key, int idProject, int idUser)
102: throws Exception {
103: int transNumber = -1;
104: try {
105: transNumber = SQLEngine.beginTransaction(0,
106: ApiConstants.COMMON_REQ);
107:
108: PreparedStatement prep = SQLEngine
109: .getSQLCommonQuery("deleteConfig"); //ok
110: prep.setString(1, key);
111: prep.setInt(2, idProject);
112: prep.setInt(3, idUser);
113: SQLEngine.runDeleteQuery(prep);
114:
115: SQLEngine.commitTrans(transNumber);
116: } catch (Exception e) {
117: SQLEngine.rollBackTrans(transNumber);
118: throw e;
119: }
120: }
121:
122: public void deleteAllUserConf(int idUser) throws Exception {
123: int transNumber = -1;
124: try {
125: transNumber = SQLEngine.beginTransaction(0,
126: ApiConstants.COMMON_REQ);
127:
128: PreparedStatement prep = SQLEngine
129: .getSQLCommonQuery("deleteAllUserConfig"); //ok
130: prep.setInt(1, idUser);
131: SQLEngine.runDeleteQuery(prep);
132:
133: SQLEngine.commitTrans(transNumber);
134: } catch (Exception e) {
135: SQLEngine.rollBackTrans(transNumber);
136: throw e;
137: }
138: }
139:
140: public void deleteAllProjectConf(int idProject) throws Exception {
141: int transNumber = -1;
142: try {
143: transNumber = SQLEngine.beginTransaction(0,
144: ApiConstants.COMMON_REQ);
145:
146: PreparedStatement prep = SQLEngine
147: .getSQLCommonQuery("deleteAllProjectConfig"); //ok
148: prep.setInt(1, idProject);
149: SQLEngine.runDeleteQuery(prep);
150:
151: SQLEngine.commitTrans(transNumber);
152: } catch (Exception e) {
153: SQLEngine.rollBackTrans(transNumber);
154: throw e;
155: }
156: }
157:
158: public String getSalomeConf(String key) throws Exception {
159: return getOneConfig(key, 0, 0);
160: }
161:
162: public String getUserConf(String key, int idUser) throws Exception {
163: return getOneConfig(key, 0, idUser);
164: }
165:
166: public String getProjectConf(String key, int idProject)
167: throws Exception {
168: return getOneConfig(key, idProject, 0);
169: }
170:
171: public synchronized String getOneConfig(String key, int idProject,
172: int idUser) throws Exception {
173: String res = null;
174: PreparedStatement prep = SQLEngine
175: .getSQLCommonQuery("selectOneConfig"); //ok
176: prep.setString(1, key);
177: prep.setInt(2, idProject);
178: prep.setInt(3, idUser);
179: ResultSet stmtRes = SQLEngine.runSelectQuery(prep);
180: if (stmtRes.next()) {
181: res = stmtRes.getString("VALEUR");
182: }
183: return res;
184: }
185:
186: public Hashtable getAllSalomeConf() throws Exception {
187: return getOneConfig(0, 0);
188: }
189:
190: public Hashtable getAllUserConf(int idUser) throws Exception {
191: return getOneConfig(0, idUser);
192: }
193:
194: public Hashtable getAllProjectConf(int idProject) throws Exception {
195: return getOneConfig(idProject, 0);
196: }
197:
198: public synchronized Hashtable getOneConfig(int idProject, int idUser)
199: throws Exception {
200: Hashtable res = new Hashtable();
201: PreparedStatement prep = SQLEngine
202: .getSQLCommonQuery("selectAllConfig"); //ok
203: prep.setInt(1, idProject);
204: prep.setInt(2, idUser);
205: ResultSet stmtRes = SQLEngine.runSelectQuery(prep);
206: while (stmtRes.next()) {
207: String key = stmtRes.getString("CLE");
208: String value = stmtRes.getString("VALEUR");
209: res.put(key, value);
210: }
211: return res;
212: }
213: }
|