Skip to content

Commit

Permalink
Merge branch 'UserInfo'
Browse files Browse the repository at this point in the history
  • Loading branch information
Joscorbe committed Dec 10, 2015
2 parents f2c7b03 + 998e577 commit f3e92c8
Show file tree
Hide file tree
Showing 33 changed files with 2,582 additions and 2,205 deletions.
27 changes: 17 additions & 10 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>ch.cern</groupId>
<artifactId>DBOnDemand</artifactId>
<version>5.4.3</version>
<version>5.5.0</version>
<packaging>war</packaging>

<name>DBOnDemand</name>
Expand Down Expand Up @@ -48,6 +48,16 @@
</repositories>

<dependencies>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20150729</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
Expand Down Expand Up @@ -107,23 +117,20 @@
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.6.4</version>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.6.4</version>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
</dependency>

<dependency>
<groupId>com.google.visualization</groupId>
<artifactId>visualization-datasource</artifactId>
<version>${google-visualization.version}</version>
</dependency>

<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/ch/cern/dbod/appservlet/ConfigLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ public static String getDBTuna4PgPath()
return propertiesFile.getProperty("appdynn_dbtuna4pg_path");
}

public static String getRestApiPath()
{
return propertiesFile.getProperty("restapi_path");
}

private static Properties init()
{
Properties prop = new Properties();
Expand Down
163 changes: 37 additions & 126 deletions src/main/java/ch/cern/dbod/db/dao/InstanceDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

package ch.cern.dbod.db.dao;

import ch.cern.dbod.util.RestHelper;
import ch.cern.dbod.db.entity.Instance;
import ch.cern.dbod.db.entity.InstanceChange;
import ch.cern.dbod.db.entity.Upgrade;
Expand All @@ -19,9 +20,7 @@
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
Expand All @@ -32,6 +31,7 @@
/**
* DAO for Instance entity.
* @author Daniel Gomez Blanco
* @author Jose Andres Cordero Benitez
*/
public class InstanceDAO {

Expand All @@ -55,72 +55,29 @@ private Connection getConnection() throws NamingException, SQLException {
* @return List of all the instances in the database.
*/
public List<Instance> selectAll(List<Upgrade> upgrades) {
Connection connection = null;
PreparedStatement statement = null;
ResultSet result = null;
ArrayList<Instance> instances = new ArrayList<>();
try {
//Get connection
connection = getConnection();

//Prepare query for the prepared statement (to avoid SQL injection)
StringBuilder query = new StringBuilder();
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"
+ " FROM dod_instances WHERE status = '1'"
+ " ORDER BY db_name");
statement = connection.prepareStatement(query.toString());

//Execute query
result = statement.executeQuery();

//Instantiate instance objects
while (result.next()) {
Instance instance = new Instance();
instance.setUsername(result.getString(1));
instance.setDbName(result.getString(2));
instance.setEGroup(result.getString(3));
instance.setCategory(result.getString(4));
instance.setCreationDate(new java.util.Date(result.getDate(5).getTime()));
if (result.getDate(6) != null)
instance.setExpiryDate(new java.util.Date(result.getDate(6).getTime()));
instance.setDbType(result.getString(7));
instance.setDbSize(result.getInt(8));
instance.setNoConnections(result.getInt(9));
instance.setProject(result.getString(10));
instance.setDescription(result.getString(11));
instance.setVersion(result.getString(12));
instance.setState(result.getString(13));
instance.setStatus(result.getBoolean(14));
instance.setMaster(result.getString(15));
instance.setSlave(result.getString(16));
instance.setHost(result.getString(17));
//Check if instance needs upgrade
if (upgrades != null) {
for (int i=0; i < upgrades.size(); i++) {
Upgrade upgrade = upgrades.get(i);
List<Instance> instances = new ArrayList<>();
try
{
//Get database information from REST API
Instance[] array = RestHelper.getObjectListFromRestApi("/entity/all/", Instance[].class);
if (array != null)
instances = Arrays.asList(array);

//Check if instance needs upgrade
if (upgrades != null) {
for (Instance instance : instances) {
for (Upgrade upgrade : upgrades) {
if (upgrade.getDbType().equals(instance.getDbType()) && upgrade.getCategory().equals(instance.getCategory())
&& upgrade.getVersionFrom().equals(instance.getVersion()))
&& upgrade.getVersionFrom().equals(instance.getVersion()))
instance.setUpgradeTo(upgrade.getVersionTo());
}
}
instances.add(instance);
}
} catch (NamingException | SQLException ex) {
Logger.getLogger(InstanceDAO.class.getName()).log(Level.SEVERE, "ERROR SELECTING INSTANCES FOR ADMIN",ex);
} finally {
try {
result.close();
} catch (Exception e) {
}
try {
statement.close();
} catch (Exception e) {
}
try {
connection.close();
} catch (Exception e) {
}
}
catch (Exception ex) {
Logger.getLogger(InstanceDAO.class.getName()).log(Level.SEVERE, null, ex);
}

return instances;
}

Expand Down Expand Up @@ -381,73 +338,27 @@ public List<Instance> selectInstancesPerHost(String host, List<Upgrade> upgrades
* @return instance for the username and DB name specified.
*/
public Instance selectByDbName(String dbName, List<Upgrade> upgrades) {
Connection connection = null;
PreparedStatement statement = null;
ResultSet result = null;
Instance instance = null;
try {
//Get connection
connection = getConnection();

//Prepare query for the prepared statement (to avoid SQL injection)
StringBuilder query = new StringBuilder();
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"
+ " FROM dod_instances WHERE db_name = ? AND status = '1'");
statement = connection.prepareStatement(query.toString());

//Assign values to variables
statement.setString(1, dbName);

//Execute query
result = statement.executeQuery();

//Instantiate instance object
if (result.next()) {
instance = new Instance();
instance.setUsername(result.getString(1));
instance.setDbName(result.getString(2));
instance.setEGroup(result.getString(3));
instance.setCategory(result.getString(4));
instance.setCreationDate(new java.util.Date(result.getDate(5).getTime()));
if (result.getDate(6) != null)
instance.setExpiryDate(new java.util.Date(result.getDate(6).getTime()));
instance.setDbType(result.getString(7));
instance.setDbSize(result.getInt(8));
instance.setNoConnections(result.getInt(9));
instance.setProject(result.getString(10));
instance.setDescription(result.getString(11));
instance.setVersion(result.getString(12));
instance.setState(result.getString(13));
instance.setStatus(result.getBoolean(14));
instance.setMaster(result.getString(15));
instance.setSlave(result.getString(16));
instance.setHost(result.getString(17));
//Check if instance needs upgrade
if (upgrades != null) {
for (int i=0; i < upgrades.size(); i++) {
Upgrade upgrade = upgrades.get(i);
if (upgrade.getDbType().equals(instance.getDbType()) && upgrade.getCategory().equals(instance.getCategory())
&& upgrade.getVersionFrom().equals(instance.getVersion()))
instance.setUpgradeTo(upgrade.getVersionTo());
}

try
{
//Get database information from REST API
instance = RestHelper.getObjectFromRestApi("/entity/" + dbName, Instance.class);

//Check if instance needs upgrade
if (instance != null && upgrades != null) {
for (int i=0; i < upgrades.size(); i++) {
Upgrade upgrade = upgrades.get(i);
if (upgrade.getDbType().equals(instance.getDbType()) && upgrade.getCategory().equals(instance.getCategory())
&& upgrade.getVersionFrom().equals(instance.getVersion()))
instance.setUpgradeTo(upgrade.getVersionTo());
}
}
} catch (NamingException | SQLException ex) {
Logger.getLogger(InstanceDAO.class.getName()).log(Level.SEVERE, "ERROR SELECTING INSTANCE FOR DB NAME " + dbName ,ex);
} finally {
try {
result.close();
} catch (Exception e) {
}
try {
statement.close();
} catch (Exception e) {
}
try {
connection.close();
} catch (Exception e) {
}
}
catch (Exception ex) {
Logger.getLogger(InstanceDAO.class.getName()).log(Level.SEVERE, null, ex);
}

return instance;
}

Expand Down
Loading

0 comments on commit f3e92c8

Please sign in to comment.