WIP

Project: http://git-wip-us.apache.org/repos/asf/airavata/repo
Commit: http://git-wip-us.apache.org/repos/asf/airavata/commit/146ccc77
Tree: http://git-wip-us.apache.org/repos/asf/airavata/tree/146ccc77
Diff: http://git-wip-us.apache.org/repos/asf/airavata/diff/146ccc77

Branch: refs/heads/airavata-gov-registry
Commit: 146ccc774ed18cb0cf1af3b31514121911520ad9
Parents: ff3fda1
Author: scnakandala <[email protected]>
Authored: Sun Oct 9 00:57:39 2016 -0400
Committer: scnakandala <[email protected]>
Committed: Sun Oct 9 00:57:39 2016 -0400

----------------------------------------------------------------------
 .../src/main/assembly/bin-assembly.xml          |   2 +-
 .../sharing-registry-core/pom.xml               |   5 +
 .../registry/db/entities/SharingUserEntity.java | 112 +++++++
 .../registry/db/entities/UserEntity.java        | 112 -------
 .../repositories/GroupMembershipRepository.java |   4 +-
 .../db/repositories/UserRepository.java         |   6 +-
 .../sharing/registry/db/utils/JPAUtils.java     |  18 +-
 .../server/SharingRegistryServerHandler.java    |  15 +-
 .../src/main/resources/META-INF/persistence.xml |   2 +-
 .../main/resources/sharing-registry-derby.sql   |  36 +--
 .../main/resources/sharing-registry-mysql.sql   |   6 +-
 .../SharingRegistryServerHandlerTest.java       |   9 +
 .../sharing/registry/util/Initialize.java       | 298 +++++++++++++++++++
 13 files changed, 475 insertions(+), 150 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/airavata/blob/146ccc77/modules/distribution/src/main/assembly/bin-assembly.xml
----------------------------------------------------------------------
diff --git a/modules/distribution/src/main/assembly/bin-assembly.xml 
b/modules/distribution/src/main/assembly/bin-assembly.xml
index c902de1..0f08657 100644
--- a/modules/distribution/src/main/assembly/bin-assembly.xml
+++ b/modules/distribution/src/main/assembly/bin-assembly.xml
@@ -67,7 +67,7 @@
             </includes>
         </fileSet>
         <fileSet>
-            
<directory>../airavata-sharing-registry/airavata-sharing-registry-core/src/main/resources/
+            
<directory>../sharing-registry/sharing-registry-core/src/main/resources/
             </directory>
             <outputDirectory>bin/database_scripts
             </outputDirectory>

http://git-wip-us.apache.org/repos/asf/airavata/blob/146ccc77/modules/sharing-registry/sharing-registry-core/pom.xml
----------------------------------------------------------------------
diff --git a/modules/sharing-registry/sharing-registry-core/pom.xml 
b/modules/sharing-registry/sharing-registry-core/pom.xml
index f43f77c..3ed4de5 100644
--- a/modules/sharing-registry/sharing-registry-core/pom.xml
+++ b/modules/sharing-registry/sharing-registry-core/pom.xml
@@ -29,6 +29,11 @@
         </dependency>
         <dependency>
             <groupId>org.apache.airavata</groupId>
+            <artifactId>airavata-server-configuration</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.airavata</groupId>
             <artifactId>airavata-commons</artifactId>
             <version>${project.version}</version>
         </dependency>

http://git-wip-us.apache.org/repos/asf/airavata/blob/146ccc77/modules/sharing-registry/sharing-registry-core/src/main/java/org/apache/airavata/sharing/registry/db/entities/SharingUserEntity.java
----------------------------------------------------------------------
diff --git 
a/modules/sharing-registry/sharing-registry-core/src/main/java/org/apache/airavata/sharing/registry/db/entities/SharingUserEntity.java
 
