001: /*
002: * SalomeTMF is a Test Management Framework
003: * Copyright (C) 2005 France Telecom R&D
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: *
019: * @author Marche Mikael
020: *
021: * Contact: mikael.marche@rd.francetelecom.com
022: */
023:
024: package org.objectweb.salome_tmf.databaseSQL;
025:
026: import java.sql.PreparedStatement;
027: import java.sql.ResultSet;
028:
029: import org.objectweb.salome_tmf.api.Api;
030: import org.objectweb.salome_tmf.api.ApiConstants;
031: import org.objectweb.salome_tmf.api.Permission;
032: import org.objectweb.salome_tmf.api.Util;
033: import org.objectweb.salome_tmf.api.data.ParameterWrapper;
034: import org.objectweb.salome_tmf.api.sql.ISQLParameter;
035:
036: public class SQLParameter implements ISQLParameter {
037:
038: /**
039: * Add parameter in a project
040: * @param idProject : id in Dabase of the project
041: * @param name : name of the parameter
042: * @param description : description of the parameter
043: * @return the id of the parameter in the dadabase
044: * need permission canCreateTest
045: */
046: public int insert(int IdProject, String name, String description)
047: throws Exception {
048: if (IdProject < 1) {
049: throw new Exception(
050: "[SQLParameter->insert] entry data are not valid");
051: }
052: int transNumber = -1;
053: int idParam = -1;
054: if (!SQLEngine.specialAllow) {
055: if (!Permission.canCreateTest()) {
056: throw new SecurityException(
057: "[SQLPamameter : updateDescription -> canCreateTest]");
058: }
059: }
060: try {
061: transNumber = SQLEngine.beginTransaction(1,
062: ApiConstants.INSERT_PARAMETER);
063:
064: PreparedStatement prep = SQLEngine
065: .getSQLAddQuery("addParamToProject"); //ok
066: prep.setInt(1, IdProject);
067: prep.setString(2, name);
068: prep.setString(3, description);
069: SQLEngine.runAddQuery(prep);
070:
071: idParam = selectID(IdProject, name);
072: if (idParam < 1) {
073: throw new Exception(
074: "[SQLParameter->insert] id is not valid");
075: }
076: SQLEngine.commitTrans(transNumber);
077: } catch (Exception e) {
078: Util.log("[SQLParameter->insert]" + e);
079: if (Api.isDEBUG()) {
080: e.printStackTrace();
081: }
082: SQLEngine.rollBackTrans(transNumber);
083: throw e;
084: }
085: return idParam;
086: }
087:
088: /**
089: * get database id for a parameter identified by name in the project IdProject
090: * @param idProject
091: * @param name
092: * @return the database id of the parameter identified by name in the project IdProject
093: * @throws Exception
094: */
095: public int selectID(int IdProject, String name) throws Exception {
096: if (IdProject < 1) {
097: throw new Exception(
098: "[SQLParameter->selectID] entry data are not valid");
099: }
100: int idParam = -1;
101: PreparedStatement prep = SQLEngine
102: .getSQLSelectQuery("selectIdParam"); //ok
103: prep.setString(1, name);
104: prep.setInt(2, IdProject);
105: ResultSet result = SQLEngine.runSelectQuery(prep);
106: if (result.next()) {
107: idParam = result.getInt("id_param_test");
108: }
109:
110: return idParam;
111: }
112:
113: /**
114: * Update parameter (identifed by idParameter) description in DataBase
115: * @param idParameter
116: * @param description : the new description of the parameter
117: * @throws Exception
118: * need permission canUpdateTest
119: */
120: public void updateDescription(int idParameter, String description)
121: throws Exception {
122: if (idParameter < 1) {
123: throw new Exception(
124: "[SQLParameter->updateDescription] entry data are not valid");
125: }
126: int transNumber = -1;
127: if (!SQLEngine.specialAllow) {
128: if (!Permission.canUpdateTest()) {
129: throw new SecurityException(
130: "[SQLPamameter : updateDescription --> canUpdateTest]");
131: }
132: }
133: try {
134: transNumber = SQLEngine.beginTransaction(1,
135: ApiConstants.UPDATE_PARAMETER);
136: PreparedStatement prep = SQLEngine
137: .getSQLUpdateQuery("updateParamProjectUsingID"); //ok
138: prep.setString(1, description);
139: prep.setInt(2, idParameter);
140:
141: SQLEngine.runUpdateQuery(prep);
142:
143: SQLEngine.commitTrans(transNumber);
144: } catch (Exception e) {
145: Util.log("[SQLParameter->updateDescription]" + e);
146: if (Api.isDEBUG()) {
147: e.printStackTrace();
148: }
149: SQLEngine.rollBackTrans(transNumber);
150: throw e;
151: }
152: }
153:
154: /**
155: * Delete a parameter in Salome Database, include the suppresion of the parameter
156: * in test, and in action
157: * @param idParameter
158: * @throws Exception
159: * need permission canDeleteTest
160: */
161: public void delete(int idParameter) throws Exception {
162: int transNumber = -1;
163: if (idParameter < 1) {
164: throw new Exception(
165: "[SQLParameter->delete] entry data are not valid");
166: }
167: if (!SQLEngine.specialAllow) {
168: if (!Permission.canDeleteTest()) {
169: throw new SecurityException(
170: "[SQLPamameter : updateDescription --> canDeleteTest]");
171: }
172: }
173: try {
174: transNumber = SQLEngine.beginTransaction(111,
175: ApiConstants.DELETE_PARAMETER);
176:
177: PreparedStatement prep = SQLEngine
178: .getSQLDeleteQuery("deleteParamUsingID"); //ok
179: prep.setInt(1, idParameter);
180: SQLEngine.runDeleteQuery(prep);
181:
182: prep = prep = SQLEngine
183: .getSQLDeleteQuery("deleteParamFromAllTest"); //ok
184: prep.setInt(1, idParameter);
185: SQLEngine.runDeleteQuery(prep);
186:
187: prep = prep = SQLEngine
188: .getSQLDeleteQuery("deleteParamFromAllAction"); //ok
189: prep.setInt(1, idParameter);
190: SQLEngine.runDeleteQuery(prep);
191:
192: prep = prep = SQLEngine
193: .getSQLDeleteQuery("deleteParamFromAllEnvDataSet"); //ok
194: prep.setInt(1, idParameter);
195: SQLEngine.runDeleteQuery(prep);
196:
197: SQLEngine.commitTrans(transNumber);
198:
199: } catch (Exception e) {
200: Util.log("[SQLParameter->delete]" + e);
201: if (Api.isDEBUG()) {
202: e.printStackTrace();
203: }
204: SQLEngine.rollBackTrans(transNumber);
205: throw e;
206: }
207: }
208:
209: /**
210: * Get a ParameterWrapper representing the parameter idParameter in the database
211: * @param idParameter
212: * @return
213: * @throws Exception
214: */
215: public ParameterWrapper getParameterWrapper(int idParameter)
216: throws Exception {
217: if (idParameter < 1) {
218: throw new Exception(
219: "[SQLParameter->getParameterWrapper] entry data are not valid");
220: }
221:
222: ParameterWrapper pParameterWrapper = null;
223: int transNuber = -1;
224: try {
225: transNuber = SQLEngine.beginTransaction(1,
226: ApiConstants.LOADING);
227:
228: PreparedStatement prep = SQLEngine
229: .getSQLSelectQuery("selectParamByID"); //ok
230: prep.setInt(1, idParameter);
231: ResultSet result = SQLEngine.runSelectQuery(prep);
232: if (result.next()) {
233: pParameterWrapper = new ParameterWrapper();
234: pParameterWrapper.setName(result
235: .getString("nom_param_test"));
236: pParameterWrapper.setDescription(result
237: .getString("desc_param_test"));
238: pParameterWrapper.setIdBDD(result
239: .getInt("id_param_test"));
240: }
241:
242: SQLEngine.commitTrans(transNuber);
243: } catch (Exception e) {
244: SQLEngine.rollBackTrans(transNuber);
245: throw e;
246: }
247: return pParameterWrapper;
248: }
249: }
|