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.net.InetAddress;
027: import java.sql.Date;
028: import java.sql.PreparedStatement;
029: import java.sql.ResultSet;
030: import java.sql.Time;
031: import java.util.Vector;
032:
033: import org.objectweb.salome_tmf.api.ApiConstants;
034: import org.objectweb.salome_tmf.api.Util;
035: import org.objectweb.salome_tmf.api.data.ConnectionWrapper;
036: import org.objectweb.salome_tmf.api.sql.ISQLSession;
037:
038: public class SQLSession implements ISQLSession {
039:
040: /**
041: * Insert a session on table SESSION
042: * @param project
043: * @param login
044: * @throws Exception
045: */
046: public int addSession(String project, String login)
047: throws Exception {
048: int idSession = -1;
049: int transNumber = -1;
050: try {
051: transNumber = SQLEngine.beginTransaction(0,
052: ApiConstants.COMMON_REQ);
053:
054: Date dateActuelle = Util.getCurrentDate();
055: Time heureActuelle = Util.getCurrentTime();
056: String host = InetAddress.getLocalHost()
057: .getCanonicalHostName();
058: Util.log("[SQLSession->addSession] " + project + ", "
059: + login + ", " + host + ", " + dateActuelle + ", "
060: + heureActuelle);
061: PreparedStatement prep = SQLEngine
062: .getSQLCommonQuery("insertConnection"); //ok
063: prep.setString(1, project);
064: prep.setString(2, login);
065: prep.setString(3, host);
066: prep.setDate(4, dateActuelle);
067: prep.setTime(5, heureActuelle);
068: SQLEngine.runAddQuery(prep);
069:
070: idSession = getID(project, login, host, dateActuelle,
071: heureActuelle);
072:
073: SQLEngine.commitTrans(transNumber);
074:
075: } catch (Exception e) {
076: Util.log("[SQLSession->addSession]" + e);
077: e.printStackTrace();
078: SQLEngine.rollBackTrans(transNumber);
079: throw e;
080: }
081: return idSession;
082: }
083:
084: int getID(String project, String login, String host, Date date,
085: Time time) throws Exception {
086: int idSession = -1;
087: PreparedStatement prep = SQLEngine
088: .getSQLCommonQuery("selectConnection"); //ok
089: prep.setString(1, project);
090: prep.setString(2, login);
091: prep.setString(3, host);
092: prep.setDate(4, date);
093: prep.setTime(5, time);
094: ResultSet stmtRes = SQLEngine.runSelectQuery(prep);
095: if (stmtRes.next()) {
096: idSession = stmtRes.getInt("id_connection");
097: }
098: return idSession;
099: }
100:
101: /**
102: * Delete a session id in table SESSION
103: * @param id
104: * @throws Exception
105: */
106: public void deleteSession(int id) throws Exception {
107: int transNumber = -1;
108: try {
109: transNumber = SQLEngine.beginTransaction(0,
110: ApiConstants.COMMON_REQ);
111:
112: PreparedStatement prep = SQLEngine
113: .getSQLCommonQuery("deleteAConnection"); //ok
114: prep.setInt(1, id);
115: SQLEngine.runDeleteQuery(prep);
116:
117: SQLEngine.commitTrans(transNumber);
118: } catch (Exception e) {
119: Util.log("[SQLSession->deleteSession]" + e);
120: SQLEngine.rollBackTrans(transNumber);
121: throw e;
122: }
123: }
124:
125: /**
126: * Delete all session in table SESSION
127: * @throws Exception
128: */
129: public void deleteAllSession() throws Exception {
130: int transNumber = -1;
131: try {
132: transNumber = SQLEngine.beginTransaction(0,
133: ApiConstants.COMMON_REQ);
134:
135: PreparedStatement prep = SQLEngine
136: .getSQLCommonQuery("deleteAllConnections"); //ok
137: SQLEngine.runDeleteQuery(prep);
138:
139: SQLEngine.commitTrans(transNumber);
140: } catch (Exception e) {
141: Util.log("[SQLSession->deleteAllSession]" + e);
142: SQLEngine.rollBackTrans(transNumber);
143: throw e;
144: }
145: }
146:
147: /**
148: * Get a vector of ConnectionWrapper representing all session in the databse (table SESSION)
149: * @return
150: * @throws Exception
151: */
152: public ConnectionWrapper[] getAllSession() throws Exception {
153: Vector res = new Vector();
154: PreparedStatement prep = SQLEngine
155: .getSQLCommonQuery("selectAllConnections"); //ok
156: ResultSet stmtRes = SQLEngine.runSelectQuery(prep);
157: while (stmtRes.next()) {
158: ConnectionWrapper pConnectionWrapper = new ConnectionWrapper();
159: pConnectionWrapper.setId(stmtRes.getInt("id_connection"));
160: pConnectionWrapper.setProjectConnected(stmtRes
161: .getString("project_connec"));
162: pConnectionWrapper.setLoginConnected(stmtRes
163: .getString("login_connec"));
164: pConnectionWrapper.setHostConnected(stmtRes
165: .getString("host_connec"));
166: pConnectionWrapper.setDateConnected(stmtRes
167: .getDate("date_connec"));
168: pConnectionWrapper.setTimeConnected(stmtRes.getTime(
169: "hour_connec").getTime());
170: res.add(pConnectionWrapper);
171: }
172: ConnectionWrapper[] cwArray = new ConnectionWrapper[res.size()];
173: for (int i = 0; i < res.size(); i++) {
174: cwArray[i] = (ConnectionWrapper) res.get(i);
175: }
176:
177: return cwArray;
178: }
179:
180: /**
181: * Get a ConnectionWrapper representing a session idSession in the databse (table SESSION)
182: * @param idSession
183: * @return
184: * @throws Exception
185: */
186: public ConnectionWrapper getSession(int idSession) throws Exception {
187: ConnectionWrapper pConnectionWrapper = null;
188: PreparedStatement prep = SQLEngine
189: .getSQLCommonQuery("selectConnectionInfos"); //ok
190: prep.setInt(1, idSession);
191: ResultSet stmtRes = SQLEngine.runSelectQuery(prep);
192: if (stmtRes.next()) {
193: pConnectionWrapper = new ConnectionWrapper();
194: pConnectionWrapper.setId(stmtRes.getInt("id_connection"));
195: pConnectionWrapper.setProjectConnected(stmtRes
196: .getString("project_connec"));
197: pConnectionWrapper.setLoginConnected(stmtRes
198: .getString("login_connec"));
199: pConnectionWrapper.setHostConnected(stmtRes
200: .getString("host_connec"));
201: pConnectionWrapper.setDateConnected(stmtRes
202: .getDate("date_connec"));
203: pConnectionWrapper.setTimeConnected(stmtRes.getTime(
204: "hour_connec").getTime());
205: }
206: return pConnectionWrapper;
207: }
208: }
|