-
Notifications
You must be signed in to change notification settings - Fork 115
Adding database model files #408
base: database-model
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0"> | ||
|
||
<persistence-unit name="NewPersistenceUnit" > | ||
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider> | ||
|
||
<class>org.apache.stratos.messaging.domain.application.signup.ApplicationSignUp</class> | ||
<class>org.apache.stratos.messaging.domain.application.signup.ArtifactRepository</class> | ||
<class>org.apache.stratos.messaging.domain.application.signup.DomainMapping</class> | ||
<class>org.apache.stratos.messaging.domain.topology.Cluster</class> | ||
<properties> | ||
<property name="openjpa.ConnectionURL" value="jdbc:mysql://localhost:3306/StratosManager"/> | ||
<property name="openjpa.ConnectionDriverName" value="com.mysql.jdbc.Driver"/> | ||
<property name="openjpa.ConnectionUserName" value="root"/> | ||
<property name="openjpa.ConnectionPassword" value="1234"/> | ||
<property name="openjpa.Log" value="SQL=TRACE, Tool=INFO"/> | ||
<property name="openjpa.ConnectionFactoryProperties" value="PrettyPrint=true, PrettyPrintLineLength=72, PrintParameters=true, MaxActive=10, MaxIdle=5, MinIdle=2, MaxWait=60000"/> | ||
</properties> | ||
</persistence-unit> | ||
</persistence> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be better to rename this file to something similar to stratos-manager.sql. Please refer existing database files (stratos-distribution/dbscripts/). |
||
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0; | ||
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL,ALLOW_INVALID_DATES'; | ||
|
||
CREATE SCHEMA IF NOT EXISTS `StratosManager` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ; | ||
USE `StratosManager` ; | ||
|
||
-- ----------------------------------------------------- | ||
-- Table `StratosManager`.`Cluster` | ||
-- ----------------------------------------------------- | ||
CREATE TABLE IF NOT EXISTS `StratosManager`.`Cluster` ( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Table names and column names might need to use the same naming convention we currently use: upper case letters with underscore separated words. Please refer existing database files (stratos-distribution/dbscripts/) |
||
`clusterId` VARCHAR(255) NULL, | ||
`serviceName` VARCHAR(255) NULL, | ||
`autoscalePolicyName` VARCHAR(255) NULL, | ||
`deploymentPolicyName` VARCHAR(255) NULL, | ||
`hostNames` VARCHAR(255) NULL, | ||
`tenantRange` VARCHAR(45) NULL, | ||
`isLbCluster` TINYINT(1) NULL, | ||
`isKubernetesCluster` TINYINT(1) NULL, | ||
`appId` VARCHAR(45) NULL, | ||
PRIMARY KEY (`clusterId`)) | ||
ENGINE = InnoDB; | ||
|
||
|
||
-- ----------------------------------------------------- | ||
-- Table `StratosManager`.`ArtifactRepository` | ||
-- ----------------------------------------------------- | ||
CREATE TABLE IF NOT EXISTS `StratosManager`.`ArtifactRepository` ( | ||
`alias` VARCHAR(255) NOT NULL, | ||
`isPrivateRepo` TINYINT(1) NULL, | ||
`repoUserName` VARCHAR(255) NULL, | ||
`repoPassword` VARCHAR(255) NULL, | ||
`repoURL` VARCHAR(255) NULL, | ||
`catridgeType` VARCHAR(255) NULL, | ||
PRIMARY KEY (`alias`)) | ||
ENGINE = InnoDB; | ||
|
||
|
||
-- ----------------------------------------------------- | ||
-- Table `StratosManager`.`ApplicationSignUp` | ||
-- ----------------------------------------------------- | ||
CREATE TABLE IF NOT EXISTS `StratosManager`.`ApplicationSignUp` ( | ||
`applicationId` VARCHAR(255) NOT NULL, | ||
`tenantId` INT NOT NULL, | ||
`Cluster_clusterId` VARCHAR(255) NOT NULL, | ||
`ArtifactRepository_alias` VARCHAR(255) NOT NULL, | ||
PRIMARY KEY (`applicationId`, `tenantId`), | ||
INDEX `fk_ApplicationSignUp_Cluster_idx` (`Cluster_clusterId` ASC), | ||
INDEX `fk_ApplicationSignUp_ArtifactRepository1_idx` (`ArtifactRepository_alias` ASC), | ||
CONSTRAINT `fk_ApplicationSignUp_Cluster` | ||
FOREIGN KEY (`Cluster_clusterId`) | ||
REFERENCES `StratosManager`.`Cluster` (`clusterId`) | ||
ON DELETE NO ACTION | ||
ON UPDATE NO ACTION, | ||
CONSTRAINT `fk_ApplicationSignUp_ArtifactRepository1` | ||
FOREIGN KEY (`ArtifactRepository_alias`) | ||
REFERENCES `StratosManager`.`ArtifactRepository` (`alias`) | ||
ON DELETE NO ACTION | ||
ON UPDATE NO ACTION) | ||
ENGINE = InnoDB; | ||
|
||
|
||
-- ----------------------------------------------------- | ||
-- Table `StratosManager`.`DomainMapping` | ||
-- ----------------------------------------------------- | ||
CREATE TABLE IF NOT EXISTS `StratosManager`.`DomainMapping` ( | ||
`domainName` VARCHAR(255) NOT NULL, | ||
`serviceName` VARCHAR(255) NULL, | ||
`contextPath` VARCHAR(255) NULL, | ||
`ApplicationSignUp_applicationId` VARCHAR(255) NOT NULL, | ||
`ApplicationSignUp_tenantId` INT NOT NULL, | ||
`Cluster_clusterId` VARCHAR(255) NOT NULL, | ||
PRIMARY KEY (`domainName`, `ApplicationSignUp_tenantId`, `ApplicationSignUp_applicationId`), | ||
INDEX `fk_DomainMapping_ApplicationSignUp1_idx` (`ApplicationSignUp_applicationId` ASC, `ApplicationSignUp_tenantId` ASC), | ||
INDEX `fk_DomainMapping_Cluster1_idx` (`Cluster_clusterId` ASC), | ||
CONSTRAINT `fk_DomainMapping_ApplicationSignUp1` | ||
FOREIGN KEY (`ApplicationSignUp_applicationId` , `ApplicationSignUp_tenantId`) | ||
REFERENCES `StratosManager`.`ApplicationSignUp` (`applicationId` , `tenantId`) | ||
ON DELETE NO ACTION | ||
ON UPDATE NO ACTION, | ||
CONSTRAINT `fk_DomainMapping_Cluster1` | ||
FOREIGN KEY (`Cluster_clusterId`) | ||
REFERENCES `StratosManager`.`Cluster` (`clusterId`) | ||
ON DELETE NO ACTION | ||
ON UPDATE NO ACTION) | ||
ENGINE = InnoDB; | ||
|
||
|
||
SET SQL_MODE=@OLD_SQL_MODE; | ||
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS; | ||
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,259 @@ | ||
package org.apache.stratos.manager.Persistence; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Licence header is missing. |
||
|
||
import org.apache.commons.logging.Log; | ||
import org.apache.commons.logging.LogFactory; | ||
|
||
import javax.persistence.*; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you format the code. It looks like it is not formatted. |
||
|
||
import java.util.*; | ||
/** | ||
* Created by aarthy on 7/27/15. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove author tag. |
||
*/ | ||
public class PersistenceManager { | ||
|
||
private static final Log log; | ||
|
||
static { | ||
log = LogFactory.getLog(PersistenceManager.class); | ||
} | ||
|
||
private EntityManagerFactory entitymanagerFactory = Persistence.createEntityManagerFactory("PersistenceUnit"); | ||
|
||
private static final PersistenceManager instance = new PersistenceManager(); | ||
|
||
EntityManager entityManager = null; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We might not need to keep blank lines between variables. |
||
|
||
|
||
public static PersistenceManager getInstance() { | ||
return instance; | ||
} | ||
|
||
|
||
/** | ||
* Add object to persist | ||
* @param object | ||
* @throws PersistenceException | ||
*/ | ||
public void add(Object object) | ||
{ | ||
System.out.printf("entered"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove sys.out s. |
||
|
||
System.out.printf("true"); | ||
try { | ||
entityManager = this.entitymanagerFactory.createEntityManager(); | ||
entityManager.getTransaction().begin(); | ||
entityManager.persist(object); | ||
entityManager.flush(); | ||
entityManager.getTransaction().commit(); | ||
String msg="Added Successfully"; | ||
log.info(msg); | ||
} | ||
catch (PersistenceException e) | ||
{ | ||
String msg="Error while adding"; | ||
log.error(msg); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Exception object is not included in the log: log.error(msg, e). |
||
} | ||
|
||
} | ||
|
||
|
||
/** | ||
* remove object by primary key | ||
* @param object | ||
* @param primaryKey | ||
* @throws PersistenceException | ||
*/ | ||
|
||
public void remove(Object object,Object primaryKey) | ||
{ | ||
try { | ||
entityManager = this.entitymanagerFactory.createEntityManager(); | ||
entityManager.getTransaction().begin(); | ||
Object found=entityManager.find(object.getClass(), primaryKey); | ||
if(found!=null) { | ||
entityManager.remove(found); | ||
entityManager.getTransaction().commit(); | ||
String msg = "Deleted sucessfully"; | ||
log.info(msg); | ||
} | ||
else | ||
{ | ||
String msg ="Object does not exists"; | ||
log.error(msg); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Exception object is not included in the log: log.error(msg, e). |
||
} | ||
} | ||
catch (PersistenceException e) | ||
{ | ||
|
||
String msg="Error while Deleting"; | ||
log.error(msg); | ||
} | ||
|
||
} | ||
|
||
/** | ||
* retrieve an object by primary key | ||
* @param object | ||
* @param primaryKey | ||
* @return | ||
*/ | ||
|
||
public Object retrieve(Object object,Object primaryKey) | ||
{ | ||
Object found=null; | ||
try{ | ||
|
||
entityManager=this.entitymanagerFactory.createEntityManager(); | ||
entityManager.getTransaction().begin(); | ||
found= entityManager.find(found.getClass(), primaryKey); | ||
if(found!=null) | ||
log.info("Object Found"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As a convention we need to use braces when defining if statements even with one line. |
||
else | ||
log.error("Object not Found"); | ||
return found; | ||
|
||
} | ||
catch (PersistenceException e) | ||
{ | ||
|
||
String msg="Error while retrieving"; | ||
log.error(msg); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Exception object is not included in the log: log.error(msg, e). |
||
return found; | ||
} | ||
|
||
} | ||
|
||
/** | ||
* | ||
* @param tableName | ||
* @return | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Better to explain @return as well. |
||
*/ | ||
|
||
|
||
public List retrieveAll(String tableName) | ||
{ | ||
List objectList=new ArrayList<Object>(); | ||
try{ | ||
|
||
entityManager=this.entitymanagerFactory.createEntityManager(); | ||
entityManager.getTransaction().begin(); | ||
String msg="Successfully retrieved"; | ||
|
||
objectList= entityManager.createQuery("select obj from "+tableName+" obj" ).getResultList(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. formatting is wrong here as well. |
||
|
||
if(objectList!=null) | ||
{ | ||
log.info(msg); | ||
return objectList; | ||
} | ||
else | ||
return null; | ||
|
||
} | ||
catch (javax.persistence.PersistenceException e) | ||
{ | ||
String msg="Object not found"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Have a more descriptive error message |
||
System.out.println(msg); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use log instead of system.out. |
||
return null; | ||
} | ||
|
||
} | ||
|
||
/** | ||
* | ||
* @param object | ||
* @param primaryKey | ||
* @param setValues | ||
* @param whereValues | ||
*/ | ||
|
||
public void update(Object object,Object primaryKey,Map<String,Object> setValues,Map<String,Object> whereValues) | ||
{ | ||
String setQuery=""; | ||
String updateQuery=""; | ||
String whereQuery=""; | ||
|
||
try { | ||
|
||
Object foundObject =retrieve(object,primaryKey); | ||
|
||
if(foundObject!=null) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As Lahiru has mentioned above, code seems to be not formatted: if(foundObject != null) |
||
{ | ||
|
||
String query ="Update "+ object +" obj Set "; | ||
int count =0; | ||
for (String key : setValues.keySet()) { | ||
setQuery += "obj."+key + "="; | ||
|
||
if(setValues.get(key)instanceof String) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What would be the reason for this logic? |
||
{ | ||
setQuery+="'"+setValues.get(key)+"'"; | ||
} | ||
else | ||
{ | ||
setQuery+=setValues.get(key); | ||
} | ||
|
||
count++; | ||
if(setValues.size()>1 && count!=setValues.size()) | ||
{ | ||
setQuery+=","; | ||
} | ||
} | ||
updateQuery=query+setQuery+" Where "; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we construct SQL queries when using an ORM? |
||
|
||
int pkCount=0; | ||
for (String key : whereValues.keySet()){ | ||
|
||
whereQuery+="obj."+key+"="; | ||
if(whereValues.get(key)instanceof String) | ||
{ whereQuery+="'"+whereValues.get(key)+"'"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This logic might be vulnerable to SQL injection. |
||
} | ||
else | ||
{ whereQuery+=whereValues.get(key); | ||
} | ||
pkCount++; | ||
if(whereValues.size()>1 && pkCount!=whereValues.size()) | ||
{ whereQuery+=" and "; | ||
} | ||
} | ||
|
||
updateQuery+=whereQuery; | ||
entityManager=this.entitymanagerFactory.createEntityManager(); | ||
|
||
entityManager.getTransaction().begin(); | ||
|
||
Query queryString= entityManager.createQuery(updateQuery); | ||
int updatedCount =queryString.executeUpdate(); | ||
|
||
if (updatedCount==1) | ||
{ | ||
entityManager.getTransaction().commit(); | ||
String msg="updated Successfully"; | ||
log.info(msg); | ||
} | ||
|
||
else | ||
{ | ||
String msg= "Error while Updating"; | ||
log.error(msg); | ||
} | ||
} | ||
else { | ||
String msg="Object not found"; | ||
log.error(msg); | ||
} | ||
|
||
} | ||
catch (PersistenceException e) | ||
{ | ||
String msg="Error while updating"; | ||
log.error(msg); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Exception object is not included in the log: log.error(msg, e). |
||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It might be better to remove blank lines and format the code properly. |
||
|
||
|
||
} | ||
|
||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add licence header