b/modules/sharing-registry/sharing-registry-core/src/main/java/org/apache/airavata/sharing/registry/db/entities/SharingUserEntity.java
new file mode 100644
index 0000000..31e6e6e
--- /dev/null
+++ 
b/modules/sharing-registry/sharing-registry-core/src/main/java/org/apache/airavata/sharing/registry/db/entities/SharingUserEntity.java
@@ -0,0 +1,112 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+*/
+package org.apache.airavata.sharing.registry.db.entities;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.persistence.*;
+
+@Entity
+@Table(name = "SHARING_USER", schema = "")
+public class SharingUserEntity {
+    private final static Logger logger = 
LoggerFactory.getLogger(SharingUserEntity.class);
+    private String userId;
+    private String domainId;
+    private String userName;
+    private Long createdTime;
+    private Long updatedTime;
+
+    @Id
+    @Column(name = "USER_ID")
+    public String getUserId() {
+        return userId;
+    }
+
+    public void setUserId(String userId) {
+        this.userId = userId;
+    }
+
+    @Basic
+    @Column(name = "DOMAIN_ID")
+    public String getDomainId() {
+        return domainId;
+    }
+
+    public void setDomainId(String domainId) {
+        this.domainId = domainId;
+    }
+
+    @Basic
+    @Column(name = "USER_NAME")
+    public String getUserName() {
+        return userName;
+    }
+
+    public void setUserName(String userName) {
+        this.userName = userName;
+    }
+
+    @Basic
+    @Column(name = "CREATED_TIME")
+    public Long getCreatedTime() {
+        return createdTime;
+    }
+
+    public void setCreatedTime(Long createdTime) {
+        this.createdTime = createdTime;
+    }
+
+    @Basic
+    @Column(name = "UPDATED_TIME")
+    public Long getUpdatedTime() {
+        return updatedTime;
+    }
+
+    public void setUpdatedTime(Long updatedTime) {
+        this.updatedTime = updatedTime;
+    }
+
+    @Override
+    public boolean equals(Object o) {
+        if (this == o) return true;
+        if (o == null || getClass() != o.getClass()) return false;
+
+        SharingUserEntity that = (SharingUserEntity) o;
+
+        if (userId != null ? !userId.equals(that.userId) : that.userId != 
null) return false;
+        if (domainId != null ? !domainId.equals(that.domainId) : that.domainId 
!= null) return false;
+        if (userName != null ? !userName.equals(that.userName) : that.userName 
!= null) return false;
+        if (createdTime != null ? !createdTime.equals(that.createdTime) : 
that.createdTime != null) return false;
+        if (updatedTime != null ? !updatedTime.equals(that.updatedTime) : 
that.updatedTime != null) return false;
+
+        return true;
+    }
+
+    @Override
+    public int hashCode() {
+        int result = userId != null ? userId.hashCode() : 0;
+        result = 31 * result + (userName != null ? userName.hashCode() : 0);
+        result = 31 * result + (createdTime != null ? createdTime.hashCode() : 
0);
+        result = 31 * result + (updatedTime != null ? updatedTime.hashCode() : 
0);
+        return result;
+    }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/airavata/blob/146ccc77/modules/sharing-registry/sharing-registry-core/src/main/java/org/apache/airavata/sharing/registry/db/entities/UserEntity.java
----------------------------------------------------------------------
diff --git 
a/modules/sharing-registry/sharing-registry-core/src/main/java/org/apache/airavata/sharing/registry/db/entities/UserEntity.java
 
b/modules/sharing-registry/sharing-registry-core/src/main/java/org/apache/airavata/sharing/registry/db/entities/UserEntity.java
deleted file mode 100644
index 256d2e7..0000000
--- 
a/modules/sharing-registry/sharing-registry-core/src/main/java/org/apache/airavata/sharing/registry/db/entities/UserEntity.java
+++ /dev/null
@@ -1,112 +0,0 @@
-/*
- *
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- *
-*/
-package org.apache.airavata.sharing.registry.db.entities;
-
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import javax.persistence.*;
-
-@Entity
-@Table(name = "USER", schema = "")
-public class UserEntity {
-    private final static Logger logger = 
LoggerFactory.getLogger(UserEntity.class);
-    private String userId;
-    private String domainId;
-    private String userName;
-    private Long createdTime;
-    private Long updatedTime;
-
-    @Id
-    @Column(name = "USER_ID")
-    public String getUserId() {
-        return userId;
-    }
-
-    public void setUserId(String userId) {
-        this.userId = userId;
-    }
-
-    @Basic
-    @Column(name = "DOMAIN_ID")
-    public String getDomainId() {
-        return domainId;
-    }
-
-    public void setDomainId(String domainId) {
-        this.domainId = domainId;
-    }
-
-    @Basic
-    @Column(name = "USER_NAME")
-    public String getUserName() {
-        return userName;
-    }
-
-    public void setUserName(String userName) {
-        this.userName = userName;
-    }
-
-    @Basic
-    @Column(name = "CREATED_TIME")
-    public Long getCreatedTime() {
-        return createdTime;
-    }
-
-    public void setCreatedTime(Long createdTime) {
-        this.createdTime = createdTime;
-    }
-
-    @Basic
-    @Column(name = "UPDATED_TIME")
-    public Long getUpdatedTime() {
-        return updatedTime;
-    }
-
-    public void setUpdatedTime(Long updatedTime) {
-        this.updatedTime = updatedTime;
-    }
-
-    @Override
-    public boolean equals(Object o) {
-        if (this == o) return true;
-        if (o == null || getClass() != o.getClass()) return false;
-
-        UserEntity that = (UserEntity) o;
-
-        if (userId != null ? !userId.equals(that.userId) : that.userId != 
null) return false;
-        if (domainId != null ? !domainId.equals(that.domainId) : that.domainId 
!= null) return false;
-        if (userName != null ? !userName.equals(that.userName) : that.userName 
!= null) return false;
-        if (createdTime != null ? !createdTime.equals(that.createdTime) : 
that.createdTime != null) return false;
-        if (updatedTime != null ? !updatedTime.equals(that.updatedTime) : 
that.updatedTime != null) return false;
-
-        return true;
-    }
-
-    @Override
-    public int hashCode() {
-        int result = userId != null ? userId.hashCode() : 0;
-        result = 31 * result + (userName != null ? userName.hashCode() : 0);
-        result = 31 * result + (createdTime != null ? createdTime.hashCode() : 
0);
-        result = 31 * result + (updatedTime != null ? updatedTime.hashCode() : 
0);
-        return result;
-    }
-}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/airavata/blob/146ccc77/modules/sharing-registry/sharing-registry-core/src/main/java/org/apache/airavata/sharing/registry/db/repositories/GroupMembershipRepository.java
----------------------------------------------------------------------
diff --git 
a/modules/sharing-registry/sharing-registry-core/src/main/java/org/apache/airavata/sharing/registry/db/repositories/GroupMembershipRepository.java
 
b/modules/sharing-registry/sharing-registry-core/src/main/java/org/apache/airavata/sharing/registry/db/repositories/GroupMembershipRepository.java
index e976ba9..87d5d4c 100644
--- 
a/modules/sharing-registry/sharing-registry-core/src/main/java/org/apache/airavata/sharing/registry/db/repositories/GroupMembershipRepository.java
+++ 
b/modules/sharing-registry/sharing-registry-core/src/main/java/org/apache/airavata/sharing/registry/db/repositories/GroupMembershipRepository.java
@@ -22,7 +22,7 @@ package org.apache.airavata.sharing.registry.db.repositories;
 
 import org.apache.airavata.sharing.registry.db.entities.GroupMembershipEntity;
 import 
org.apache.airavata.sharing.registry.db.entities.GroupMembershipEntityPK;
-import org.apache.airavata.sharing.registry.db.entities.UserEntity;
+import org.apache.airavata.sharing.registry.db.entities.SharingUserEntity;
 import org.apache.airavata.sharing.registry.db.entities.UserGroupEntity;
 import org.apache.airavata.sharing.registry.db.utils.DBConstants;
 import org.apache.airavata.sharing.registry.models.*;
@@ -42,7 +42,7 @@ public class GroupMembershipRepository extends 
AbstractRepository<GroupMembershi
     }
 
     public List<User> getAllChildUsers(String groupId) throws 
