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 Fayçal SOUGRATI
020: *
021: * Contact: mikael.marche@rd.francetelecom.com
022: */
023:
024: package org.objectweb.salome_tmf.api.api2ihm;
025:
026: import java.io.IOException;
027: import java.io.InputStream;
028: import java.sql.Date;
029: import java.sql.PreparedStatement;
030: import java.sql.SQLException;
031: import java.sql.Time;
032: import java.util.Calendar;
033: import java.util.GregorianCalendar;
034: import java.util.Properties;
035:
036: import org.objectweb.salome_tmf.api.Api;
037: import org.objectweb.salome_tmf.api.api2db.DataBase;
038: import org.objectweb.salome_tmf.api.api2db.DataSet;
039:
040: /**
041: * Classe contenant des fonctions de calcul ou autres fonctions utiles
042: * @author Fayçal SOUGRATI
043: */
044: public class Utile {
045:
046: /**
047: * Separateur systeme ("/", "\" ...)
048: */
049: //static String fs = System.getProperty("file.separator");
050: static String fs = "/";
051:
052: /**
053: * Ouverture d'un fichier "properties"
054: * @param file fichier properties
055: * @return properties
056: */
057: public static Properties getPropertiesFile(String file) {
058: Properties prop = new Properties();
059:
060: try {
061: InputStream in = Api.class.getResourceAsStream(file);
062: //FileInputStream in = new FileInputStream(file);
063: prop.load(in);
064: in.close();
065: } catch (IOException e) {
066: e.printStackTrace();
067: }
068:
069: return prop;
070: }
071:
072: /**
073: * Ouverture d'un fichier "properties"
074: * @param url fichier properties
075: * @return properties
076: */
077: public static Properties getPropertiesFile(java.net.URL url)
078: throws Exception {
079: Properties prop = new Properties();
080: InputStream in = url.openStream();
081: prop.load(in);
082: in.close();
083: return prop;
084: }
085:
086: /**
087: * Recherche de la date actuelle
088: * @return date actuelle
089: */
090: public static Date getCurrentDate() {
091: Calendar calendar = new GregorianCalendar();
092: int year = calendar.get(Calendar.YEAR);
093: int month = calendar.get(Calendar.MONTH) + 1;
094: int day = calendar.get(Calendar.DAY_OF_MONTH);
095: String chaineDateActuelle = year + "-" + month + "-" + day;
096: Date dateActuelle = Date.valueOf(chaineDateActuelle);
097:
098: return dateActuelle;
099: }
100:
101: /**
102: * Recherche de l'heure courante
103: * @return heure actuelle
104: */
105: public static Time getCurrentTime() {
106: Calendar calendar = new GregorianCalendar();
107: int heure = calendar.get(Calendar.HOUR_OF_DAY);
108: int minute = calendar.get(Calendar.MINUTE);
109: int seconde = calendar.get(Calendar.SECOND);
110: String chaineHeureActuelle = heure + ":" + minute + ":"
111: + seconde;
112: Time heureActuelle = Time.valueOf(chaineHeureActuelle);
113:
114: return heureActuelle;
115: }
116:
117: /**
118: * Recherche de l'ID d'un projet a partir de son nom
119: * @param database BdD
120: * @param prop properties
121: * @param projectName nom du projet
122: * @return ID projet
123: */
124: public static int getIdProject(DataBase database, Properties prop,
125: String projectName) {
126: int idProject = 0;
127:
128: try {
129: PreparedStatement prep = database.prepareStatement(prop
130: .getProperty("selectIdProject"));
131: prep.setString(1, projectName);
132: DataSet DS = new DataSet(prep.executeQuery());
133:
134: if (DS.getResults().next()) {
135: idProject = DS.getResults().getInt("id_projet");
136: }
137: } catch (SQLException e) {
138: e.printStackTrace();
139: } catch (Exception E) {
140: E.printStackTrace();
141: }
142:
143: return idProject;
144: }
145:
146: /**
147: * Recherche de l'ID d'un utilisateur a partir de son login
148: * @param database base de données
149: * @param prop properties
150: * @param loginPerson login personne
151: * @return ID personne
152: */
153: public static int getIdPerson(DataBase database, Properties prop,
154: String loginPerson) {
155: int idPerson = 0;
156:
157: try {
158: PreparedStatement prep = database.prepareStatement(prop
159: .getProperty("selectIdPers"));
160: prep.setString(1, loginPerson);
161: DataSet DS = new DataSet(prep.executeQuery());
162:
163: if (DS.getResults().next()) {
164: idPerson = DS.getResults().getInt("id_personne");
165: }
166: } catch (SQLException e) {
167: e.printStackTrace();
168: } catch (Exception E) {
169: E.printStackTrace();
170: }
171:
172: return idPerson;
173: }
174:
175: /**
176: * Selection de l'ID d'un groupe
177: * @param database BdD
178: * @param prop properties
179: * @param idProject ID projet
180: * @param groupName nom groupe utilisateur
181: * @return ID groupe
182: */
183: public static int getIdGroup(DataBase database, Properties prop,
184: int idProject, String groupName) {
185: int idGroup = 0;
186:
187: try {
188: PreparedStatement prep = database.prepareStatement(prop
189: .getProperty("selectIdGroup"));
190: prep.setInt(1, idProject);
191: prep.setString(2, groupName);
192: DataSet DS = new DataSet(prep.executeQuery());
193:
194: if (DS.getResults().next()) {
195: idGroup = DS.getResults().getInt("id_groupe");
196: }
197: } catch (SQLException e) {
198: e.printStackTrace();
199: } catch (Exception E) {
200: E.printStackTrace();
201: }
202:
203: return idGroup;
204: }
205:
206: /**
207: * Selection de l'ID d'une Famille
208: * @param database BdD
209: * @param prop properties
210: * @param idProject ID projet
211: * @param FamilleName nom groupe utilisateur
212: * @return ID Famille
213: */
214: public static int getIdFamille(DataBase database, Properties prop,
215: int idProject, String familleName) {
216: int idFamille = 0;
217:
218: try {
219: PreparedStatement prep = database.prepareStatement(prop
220: .getProperty("selectIdFamille"));
221: prep.setInt(1, idProject);
222: prep.setString(2, familleName);
223: DataSet DS = new DataSet(prep.executeQuery());
224:
225: if (DS.getResults().next()) {
226: idFamille = DS.getResults().getInt("id_famille");
227: }
228: } catch (SQLException e) {
229: e.printStackTrace();
230: } catch (Exception E) {
231: E.printStackTrace();
232: }
233:
234: return idFamille;
235: }
236:
237: public static int getIdSuite(DataBase database, Properties prop,
238: int idProject, int id_famille, String suiteName) {
239: int idSuite = 0;
240:
241: try {
242: PreparedStatement prep = database.prepareStatement(prop
243: .getProperty("selectIdSuite"));
244: prep.setInt(1, idProject);
245: prep.setInt(2, id_famille);
246: prep.setString(3, suiteName);
247: DataSet DS = new DataSet(prep.executeQuery());
248:
249: if (DS.getResults().next()) {
250: idSuite = DS.getResults().getInt("id_suite");
251: }
252: } catch (SQLException e) {
253: e.printStackTrace();
254: } catch (Exception E) {
255: E.printStackTrace();
256: }
257:
258: return idSuite;
259: }
260:
261: public static int getIdCas(DataBase database, Properties prop,
262: int id_suite, String casName) {
263: int idCas = 0;
264:
265: try {
266: PreparedStatement prep = database.prepareStatement(prop
267: .getProperty("selectIdCas"));
268: prep.setInt(1, id_suite);
269: prep.setString(2, casName);
270: DataSet DS = new DataSet(prep.executeQuery());
271:
272: if (DS.getResults().next()) {
273: idCas = DS.getResults().getInt("id_cas");
274: }
275: } catch (SQLException e) {
276: e.printStackTrace();
277: } catch (Exception E) {
278: E.printStackTrace();
279: }
280:
281: return idCas;
282: }
283:
284: public static int getIdAction(DataBase database, Properties prop,
285: int id_cas, String actionName) {
286: int idAction = 0;
287:
288: try {
289: PreparedStatement prep = database.prepareStatement(prop
290: .getProperty("selectIdAction"));
291: prep.setInt(1, id_cas);
292: prep.setString(2, actionName);
293: DataSet DS = new DataSet(prep.executeQuery());
294:
295: if (DS.getResults().next()) {
296: idAction = DS.getResults().getInt("id_action");
297: }
298: } catch (SQLException e) {
299: e.printStackTrace();
300: } catch (Exception E) {
301: E.printStackTrace();
302: }
303:
304: return idAction;
305: }
306:
307: public static int getIdParam(DataBase database, Properties prop,
308: int id_projet, String paramName) {
309: int idParam = 0;
310:
311: try {
312: PreparedStatement prep = database.prepareStatement(prop
313: .getProperty("selectIdParam"));
314: prep.setInt(1, id_projet);
315: prep.setString(2, paramName);
316: DataSet DS = new DataSet(prep.executeQuery());
317:
318: if (DS.getResults().next()) {
319: idParam = DS.getResults().getInt("id_param_test");
320: }
321: } catch (SQLException e) {
322: e.printStackTrace();
323: } catch (Exception E) {
324: E.printStackTrace();
325: }
326:
327: return idParam;
328: }
329:
330: public static int getIdCampagne(DataBase database, Properties prop,
331: int id_projet, String campName) {
332: int idCamp = 0;
333:
334: try {
335: PreparedStatement prep = database.prepareStatement(prop
336: .getProperty("selectIdCamp"));
337: prep.setInt(1, id_projet);
338: prep.setString(2, campName);
339: DataSet DS = new DataSet(prep.executeQuery());
340:
341: if (DS.getResults().next()) {
342: idCamp = DS.getResults().getInt("id_camp");
343: }
344: } catch (SQLException e) {
345: e.printStackTrace();
346: } catch (Exception E) {
347: E.printStackTrace();
348: }
349:
350: return idCamp;
351: }
352:
353: /**
354: * Selection d'un utilisateur a partir de son ID
355: * @param database BdD
356: * @param prop propeties
357: * @param userId ID personne
358: * @return Nom complet de l'utilisateur
359: */
360: public static String selectUserCompleteNameFromId(
361: DataBase database, Properties prop, int userId) {
362: String userCompleteName = null;
363:
364: try {
365: PreparedStatement prep = database.prepareStatement(prop
366: .getProperty("selectUserFromId"));
367: prep.setInt(1, userId);
368:
369: DataSet DS = new DataSet(prep.executeQuery());
370:
371: if (DS.getResults().next()) {
372: userCompleteName = DS.getResults().getString(
373: "nom_personne")
374: + " "
375: + DS.getResults().getString("prenom_personne");
376: }
377: } catch (SQLException e) {
378: e.printStackTrace();
379: }
380:
381: return userCompleteName;
382: }
383:
384: }
|