001: package org.objectweb.salome_tmf.databaseSQL;
002:
003: import java.sql.PreparedStatement;
004: import java.sql.ResultSet;
005: import java.util.Vector;
006:
007: import org.objectweb.salome_tmf.api.Api;
008: import org.objectweb.salome_tmf.api.ApiConstants;
009: import org.objectweb.salome_tmf.api.Permission;
010: import org.objectweb.salome_tmf.api.Util;
011: import org.objectweb.salome_tmf.api.data.DataSetWrapper;
012: import org.objectweb.salome_tmf.api.data.DataUpToDateException;
013: import org.objectweb.salome_tmf.api.data.ParameterWrapper;
014: import org.objectweb.salome_tmf.api.data.ValuedParameterWrapper;
015: import org.objectweb.salome_tmf.api.sql.ISQLDataset;
016:
017: public class SQLDataset implements ISQLDataset {
018:
019: /**
020: * Insert a dataset (table JEU_DONNEES) for the campaign identified by idCamp
021: * @param idCamp : id of the campaign wich will caontain the dataset
022: * @param name of the dataset
023: * @param description of the dataset
024: * @return id of the dataset created in table JEU_DONNEES
025: * need permission canExecutCamp or canCreateCamp
026: * @throws Exception
027: */
028: public int insert(int idCamp, String name, String description)
029: throws Exception {
030: int transNumber = -1;
031: int idDataset = -1;
032: if (idCamp < 1) {
033: throw new Exception(
034: "[SQLDataset->insert] entry data are not valid");
035: }
036: if (!SQLEngine.specialAllow) {
037: if (!Permission.canCreateCamp()
038: || !Permission.canExecutCamp()) {
039: throw new SecurityException(
040: "[SQLDataset : insert -> canCreateCamp or canExecutCamp]");
041: }
042: }
043: if (SQLObjectFactory.getInstanceOfISQLCampaign().getCampaign(
044: idCamp) == null) {
045: throw new DataUpToDateException();
046: }
047: try {
048: transNumber = SQLEngine.beginTransaction(11,
049: ApiConstants.INSERT_DATA_SET);
050:
051: PreparedStatement prep = SQLEngine
052: .getSQLAddQuery("addJeuDonneesToCamp"); //ok
053: prep.setInt(1, idCamp);
054: prep.setString(2, name);
055: prep.setString(3, description);
056: SQLEngine.runAddQuery(prep);
057:
058: idDataset = getID(idCamp, name);
059: if (idDataset < 1) {
060: throw new Exception(
061: "[SQLDataset->insert] id are not valid");
062: }
063: SQLEngine.commitTrans(transNumber);
064: } catch (Exception e) {
065: Util.log("[SQLDataset->insert]" + e);
066: if (Api.isDEBUG()) {
067: e.printStackTrace();
068: }
069: SQLEngine.rollBackTrans(transNumber);
070: throw e;
071: }
072: return idDataset;
073: }
074:
075: /**
076: * Map a value for the parameter idParam in the table VALEUR_PARAM
077: * @param idDataset : id of the dataset in the table JEU_DONNEES
078: * @param idParam : id of the parameter in the table PARAM_TEST
079: * @param value
080: * @throws Exception
081: * need permission canExecutCamp or canCreateCamp
082: */
083: public void addParamValue(int idDataset, int idParam, String value)
084: throws Exception {
085: int transNumber = -1;
086: if (!SQLEngine.specialAllow) {
087: if (!(Permission.canExecutCamp() || Permission
088: .canCreateCamp())) {
089: throw new SecurityException(
090: "[SQLDataset : addParamValue -> canExecutCamp]");
091: }
092: }
093: if (idDataset < 1) {
094: throw new Exception(
095: "[SQLDataset->addParamValue] entry data are not valid");
096: }
097: if (SQLObjectFactory.getInstanceOfISQLParameter()
098: .getParameterWrapper(idParam) == null) {
099: throw new DataUpToDateException();
100: }
101: if (getWrapper(idDataset) == null) {
102: throw new DataUpToDateException();
103: }
104: try {
105: transNumber = SQLEngine.beginTransaction(11,
106: ApiConstants.UPDATE_DATA_SET);
107:
108: PreparedStatement prep = SQLEngine
109: .getSQLAddQuery("addParamValueToJeuDonneesUsingID"); //ok
110: prep.setInt(1, idDataset);
111: prep.setInt(2, idParam);
112: prep.setString(3, value);
113: SQLEngine.runAddQuery(prep);
114:
115: SQLEngine.commitTrans(transNumber);
116: } catch (Exception e) {
117: Util.log("[SQLDataset->addParamValue]" + e);
118: if (Api.isDEBUG()) {
119: e.printStackTrace();
120: }
121: SQLEngine.rollBackTrans(transNumber);
122: throw e;
123: }
124: }
125:
126: /**
127: * Update the name and the description of the dataset idDataset
128: * @param idDataset : id of the dataset in table JEU_DONNEES
129: * @param name
130: * @param description
131: * need permission canExecutCamp or canUpdateCamp
132: * @throws Exception
133: */
134: public void update(int idDataset, String name, String description)
135: throws Exception {
136: int transNumber = -1;
137: if (!SQLEngine.specialAllow) {
138: if (!(Permission.canExecutCamp() || Permission
139: .canUpdateCamp())) {
140: throw new SecurityException(
141: "[SQLDataset : update -> canExecutCamp]");
142: }
143: }
144: if (idDataset < 1) {
145: throw new Exception(
146: "[SQLDataset->update] entry data are not valid");
147: }
148: try {
149: transNumber = SQLEngine.beginTransaction(10,
150: ApiConstants.UPDATE_DATA_SET);
151:
152: PreparedStatement prep = SQLEngine
153: .getSQLUpdateQuery("updateJeuDonneeUsingID"); //ok
154: prep.setString(1, name);
155: prep.setString(2, description);
156: prep.setInt(3, idDataset);
157: SQLEngine.runUpdateQuery(prep);
158:
159: SQLEngine.commitTrans(transNumber);
160: } catch (Exception e) {
161: Util.log("[SQLDataset->update]" + e);
162: if (Api.isDEBUG()) {
163: e.printStackTrace();
164: }
165: SQLEngine.rollBackTrans(transNumber);
166: throw e;
167: }
168: }
169:
170: /**
171: * Update a value maped to the parameter idParam for the dataset idDataset
172: * @param idDataset : id of the dataset in table JEU_DONNEES
173: * @param idParam
174: * @param value
175: * @param description
176: * @throws Exception
177: * need permission canExecutCamp or canUpdateCamp
178: */
179: public void updateParamValue(int idDataset, int idParam,
180: String value, String description) throws Exception {
181: int transNumber = -1;
182: if (idDataset < 1 || idParam < 1) {
183: throw new Exception(
184: "[SQLDataset->updateParamValue] entry data are not valid");
185: }
186: if (!SQLEngine.specialAllow) {
187: if (!(Permission.canUpdateCamp() || Permission
188: .canExecutCamp())) {
189: throw new SecurityException(
190: "[SQLDataset : update -> canUpdateCamp]");
191: }
192: }
193: try {
194: transNumber = SQLEngine.beginTransaction(11,
195: ApiConstants.UPDATE_DATA_SET);
196:
197: PreparedStatement prep = SQLEngine
198: .getSQLUpdateQuery("updateParamValueToJeuDonnees"); //ok
199: prep.setString(1, value);
200: prep.setString(2, description);
201: prep.setInt(3, idDataset);
202: prep.setInt(4, idParam);
203: SQLEngine.runUpdateQuery(prep);
204:
205: SQLEngine.commitTrans(transNumber);
206: } catch (Exception e) {
207: Util.log("[SQLDataset->updateParamValue]" + e);
208: if (Api.isDEBUG()) {
209: e.printStackTrace();
210: }
211: SQLEngine.rollBackTrans(transNumber);
212: throw e;
213: }
214: }
215:
216: /**
217: * Delete the dataset and all mapped values in the database
218: * if the dataset is used by executions, the executions are deleted
219: * @param idDataset : id of the dataset in table JEU_DONNEES
220: * @throws Exception
221: * @see ISQLExecution.delete(int)
222: * need permission canExecutCamp or canDeleteCamp
223: */
224: public void delete(int idDataset) throws Exception {
225: int transNumber = -1;
226: if (idDataset < 1) {
227: throw new Exception(
228: "[SQLDataset->delete] entry data are not valid");
229: }
230: if (!SQLEngine.specialAllow) {
231: if (!(Permission.canExecutCamp() || Permission
232: .canDeleteCamp())) {
233: throw new SecurityException(
234: "[SQLDataset : delete -> canExecutCamp]");
235: }
236: }
237: try {
238: transNumber = SQLEngine.beginTransaction(10,
239: ApiConstants.DELETE_DATA_SET);
240: /* Delete execution wich using the dataset */
241: PreparedStatement prep = SQLEngine
242: .getSQLSelectQuery("selectExecutionFromJeuDeDonnee"); //OK
243: prep.setInt(1, idDataset);
244: ResultSet stmtRes = SQLEngine.runSelectQuery(prep);
245: while (stmtRes.next()) {
246: int idExec = stmtRes.getInt("id_exec_camp");
247: SQLObjectFactory.getInstanceOfISQLExecution().delete(
248: idExec);
249: }
250: /* Delete associated value */
251: prep = SQLEngine.getSQLDeleteQuery("deleteJeuDonneesValue"); //ok
252: prep.setInt(1, idDataset);
253: SQLEngine.runDeleteQuery(prep);
254:
255: /* Delete dataset reference*/
256: prep = SQLEngine
257: .getSQLDeleteQuery("deleteJeuDonneesUsingID"); //ok
258: prep.setInt(1, idDataset);
259: SQLEngine.runDeleteQuery(prep);
260:
261: SQLEngine.commitTrans(transNumber);
262: } catch (Exception e) {
263: Util.log("[SQLDataset->delete]" + e);
264: if (Api.isDEBUG()) {
265: e.printStackTrace();
266: }
267: SQLEngine.rollBackTrans(transNumber);
268: throw e;
269: }
270: }
271:
272: /**
273: * Get the Id of the dataset identified by name for the campaign idCamp
274: * @param idCamp
275: * @param name
276: * @return id of the dataset created in table JEU_DONNEES
277: * @throws Exception
278: */
279: public int getID(int idCamp, String name) throws Exception {
280: if (idCamp < 1) {
281: throw new Exception(
282: "[SQLDataset->getID] entry data are not valid");
283: }
284: int idDataset = -1;
285: int transNuber = -1;
286: try {
287: transNuber = SQLEngine.beginTransaction(10,
288: ApiConstants.LOADING);
289:
290: PreparedStatement prep = SQLEngine
291: .getSQLSelectQuery("selectIdJeuDonnees"); //ok
292: prep.setInt(1, idCamp);
293: prep.setString(2, name);
294: ResultSet stmtRes = SQLEngine.runSelectQuery(prep);
295:
296: if (stmtRes.next()) {
297: idDataset = stmtRes.getInt("id_jeu_donnees");
298: }
299: SQLEngine.commitTrans(transNuber);
300: } catch (Exception e) {
301: SQLEngine.rollBackTrans(transNuber);
302: throw e;
303: }
304: return idDataset;
305: }
306:
307: /**
308: * Get A DataSetWrapper representing the dataset idDataSet in database
309: * @param idDataSet
310: * @return
311: * @throws Exception
312: */
313: public DataSetWrapper getWrapper(int idDataSet) throws Exception {
314: if (idDataSet < 1) {
315: throw new Exception(
316: "[SQLDataset->getWrapper] entry data are not valid");
317: }
318: DataSetWrapper pDataSet = null;
319:
320: int transNuber = -1;
321: try {
322: transNuber = SQLEngine.beginTransaction(10,
323: ApiConstants.LOADING);
324:
325: PreparedStatement prep = SQLEngine
326: .getSQLSelectQuery("selectJeuDonneesByID"); //ok
327: prep.setInt(1, idDataSet);
328: ResultSet stmtRes = SQLEngine.runSelectQuery(prep);
329: if (stmtRes.next()) {
330: pDataSet = new DataSetWrapper();
331: pDataSet.setDescription(stmtRes
332: .getString("desc_jeu_donnees"));
333: pDataSet.setName(stmtRes.getString("nom_jeu_donnees"));
334: pDataSet.setIdBDD(stmtRes.getInt("id_jeu_donnees"));
335: }
336:
337: SQLEngine.commitTrans(transNuber);
338: } catch (Exception e) {
339: SQLEngine.rollBackTrans(transNuber);
340: throw e;
341: }
342: return pDataSet;
343: }
344:
345: /**
346: * Get a Vector of ValuedParameterWrapper for the he dataset idDataSet
347: * @param idEnv
348: * @return
349: * @throws Exception
350: */
351: public ValuedParameterWrapper[] getDefinedParameters(int idDataSet)
352: throws Exception {
353: if (idDataSet < 1) {
354: throw new Exception(
355: "[SQLDataset->getDefinedParameters] entry data are not valid");
356: }
357: Vector result = new Vector();
358: PreparedStatement prep = SQLEngine
359: .getSQLSelectQuery("selectParamsInDataSet"); //ok
360: prep.setInt(1, idDataSet);
361: ResultSet stmtRes = SQLEngine.runSelectQuery(prep);
362: while (stmtRes.next()) {
363: ValuedParameterWrapper pValuedParameterWrapper = new ValuedParameterWrapper();
364: ParameterWrapper pParameter = new ParameterWrapper();
365: pParameter.setName(stmtRes.getString("nom_param_test"));
366: pParameter.setDescription(stmtRes
367: .getString("desc_param_test"));
368: pParameter.setIdBDD(stmtRes.getInt("id_param_test"));
369: pValuedParameterWrapper.setParameterWrapper(pParameter);
370: pValuedParameterWrapper.setIdBDD(stmtRes
371: .getInt("id_valeur"));
372: pValuedParameterWrapper.setDescription(stmtRes
373: .getString("desc_valeur"));
374: pValuedParameterWrapper.setValue(stmtRes
375: .getString("valeur"));
376: result.add(pValuedParameterWrapper);
377: }
378: ValuedParameterWrapper[] vpwArray = new ValuedParameterWrapper[result
379: .size()];
380: for (int i = 0; i < result.size(); i++) {
381: vpwArray[i] = (ValuedParameterWrapper) result.get(i);
382: }
383: return vpwArray;
384: }
385:
386: }
|