SharingRegistryException {
-        String queryString = "SELECT U FROM " + 
UserEntity.class.getSimpleName() + " U, " + 
GroupMembershipEntity.class.getSimpleName()
+        String queryString = "SELECT U FROM " + 
SharingUserEntity.class.getSimpleName() + " U, " + 
GroupMembershipEntity.class.getSimpleName()
                 + " GM WHERE GM." + DBConstants.GroupMembershipTable.CHILD_ID 
+ " = U." + DBConstants.UserTable.USER_ID + " AND " +
                 "gm." + 
DBConstants.GroupMembershipTable.PARENT_ID+"='"+groupId + "' AND gm." + 
DBConstants.GroupMembershipTable.CHILD_TYPE
                 + "='" + GroupChildType.USER.toString() + "'";

http://git-wip-us.apache.org/repos/asf/airavata/blob/146ccc77/modules/sharing-registry/sharing-registry-core/src/main/java/org/apache/airavata/sharing/registry/db/repositories/UserRepository.java
----------------------------------------------------------------------
diff --git 
a/modules/sharing-registry/sharing-registry-core/src/main/java/org/apache/airavata/sharing/registry/db/repositories/UserRepository.java
 
b/modules/sharing-registry/sharing-registry-core/src/main/java/org/apache/airavata/sharing/registry/db/repositories/UserRepository.java
index f4ec03f..6a17f46 100644
--- 
a/modules/sharing-registry/sharing-registry-core/src/main/java/org/apache/airavata/sharing/registry/db/repositories/UserRepository.java
+++ 
b/modules/sharing-registry/sharing-registry-core/src/main/java/org/apache/airavata/sharing/registry/db/repositories/UserRepository.java
@@ -21,15 +21,15 @@
 package org.apache.airavata.sharing.registry.db.repositories;
 
 
-import org.apache.airavata.sharing.registry.db.entities.UserEntity;
+import org.apache.airavata.sharing.registry.db.entities.SharingUserEntity;
 import org.apache.airavata.sharing.registry.models.User;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-public class UserRepository extends AbstractRepository<User, UserEntity, 
String> {
+public class UserRepository extends AbstractRepository<User, 
SharingUserEntity, String> {
     private final static Logger logger = 
LoggerFactory.getLogger(UserRepository.class);
 
     public UserRepository() {
-        super(User.class, UserEntity.class);
+        super(User.class, SharingUserEntity.class);
     }
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/airavata/blob/146ccc77/modules/sharing-registry/sharing-registry-core/src/main/java/org/apache/airavata/sharing/registry/db/utils/JPAUtils.java
----------------------------------------------------------------------
diff --git 
a/modules/sharing-registry/sharing-registry-core/src/main/java/org/apache/airavata/sharing/registry/db/utils/JPAUtils.java
 
b/modules/sharing-registry/sharing-registry-core/src/main/java/org/apache/airavata/sharing/registry/db/utils/JPAUtils.java
index ed147e2..f879c15 100644
--- 
a/modules/sharing-registry/sharing-registry-core/src/main/java/org/apache/airavata/sharing/registry/db/utils/JPAUtils.java
+++ 
b/modules/sharing-registry/sharing-registry-core/src/main/java/org/apache/airavata/sharing/registry/db/utils/JPAUtils.java
@@ -39,14 +39,14 @@ import java.util.Map;
 public class JPAUtils {
     private final static Logger logger = 
LoggerFactory.getLogger(JPAUtils.class);
 
-    private static final String PERSISTENCE_UNIT_NAME = 
"airavata-sharing-registry";
-    private static final String SHARING_REG_JDBC_DRIVER = 
"sharingcatalog.jdbc.driver";
-    private static final String SHARING_REG_JDBC_URL = 
"sharingcatalog.jdbc.url";
-    private static final String SHARING_REG_JDBC_USER = 
"sharingcatalog.jdbc.user";
-    private static final String SHARING_REG_JDBC_PWD = 
"sharingcatalog.jdbc.password";
-    private static final String SHARING_REG_VALIDATION_QUERY = 
"sharingcatalog.validationQuery";
-    private static final String JPA_CACHE_SIZE = "jpa.cache.size";
-    private static final String JPA_CACHE_ENABLED = "cache.enable";
+    public static final String PERSISTENCE_UNIT_NAME = 
"airavata-sharing-registry";
+    public static final String SHARING_REG_JDBC_DRIVER = 
"sharingcatalog.jdbc.driver";
+    public static final String SHARING_REG_JDBC_URL = 
"sharingcatalog.jdbc.url";
+    public static final String SHARING_REG_JDBC_USER = 
"sharingcatalog.jdbc.user";
+    public static final String SHARING_REG_JDBC_PWD = 
"sharingcatalog.jdbc.password";
+    public static final String SHARING_REG_VALIDATION_QUERY = 
"sharingcatalog.validationQuery";
+    public static final String JPA_CACHE_SIZE = "jpa.cache.size";
+    public static final String JPA_CACHE_ENABLED = "cache.enable";
 
     public static final String CONFIGURATION = "CONFIGURATION";
     public static final String START_DERBY_ENABLE = "start.derby.server.mode";
@@ -219,7 +219,7 @@ public class JPAUtils {
         }
     }
 
-    private static String readServerProperties(String propertyName) throws 
SharingRegistryException {
+    public static String readServerProperties(String propertyName) throws 
SharingRegistryException {
         try {
             return ServerSettings.getSetting(propertyName);
         } catch (ApplicationSettingsException e) {

http://git-wip-us.apache.org/repos/asf/airavata/blob/146ccc77/modules/sharing-registry/sharing-registry-core/src/main/java/org/apache/airavata/sharing/registry/server/SharingRegistryServerHandler.java
----------------------------------------------------------------------
diff --git 
a/modules/sharing-registry/sharing-registry-core/src/main/java/org/apache/airavata/sharing/registry/server/SharingRegistryServerHandler.java
 
b/modules/sharing-registry/sharing-registry-core/src/main/java/org/apache/airavata/sharing/registry/server/SharingRegistryServerHandler.java
index a5a32d2..b260018 100644
--- 
a/modules/sharing-registry/sharing-registry-core/src/main/java/org/apache/airavata/sharing/registry/server/SharingRegistryServerHandler.java
+++ 
b/modules/sharing-registry/sharing-registry-core/src/main/java/org/apache/airavata/sharing/registry/server/SharingRegistryServerHandler.java
@@ -26,6 +26,7 @@ import 
org.apache.airavata.sharing.registry.db.entities.GroupMembershipEntityPK;
 import org.apache.airavata.sharing.registry.db.entities.SharingEntityPK;
 import org.apache.airavata.sharing.registry.db.repositories.*;
 import org.apache.airavata.sharing.registry.db.utils.DBConstants;
+import org.apache.airavata.sharing.registry.db.utils.JPAUtils;
 import org.apache.airavata.sharing.registry.models.*;
 import org.apache.airavata.sharing.registry.service.cpi.GovRegistryService;
 import org.apache.thrift.TException;
@@ -50,6 +51,8 @@ public class SharingRegistryServerHandler implements 
GovRegistryService.Iface{
     private SharingRepository sharingRepository;
 
     public SharingRegistryServerHandler() throws ApplicationSettingsException, 
TException {
+        JPAUtils.initializeDB();
+
         this.domainRepository = new DomainRepository();
         this.userRepository = new UserRepository();
         this.userGroupRepository = new UserGroupRepository();
@@ -422,7 +425,17 @@ public class SharingRegistryServerHandler implements 
GovRegistryService.Iface{
             user.setDomainId(entity.domainId);
             user.setUserName(user.userId.split("@")[0]);
 
-            userRepository.create(user);
+            createUser(user);
+
+            UserGroup userGroup = new UserGroup();
+            userGroup.setGroupId(user.userId);
+            userGroup.setDomainId(user.domainId);
+            userGroup.setOwnerId(user.userId);
+            userGroup.setName(user.userName);
+            userGroup.setDescription("Single user group for " + user.userName);
+            userGroup.setGroupType(GroupType.SINGLE_USER);
+
+            createGroup(userGroup);
         }
 
         entity.setCreatedTime(System.currentTimeMillis());

http://git-wip-us.apache.org/repos/asf/airavata/blob/146ccc77/modules/sharing-registry/sharing-registry-core/src/main/resources/META-INF/persistence.xml
----------------------------------------------------------------------
diff --git 
a/modules/sharing-registry/sharing-registry-core/src/main/resources/META-INF/persistence.xml
 
b/modules/sharing-registry/sharing-registry-core/src/main/resources/META-INF/persistence.xml
index a9f808c..7b08528 100644
--- 
a/modules/sharing-registry/sharing-registry-core/src/main/resources/META-INF/persistence.xml
+++ 
b/modules/sharing-registry/sharing-registry-core/src/main/resources/META-INF/persistence.xml
@@ -9,7 +9,7 @@
         
<class>org.apache.airavata.sharing.registry.db.entities.GroupMembershipEntity</class>
         
<class>org.apache.airavata.sharing.registry.db.entities.PermissionTypeEntity</class>
         
<class>org.apache.airavata.sharing.registry.db.entities.SharingEntity</class>
-        
<class>org.apache.airavata.sharing.registry.db.entities.UserEntity</class>
+        
<class>org.apache.airavata.sharing.registry.db.entities.SharingUserEntity</class>
         
<class>org.apache.airavata.sharing.registry.db.entities.UserGroupEntity</class>
     </persistence-unit>
 </persistence>

http://git-wip-us.apache.org/repos/asf/airavata/blob/146ccc77/modules/sharing-registry/sharing-registry-core/src/main/resources/sharing-registry-derby.sql
----------------------------------------------------------------------
diff --git 
a/modules/sharing-registry/sharing-registry-core/src/main/resources/sharing-registry-derby.sql
 
b/modules/sharing-registry/sharing-registry-core/src/main/resources/sharing-registry-derby.sql
index a7bb905..4e0fab3 100644
--- 
a/modules/sharing-registry/sharing-registry-core/src/main/resources/sharing-registry-derby.sql
+++ 
b/modules/sharing-registry/sharing-registry-core/src/main/resources/sharing-registry-derby.sql
@@ -28,14 +28,14 @@ CREATE TABLE DOMAIN (
   PRIMARY KEY (DOMAIN_ID)
 );
 
-CREATE TABLE USER (
+CREATE TABLE SHARING_USER (
   USER_ID VARCHAR(255) NOT NULL,
   DOMAIN_ID VARCHAR(255) NOT NULL,
   USER_NAME VARCHAR(255) NOT NULL,
   CREATED_TIME BIGINT NOT NULL,
   UPDATED_TIME BIGINT NOT NULL,
   PRIMARY KEY (USER_ID),
-  FOREIGN KEY (DOMAIN_ID) REFERENCES DOMAIN(DOMAIN_ID) ON DELETE CASCADE ON 
UPDATE CASCADE
+  FOREIGN KEY (DOMAIN_ID) REFERENCES DOMAIN(DOMAIN_ID) ON DELETE CASCADE ON 
UPDATE NO ACTION
 );
 
 CREATE TABLE USER_GROUP (
@@ -48,8 +48,8 @@ CREATE TABLE USER_GROUP (
   CREATED_TIME BIGINT NOT NULL,
   UPDATED_TIME BIGINT NOT NULL,
   PRIMARY KEY (GROUP_ID),
-  FOREIGN KEY (DOMAIN_ID) REFERENCES DOMAIN(DOMAIN_ID) ON DELETE CASCADE ON 
UPDATE CASCADE,
-  FOREIGN KEY (OWNER_ID) REFERENCES USER(USER_ID) ON DELETE CASCADE ON UPDATE 
CASCADE
+  FOREIGN KEY (DOMAIN_ID) REFERENCES DOMAIN(DOMAIN_ID) ON DELETE CASCADE ON 
UPDATE NO ACTION,
+  FOREIGN KEY (OWNER_ID) REFERENCES SHARING_USER(USER_ID) ON DELETE CASCADE ON 
UPDATE NO ACTION
 );
 
 
@@ -60,8 +60,8 @@ CREATE TABLE GROUP_MEMBERSHIP (
   CREATED_TIME BIGINT NOT NULL,
   UPDATED_TIME BIGINT NOT NULL,
   PRIMARY KEY (PARENT_ID, CHILD_ID),
-  FOREIGN KEY (PARENT_ID) REFERENCES USER_GROUP(GROUP_ID) ON DELETE CASCADE ON 
UPDATE CASCADE,
-  FOREIGN KEY (CHILD_ID) REFERENCES USER_GROUP(GROUP_ID) ON DELETE CASCADE ON 
UPDATE CASCADE
+  FOREIGN KEY (PARENT_ID) REFERENCES USER_GROUP(GROUP_ID) ON DELETE CASCADE ON 
UPDATE NO ACTION,
+  FOREIGN KEY (CHILD_ID) REFERENCES USER_GROUP(GROUP_ID) ON DELETE CASCADE ON 
UPDATE NO ACTION
 );
 
 CREATE TABLE ENTITY_TYPE (
@@ -72,7 +72,7 @@ CREATE TABLE ENTITY_TYPE (
   CREATED_TIME BIGINT NOT NULL,
   UPDATED_TIME BIGINT NOT NULL,
   PRIMARY KEY (ENTITY_TYPE_ID),
-  FOREIGN KEY (DOMAIN_ID) REFERENCES DOMAIN(DOMAIN_ID) ON DELETE CASCADE ON 
UPDATE CASCADE
+  FOREIGN KEY (DOMAIN_ID) REFERENCES DOMAIN(DOMAIN_ID) ON DELETE CASCADE ON 
UPDATE NO ACTION
 );
 
 CREATE TABLE PERMISSION_TYPE (
@@ -83,7 +83,7 @@ CREATE TABLE PERMISSION_TYPE (
   CREATED_TIME BIGINT NOT NULL,
   UPDATED_TIME BIGINT NOT NULL,
   PRIMARY KEY (PERMISSION_TYPE_ID),
-  FOREIGN KEY (DOMAIN_ID) REFERENCES DOMAIN(DOMAIN_ID) ON DELETE CASCADE ON 
UPDATE CASCADE
+  FOREIGN KEY (DOMAIN_ID) REFERENCES DOMAIN(DOMAIN_ID) ON DELETE CASCADE ON 
UPDATE NO ACTION
 );
 
 CREATE TABLE ENTITY (
@@ -94,24 +94,24 @@ CREATE TABLE ENTITY (
   PARENT_ENTITY_ID VARCHAR(255),
   NAME VARCHAR(255) NOT NULL,
   DESCRIPTION VARCHAR(255),
-  FULL_TEXT TEXT,
+  FULL_TEXT CLOB,
   CREATED_TIME BIGINT NOT NULL,
   UPDATED_TIME BIGINT NOT NULL,
   PRIMARY KEY (ENTITY_ID),
-  FOREIGN KEY (DOMAIN_ID) REFERENCES DOMAIN(DOMAIN_ID) ON DELETE CASCADE ON 
UPDATE CASCADE,
-  FOREIGN KEY (ENTITY_TYPE_ID) REFERENCES ENTITY_TYPE(ENTITY_TYPE_ID) ON 
DELETE CASCADE ON UPDATE CASCADE,
-  FOREIGN KEY (OWNER_ID) REFERENCES USER(USER_ID) ON DELETE CASCADE ON UPDATE 
CASCADE,
-  FOREIGN KEY (PARENT_ENTITY_ID) REFERENCES ENTITY(ENTITY_ID) ON DELETE 
CASCADE ON UPDATE CASCADE
+  FOREIGN KEY (DOMAIN_ID) REFERENCES DOMAIN(DOMAIN_ID) ON DELETE CASCADE ON 
UPDATE NO ACTION,
+  FOREIGN KEY (ENTITY_TYPE_ID) REFERENCES ENTITY_TYPE(ENTITY_TYPE_ID) ON 
DELETE CASCADE ON UPDATE NO ACTION,
+  FOREIGN KEY (OWNER_ID) REFERENCES SHARING_USER(USER_ID) ON DELETE CASCADE ON 
UPDATE NO ACTION,
+  FOREIGN KEY (PARENT_ENTITY_ID) REFERENCES ENTITY(ENTITY_ID) ON DELETE 
CASCADE ON UPDATE NO ACTION
 );
 
-ALTER TABLE ENTITY ADD FULLTEXT FULL_TEXT_INDEX(FULL_TEXT);
+-- ALTER TABLE ENTITY ADD FULLTEXT FULL_TEXT_INDEX(FULL_TEXT);
 
 CREATE TABLE ENTITY_METADATA (
   ENTITY_ID VARCHAR (255) NOT NULL,
   META_KEY VARCHAR (255) NOT NULL,
   META_VALUE VARCHAR (255) NOT NULL,
   PRIMARY KEY (ENTITY_ID, META_KEY),
-  FOREIGN KEY (ENTITY_ID) REFERENCES ENTITY(ENTITY_ID) ON DELETE CASCADE ON 
UPDATE CASCADE
+  FOREIGN KEY (ENTITY_ID) REFERENCES ENTITY(ENTITY_ID) ON DELETE CASCADE ON 
UPDATE NO ACTION
 );
 
 CREATE TABLE SHARING (
@@ -123,9 +123,9 @@ CREATE TABLE SHARING (
   CREATED_TIME BIGINT NOT NULL,
   UPDATED_TIME BIGINT NOT NULL,
   PRIMARY KEY (PERMISSION_TYPE_ID, ENTITY_ID, GROUP_ID, INHERITED_PARENT_ID),
-  FOREIGN KEY (PERMISSION_TYPE_ID) REFERENCES 
PERMISSION_TYPE(PERMISSION_TYPE_ID) ON DELETE CASCADE ON UPDATE CASCADE,
-  FOREIGN KEY (INHERITED_PARENT_ID) REFERENCES ENTITY(ENTITY_ID) ON DELETE 
CASCADE ON UPDATE CASCADE,
-  FOREIGN KEY (GROUP_ID) REFERENCES USER_GROUP(GROUP_ID) ON DELETE CASCADE ON 
UPDATE CASCADE
+  FOREIGN KEY (PERMISSION_TYPE_ID) REFERENCES 
PERMISSION_TYPE(PERMISSION_TYPE_ID) ON DELETE CASCADE ON UPDATE NO ACTION,
+  FOREIGN KEY (INHERITED_PARENT_ID) REFERENCES ENTITY(ENTITY_ID) ON DELETE 
CASCADE ON UPDATE NO ACTION,
+  FOREIGN KEY (GROUP_ID) REFERENCES USER_GROUP(GROUP_ID) ON DELETE CASCADE ON 
UPDATE NO ACTION
 );
 
 CREATE TABLE CONFIGURATION

http://git-wip-us.apache.org/repos/asf/airavata/blob/146ccc77/modules/sharing-registry/sharing-registry-core/src/main/resources/sharing-registry-mysql.sql
----------------------------------------------------------------------
diff --git 
a/modules/sharing-registry/sharing-registry-core/src/main/resources/sharing-registry-mysql.sql
 
b/modules/sharing-registry/sharing-registry-core/src/main/resources/sharing-registry-mysql.sql
index a7bb905..ebb3ec0 100644
--- 
a/modules/sharing-registry/sharing-registry-core/src/main/resources/sharing-registry-mysql.sql
+++ 
b/modules/sharing-registry/sharing-registry-core/src/main/resources/sharing-registry-mysql.sql
@@ -28,7 +28,7 @@ CREATE TABLE DOMAIN (
   PRIMARY KEY (DOMAIN_ID)
 );
 
-CREATE TABLE USER (
+CREATE TABLE SHARING_USER (
   USER_ID VARCHAR(255) NOT NULL,
   DOMAIN_ID VARCHAR(255) NOT NULL,
   USER_NAME VARCHAR(255) NOT NULL,
@@ -49,7 +49,7 @@ CREATE TABLE USER_GROUP (
   UPDATED_TIME BIGINT NOT NULL,
   PRIMARY KEY (GROUP_ID),
   FOREIGN KEY (DOMAIN_ID) REFERENCES DOMAIN(DOMAIN_ID) ON DELETE CASCADE ON 
UPDATE CASCADE,
-  FOREIGN KEY (OWNER_ID) REFERENCES USER(USER_ID) ON DELETE CASCADE ON UPDATE 
CASCADE
+  FOREIGN KEY (OWNER_ID) REFERENCES SHARING_USER(USER_ID) ON DELETE CASCADE ON 
UPDATE CASCADE
 );
 
 
@@ -100,7 +100,7 @@ CREATE TABLE ENTITY (
   PRIMARY KEY (ENTITY_ID),
   FOREIGN KEY (DOMAIN_ID) REFERENCES DOMAIN(DOMAIN_ID) ON DELETE CASCADE ON 
UPDATE CASCADE,
   FOREIGN KEY (ENTITY_TYPE_ID) REFERENCES ENTITY_TYPE(ENTITY_TYPE_ID) ON 
DELETE CASCADE ON UPDATE CASCADE,
-  FOREIGN KEY (OWNER_ID) REFERENCES USER(USER_ID) ON DELETE CASCADE ON UPDATE 
CASCADE,
+  FOREIGN KEY (OWNER_ID) REFERENCES SHARING_USER(USER_ID) ON DELETE CASCADE ON 
UPDATE CASCADE,
   FOREIGN KEY (PARENT_ENTITY_ID) REFERENCES ENTITY(ENTITY_ID) ON DELETE 
CASCADE ON UPDATE CASCADE
 );
 

http://git-wip-us.apache.org/repos/asf/airavata/blob/146ccc77/modules/sharing-registry/sharing-registry-core/src/test/java/org/apache/airavata/sharing/registry/SharingRegistryServerHandlerTest.java
----------------------------------------------------------------------
diff --git 
a/modules/sharing-registry/sharing-registry-core/src/test/java/org/apache/airavata/sharing/registry/SharingRegistryServerHandlerTest.java
 
b/modules/sharing-registry/sharing-registry-core/src/test/java/org/apache/airavata/sharing/registry/SharingRegistryServerHandlerTest.java
index 0fd4e1d..b35bfef 100644
--- 
a/modules/sharing-registry/sharing-registry-core/src/test/java/org/apache/airavata/sharing/registry/SharingRegistryServerHandlerTest.java
+++ 
b/modules/sharing-registry/sharing-registry-core/src/test/java/org/apache/airavata/sharing/registry/SharingRegistryServerHandlerTest.java
@@ -24,11 +24,14 @@ import junit.framework.Assert;
 import org.apache.airavata.common.exception.ApplicationSettingsException;
 import org.apache.airavata.sharing.registry.models.*;
 import 
org.apache.airavata.sharing.registry.server.SharingRegistryServerHandler;
+import org.apache.airavata.sharing.registry.util.Initialize;
 import org.apache.thrift.TException;
+import org.junit.BeforeClass;
 import org.junit.Test;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import java.sql.SQLException;
 import java.util.Arrays;
 import java.util.HashMap;
 import java.util.Map;
@@ -36,6 +39,12 @@ import java.util.Map;
 public class SharingRegistryServerHandlerTest {
     private final static Logger logger = 
LoggerFactory.getLogger(SharingRegistryServerHandlerTest.class);
 
+    @BeforeClass
+    public static void setup() throws SharingRegistryException, SQLException {
+        Initialize initialize = new Initialize("sharing-registry-derby.sql");
+        initialize.initializeDB();
+    }
+
     @Test
     public void test() throws TException, ApplicationSettingsException {
         SharingRegistryServerHandler govRegistryServerHandler = new 
SharingRegistryServerHandler();

http://git-wip-us.apache.org/repos/asf/airavata/blob/146ccc77/modules/sharing-registry/sharing-registry-core/src/test/java/org/apache/airavata/sharing/registry/util/Initialize.java
----------------------------------------------------------------------
diff --git 
a/modules/sharing-registry/sharing-registry-core/src/test/java/org/apache/airavata/sharing/registry/util/Initialize.java
 
b/modules/sharing-registry/sharing-registry-core/src/test/java/org/apache/airavata/sharing/registry/util/Initialize.java
new file mode 100644
index 0000000..4a89094
--- /dev/null
+++ 
b/modules/sharing-registry/sharing-registry-core/src/test/java/org/apache/airavata/sharing/registry/util/Initialize.java
@@ -0,0 +1,298 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+
+package org.apache.airavata.sharing.registry.util;
+
+import org.apache.airavata.common.exception.ApplicationSettingsException;
+import org.apache.airavata.common.utils.ServerSettings;
+import org.apache.airavata.sharing.registry.db.utils.JPAUtils;
+import org.apache.derby.drda.NetworkServerControl;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.net.InetAddress;
+import java.net.URI;
+import java.sql.*;
+import java.util.StringTokenizer;
+
+public class Initialize {
+    private static final Logger logger = 
LoggerFactory.getLogger(Initialize.class);
+    public static final String DERBY_SERVER_MODE_SYS_PROPERTY = 
"derby.drda.startNetworkServer";
+    public  String scriptName ;
+    private NetworkServerControl server;
+    private static final String delimiter = ";";
+    public static final String PERSISTANT_DATA = "Configuration";
+
+    public Initialize(String scriptName) {
+        this.scriptName = scriptName;
+    }
+
+    public static boolean checkStringBufferEndsWith(StringBuffer buffer, 
String suffix) {
+        if (suffix.length() > buffer.length()) {
+            return false;
+        }
+        // this loop is done on purpose to avoid memory allocation performance
+        // problems on various JDKs
+        // StringBuffer.lastIndexOf() was introduced in jdk 1.4 and
+        // implementation is ok though does allocation/copying
+        // StringBuffer.toString().endsWith() does massive memory
+        // allocation/copying on JDK 1.5
+        // See http://issues.apache.org/bugzilla/show_bug.cgi?id=37169
+        int endIndex = suffix.length() - 1;
+        int bufferIndex = buffer.length() - 1;
+        while (endIndex >= 0) {
+            if (buffer.charAt(bufferIndex) != suffix.charAt(endIndex)) {
+                return false;
+            }
+            bufferIndex--;
+            endIndex--;
+        }
+        return true;
+    }
+
+    private static boolean isServerStarted(NetworkServerControl server, int 
ntries)
+    {
+        for (int i = 1; i <= ntries; i ++)
+        {
+            try {
+                Thread.sleep(500);
+                server.ping();
+                return true;
+            }
+            catch (Exception e) {
+                if (i == ntries)
+                    return false;
+            }
+        }
+        return false;
+    }
+
+    public void initializeDB() throws SQLException{
+        String jdbcUrl = null;
+        String jdbcUser = null;
+        String jdbcPassword = null;
+        try{
+            jdbcUrl = ServerSettings.getSetting("sharingcatalog.jdbc.url");
+            jdbcUser = ServerSettings.getSetting("sharingcatalog.jdbc.user");
+            jdbcPassword = 
ServerSettings.getSetting("sharingcatalog.jdbc.password");
+            jdbcUrl = jdbcUrl + "?" + "user=" + jdbcUser + "&" + "password=" + 
jdbcPassword;
+        } catch (ApplicationSettingsException e) {
+            logger.error("Unable to read properties", e);
+        }
+        startDerbyInServerMode();
+        if(!isServerStarted(server, 20)){
+           throw new RuntimeException("Derby server cound not started within 
five seconds...");
+        }
+
+        Connection conn = null;
+        try {
+            
Class.forName(JPAUtils.readServerProperties(JPAUtils.SHARING_REG_JDBC_DRIVER)).newInstance();
+            conn = DriverManager.getConnection(jdbcUrl, jdbcUser, 
jdbcPassword);
+            if (!isDatabaseStructureCreated(PERSISTANT_DATA, conn)) {
+                executeSQLScript(conn);
+                logger.info("New Database created for Registry");
+            } else {
+                logger.debug("Database already created for Registry!");
+            }
+        } catch (Exception e) {
+            logger.error(e.getMessage(), e);
+            throw new RuntimeException("Database failure", e);
+        } finally {
+            try {
+                if (conn != null){
+                    if (!conn.getAutoCommit()) {
+                        conn.commit();
+                    }
+                    conn.close();
+                }
+            } catch (SQLException e) {
+                logger.error(e.getMessage(), e);
+            }
+        }
+    }
+
+    public static boolean isDatabaseStructureCreated(String tableName, 
Connection conn) {
+        try {
+            System.out.println("Running a query to test the database tables 
existence.");
+            // check whether the tables are already created with a query
+            Statement statement = null;
+            try {
+                statement = conn.createStatement();
+                ResultSet rs = statement.executeQuery("select * from " + 
tableName);
+                if (rs != null) {
+                    rs.close();
+                }
+            } finally {
+                try {
+                    if (statement != null) {
+                        statement.close();
+                    }
+                } catch (SQLException e) {
+                    return false;
+                }
+            }
+        } catch (SQLException e) {
+            return false;
+        }
+
+        return true;
+    }
+
+    private void executeSQLScript(Connection conn) throws Exception {
+        StringBuffer sql = new StringBuffer();
+        BufferedReader reader = null;
+        try{
+
+        InputStream inputStream = 
this.getClass().getClassLoader().getResourceAsStream(scriptName);
+        reader = new BufferedReader(new InputStreamReader(inputStream));
+        String line;
+        while ((line = reader.readLine()) != null) {
+            line = line.trim();
+            if (line.startsWith("//")) {
+                continue;
+            }
+            if (line.startsWith("--")) {
+                continue;
+            }
+            StringTokenizer st = new StringTokenizer(line);
+            if (st.hasMoreTokens()) {
+                String token = st.nextToken();
+                if ("REM".equalsIgnoreCase(token)) {
+                    continue;
+                }
+            }
+            sql.append(" ").append(line);
+
+            // SQL defines "--" as a comment to EOL
+            // and in Oracle it may contain a hint
+            // so we cannot just remove it, instead we must end it
+            if (line.indexOf("--") >= 0) {
+                sql.append("\n");
+            }
+            if ((checkStringBufferEndsWith(sql, delimiter))) {
+                executeSQL(sql.substring(0, sql.length() - 
delimiter.length()), conn);
+                sql.replace(0, sql.length(), "");
+            }
+        }
+        // Catch any statements not followed by ;
+        if (sql.length() > 0) {
+            executeSQL(sql.toString(), conn);
+        }
+        }catch (IOException e){
+            logger.error("Error occurred while executing SQL script for 
creating Airavata database", e);
+            throw new Exception("Error occurred while executing SQL script for 
creating Airavata database", e);
+        }finally {
+            if (reader != null) {
+                reader.close();
+            }
+
+        }
+
+    }
+
+    private static void executeSQL(String sql, Connection conn) throws 
Exception {
+        // Check and ignore empty statements
+        if ("".equals(sql.trim())) {
+            return;
+        }
+
+        Statement statement = null;
+        try {
+            logger.debug("SQL : " + sql);
+
+            boolean ret;
+            int updateCount = 0, updateCountTotal = 0;
+            statement = conn.createStatement();
+            ret = statement.execute(sql);
+            updateCount = statement.getUpdateCount();
+            do {
+                if (!ret) {
+                    if (updateCount != -1) {
+                        updateCountTotal += updateCount;
+                    }
+                }
+                ret = statement.getMoreResults();
+                if (ret) {
+                    updateCount = statement.getUpdateCount();
+                }
+            } while (ret);
+
+            logger.debug(sql + " : " + updateCountTotal + " rows affected");
+
+            SQLWarning warning = conn.getWarnings();
+            while (warning != null) {
+                logger.warn(warning + " sql warning");
+                warning = warning.getNextWarning();
+            }
+            conn.clearWarnings();
+        } catch (SQLException e) {
+            if (e.getSQLState().equals("X0Y32")) {
+                // eliminating the table already exception for the derby
+                // database
+                logger.info("Table Already Exists", e);
+            } else {
+                throw new Exception("Error occurred while executing : " + sql, 
e);
+            }
+        } finally {
+            if (statement != null) {
+                try {
+                    statement.close();
+                } catch (SQLException e) {
+                    logger.error("Error occurred while closing result set.", 
e);
+                }
+            }
+        }
+    }
+
+    private void startDerbyInServerMode() {
+        try {
+            System.setProperty(DERBY_SERVER_MODE_SYS_PROPERTY, "true");
+            String jdbcURL = 
JPAUtils.readServerProperties(JPAUtils.SHARING_REG_JDBC_URL);
+            String cleanURI = jdbcURL.substring(5);
+            URI uri = URI.create(cleanURI);
+            server = new 
NetworkServerControl(InetAddress.getByName(uri.getHost()),
+                    20000,
+                    
JPAUtils.readServerProperties(JPAUtils.SHARING_REG_JDBC_USER), 
JPAUtils.readServerProperties(JPAUtils.SHARING_REG_JDBC_USER));
+            java.io.PrintWriter consoleWriter = new 
java.io.PrintWriter(System.out, true);
+            server.start(consoleWriter);
+        } catch (IOException e) {
+            logger.error("Unable to start Apache derby in the server mode! 
Check whether " +
+                    "specified port is available");
+        } catch (Exception e) {
+            logger.error("Unable to start Apache derby in the server mode! 
Check whether " +
+                    "specified port is available");
+        }
+
+    }
+
+    public void stopDerbyServer() throws SQLException{
+        try {
+            server.shutdown();
+        } catch (Exception e) {
+            logger.error(e.getMessage(), e);
+            throw new SQLException("Error while stopping derby server", e);
+        }
+    }
+}

Reply via email to