99
1010package ch .cern .dbod .db .dao ;
1111
12+ import ch .cern .dbod .util .RestHelper ;
1213import ch .cern .dbod .db .entity .Instance ;
1314import ch .cern .dbod .db .entity .InstanceChange ;
1415import ch .cern .dbod .db .entity .Upgrade ;
1920import java .sql .SQLException ;
2021import java .text .DateFormat ;
2122import java .text .SimpleDateFormat ;
22- import java .util .ArrayList ;
23- import java .util .List ;
24- import java .util .StringTokenizer ;
23+ import java .util .*;
2524import java .util .logging .Level ;
2625import java .util .logging .Logger ;
2726import javax .naming .Context ;
3231/**
3332 * DAO for Instance entity.
3433 * @author Daniel Gomez Blanco
34+ * @author Jose Andres Cordero Benitez
3535 */
3636public class InstanceDAO {
3737
@@ -55,72 +55,29 @@ private Connection getConnection() throws NamingException, SQLException {
5555 * @return List of all the instances in the database.
5656 */
5757 public List <Instance > selectAll (List <Upgrade > upgrades ) {
58- Connection connection = null ;
59- PreparedStatement statement = null ;
60- ResultSet result = null ;
61- ArrayList <Instance > instances = new ArrayList <>();
62- try {
63- //Get connection
64- connection = getConnection ();
65-
66- //Prepare query for the prepared statement (to avoid SQL injection)
67- StringBuilder query = new StringBuilder ();
68- query .append ("SELECT username, db_name, e_group, category, creation_date, expiry_date, db_type, db_size, no_connections, project, description, version, state, status, master, slave, host"
69- + " FROM dod_instances WHERE status = '1'"
70- + " ORDER BY db_name" );
71- statement = connection .prepareStatement (query .toString ());
72-
73- //Execute query
74- result = statement .executeQuery ();
75-
76- //Instantiate instance objects
77- while (result .next ()) {
78- Instance instance = new Instance ();
79- instance .setUsername (result .getString (1 ));
80- instance .setDbName (result .getString (2 ));
81- instance .setEGroup (result .getString (3 ));
82- instance .setCategory (result .getString (4 ));
83- instance .setCreationDate (new java .util .Date (result .getDate (5 ).getTime ()));
84- if (result .getDate (6 ) != null )
85- instance .setExpiryDate (new java .util .Date (result .getDate (6 ).getTime ()));
86- instance .setDbType (result .getString (7 ));
87- instance .setDbSize (result .getInt (8 ));
88- instance .setNoConnections (result .getInt (9 ));
89- instance .setProject (result .getString (10 ));
90- instance .setDescription (result .getString (11 ));
91- instance .setVersion (result .getString (12 ));
92- instance .setState (result .getString (13 ));
93- instance .setStatus (result .getBoolean (14 ));
94- instance .setMaster (result .getString (15 ));
95- instance .setSlave (result .getString (16 ));
96- instance .setHost (result .getString (17 ));
97- //Check if instance needs upgrade
98- if (upgrades != null ) {
99- for (int i =0 ; i < upgrades .size (); i ++) {
100- Upgrade upgrade = upgrades .get (i );
58+ List <Instance > instances = new ArrayList <>();
59+ try
60+ {
61+ //Get database information from REST API
62+ Instance [] array = RestHelper .getObjectListFromRestApi ("/entity/all/" , Instance [].class );
63+ if (array != null )
64+ instances = Arrays .asList (array );
65+
66+ //Check if instance needs upgrade
67+ if (upgrades != null ) {
68+ for (Instance instance : instances ) {
69+ for (Upgrade upgrade : upgrades ) {
10170 if (upgrade .getDbType ().equals (instance .getDbType ()) && upgrade .getCategory ().equals (instance .getCategory ())
102- && upgrade .getVersionFrom ().equals (instance .getVersion ()))
71+ && upgrade .getVersionFrom ().equals (instance .getVersion ()))
10372 instance .setUpgradeTo (upgrade .getVersionTo ());
10473 }
10574 }
106- instances .add (instance );
107- }
108- } catch (NamingException | SQLException ex ) {
109- Logger .getLogger (InstanceDAO .class .getName ()).log (Level .SEVERE , "ERROR SELECTING INSTANCES FOR ADMIN" ,ex );
110- } finally {
111- try {
112- result .close ();
113- } catch (Exception e ) {
114- }
115- try {
116- statement .close ();
117- } catch (Exception e ) {
118- }
119- try {
120- connection .close ();
121- } catch (Exception e ) {
12275 }
12376 }
77+ catch (Exception ex ) {
78+ Logger .getLogger (InstanceDAO .class .getName ()).log (Level .SEVERE , null , ex );
79+ }
80+
12481 return instances ;
12582 }
12683
@@ -381,73 +338,27 @@ public List<Instance> selectInstancesPerHost(String host, List<Upgrade> upgrades
381338 * @return instance for the username and DB name specified.
382339 */
383340 public Instance selectByDbName (String dbName , List <Upgrade > upgrades ) {
384- Connection connection = null ;
385- PreparedStatement statement = null ;
386- ResultSet result = null ;
387341 Instance instance = null ;
388- try {
389- //Get connection
390- connection = getConnection ();
391-
392- //Prepare query for the prepared statement (to avoid SQL injection)
393- StringBuilder query = new StringBuilder ();
394- query .append ("SELECT username, db_name, e_group, category, creation_date, expiry_date, db_type, db_size, no_connections, project, description, version, state, status, master, slave, host"
395- + " FROM dod_instances WHERE db_name = ? AND status = '1'" );
396- statement = connection .prepareStatement (query .toString ());
397-
398- //Assign values to variables
399- statement .setString (1 , dbName );
400-
401- //Execute query
402- result = statement .executeQuery ();
403-
404- //Instantiate instance object
405- if (result .next ()) {
406- instance = new Instance ();
407- instance .setUsername (result .getString (1 ));
408- instance .setDbName (result .getString (2 ));
409- instance .setEGroup (result .getString (3 ));
410- instance .setCategory (result .getString (4 ));
411- instance .setCreationDate (new java .util .Date (result .getDate (5 ).getTime ()));
412- if (result .getDate (6 ) != null )
413- instance .setExpiryDate (new java .util .Date (result .getDate (6 ).getTime ()));
414- instance .setDbType (result .getString (7 ));
415- instance .setDbSize (result .getInt (8 ));
416- instance .setNoConnections (result .getInt (9 ));
417- instance .setProject (result .getString (10 ));
418- instance .setDescription (result .getString (11 ));
419- instance .setVersion (result .getString (12 ));
420- instance .setState (result .getString (13 ));
421- instance .setStatus (result .getBoolean (14 ));
422- instance .setMaster (result .getString (15 ));
423- instance .setSlave (result .getString (16 ));
424- instance .setHost (result .getString (17 ));
425- //Check if instance needs upgrade
426- if (upgrades != null ) {
427- for (int i =0 ; i < upgrades .size (); i ++) {
428- Upgrade upgrade = upgrades .get (i );
429- if (upgrade .getDbType ().equals (instance .getDbType ()) && upgrade .getCategory ().equals (instance .getCategory ())
430- && upgrade .getVersionFrom ().equals (instance .getVersion ()))
431- instance .setUpgradeTo (upgrade .getVersionTo ());
432- }
342+
343+ try
344+ {
345+ //Get database information from REST API
346+ instance = RestHelper .getObjectFromRestApi ("/entity/" + dbName , Instance .class );
347+
348+ //Check if instance needs upgrade
349+ if (instance != null && upgrades != null ) {
350+ for (int i =0 ; i < upgrades .size (); i ++) {
351+ Upgrade upgrade = upgrades .get (i );
352+ if (upgrade .getDbType ().equals (instance .getDbType ()) && upgrade .getCategory ().equals (instance .getCategory ())
353+ && upgrade .getVersionFrom ().equals (instance .getVersion ()))
354+ instance .setUpgradeTo (upgrade .getVersionTo ());
433355 }
434356 }
435- } catch (NamingException | SQLException ex ) {
436- Logger .getLogger (InstanceDAO .class .getName ()).log (Level .SEVERE , "ERROR SELECTING INSTANCE FOR DB NAME " + dbName ,ex );
437- } finally {
438- try {
439- result .close ();
440- } catch (Exception e ) {
441- }
442- try {
443- statement .close ();
444- } catch (Exception e ) {
445- }
446- try {
447- connection .close ();
448- } catch (Exception e ) {
449- }
450357 }
358+ catch (Exception ex ) {
359+ Logger .getLogger (InstanceDAO .class .getName ()).log (Level .SEVERE , null , ex );
360+ }
361+
451362 return instance ;
452363 }
453364
0 commit comments