This is an automated email from the ASF dual-hosted git repository. yasith pushed a commit to branch service-layer in repository https://gitbox.apache.org/repos/asf/airavata.git
commit fcc9910e3b024a176696a697dca6f30e4b0272c8 Author: yasithdev <[email protected]> AuthorDate: Mon Nov 10 18:42:24 2025 -0500 update sharingregistryserverhandler --- .../airavata/service/SharingRegistryService.java | 918 +++++++++++++++++++++ .../server/SharingRegistryServerHandler.java | 842 ++++--------------- 2 files changed, 1094 insertions(+), 666 deletions(-) diff --git a/airavata-api/src/main/java/org/apache/airavata/service/SharingRegistryService.java b/airavata-api/src/main/java/org/apache/airavata/service/SharingRegistryService.java new file mode 100644 index 0000000000..ad9552f8e3 --- /dev/null +++ b/airavata-api/src/main/java/org/apache/airavata/service/SharingRegistryService.java @@ -0,0 +1,918 @@ +/** +* +* 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.service; + +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; +import java.util.*; +import org.apache.airavata.sharing.registry.db.entities.*; +import org.apache.airavata.sharing.registry.db.repositories.*; +import org.apache.airavata.sharing.registry.db.utils.DBConstants; +import org.apache.airavata.sharing.registry.models.*; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class SharingRegistryService { + private static final Logger logger = LoggerFactory.getLogger(SharingRegistryService.class); + + public static String OWNER_PERMISSION_NAME = "OWNER"; + + /** + * * Domain Operations + * * + */ + public String createDomain(Domain domain) throws SharingRegistryException, DuplicateEntryException { + if ((new DomainRepository()).get(domain.getDomainId()) != null) + throw new DuplicateEntryException("There exist domain with given domain id"); + + domain.setCreatedTime(System.currentTimeMillis()); + domain.setUpdatedTime(System.currentTimeMillis()); + (new DomainRepository()).create(domain); + + // create the global permission for the domain + PermissionType permissionType = new PermissionType(); + permissionType.setPermissionTypeId(domain.getDomainId() + ":" + OWNER_PERMISSION_NAME); + permissionType.setDomainId(domain.getDomainId()); + permissionType.setName(OWNER_PERMISSION_NAME); + permissionType.setDescription("GLOBAL permission to " + domain.getDomainId()); + permissionType.setCreatedTime(System.currentTimeMillis()); + permissionType.setUpdatedTime(System.currentTimeMillis()); + (new PermissionTypeRepository()).create(permissionType); + + return domain.getDomainId(); + } + + public boolean updateDomain(Domain domain) throws SharingRegistryException { + Domain oldDomain = (new DomainRepository()).get(domain.getDomainId()); + domain.setCreatedTime(oldDomain.getCreatedTime()); + domain.setUpdatedTime(System.currentTimeMillis()); + domain = getUpdatedObject(oldDomain, domain); + (new DomainRepository()).update(domain); + return true; + } + + public boolean isDomainExists(String domainId) throws SharingRegistryException { + return (new DomainRepository()).isExists(domainId); + } + + public boolean deleteDomain(String domainId) throws SharingRegistryException { + (new DomainRepository()).delete(domainId); + return true; + } + + public Domain getDomain(String domainId) throws SharingRegistryException { + return (new DomainRepository()).get(domainId); + } + + public List<Domain> getDomains(int offset, int limit) throws SharingRegistryException { + return (new DomainRepository()).select(new HashMap<>(), offset, limit); + } + + /** + * * User Operations + * * + */ + public String createUser(User user) throws SharingRegistryException, DuplicateEntryException { + UserPK userPK = new UserPK(); + userPK.setUserId(user.getUserId()); + userPK.setDomainId(user.getDomainId()); + if ((new UserRepository()).get(userPK) != null) + throw new DuplicateEntryException("There exist user with given user id"); + + user.setCreatedTime(System.currentTimeMillis()); + user.setUpdatedTime(System.currentTimeMillis()); + (new UserRepository()).create(user); + + UserGroup userGroup = new UserGroup(); + userGroup.setGroupId(user.getUserId()); + userGroup.setDomainId(user.getDomainId()); + userGroup.setName(user.getUserName()); + userGroup.setDescription("user " + user.getUserName() + " group"); + userGroup.setOwnerId(user.getUserId()); + userGroup.setGroupType(GroupType.USER_LEVEL_GROUP); + userGroup.setGroupCardinality(GroupCardinality.SINGLE_USER); + (new UserGroupRepository()).create(userGroup); + + Domain domain = new DomainRepository().get(user.getDomainId()); + if (domain.getInitialUserGroupId() != null) { + addUsersToGroup( + user.getDomainId(), + Collections.singletonList(user.getUserId()), + domain.getInitialUserGroupId()); + } + + return user.getUserId(); + } + + public boolean updatedUser(User user) throws SharingRegistryException { + UserPK userPK = new UserPK(); + userPK.setUserId(user.getUserId()); + userPK.setDomainId(user.getDomainId()); + User oldUser = (new UserRepository()).get(userPK); + user.setCreatedTime(oldUser.getCreatedTime()); + user.setUpdatedTime(System.currentTimeMillis()); + user = getUpdatedObject(oldUser, user); + (new UserRepository()).update(user); + + UserGroupPK userGroupPK = new UserGroupPK(); + userGroupPK.setGroupId(user.getUserId()); + userGroupPK.setDomainId(user.getDomainId()); + UserGroup userGroup = (new UserGroupRepository()).get(userGroupPK); + userGroup.setName(user.getUserName()); + userGroup.setDescription("user " + user.getUserName() + " group"); + updateGroup(userGroup); + return true; + } + + public boolean isUserExists(String domainId, String userId) throws SharingRegistryException { + UserPK userPK = new UserPK(); + userPK.setDomainId(domainId); + userPK.setUserId(userId); + return (new UserRepository()).isExists(userPK); + } + + public boolean deleteUser(String domainId, String userId) throws SharingRegistryException { + UserPK userPK = new UserPK(); + userPK.setUserId(userId); + userPK.setDomainId(domainId); + (new UserRepository()).delete(userPK); + + UserGroupPK userGroupPK = new UserGroupPK(); + userGroupPK.setGroupId(userId); + userGroupPK.setDomainId(domainId); + (new UserGroupRepository()).delete(userGroupPK); + return true; + } + + public User getUser(String domainId, String userId) throws SharingRegistryException { + UserPK userPK = new UserPK(); + userPK.setUserId(userId); + userPK.setDomainId(domainId); + return (new UserRepository()).get(userPK); + } + + public List<User> getUsers(String domain, int offset, int limit) throws SharingRegistryException { + HashMap<String, String> filters = new HashMap<>(); + filters.put(DBConstants.UserTable.DOMAIN_ID, domain); + return (new UserRepository()).select(filters, offset, limit); + } + + /** + * * Group Operations + * * + */ + public String createGroup(UserGroup group) throws SharingRegistryException { + UserGroupPK userGroupPK = new UserGroupPK(); + userGroupPK.setGroupId(group.getGroupId()); + userGroupPK.setDomainId(group.getDomainId()); + if ((new UserGroupRepository()).get(userGroupPK) != null) + throw new SharingRegistryException("There exist group with given group id"); + // Client created groups are always of type MULTI_USER + group.setGroupCardinality(GroupCardinality.MULTI_USER); + group.setCreatedTime(System.currentTimeMillis()); + group.setUpdatedTime(System.currentTimeMillis()); + // Add group admins once the group is created + group.unsetGroupAdmins(); + (new UserGroupRepository()).create(group); + + addUsersToGroup(group.getDomainId(), Arrays.asList(group.getOwnerId()), group.getGroupId()); + return group.getGroupId(); + } + + public boolean updateGroup(UserGroup group) throws SharingRegistryException { + group.setUpdatedTime(System.currentTimeMillis()); + UserGroupPK userGroupPK = new UserGroupPK(); + userGroupPK.setGroupId(group.getGroupId()); + userGroupPK.setDomainId(group.getDomainId()); + UserGroup oldGroup = (new UserGroupRepository()).get(userGroupPK); + group.setGroupCardinality(oldGroup.getGroupCardinality()); + group.setCreatedTime(oldGroup.getCreatedTime()); + group = getUpdatedObject(oldGroup, group); + + if (!group.getOwnerId().equals(oldGroup.getOwnerId())) + throw new SharingRegistryException("Group owner cannot be changed"); + + (new UserGroupRepository()).update(group); + return true; + } + + public boolean isGroupExists(String domainId, String groupId) throws SharingRegistryException { + UserGroupPK userGroupPK = new UserGroupPK(); + userGroupPK.setDomainId(domainId); + userGroupPK.setGroupId(groupId); + return (new UserGroupRepository()).isExists(userGroupPK); + } + + public boolean deleteGroup(String domainId, String groupId) throws SharingRegistryException { + UserGroupPK userGroupPK = new UserGroupPK(); + userGroupPK.setGroupId(groupId); + userGroupPK.setDomainId(domainId); + (new UserGroupRepository()).delete(userGroupPK); + return true; + } + + public UserGroup getGroup(String domainId, String groupId) throws SharingRegistryException { + UserGroupPK userGroupPK = new UserGroupPK(); + userGroupPK.setGroupId(groupId); + userGroupPK.setDomainId(domainId); + return (new UserGroupRepository()).get(userGroupPK); + } + + public List<UserGroup> getGroups(String domain, int offset, int limit) throws SharingRegistryException { + HashMap<String, String> filters = new HashMap<>(); + filters.put(DBConstants.UserGroupTable.DOMAIN_ID, domain); + // Only return groups with MULTI_USER cardinality which is the only type of cardinality allowed for client + // created groups + filters.put(DBConstants.UserGroupTable.GROUP_CARDINALITY, GroupCardinality.MULTI_USER.name()); + return (new UserGroupRepository()).select(filters, offset, limit); + } + + public boolean addUsersToGroup(String domainId, List<String> userIds, String groupId) + throws SharingRegistryException { + for (int i = 0; i < userIds.size(); i++) { + GroupMembership groupMembership = new GroupMembership(); + groupMembership.setParentId(groupId); + groupMembership.setChildId(userIds.get(i)); + groupMembership.setChildType(GroupChildType.USER); + groupMembership.setDomainId(domainId); + groupMembership.setCreatedTime(System.currentTimeMillis()); + groupMembership.setUpdatedTime(System.currentTimeMillis()); + (new GroupMembershipRepository()).create(groupMembership); + } + return true; + } + + public boolean removeUsersFromGroup(String domainId, List<String> userIds, String groupId) + throws SharingRegistryException { + for (String userId : userIds) { + if (hasOwnerAccess(domainId, groupId, userId)) { + throw new SharingRegistryException( + "List of User Ids contains Owner Id. Cannot remove owner from the group"); + } + } + + for (int i = 0; i < userIds.size(); i++) { + GroupMembershipPK groupMembershipPK = new GroupMembershipPK(); + groupMembershipPK.setParentId(groupId); + groupMembershipPK.setChildId(userIds.get(i)); + groupMembershipPK.setDomainId(domainId); + (new GroupMembershipRepository()).delete(groupMembershipPK); + } + return true; + } + + public boolean transferGroupOwnership(String domainId, String groupId, String newOwnerId) + throws SharingRegistryException, DuplicateEntryException { + List<User> groupUser = getGroupMembersOfTypeUser(domainId, groupId, 0, -1); + if (!isUserBelongsToGroup(groupUser, newOwnerId)) { + throw new SharingRegistryException("New group owner is not part of the group"); + } + + if (hasOwnerAccess(domainId, groupId, newOwnerId)) { + throw new DuplicateEntryException("User already the current owner of the group"); + } + // remove the new owner as Admin if present + if (hasAdminAccess(domainId, groupId, newOwnerId)) { + removeGroupAdmins(domainId, groupId, Arrays.asList(newOwnerId)); + } + + UserGroupPK userGroupPK = new UserGroupPK(); + userGroupPK.setGroupId(groupId); + userGroupPK.setDomainId(domainId); + UserGroup userGroup = (new UserGroupRepository()).get(userGroupPK); + UserGroup newUserGroup = new UserGroup(); + newUserGroup.setUpdatedTime(System.currentTimeMillis()); + newUserGroup.setOwnerId(newOwnerId); + newUserGroup.setGroupCardinality(GroupCardinality.MULTI_USER); + newUserGroup.setCreatedTime(userGroup.getCreatedTime()); + newUserGroup = getUpdatedObject(userGroup, newUserGroup); + + (new UserGroupRepository()).update(newUserGroup); + + return true; + } + + private boolean isUserBelongsToGroup(List<User> groupUser, String newOwnerId) { + for (User user : groupUser) { + if (user.getUserId().equals(newOwnerId)) { + return true; + } + } + return false; + } + + public boolean addGroupAdmins(String domainId, String groupId, List<String> adminIds) + throws SharingRegistryException, DuplicateEntryException { + List<User> groupUser = getGroupMembersOfTypeUser(domainId, groupId, 0, -1); + + for (String adminId : adminIds) { + if (!isUserBelongsToGroup(groupUser, adminId)) { + throw new SharingRegistryException( + "Admin not the user of the group. GroupId : " + groupId + ", AdminId : " + adminId); + } + GroupAdminPK groupAdminPK = new GroupAdminPK(); + groupAdminPK.setGroupId(groupId); + groupAdminPK.setAdminId(adminId); + groupAdminPK.setDomainId(domainId); + + if ((new GroupAdminRepository()).get(groupAdminPK) != null) + throw new DuplicateEntryException("User already an admin for the group"); + + GroupAdmin admin = new GroupAdmin(); + admin.setAdminId(adminId); + admin.setDomainId(domainId); + admin.setGroupId(groupId); + (new GroupAdminRepository()).create(admin); + } + return true; + } + + public boolean removeGroupAdmins(String domainId, String groupId, List<String> adminIds) + throws SharingRegistryException { + for (String adminId : adminIds) { + GroupAdminPK groupAdminPK = new GroupAdminPK(); + groupAdminPK.setAdminId(adminId); + groupAdminPK.setDomainId(domainId); + groupAdminPK.setGroupId(groupId); + (new GroupAdminRepository()).delete(groupAdminPK); + } + return true; + } + + public boolean hasAdminAccess(String domainId, String groupId, String adminId) + throws SharingRegistryException { + GroupAdminPK groupAdminPK = new GroupAdminPK(); + groupAdminPK.setGroupId(groupId); + groupAdminPK.setAdminId(adminId); + groupAdminPK.setDomainId(domainId); + + if ((new GroupAdminRepository()).get(groupAdminPK) != null) return true; + return false; + } + + public boolean hasOwnerAccess(String domainId, String groupId, String ownerId) + throws SharingRegistryException { + UserGroupPK userGroupPK = new UserGroupPK(); + userGroupPK.setGroupId(groupId); + userGroupPK.setDomainId(domainId); + UserGroup getGroup = (new UserGroupRepository()).get(userGroupPK); + + if (getGroup.getOwnerId().equals(ownerId)) return true; + return false; + } + + public List<User> getGroupMembersOfTypeUser(String domainId, String groupId, int offset, int limit) + throws SharingRegistryException { + // TODO limit offset + List<User> groupMemberUsers = (new GroupMembershipRepository()).getAllChildUsers(domainId, groupId); + return groupMemberUsers; + } + + public List<UserGroup> getGroupMembersOfTypeGroup(String domainId, String groupId, int offset, int limit) + throws SharingRegistryException { + // TODO limit offset + List<UserGroup> groupMemberGroups = (new GroupMembershipRepository()).getAllChildGroups(domainId, groupId); + return groupMemberGroups; + } + + public boolean addChildGroupsToParentGroup(String domainId, List<String> childIds, String groupId) + throws SharingRegistryException { + for (String childId : childIds) { + // Todo check for cyclic dependencies + GroupMembership groupMembership = new GroupMembership(); + groupMembership.setParentId(groupId); + groupMembership.setChildId(childId); + groupMembership.setChildType(GroupChildType.GROUP); + groupMembership.setDomainId(domainId); + groupMembership.setCreatedTime(System.currentTimeMillis()); + groupMembership.setUpdatedTime(System.currentTimeMillis()); + (new GroupMembershipRepository()).create(groupMembership); + } + return true; + } + + public boolean removeChildGroupFromParentGroup(String domainId, String childId, String groupId) + throws SharingRegistryException { + GroupMembershipPK groupMembershipPK = new GroupMembershipPK(); + groupMembershipPK.setParentId(groupId); + groupMembershipPK.setChildId(childId); + groupMembershipPK.setDomainId(domainId); + (new GroupMembershipRepository()).delete(groupMembershipPK); + return true; + } + + public List<UserGroup> getAllMemberGroupsForUser(String domainId, String userId) + throws SharingRegistryException { + GroupMembershipRepository groupMembershipRepository = new GroupMembershipRepository(); + return groupMembershipRepository.getAllMemberGroupsForUser(domainId, userId); + } + + /** + * * EntityType Operations + * * + */ + public String createEntityType(EntityType entityType) + throws SharingRegistryException, DuplicateEntryException { + EntityTypePK entityTypePK = new EntityTypePK(); + entityTypePK.setDomainId(entityType.getDomainId()); + entityTypePK.setEntityTypeId(entityType.getEntityTypeId()); + if ((new EntityTypeRepository()).get(entityTypePK) != null) + throw new DuplicateEntryException("There exist EntityType with given EntityType id"); + + entityType.setCreatedTime(System.currentTimeMillis()); + entityType.setUpdatedTime(System.currentTimeMillis()); + (new EntityTypeRepository()).create(entityType); + return entityType.getEntityTypeId(); + } + + public boolean updateEntityType(EntityType entityType) throws SharingRegistryException { + entityType.setUpdatedTime(System.currentTimeMillis()); + EntityTypePK entityTypePK = new EntityTypePK(); + entityTypePK.setDomainId(entityType.getDomainId()); + entityTypePK.setEntityTypeId(entityType.getEntityTypeId()); + EntityType oldEntityType = (new EntityTypeRepository()).get(entityTypePK); + entityType.setCreatedTime(oldEntityType.getCreatedTime()); + entityType = getUpdatedObject(oldEntityType, entityType); + (new EntityTypeRepository()).update(entityType); + return true; + } + + public boolean isEntityTypeExists(String domainId, String entityTypeId) + throws SharingRegistryException { + EntityTypePK entityTypePK = new EntityTypePK(); + entityTypePK.setDomainId(domainId); + entityTypePK.setEntityTypeId(entityTypeId); + return (new EntityTypeRepository()).isExists(entityTypePK); + } + + public boolean deleteEntityType(String domainId, String entityTypeId) throws SharingRegistryException { + EntityTypePK entityTypePK = new EntityTypePK(); + entityTypePK.setDomainId(domainId); + entityTypePK.setEntityTypeId(entityTypeId); + (new EntityTypeRepository()).delete(entityTypePK); + return true; + } + + public EntityType getEntityType(String domainId, String entityTypeId) throws SharingRegistryException { + EntityTypePK entityTypePK = new EntityTypePK(); + entityTypePK.setDomainId(domainId); + entityTypePK.setEntityTypeId(entityTypeId); + return (new EntityTypeRepository()).get(entityTypePK); + } + + public List<EntityType> getEntityTypes(String domain, int offset, int limit) throws SharingRegistryException { + HashMap<String, String> filters = new HashMap<>(); + filters.put(DBConstants.EntityTypeTable.DOMAIN_ID, domain); + return (new EntityTypeRepository()).select(filters, offset, limit); + } + + /** + * * Permission Operations + * * + */ + public String createPermissionType(PermissionType permissionType) + throws SharingRegistryException, DuplicateEntryException { + PermissionTypePK permissionTypePK = new PermissionTypePK(); + permissionTypePK.setDomainId(permissionType.getDomainId()); + permissionTypePK.setPermissionTypeId(permissionType.getPermissionTypeId()); + if ((new PermissionTypeRepository()).get(permissionTypePK) != null) + throw new DuplicateEntryException("There exist PermissionType with given PermissionType id"); + permissionType.setCreatedTime(System.currentTimeMillis()); + permissionType.setUpdatedTime(System.currentTimeMillis()); + (new PermissionTypeRepository()).create(permissionType); + return permissionType.getPermissionTypeId(); + } + + public boolean updatePermissionType(PermissionType permissionType) throws SharingRegistryException { + permissionType.setUpdatedTime(System.currentTimeMillis()); + PermissionTypePK permissionTypePK = new PermissionTypePK(); + permissionTypePK.setDomainId(permissionType.getDomainId()); + permissionTypePK.setPermissionTypeId(permissionType.getPermissionTypeId()); + PermissionType oldPermissionType = (new PermissionTypeRepository()).get(permissionTypePK); + permissionType = getUpdatedObject(oldPermissionType, permissionType); + (new PermissionTypeRepository()).update(permissionType); + return true; + } + + public boolean isPermissionExists(String domainId, String permissionId) + throws SharingRegistryException { + PermissionTypePK permissionTypePK = new PermissionTypePK(); + permissionTypePK.setDomainId(domainId); + permissionTypePK.setPermissionTypeId(permissionId); + return (new PermissionTypeRepository()).isExists(permissionTypePK); + } + + public boolean deletePermissionType(String domainId, String permissionTypeId) + throws SharingRegistryException { + PermissionTypePK permissionTypePK = new PermissionTypePK(); + permissionTypePK.setDomainId(domainId); + permissionTypePK.setPermissionTypeId(permissionTypeId); + (new PermissionTypeRepository()).delete(permissionTypePK); + return true; + } + + public PermissionType getPermissionType(String domainId, String permissionTypeId) + throws SharingRegistryException { + PermissionTypePK permissionTypePK = new PermissionTypePK(); + permissionTypePK.setDomainId(domainId); + permissionTypePK.setPermissionTypeId(permissionTypeId); + return (new PermissionTypeRepository()).get(permissionTypePK); + } + + public List<PermissionType> getPermissionTypes(String domain, int offset, int limit) + throws SharingRegistryException { + HashMap<String, String> filters = new HashMap<>(); + filters.put(DBConstants.PermissionTypeTable.DOMAIN_ID, domain); + return (new PermissionTypeRepository()).select(filters, offset, limit); + } + + /** + * * Entity Operations + * * + */ + public String createEntity(Entity entity) throws SharingRegistryException, DuplicateEntryException { + EntityPK entityPK = new EntityPK(); + entityPK.setDomainId(entity.getDomainId()); + entityPK.setEntityId(entity.getEntityId()); + if ((new EntityRepository()).get(entityPK) != null) + throw new DuplicateEntryException("There exist Entity with given Entity id"); + + UserPK userPK = new UserPK(); + userPK.setDomainId(entity.getDomainId()); + userPK.setUserId(entity.getOwnerId()); + if (!(new UserRepository()).isExists(userPK)) { + // Todo this is for Airavata easy integration. Proper thing is to throw an exception here + User user = new User(); + user.setUserId(entity.getOwnerId()); + user.setDomainId(entity.getDomainId()); + user.setUserName(user.getUserId().split("@")[0]); + + createUser(user); + } + entity.setCreatedTime(System.currentTimeMillis()); + entity.setUpdatedTime(System.currentTimeMillis()); + + if (entity.getOriginalEntityCreationTime() == 0) { + entity.setOriginalEntityCreationTime(entity.getCreatedTime()); + } + (new EntityRepository()).create(entity); + + // Assigning global permission for the owner + Sharing newSharing = new Sharing(); + newSharing.setPermissionTypeId( + (new PermissionTypeRepository()).getOwnerPermissionTypeIdForDomain(entity.getDomainId())); + newSharing.setEntityId(entity.getEntityId()); + newSharing.setGroupId(entity.getOwnerId()); + newSharing.setSharingType(SharingType.DIRECT_CASCADING); + newSharing.setInheritedParentId(entity.getEntityId()); + newSharing.setDomainId(entity.getDomainId()); + newSharing.setCreatedTime(System.currentTimeMillis()); + newSharing.setUpdatedTime(System.currentTimeMillis()); + + (new SharingRepository()).create(newSharing); + + // creating records for inherited permissions + if (entity.getParentEntityId() != null && entity.getParentEntityId() != "") { + addCascadingPermissionsForEntity(entity); + } + + return entity.getEntityId(); + } + + private void addCascadingPermissionsForEntity(Entity entity) throws SharingRegistryException { + Sharing newSharing; + List<Sharing> sharings = (new SharingRepository()) + .getCascadingPermissionsForEntity(entity.getDomainId(), entity.getParentEntityId()); + for (Sharing sharing : sharings) { + newSharing = new Sharing(); + newSharing.setPermissionTypeId(sharing.getPermissionTypeId()); + newSharing.setEntityId(entity.getEntityId()); + newSharing.setGroupId(sharing.getGroupId()); + newSharing.setInheritedParentId(sharing.getInheritedParentId()); + newSharing.setSharingType(SharingType.INDIRECT_CASCADING); + newSharing.setDomainId(entity.getDomainId()); + newSharing.setCreatedTime(System.currentTimeMillis()); + newSharing.setUpdatedTime(System.currentTimeMillis()); + + (new SharingRepository()).create(newSharing); + } + } + + public boolean updateEntity(Entity entity) throws SharingRegistryException { + // TODO Check for permission changes + entity.setUpdatedTime(System.currentTimeMillis()); + EntityPK entityPK = new EntityPK(); + entityPK.setDomainId(entity.getDomainId()); + entityPK.setEntityId(entity.getEntityId()); + Entity oldEntity = (new EntityRepository()).get(entityPK); + entity.setCreatedTime(oldEntity.getCreatedTime()); + // check if parent entity changed and re-add inherited permissions + if (!Objects.equals(oldEntity.getParentEntityId(), entity.getParentEntityId())) { + logger.debug("Parent entity changed for {}, updating inherited permissions", entity.getEntityId()); + if (oldEntity.getParentEntityId() != null && oldEntity.getParentEntityId() != "") { + logger.debug( + "Removing inherited permissions from {} that were inherited from parent {}", + entity.getEntityId(), + oldEntity.getParentEntityId()); + (new SharingRepository()) + .removeAllIndirectCascadingPermissionsForEntity(entity.getDomainId(), entity.getEntityId()); + } + if (entity.getParentEntityId() != null && entity.getParentEntityId() != "") { + // re-add INDIRECT_CASCADING permissions + logger.debug( + "Adding inherited permissions to {} that are inherited from parent {}", + entity.getEntityId(), + entity.getParentEntityId()); + addCascadingPermissionsForEntity(entity); + } + } + entity = getUpdatedObject(oldEntity, entity); + entity.setSharedCount((new SharingRepository()).getSharedCount(entity.getDomainId(), entity.getEntityId())); + (new EntityRepository()).update(entity); + return true; + } + + public boolean isEntityExists(String domainId, String entityId) throws SharingRegistryException { + EntityPK entityPK = new EntityPK(); + entityPK.setDomainId(domainId); + entityPK.setEntityId(entityId); + return (new EntityRepository()).isExists(entityPK); + } + + public boolean deleteEntity(String domainId, String entityId) throws SharingRegistryException { + // TODO Check for permission changes + EntityPK entityPK = new EntityPK(); + entityPK.setDomainId(domainId); + entityPK.setEntityId(entityId); + (new EntityRepository()).delete(entityPK); + return true; + } + + public Entity getEntity(String domainId, String entityId) throws SharingRegistryException { + EntityPK entityPK = new EntityPK(); + entityPK.setDomainId(domainId); + entityPK.setEntityId(entityId); + return (new EntityRepository()).get(entityPK); + } + + public List<Entity> searchEntities( + String domainId, String userId, List<SearchCriteria> filters, int offset, int limit) + throws SharingRegistryException { + List<String> groupIds = new ArrayList<>(); + groupIds.add(userId); + (new GroupMembershipRepository()) + .getAllParentMembershipsForChild(domainId, userId).stream() + .forEach(gm -> groupIds.add(gm.getParentId())); + return (new EntityRepository()).searchEntities(domainId, groupIds, filters, offset, limit); + } + + public List<User> getListOfSharedUsers(String domainId, String entityId, String permissionTypeId) + throws SharingRegistryException { + return (new UserRepository()).getAccessibleUsers(domainId, entityId, permissionTypeId); + } + + public List<User> getListOfDirectlySharedUsers(String domainId, String entityId, String permissionTypeId) + throws SharingRegistryException { + return (new UserRepository()).getDirectlyAccessibleUsers(domainId, entityId, permissionTypeId); + } + + public List<UserGroup> getListOfSharedGroups(String domainId, String entityId, String permissionTypeId) + throws SharingRegistryException { + return (new UserGroupRepository()).getAccessibleGroups(domainId, entityId, permissionTypeId); + } + + public List<UserGroup> getListOfDirectlySharedGroups(String domainId, String entityId, String permissionTypeId) + throws SharingRegistryException { + return (new UserGroupRepository()).getDirectlyAccessibleGroups(domainId, entityId, permissionTypeId); + } + + public boolean shareEntityWithUsers( + String domainId, String entityId, List<String> userList, String permissionTypeId, boolean cascadePermission) + throws SharingRegistryException { + return shareEntity(domainId, entityId, userList, permissionTypeId, cascadePermission); + } + + public boolean shareEntityWithGroups( + String domainId, + String entityId, + List<String> groupList, + String permissionTypeId, + boolean cascadePermission) + throws SharingRegistryException { + return shareEntity(domainId, entityId, groupList, permissionTypeId, cascadePermission); + } + + private boolean shareEntity( + String domainId, + String entityId, + List<String> groupOrUserList, + String permissionTypeId, + boolean cascadePermission) + throws SharingRegistryException { + if (permissionTypeId.equals((new PermissionTypeRepository()).getOwnerPermissionTypeIdForDomain(domainId))) { + throw new SharingRegistryException(OWNER_PERMISSION_NAME + " permission cannot be assigned or removed"); + } + + List<Sharing> sharings = new ArrayList<>(); + + // Adding permission for the specified users/groups for the specified entity + LinkedList<Entity> temp = new LinkedList<>(); + for (String userId : groupOrUserList) { + Sharing sharing = new Sharing(); + sharing.setPermissionTypeId(permissionTypeId); + sharing.setEntityId(entityId); + sharing.setGroupId(userId); + sharing.setInheritedParentId(entityId); + sharing.setDomainId(domainId); + if (cascadePermission) { + sharing.setSharingType(SharingType.DIRECT_CASCADING); + } else { + sharing.setSharingType(SharingType.DIRECT_NON_CASCADING); + } + sharing.setCreatedTime(System.currentTimeMillis()); + sharing.setUpdatedTime(System.currentTimeMillis()); + + sharings.add(sharing); + } + + if (cascadePermission) { + // Adding permission for the specified users/groups for all child entities + (new EntityRepository()) + .getChildEntities(domainId, entityId).stream().forEach(e -> temp.addLast(e)); + while (temp.size() > 0) { + Entity entity = temp.pop(); + String childEntityId = entity.getEntityId(); + for (String userId : groupOrUserList) { + Sharing sharing = new Sharing(); + sharing.setPermissionTypeId(permissionTypeId); + sharing.setEntityId(childEntityId); + sharing.setGroupId(userId); + sharing.setInheritedParentId(entityId); + sharing.setSharingType(SharingType.INDIRECT_CASCADING); + sharing.setInheritedParentId(entityId); + sharing.setDomainId(domainId); + sharing.setCreatedTime(System.currentTimeMillis()); + sharing.setUpdatedTime(System.currentTimeMillis()); + sharings.add(sharing); + (new EntityRepository()) + .getChildEntities(domainId, childEntityId).stream() + .forEach(e -> temp.addLast(e)); + } + } + } + (new SharingRepository()).create(sharings); + + EntityPK entityPK = new EntityPK(); + entityPK.setDomainId(domainId); + entityPK.setEntityId(entityId); + Entity entity = (new EntityRepository()).get(entityPK); + entity.setSharedCount((new SharingRepository()).getSharedCount(domainId, entityId)); + (new EntityRepository()).update(entity); + return true; + } + + public boolean revokeEntitySharingFromUsers( + String domainId, String entityId, List<String> userList, String permissionTypeId) + throws SharingRegistryException { + if (permissionTypeId.equals((new PermissionTypeRepository()).getOwnerPermissionTypeIdForDomain(domainId))) { + throw new SharingRegistryException(OWNER_PERMISSION_NAME + " permission cannot be assigned or removed"); + } + return revokeEntitySharing(domainId, entityId, userList, permissionTypeId); + } + + public boolean revokeEntitySharingFromGroups( + String domainId, String entityId, List<String> groupList, String permissionTypeId) + throws SharingRegistryException { + if (permissionTypeId.equals((new PermissionTypeRepository()).getOwnerPermissionTypeIdForDomain(domainId))) { + throw new SharingRegistryException(OWNER_PERMISSION_NAME + " permission cannot be assigned or removed"); + } + return revokeEntitySharing(domainId, entityId, groupList, permissionTypeId); + } + + public boolean userHasAccess(String domainId, String userId, String entityId, String permissionTypeId) + throws SharingRegistryException { + // check whether the user has permission directly or indirectly + List<GroupMembership> parentMemberships = + (new GroupMembershipRepository()).getAllParentMembershipsForChild(domainId, userId); + List<String> groupIds = new ArrayList<>(); + parentMemberships.stream().forEach(pm -> groupIds.add(pm.getParentId())); + groupIds.add(userId); + return (new SharingRepository()) + .hasAccess( + domainId, + entityId, + groupIds, + Arrays.asList( + permissionTypeId, + (new PermissionTypeRepository()).getOwnerPermissionTypeIdForDomain(domainId))); + } + + public boolean revokeEntitySharing( + String domainId, String entityId, List<String> groupOrUserList, String permissionTypeId) + throws SharingRegistryException { + if (permissionTypeId.equals((new PermissionTypeRepository()).getOwnerPermissionTypeIdForDomain(domainId))) { + throw new SharingRegistryException(OWNER_PERMISSION_NAME + " permission cannot be removed"); + } + + // revoking permission for the entity + for (String groupId : groupOrUserList) { + SharingPK sharingPK = new SharingPK(); + sharingPK.setEntityId(entityId); + sharingPK.setGroupId(groupId); + sharingPK.setPermissionTypeId(permissionTypeId); + sharingPK.setInheritedParentId(entityId); + sharingPK.setDomainId(domainId); + + (new SharingRepository()).delete(sharingPK); + } + + // revoking permission from inheritance + List<Sharing> temp = new ArrayList<>(); + (new SharingRepository()) + .getIndirectSharedChildren(domainId, entityId, permissionTypeId).stream() + .forEach(s -> temp.add(s)); + for (Sharing sharing : temp) { + String childEntityId = sharing.getEntityId(); + for (String groupId : groupOrUserList) { + SharingPK sharingPK = new SharingPK(); + sharingPK.setEntityId(childEntityId); + sharingPK.setGroupId(groupId); + sharingPK.setPermissionTypeId(permissionTypeId); + sharingPK.setInheritedParentId(entityId); + sharingPK.setDomainId(domainId); + + (new SharingRepository()).delete(sharingPK); + } + } + + EntityPK entityPK = new EntityPK(); + entityPK.setDomainId(domainId); + entityPK.setEntityId(entityId); + Entity entity = (new EntityRepository()).get(entityPK); + entity.setSharedCount((new SharingRepository()).getSharedCount(domainId, entityId)); + (new EntityRepository()).update(entity); + return true; + } + + private <T> T getUpdatedObject(T oldEntity, T newEntity) throws SharingRegistryException { + Field[] newEntityFields = newEntity.getClass().getDeclaredFields(); + Hashtable newHT = fieldsToHT(newEntityFields, newEntity); + + Class oldEntityClass = oldEntity.getClass(); + Field[] oldEntityFields = oldEntityClass.getDeclaredFields(); + + for (Field field : oldEntityFields) { + if (!Modifier.isFinal(field.getModifiers())) { + field.setAccessible(true); + Object o = newHT.get(field.getName()); + if (o != null) { + Field f = null; + try { + f = oldEntityClass.getDeclaredField(field.getName()); + f.setAccessible(true); + logger.debug("setting " + f.getName()); + f.set(oldEntity, o); + } catch (Exception e) { + throw new SharingRegistryException(e.getMessage()); + } + } + } + } + return oldEntity; + } + + private static Hashtable<String, Object> fieldsToHT(Field[] fields, Object obj) { + Hashtable<String, Object> hashtable = new Hashtable<>(); + for (Field field : fields) { + field.setAccessible(true); + try { + Object retrievedObject = field.get(obj); + if (retrievedObject != null) { + logger.debug("scanning " + field.getName()); + hashtable.put(field.getName(), field.get(obj)); + } + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + } + return hashtable; + } +} + diff --git a/airavata-api/src/main/java/org/apache/airavata/sharing/registry/server/SharingRegistryServerHandler.java b/airavata-api/src/main/java/org/apache/airavata/sharing/registry/server/SharingRegistryServerHandler.java index c171359b7f..e7c244fe29 100644 --- a/airavata-api/src/main/java/org/apache/airavata/sharing/registry/server/SharingRegistryServerHandler.java +++ b/airavata-api/src/main/java/org/apache/airavata/sharing/registry/server/SharingRegistryServerHandler.java @@ -19,14 +19,9 @@ */ package org.apache.airavata.sharing.registry.server; -import java.lang.reflect.Field; -import java.lang.reflect.Modifier; -import java.util.*; +import java.util.List; import org.apache.airavata.common.exception.ApplicationSettingsException; import org.apache.airavata.common.utils.DBInitializer; -import org.apache.airavata.sharing.registry.db.entities.*; -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.SharingRegistryDBInitConfig; import org.apache.airavata.sharing.registry.models.*; import org.apache.airavata.sharing.registry.service.cpi.SharingRegistryService; @@ -38,8 +33,9 @@ import org.slf4j.LoggerFactory; public class SharingRegistryServerHandler implements SharingRegistryService.Iface { private static final Logger logger = LoggerFactory.getLogger(SharingRegistryServerHandler.class); - - public static String OWNER_PERMISSION_NAME = "OWNER"; + private org.apache.airavata.service.SharingRegistryService sharingRegistryService; + + public static String OWNER_PERMISSION_NAME = org.apache.airavata.service.SharingRegistryService.OWNER_PERMISSION_NAME; public SharingRegistryServerHandler() throws ApplicationSettingsException, TException { this(new SharingRegistryDBInitConfig()); @@ -48,6 +44,7 @@ public class SharingRegistryServerHandler implements SharingRegistryService.Ifac public SharingRegistryServerHandler(SharingRegistryDBInitConfig sharingRegistryDBInitConfig) throws ApplicationSettingsException, TException { DBInitializer.initializeDB(sharingRegistryDBInitConfig); + sharingRegistryService = new org.apache.airavata.service.SharingRegistryService(); } @Override @@ -62,24 +59,9 @@ public class SharingRegistryServerHandler implements SharingRegistryService.Ifac @Override public String createDomain(Domain domain) throws SharingRegistryException, DuplicateEntryException, TException { try { - if ((new DomainRepository()).get(domain.getDomainId()) != null) - throw new DuplicateEntryException("There exist domain with given domain id"); - - domain.setCreatedTime(System.currentTimeMillis()); - domain.setUpdatedTime(System.currentTimeMillis()); - (new DomainRepository()).create(domain); - - // create the global permission for the domain - PermissionType permissionType = new PermissionType(); - permissionType.setPermissionTypeId(domain.getDomainId() + ":" + OWNER_PERMISSION_NAME); - permissionType.setDomainId(domain.getDomainId()); - permissionType.setName(OWNER_PERMISSION_NAME); - permissionType.setDescription("GLOBAL permission to " + domain.getDomainId()); - permissionType.setCreatedTime(System.currentTimeMillis()); - permissionType.setUpdatedTime(System.currentTimeMillis()); - (new PermissionTypeRepository()).create(permissionType); - - return domain.getDomainId(); + return sharingRegistryService.createDomain(domain); + } catch (SharingRegistryException | DuplicateEntryException e) { + throw e; } catch (Throwable ex) { logger.error(ex.getMessage(), ex); throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex)); @@ -89,12 +71,9 @@ public class SharingRegistryServerHandler implements SharingRegistryService.Ifac @Override public boolean updateDomain(Domain domain) throws SharingRegistryException, TException { try { - Domain oldDomain = (new DomainRepository()).get(domain.getDomainId()); - domain.setCreatedTime(oldDomain.getCreatedTime()); - domain.setUpdatedTime(System.currentTimeMillis()); - domain = getUpdatedObject(oldDomain, domain); - (new DomainRepository()).update(domain); - return true; + return sharingRegistryService.updateDomain(domain); + } catch (SharingRegistryException e) { + throw e; } catch (Throwable ex) { logger.error(ex.getMessage(), ex); throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex)); @@ -109,7 +88,9 @@ public class SharingRegistryServerHandler implements SharingRegistryService.Ifac @Override public boolean isDomainExists(String domainId) throws SharingRegistryException, TException { try { - return (new DomainRepository()).isExists(domainId); + return sharingRegistryService.isDomainExists(domainId); + } catch (SharingRegistryException e) { + throw e; } catch (Throwable ex) { logger.error(ex.getMessage(), ex); throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex)); @@ -119,8 +100,9 @@ public class SharingRegistryServerHandler implements SharingRegistryService.Ifac @Override public boolean deleteDomain(String domainId) throws SharingRegistryException, TException { try { - (new DomainRepository()).delete(domainId); - return true; + return sharingRegistryService.deleteDomain(domainId); + } catch (SharingRegistryException e) { + throw e; } catch (Throwable ex) { logger.error(ex.getMessage(), ex); throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex)); @@ -130,7 +112,9 @@ public class SharingRegistryServerHandler implements SharingRegistryService.Ifac @Override public Domain getDomain(String domainId) throws SharingRegistryException, TException { try { - return (new DomainRepository()).get(domainId); + return sharingRegistryService.getDomain(domainId); + } catch (SharingRegistryException e) { + throw e; } catch (Throwable ex) { logger.error(ex.getMessage(), ex); throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex)); @@ -140,7 +124,9 @@ public class SharingRegistryServerHandler implements SharingRegistryService.Ifac @Override public List<Domain> getDomains(int offset, int limit) throws TException { try { - return (new DomainRepository()).select(new HashMap<>(), offset, limit); + return sharingRegistryService.getDomains(offset, limit); + } catch (SharingRegistryException e) { + throw e; } catch (Throwable ex) { logger.error(ex.getMessage(), ex); throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex)); @@ -154,35 +140,9 @@ public class SharingRegistryServerHandler implements SharingRegistryService.Ifac @Override public String createUser(User user) throws SharingRegistryException, DuplicateEntryException, TException { try { - UserPK userPK = new UserPK(); - userPK.setUserId(user.getUserId()); - userPK.setDomainId(user.getDomainId()); - if ((new UserRepository()).get(userPK) != null) - throw new DuplicateEntryException("There exist user with given user id"); - - user.setCreatedTime(System.currentTimeMillis()); - user.setUpdatedTime(System.currentTimeMillis()); - (new UserRepository()).create(user); - - UserGroup userGroup = new UserGroup(); - userGroup.setGroupId(user.getUserId()); - userGroup.setDomainId(user.getDomainId()); - userGroup.setName(user.getUserName()); - userGroup.setDescription("user " + user.getUserName() + " group"); - userGroup.setOwnerId(user.getUserId()); - userGroup.setGroupType(GroupType.USER_LEVEL_GROUP); - userGroup.setGroupCardinality(GroupCardinality.SINGLE_USER); - (new UserGroupRepository()).create(userGroup); - - Domain domain = new DomainRepository().get(user.getDomainId()); - if (domain.getInitialUserGroupId() != null) { - addUsersToGroup( - user.getDomainId(), - Collections.singletonList(user.getUserId()), - domain.getInitialUserGroupId()); - } - - return user.getUserId(); + return sharingRegistryService.createUser(user); + } catch (SharingRegistryException | DuplicateEntryException e) { + throw e; } catch (Throwable ex) { logger.error(ex.getMessage(), ex); throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex)); @@ -192,23 +152,9 @@ public class SharingRegistryServerHandler implements SharingRegistryService.Ifac @Override public boolean updatedUser(User user) throws SharingRegistryException, TException { try { - UserPK userPK = new UserPK(); - userPK.setUserId(user.getUserId()); - userPK.setDomainId(user.getDomainId()); - User oldUser = (new UserRepository()).get(userPK); - user.setCreatedTime(oldUser.getCreatedTime()); - user.setUpdatedTime(System.currentTimeMillis()); - user = getUpdatedObject(oldUser, user); - (new UserRepository()).update(user); - - UserGroupPK userGroupPK = new UserGroupPK(); - userGroupPK.setGroupId(user.getUserId()); - userGroupPK.setDomainId(user.getDomainId()); - UserGroup userGroup = (new UserGroupRepository()).get(userGroupPK); - userGroup.setName(user.getUserName()); - userGroup.setDescription("user " + user.getUserName() + " group"); - updateGroup(userGroup); - return true; + return sharingRegistryService.updatedUser(user); + } catch (SharingRegistryException e) { + throw e; } catch (Throwable ex) { logger.error(ex.getMessage(), ex); throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex)); @@ -223,10 +169,9 @@ public class SharingRegistryServerHandler implements SharingRegistryService.Ifac @Override public boolean isUserExists(String domainId, String userId) throws SharingRegistryException, TException { try { - UserPK userPK = new UserPK(); - userPK.setDomainId(domainId); - userPK.setUserId(userId); - return (new UserRepository()).isExists(userPK); + return sharingRegistryService.isUserExists(domainId, userId); + } catch (SharingRegistryException e) { + throw e; } catch (Throwable ex) { logger.error(ex.getMessage(), ex); throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex)); @@ -236,16 +181,9 @@ public class SharingRegistryServerHandler implements SharingRegistryService.Ifac @Override public boolean deleteUser(String domainId, String userId) throws SharingRegistryException, TException { try { - UserPK userPK = new UserPK(); - userPK.setUserId(userId); - userPK.setDomainId(domainId); - (new UserRepository()).delete(userPK); - - UserGroupPK userGroupPK = new UserGroupPK(); - userGroupPK.setGroupId(userId); - userGroupPK.setDomainId(domainId); - (new UserGroupRepository()).delete(userGroupPK); - return true; + return sharingRegistryService.deleteUser(domainId, userId); + } catch (SharingRegistryException e) { + throw e; } catch (Throwable ex) { logger.error(ex.getMessage(), ex); throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex)); @@ -255,10 +193,9 @@ public class SharingRegistryServerHandler implements SharingRegistryService.Ifac @Override public User getUser(String domainId, String userId) throws SharingRegistryException, TException { try { - UserPK userPK = new UserPK(); - userPK.setUserId(userId); - userPK.setDomainId(domainId); - return (new UserRepository()).get(userPK); + return sharingRegistryService.getUser(domainId, userId); + } catch (SharingRegistryException e) { + throw e; } catch (Throwable ex) { logger.error(ex.getMessage(), ex); throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex)); @@ -268,9 +205,9 @@ public class SharingRegistryServerHandler implements SharingRegistryService.Ifac @Override public List<User> getUsers(String domain, int offset, int limit) throws SharingRegistryException, TException { try { - HashMap<String, String> filters = new HashMap<>(); - filters.put(DBConstants.UserTable.DOMAIN_ID, domain); - return (new UserRepository()).select(filters, offset, limit); + return sharingRegistryService.getUsers(domain, offset, limit); + } catch (SharingRegistryException e) { + throw e; } catch (Throwable ex) { logger.error(ex.getMessage(), ex); throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex)); @@ -284,21 +221,9 @@ public class SharingRegistryServerHandler implements SharingRegistryService.Ifac @Override public String createGroup(UserGroup group) throws SharingRegistryException, TException { try { - UserGroupPK userGroupPK = new UserGroupPK(); - userGroupPK.setGroupId(group.getGroupId()); - userGroupPK.setDomainId(group.getDomainId()); - if ((new UserGroupRepository()).get(userGroupPK) != null) - throw new SharingRegistryException("There exist group with given group id"); - // Client created groups are always of type MULTI_USER - group.setGroupCardinality(GroupCardinality.MULTI_USER); - group.setCreatedTime(System.currentTimeMillis()); - group.setUpdatedTime(System.currentTimeMillis()); - // Add group admins once the group is created - group.unsetGroupAdmins(); - (new UserGroupRepository()).create(group); - - addUsersToGroup(group.getDomainId(), Arrays.asList(group.getOwnerId()), group.getGroupId()); - return group.getGroupId(); + return sharingRegistryService.createGroup(group); + } catch (SharingRegistryException e) { + throw e; } catch (Throwable ex) { logger.error(ex.getMessage(), ex); throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex)); @@ -308,20 +233,9 @@ public class SharingRegistryServerHandler implements SharingRegistryService.Ifac @Override public boolean updateGroup(UserGroup group) throws SharingRegistryException, TException { try { - group.setUpdatedTime(System.currentTimeMillis()); - UserGroupPK userGroupPK = new UserGroupPK(); - userGroupPK.setGroupId(group.getGroupId()); - userGroupPK.setDomainId(group.getDomainId()); - UserGroup oldGroup = (new UserGroupRepository()).get(userGroupPK); - group.setGroupCardinality(oldGroup.getGroupCardinality()); - group.setCreatedTime(oldGroup.getCreatedTime()); - group = getUpdatedObject(oldGroup, group); - - if (!group.getOwnerId().equals(oldGroup.getOwnerId())) - throw new SharingRegistryException("Group owner cannot be changed"); - - (new UserGroupRepository()).update(group); - return true; + return sharingRegistryService.updateGroup(group); + } catch (SharingRegistryException e) { + throw e; } catch (Throwable ex) { logger.error(ex.getMessage(), ex); throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex)); @@ -339,10 +253,9 @@ public class SharingRegistryServerHandler implements SharingRegistryService.Ifac @Override public boolean isGroupExists(String domainId, String groupId) throws SharingRegistryException, TException { try { - UserGroupPK userGroupPK = new UserGroupPK(); - userGroupPK.setDomainId(domainId); - userGroupPK.setGroupId(groupId); - return (new UserGroupRepository()).isExists(userGroupPK); + return sharingRegistryService.isGroupExists(domainId, groupId); + } catch (SharingRegistryException e) { + throw e; } catch (Throwable ex) { logger.error(ex.getMessage(), ex); throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex)); @@ -352,11 +265,9 @@ public class SharingRegistryServerHandler implements SharingRegistryService.Ifac @Override public boolean deleteGroup(String domainId, String groupId) throws SharingRegistryException, TException { try { - UserGroupPK userGroupPK = new UserGroupPK(); - userGroupPK.setGroupId(groupId); - userGroupPK.setDomainId(domainId); - (new UserGroupRepository()).delete(userGroupPK); - return true; + return sharingRegistryService.deleteGroup(domainId, groupId); + } catch (SharingRegistryException e) { + throw e; } catch (Throwable ex) { logger.error(ex.getMessage(), ex); throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex)); @@ -366,10 +277,9 @@ public class SharingRegistryServerHandler implements SharingRegistryService.Ifac @Override public UserGroup getGroup(String domainId, String groupId) throws SharingRegistryException, TException { try { - UserGroupPK userGroupPK = new UserGroupPK(); - userGroupPK.setGroupId(groupId); - userGroupPK.setDomainId(domainId); - return (new UserGroupRepository()).get(userGroupPK); + return sharingRegistryService.getGroup(domainId, groupId); + } catch (SharingRegistryException e) { + throw e; } catch (Throwable ex) { logger.error(ex.getMessage(), ex); throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex)); @@ -379,12 +289,9 @@ public class SharingRegistryServerHandler implements SharingRegistryService.Ifac @Override public List<UserGroup> getGroups(String domain, int offset, int limit) throws TException { try { - HashMap<String, String> filters = new HashMap<>(); - filters.put(DBConstants.UserGroupTable.DOMAIN_ID, domain); - // Only return groups with MULTI_USER cardinality which is the only type of cardinality allowed for client - // created groups - filters.put(DBConstants.UserGroupTable.GROUP_CARDINALITY, GroupCardinality.MULTI_USER.name()); - return (new UserGroupRepository()).select(filters, offset, limit); + return sharingRegistryService.getGroups(domain, offset, limit); + } catch (SharingRegistryException e) { + throw e; } catch (Throwable ex) { logger.error(ex.getMessage(), ex); throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex)); @@ -395,17 +302,9 @@ public class SharingRegistryServerHandler implements SharingRegistryService.Ifac public boolean addUsersToGroup(String domainId, List<String> userIds, String groupId) throws SharingRegistryException, TException { try { - for (int i = 0; i < userIds.size(); i++) { - GroupMembership groupMembership = new GroupMembership(); - groupMembership.setParentId(groupId); - groupMembership.setChildId(userIds.get(i)); - groupMembership.setChildType(GroupChildType.USER); - groupMembership.setDomainId(domainId); - groupMembership.setCreatedTime(System.currentTimeMillis()); - groupMembership.setUpdatedTime(System.currentTimeMillis()); - (new GroupMembershipRepository()).create(groupMembership); - } - return true; + return sharingRegistryService.addUsersToGroup(domainId, userIds, groupId); + } catch (SharingRegistryException e) { + throw e; } catch (Throwable ex) { logger.error(ex.getMessage(), ex); throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex)); @@ -416,21 +315,9 @@ public class SharingRegistryServerHandler implements SharingRegistryService.Ifac public boolean removeUsersFromGroup(String domainId, List<String> userIds, String groupId) throws SharingRegistryException, TException { try { - for (String userId : userIds) { - if (hasOwnerAccess(domainId, groupId, userId)) { - throw new SharingRegistryException( - "List of User Ids contains Owner Id. Cannot remove owner from the group"); - } - } - - for (int i = 0; i < userIds.size(); i++) { - GroupMembershipPK groupMembershipPK = new GroupMembershipPK(); - groupMembershipPK.setParentId(groupId); - groupMembershipPK.setChildId(userIds.get(i)); - groupMembershipPK.setDomainId(domainId); - (new GroupMembershipRepository()).delete(groupMembershipPK); - } - return true; + return sharingRegistryService.removeUsersFromGroup(domainId, userIds, groupId); + } catch (SharingRegistryException e) { + throw e; } catch (Throwable ex) { logger.error(ex.getMessage(), ex); throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex)); @@ -441,74 +328,22 @@ public class SharingRegistryServerHandler implements SharingRegistryService.Ifac public boolean transferGroupOwnership(String domainId, String groupId, String newOwnerId) throws SharingRegistryException, TException { try { - List<User> groupUser = getGroupMembersOfTypeUser(domainId, groupId, 0, -1); - if (!isUserBelongsToGroup(groupUser, newOwnerId)) { - throw new SharingRegistryException("New group owner is not part of the group"); - } - - if (hasOwnerAccess(domainId, groupId, newOwnerId)) { - throw new DuplicateEntryException("User already the current owner of the group"); - } - // remove the new owner as Admin if present - if (hasAdminAccess(domainId, groupId, newOwnerId)) { - removeGroupAdmins(domainId, groupId, Arrays.asList(newOwnerId)); - } - - UserGroupPK userGroupPK = new UserGroupPK(); - userGroupPK.setGroupId(groupId); - userGroupPK.setDomainId(domainId); - UserGroup userGroup = (new UserGroupRepository()).get(userGroupPK); - UserGroup newUserGroup = new UserGroup(); - newUserGroup.setUpdatedTime(System.currentTimeMillis()); - newUserGroup.setOwnerId(newOwnerId); - newUserGroup.setGroupCardinality(GroupCardinality.MULTI_USER); - newUserGroup.setCreatedTime(userGroup.getCreatedTime()); - newUserGroup = getUpdatedObject(userGroup, newUserGroup); - - (new UserGroupRepository()).update(newUserGroup); - - return true; + return sharingRegistryService.transferGroupOwnership(domainId, groupId, newOwnerId); + } catch (SharingRegistryException | DuplicateEntryException e) { + throw e; } catch (Throwable ex) { logger.error(ex.getMessage(), ex); throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex)); } } - private boolean isUserBelongsToGroup(List<User> groupUser, String newOwnerId) { - for (User user : groupUser) { - if (user.getUserId().equals(newOwnerId)) { - return true; - } - } - return false; - } - @Override public boolean addGroupAdmins(String domainId, String groupId, List<String> adminIds) throws SharingRegistryException, TException { try { - List<User> groupUser = getGroupMembersOfTypeUser(domainId, groupId, 0, -1); - - for (String adminId : adminIds) { - if (!isUserBelongsToGroup(groupUser, adminId)) { - throw new SharingRegistryException( - "Admin not the user of the group. GroupId : " + groupId + ", AdminId : " + adminId); - } - GroupAdminPK groupAdminPK = new GroupAdminPK(); - groupAdminPK.setGroupId(groupId); - groupAdminPK.setAdminId(adminId); - groupAdminPK.setDomainId(domainId); - - if ((new GroupAdminRepository()).get(groupAdminPK) != null) - throw new DuplicateEntryException("User already an admin for the group"); - - GroupAdmin admin = new GroupAdmin(); - admin.setAdminId(adminId); - admin.setDomainId(domainId); - admin.setGroupId(groupId); - (new GroupAdminRepository()).create(admin); - } - return true; + return sharingRegistryService.addGroupAdmins(domainId, groupId, adminIds); + } catch (SharingRegistryException | DuplicateEntryException e) { + throw e; } catch (Throwable ex) { logger.error(ex.getMessage(), ex); throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex)); @@ -519,14 +354,9 @@ public class SharingRegistryServerHandler implements SharingRegistryService.Ifac public boolean removeGroupAdmins(String domainId, String groupId, List<String> adminIds) throws SharingRegistryException, TException { try { - for (String adminId : adminIds) { - GroupAdminPK groupAdminPK = new GroupAdminPK(); - groupAdminPK.setAdminId(adminId); - groupAdminPK.setDomainId(domainId); - groupAdminPK.setGroupId(groupId); - (new GroupAdminRepository()).delete(groupAdminPK); - } - return true; + return sharingRegistryService.removeGroupAdmins(domainId, groupId, adminIds); + } catch (SharingRegistryException e) { + throw e; } catch (Throwable ex) { logger.error(ex.getMessage(), ex); throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex)); @@ -537,13 +367,9 @@ public class SharingRegistryServerHandler implements SharingRegistryService.Ifac public boolean hasAdminAccess(String domainId, String groupId, String adminId) throws SharingRegistryException, TException { try { - GroupAdminPK groupAdminPK = new GroupAdminPK(); - groupAdminPK.setGroupId(groupId); - groupAdminPK.setAdminId(adminId); - groupAdminPK.setDomainId(domainId); - - if ((new GroupAdminRepository()).get(groupAdminPK) != null) return true; - return false; + return sharingRegistryService.hasAdminAccess(domainId, groupId, adminId); + } catch (SharingRegistryException e) { + throw e; } catch (Throwable ex) { logger.error(ex.getMessage(), ex); throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex)); @@ -554,13 +380,9 @@ public class SharingRegistryServerHandler implements SharingRegistryService.Ifac public boolean hasOwnerAccess(String domainId, String groupId, String ownerId) throws SharingRegistryException, TException { try { - UserGroupPK userGroupPK = new UserGroupPK(); - userGroupPK.setGroupId(groupId); - userGroupPK.setDomainId(domainId); - UserGroup getGroup = (new UserGroupRepository()).get(userGroupPK); - - if (getGroup.getOwnerId().equals(ownerId)) return true; - return false; + return sharingRegistryService.hasOwnerAccess(domainId, groupId, ownerId); + } catch (SharingRegistryException e) { + throw e; } catch (Throwable ex) { logger.error(ex.getMessage(), ex); throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex)); @@ -571,9 +393,9 @@ public class SharingRegistryServerHandler implements SharingRegistryService.Ifac public List<User> getGroupMembersOfTypeUser(String domainId, String groupId, int offset, int limit) throws SharingRegistryException, TException { try { - // TODO limit offset - List<User> groupMemberUsers = (new GroupMembershipRepository()).getAllChildUsers(domainId, groupId); - return groupMemberUsers; + return sharingRegistryService.getGroupMembersOfTypeUser(domainId, groupId, offset, limit); + } catch (SharingRegistryException e) { + throw e; } catch (Throwable ex) { logger.error(ex.getMessage(), ex); throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex)); @@ -584,9 +406,9 @@ public class SharingRegistryServerHandler implements SharingRegistryService.Ifac public List<UserGroup> getGroupMembersOfTypeGroup(String domainId, String groupId, int offset, int limit) throws SharingRegistryException, TException { try { - // TODO limit offset - List<UserGroup> groupMemberGroups = (new GroupMembershipRepository()).getAllChildGroups(domainId, groupId); - return groupMemberGroups; + return sharingRegistryService.getGroupMembersOfTypeGroup(domainId, groupId, offset, limit); + } catch (SharingRegistryException e) { + throw e; } catch (Throwable ex) { logger.error(ex.getMessage(), ex); throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex)); @@ -597,18 +419,9 @@ public class SharingRegistryServerHandler implements SharingRegistryService.Ifac public boolean addChildGroupsToParentGroup(String domainId, List<String> childIds, String groupId) throws SharingRegistryException, TException { try { - for (String childId : childIds) { - // Todo check for cyclic dependencies - GroupMembership groupMembership = new GroupMembership(); - groupMembership.setParentId(groupId); - groupMembership.setChildId(childId); - groupMembership.setChildType(GroupChildType.GROUP); - groupMembership.setDomainId(domainId); - groupMembership.setCreatedTime(System.currentTimeMillis()); - groupMembership.setUpdatedTime(System.currentTimeMillis()); - (new GroupMembershipRepository()).create(groupMembership); - } - return true; + return sharingRegistryService.addChildGroupsToParentGroup(domainId, childIds, groupId); + } catch (SharingRegistryException e) { + throw e; } catch (Throwable ex) { logger.error(ex.getMessage(), ex); throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex)); @@ -619,12 +432,9 @@ public class SharingRegistryServerHandler implements SharingRegistryService.Ifac public boolean removeChildGroupFromParentGroup(String domainId, String childId, String groupId) throws SharingRegistryException, TException { try { - GroupMembershipPK groupMembershipPK = new GroupMembershipPK(); - groupMembershipPK.setParentId(groupId); - groupMembershipPK.setChildId(childId); - groupMembershipPK.setDomainId(domainId); - (new GroupMembershipRepository()).delete(groupMembershipPK); - return true; + return sharingRegistryService.removeChildGroupFromParentGroup(domainId, childId, groupId); + } catch (SharingRegistryException e) { + throw e; } catch (Throwable ex) { logger.error(ex.getMessage(), ex); throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex)); @@ -635,8 +445,9 @@ public class SharingRegistryServerHandler implements SharingRegistryService.Ifac public List<UserGroup> getAllMemberGroupsForUser(String domainId, String userId) throws SharingRegistryException, TException { try { - GroupMembershipRepository groupMembershipRepository = new GroupMembershipRepository(); - return groupMembershipRepository.getAllMemberGroupsForUser(domainId, userId); + return sharingRegistryService.getAllMemberGroupsForUser(domainId, userId); + } catch (SharingRegistryException e) { + throw e; } catch (Throwable ex) { logger.error(ex.getMessage(), ex); throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex)); @@ -651,16 +462,9 @@ public class SharingRegistryServerHandler implements SharingRegistryService.Ifac public String createEntityType(EntityType entityType) throws SharingRegistryException, DuplicateEntryException, TException { try { - EntityTypePK entityTypePK = new EntityTypePK(); - entityTypePK.setDomainId(entityType.getDomainId()); - entityTypePK.setEntityTypeId(entityType.getEntityTypeId()); - if ((new EntityTypeRepository()).get(entityTypePK) != null) - throw new DuplicateEntryException("There exist EntityType with given EntityType id"); - - entityType.setCreatedTime(System.currentTimeMillis()); - entityType.setUpdatedTime(System.currentTimeMillis()); - (new EntityTypeRepository()).create(entityType); - return entityType.getEntityTypeId(); + return sharingRegistryService.createEntityType(entityType); + } catch (SharingRegistryException | DuplicateEntryException e) { + throw e; } catch (Throwable ex) { logger.error(ex.getMessage(), ex); throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex)); @@ -670,15 +474,9 @@ public class SharingRegistryServerHandler implements SharingRegistryService.Ifac @Override public boolean updateEntityType(EntityType entityType) throws SharingRegistryException, TException { try { - entityType.setUpdatedTime(System.currentTimeMillis()); - EntityTypePK entityTypePK = new EntityTypePK(); - entityTypePK.setDomainId(entityType.getDomainId()); - entityTypePK.setEntityTypeId(entityType.getEntityTypeId()); - EntityType oldEntityType = (new EntityTypeRepository()).get(entityTypePK); - entityType.setCreatedTime(oldEntityType.getCreatedTime()); - entityType = getUpdatedObject(oldEntityType, entityType); - (new EntityTypeRepository()).update(entityType); - return true; + return sharingRegistryService.updateEntityType(entityType); + } catch (SharingRegistryException e) { + throw e; } catch (Throwable ex) { logger.error(ex.getMessage(), ex); throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex)); @@ -694,10 +492,9 @@ public class SharingRegistryServerHandler implements SharingRegistryService.Ifac public boolean isEntityTypeExists(String domainId, String entityTypeId) throws SharingRegistryException, TException { try { - EntityTypePK entityTypePK = new EntityTypePK(); - entityTypePK.setDomainId(domainId); - entityTypePK.setEntityTypeId(entityTypeId); - return (new EntityTypeRepository()).isExists(entityTypePK); + return sharingRegistryService.isEntityTypeExists(domainId, entityTypeId); + } catch (SharingRegistryException e) { + throw e; } catch (Throwable ex) { logger.error(ex.getMessage(), ex); throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex)); @@ -707,11 +504,9 @@ public class SharingRegistryServerHandler implements SharingRegistryService.Ifac @Override public boolean deleteEntityType(String domainId, String entityTypeId) throws SharingRegistryException, TException { try { - EntityTypePK entityTypePK = new EntityTypePK(); - entityTypePK.setDomainId(domainId); - entityTypePK.setEntityTypeId(entityTypeId); - (new EntityTypeRepository()).delete(entityTypePK); - return true; + return sharingRegistryService.deleteEntityType(domainId, entityTypeId); + } catch (SharingRegistryException e) { + throw e; } catch (Throwable ex) { logger.error(ex.getMessage(), ex); throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex)); @@ -721,10 +516,9 @@ public class SharingRegistryServerHandler implements SharingRegistryService.Ifac @Override public EntityType getEntityType(String domainId, String entityTypeId) throws SharingRegistryException, TException { try { - EntityTypePK entityTypePK = new EntityTypePK(); - entityTypePK.setDomainId(domainId); - entityTypePK.setEntityTypeId(entityTypeId); - return (new EntityTypeRepository()).get(entityTypePK); + return sharingRegistryService.getEntityType(domainId, entityTypeId); + } catch (SharingRegistryException e) { + throw e; } catch (Throwable ex) { logger.error(ex.getMessage(), ex); throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex)); @@ -734,9 +528,9 @@ public class SharingRegistryServerHandler implements SharingRegistryService.Ifac @Override public List<EntityType> getEntityTypes(String domain, int offset, int limit) throws TException { try { - HashMap<String, String> filters = new HashMap<>(); - filters.put(DBConstants.EntityTypeTable.DOMAIN_ID, domain); - return (new EntityTypeRepository()).select(filters, offset, limit); + return sharingRegistryService.getEntityTypes(domain, offset, limit); + } catch (SharingRegistryException e) { + throw e; } catch (Throwable ex) { logger.error(ex.getMessage(), ex); throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex)); @@ -751,15 +545,9 @@ public class SharingRegistryServerHandler implements SharingRegistryService.Ifac public String createPermissionType(PermissionType permissionType) throws SharingRegistryException, DuplicateEntryException, TException { try { - PermissionTypePK permissionTypePK = new PermissionTypePK(); - permissionTypePK.setDomainId(permissionType.getDomainId()); - permissionTypePK.setPermissionTypeId(permissionType.getPermissionTypeId()); - if ((new PermissionTypeRepository()).get(permissionTypePK) != null) - throw new DuplicateEntryException("There exist PermissionType with given PermissionType id"); - permissionType.setCreatedTime(System.currentTimeMillis()); - permissionType.setUpdatedTime(System.currentTimeMillis()); - (new PermissionTypeRepository()).create(permissionType); - return permissionType.getPermissionTypeId(); + return sharingRegistryService.createPermissionType(permissionType); + } catch (SharingRegistryException | DuplicateEntryException e) { + throw e; } catch (Throwable ex) { logger.error(ex.getMessage(), ex); throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex)); @@ -769,14 +557,9 @@ public class SharingRegistryServerHandler implements SharingRegistryService.Ifac @Override public boolean updatePermissionType(PermissionType permissionType) throws SharingRegistryException, TException { try { - permissionType.setUpdatedTime(System.currentTimeMillis()); - PermissionTypePK permissionTypePK = new PermissionTypePK(); - permissionTypePK.setDomainId(permissionType.getDomainId()); - permissionTypePK.setPermissionTypeId(permissionType.getPermissionTypeId()); - PermissionType oldPermissionType = (new PermissionTypeRepository()).get(permissionTypePK); - permissionType = getUpdatedObject(oldPermissionType, permissionType); - (new PermissionTypeRepository()).update(permissionType); - return true; + return sharingRegistryService.updatePermissionType(permissionType); + } catch (SharingRegistryException e) { + throw e; } catch (Throwable ex) { logger.error(ex.getMessage(), ex); throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex)); @@ -792,10 +575,9 @@ public class SharingRegistryServerHandler implements SharingRegistryService.Ifac public boolean isPermissionExists(String domainId, String permissionId) throws SharingRegistryException, TException { try { - PermissionTypePK permissionTypePK = new PermissionTypePK(); - permissionTypePK.setDomainId(domainId); - permissionTypePK.setPermissionTypeId(permissionId); - return (new PermissionTypeRepository()).isExists(permissionTypePK); + return sharingRegistryService.isPermissionExists(domainId, permissionId); + } catch (SharingRegistryException e) { + throw e; } catch (Throwable ex) { logger.error(ex.getMessage(), ex); throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex)); @@ -806,11 +588,9 @@ public class SharingRegistryServerHandler implements SharingRegistryService.Ifac public boolean deletePermissionType(String domainId, String permissionTypeId) throws SharingRegistryException, TException { try { - PermissionTypePK permissionTypePK = new PermissionTypePK(); - permissionTypePK.setDomainId(domainId); - permissionTypePK.setPermissionTypeId(permissionTypeId); - (new PermissionTypeRepository()).delete(permissionTypePK); - return true; + return sharingRegistryService.deletePermissionType(domainId, permissionTypeId); + } catch (SharingRegistryException e) { + throw e; } catch (Throwable ex) { logger.error(ex.getMessage(), ex); throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex)); @@ -821,10 +601,9 @@ public class SharingRegistryServerHandler implements SharingRegistryService.Ifac public PermissionType getPermissionType(String domainId, String permissionTypeId) throws SharingRegistryException, TException { try { - PermissionTypePK permissionTypePK = new PermissionTypePK(); - permissionTypePK.setDomainId(domainId); - permissionTypePK.setPermissionTypeId(permissionTypeId); - return (new PermissionTypeRepository()).get(permissionTypePK); + return sharingRegistryService.getPermissionType(domainId, permissionTypeId); + } catch (SharingRegistryException e) { + throw e; } catch (Throwable ex) { logger.error(ex.getMessage(), ex); throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex)); @@ -835,9 +614,9 @@ public class SharingRegistryServerHandler implements SharingRegistryService.Ifac public List<PermissionType> getPermissionTypes(String domain, int offset, int limit) throws SharingRegistryException, TException { try { - HashMap<String, String> filters = new HashMap<>(); - filters.put(DBConstants.PermissionTypeTable.DOMAIN_ID, domain); - return (new PermissionTypeRepository()).select(filters, offset, limit); + return sharingRegistryService.getPermissionTypes(domain, offset, limit); + } catch (SharingRegistryException e) { + throw e; } catch (Throwable ex) { logger.error(ex.getMessage(), ex); throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex)); @@ -851,52 +630,9 @@ public class SharingRegistryServerHandler implements SharingRegistryService.Ifac @Override public String createEntity(Entity entity) throws SharingRegistryException, DuplicateEntryException, TException { try { - EntityPK entityPK = new EntityPK(); - entityPK.setDomainId(entity.getDomainId()); - entityPK.setEntityId(entity.getEntityId()); - if ((new EntityRepository()).get(entityPK) != null) - throw new DuplicateEntryException("There exist Entity with given Entity id"); - - UserPK userPK = new UserPK(); - userPK.setDomainId(entity.getDomainId()); - userPK.setUserId(entity.getOwnerId()); - if (!(new UserRepository()).isExists(userPK)) { - // Todo this is for Airavata easy integration. Proper thing is to throw an exception here - User user = new User(); - user.setUserId(entity.getOwnerId()); - user.setDomainId(entity.getDomainId()); - user.setUserName(user.getUserId().split("@")[0]); - - createUser(user); - } - entity.setCreatedTime(System.currentTimeMillis()); - entity.setUpdatedTime(System.currentTimeMillis()); - - if (entity.getOriginalEntityCreationTime() == 0) { - entity.setOriginalEntityCreationTime(entity.getCreatedTime()); - } - (new EntityRepository()).create(entity); - - // Assigning global permission for the owner - Sharing newSharing = new Sharing(); - newSharing.setPermissionTypeId( - (new PermissionTypeRepository()).getOwnerPermissionTypeIdForDomain(entity.getDomainId())); - newSharing.setEntityId(entity.getEntityId()); - newSharing.setGroupId(entity.getOwnerId()); - newSharing.setSharingType(SharingType.DIRECT_CASCADING); - newSharing.setInheritedParentId(entity.getEntityId()); - newSharing.setDomainId(entity.getDomainId()); - newSharing.setCreatedTime(System.currentTimeMillis()); - newSharing.setUpdatedTime(System.currentTimeMillis()); - - (new SharingRepository()).create(newSharing); - - // creating records for inherited permissions - if (entity.getParentEntityId() != null && entity.getParentEntityId() != "") { - addCascadingPermissionsForEntity(entity); - } - - return entity.getEntityId(); + return sharingRegistryService.createEntity(entity); + } catch (SharingRegistryException | DuplicateEntryException e) { + throw e; } catch (Throwable ex) { logger.error(ex.getMessage(), ex); SharingRegistryException sharingRegistryException = new SharingRegistryException(); @@ -905,59 +641,12 @@ public class SharingRegistryServerHandler implements SharingRegistryService.Ifac } } - private void addCascadingPermissionsForEntity(Entity entity) throws SharingRegistryException { - Sharing newSharing; - List<Sharing> sharings = (new SharingRepository()) - .getCascadingPermissionsForEntity(entity.getDomainId(), entity.getParentEntityId()); - for (Sharing sharing : sharings) { - newSharing = new Sharing(); - newSharing.setPermissionTypeId(sharing.getPermissionTypeId()); - newSharing.setEntityId(entity.getEntityId()); - newSharing.setGroupId(sharing.getGroupId()); - newSharing.setInheritedParentId(sharing.getInheritedParentId()); - newSharing.setSharingType(SharingType.INDIRECT_CASCADING); - newSharing.setDomainId(entity.getDomainId()); - newSharing.setCreatedTime(System.currentTimeMillis()); - newSharing.setUpdatedTime(System.currentTimeMillis()); - - (new SharingRepository()).create(newSharing); - } - } - @Override public boolean updateEntity(Entity entity) throws SharingRegistryException, TException { try { - // TODO Check for permission changes - entity.setUpdatedTime(System.currentTimeMillis()); - EntityPK entityPK = new EntityPK(); - entityPK.setDomainId(entity.getDomainId()); - entityPK.setEntityId(entity.getEntityId()); - Entity oldEntity = (new EntityRepository()).get(entityPK); - entity.setCreatedTime(oldEntity.getCreatedTime()); - // check if parent entity changed and re-add inherited permissions - if (!Objects.equals(oldEntity.getParentEntityId(), entity.getParentEntityId())) { - logger.debug("Parent entity changed for {}, updating inherited permissions", entity.getEntityId()); - if (oldEntity.getParentEntityId() != null && oldEntity.getParentEntityId() != "") { - logger.debug( - "Removing inherited permissions from {} that were inherited from parent {}", - entity.getEntityId(), - oldEntity.getParentEntityId()); - (new SharingRepository()) - .removeAllIndirectCascadingPermissionsForEntity(entity.getDomainId(), entity.getEntityId()); - } - if (entity.getParentEntityId() != null && entity.getParentEntityId() != "") { - // re-add INDIRECT_CASCADING permissions - logger.debug( - "Adding inherited permissions to {} that are inherited from parent {}", - entity.getEntityId(), - entity.getParentEntityId()); - addCascadingPermissionsForEntity(entity); - } - } - entity = getUpdatedObject(oldEntity, entity); - entity.setSharedCount((new SharingRepository()).getSharedCount(entity.getDomainId(), entity.getEntityId())); - (new EntityRepository()).update(entity); - return true; + return sharingRegistryService.updateEntity(entity); + } catch (SharingRegistryException e) { + throw e; } catch (Throwable ex) { logger.error(ex.getMessage(), ex); throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex)); @@ -972,10 +661,9 @@ public class SharingRegistryServerHandler implements SharingRegistryService.Ifac @Override public boolean isEntityExists(String domainId, String entityId) throws SharingRegistryException, TException { try { - EntityPK entityPK = new EntityPK(); - entityPK.setDomainId(domainId); - entityPK.setEntityId(entityId); - return (new EntityRepository()).isExists(entityPK); + return sharingRegistryService.isEntityExists(domainId, entityId); + } catch (SharingRegistryException e) { + throw e; } catch (Throwable ex) { logger.error(ex.getMessage(), ex); throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex)); @@ -985,12 +673,9 @@ public class SharingRegistryServerHandler implements SharingRegistryService.Ifac @Override public boolean deleteEntity(String domainId, String entityId) throws SharingRegistryException, TException { try { - // TODO Check for permission changes - EntityPK entityPK = new EntityPK(); - entityPK.setDomainId(domainId); - entityPK.setEntityId(entityId); - (new EntityRepository()).delete(entityPK); - return true; + return sharingRegistryService.deleteEntity(domainId, entityId); + } catch (SharingRegistryException e) { + throw e; } catch (Throwable ex) { logger.error(ex.getMessage(), ex); throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex)); @@ -1000,10 +685,9 @@ public class SharingRegistryServerHandler implements SharingRegistryService.Ifac @Override public Entity getEntity(String domainId, String entityId) throws SharingRegistryException, TException { try { - EntityPK entityPK = new EntityPK(); - entityPK.setDomainId(domainId); - entityPK.setEntityId(entityId); - return (new EntityRepository()).get(entityPK); + return sharingRegistryService.getEntity(domainId, entityId); + } catch (SharingRegistryException e) { + throw e; } catch (Throwable ex) { logger.error(ex.getMessage(), ex); throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex)); @@ -1015,12 +699,9 @@ public class SharingRegistryServerHandler implements SharingRegistryService.Ifac String domainId, String userId, List<SearchCriteria> filters, int offset, int limit) throws SharingRegistryException, TException { try { - List<String> groupIds = new ArrayList<>(); - groupIds.add(userId); - (new GroupMembershipRepository()) - .getAllParentMembershipsForChild(domainId, userId).stream() - .forEach(gm -> groupIds.add(gm.getParentId())); - return (new EntityRepository()).searchEntities(domainId, groupIds, filters, offset, limit); + return sharingRegistryService.searchEntities(domainId, userId, filters, offset, limit); + } catch (SharingRegistryException e) { + throw e; } catch (Throwable ex) { logger.error(ex.getMessage(), ex); throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex)); @@ -1031,7 +712,9 @@ public class SharingRegistryServerHandler implements SharingRegistryService.Ifac public List<User> getListOfSharedUsers(String domainId, String entityId, String permissionTypeId) throws SharingRegistryException, TException { try { - return (new UserRepository()).getAccessibleUsers(domainId, entityId, permissionTypeId); + return sharingRegistryService.getListOfSharedUsers(domainId, entityId, permissionTypeId); + } catch (SharingRegistryException e) { + throw e; } catch (Throwable ex) { logger.error(ex.getMessage(), ex); throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex)); @@ -1042,7 +725,9 @@ public class SharingRegistryServerHandler implements SharingRegistryService.Ifac public List<User> getListOfDirectlySharedUsers(String domainId, String entityId, String permissionTypeId) throws SharingRegistryException, TException { try { - return (new UserRepository()).getDirectlyAccessibleUsers(domainId, entityId, permissionTypeId); + return sharingRegistryService.getListOfDirectlySharedUsers(domainId, entityId, permissionTypeId); + } catch (SharingRegistryException e) { + throw e; } catch (Throwable ex) { logger.error(ex.getMessage(), ex); SharingRegistryException sharingRegistryException = new SharingRegistryException(); @@ -1055,7 +740,9 @@ public class SharingRegistryServerHandler implements SharingRegistryService.Ifac public List<UserGroup> getListOfSharedGroups(String domainId, String entityId, String permissionTypeId) throws SharingRegistryException, TException { try { - return (new UserGroupRepository()).getAccessibleGroups(domainId, entityId, permissionTypeId); + return sharingRegistryService.getListOfSharedGroups(domainId, entityId, permissionTypeId); + } catch (SharingRegistryException e) { + throw e; } catch (Throwable ex) { logger.error(ex.getMessage(), ex); throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex)); @@ -1066,7 +753,9 @@ public class SharingRegistryServerHandler implements SharingRegistryService.Ifac public List<UserGroup> getListOfDirectlySharedGroups(String domainId, String entityId, String permissionTypeId) throws SharingRegistryException, TException { try { - return (new UserGroupRepository()).getDirectlyAccessibleGroups(domainId, entityId, permissionTypeId); + return sharingRegistryService.getListOfDirectlySharedGroups(domainId, entityId, permissionTypeId); + } catch (SharingRegistryException e) { + throw e; } catch (Throwable ex) { logger.error(ex.getMessage(), ex); SharingRegistryException sharingRegistryException = new SharingRegistryException(); @@ -1091,7 +780,9 @@ public class SharingRegistryServerHandler implements SharingRegistryService.Ifac String domainId, String entityId, List<String> userList, String permissionTypeId, boolean cascadePermission) throws SharingRegistryException, TException { try { - return shareEntity(domainId, entityId, userList, permissionTypeId, cascadePermission); + return sharingRegistryService.shareEntityWithUsers(domainId, entityId, userList, permissionTypeId, cascadePermission); + } catch (SharingRegistryException e) { + throw e; } catch (Throwable ex) { logger.error(ex.getMessage(), ex); throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex)); @@ -1107,81 +798,9 @@ public class SharingRegistryServerHandler implements SharingRegistryService.Ifac boolean cascadePermission) throws SharingRegistryException, TException { try { - return shareEntity(domainId, entityId, groupList, permissionTypeId, cascadePermission); - } catch (Throwable ex) { - logger.error(ex.getMessage(), ex); - throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex)); - } - } - - private boolean shareEntity( - String domainId, - String entityId, - List<String> groupOrUserList, - String permissionTypeId, - boolean cascadePermission) - throws SharingRegistryException, TException { - try { - if (permissionTypeId.equals((new PermissionTypeRepository()).getOwnerPermissionTypeIdForDomain(domainId))) { - throw new SharingRegistryException(OWNER_PERMISSION_NAME + " permission cannot be assigned or removed"); - } - - List<Sharing> sharings = new ArrayList<>(); - - // Adding permission for the specified users/groups for the specified entity - LinkedList<Entity> temp = new LinkedList<>(); - for (String userId : groupOrUserList) { - Sharing sharing = new Sharing(); - sharing.setPermissionTypeId(permissionTypeId); - sharing.setEntityId(entityId); - sharing.setGroupId(userId); - sharing.setInheritedParentId(entityId); - sharing.setDomainId(domainId); - if (cascadePermission) { - sharing.setSharingType(SharingType.DIRECT_CASCADING); - } else { - sharing.setSharingType(SharingType.DIRECT_NON_CASCADING); - } - sharing.setCreatedTime(System.currentTimeMillis()); - sharing.setUpdatedTime(System.currentTimeMillis()); - - sharings.add(sharing); - } - - if (cascadePermission) { - // Adding permission for the specified users/groups for all child entities - (new EntityRepository()) - .getChildEntities(domainId, entityId).stream().forEach(e -> temp.addLast(e)); - while (temp.size() > 0) { - Entity entity = temp.pop(); - String childEntityId = entity.getEntityId(); - for (String userId : groupOrUserList) { - Sharing sharing = new Sharing(); - sharing.setPermissionTypeId(permissionTypeId); - sharing.setEntityId(childEntityId); - sharing.setGroupId(userId); - sharing.setInheritedParentId(entityId); - sharing.setSharingType(SharingType.INDIRECT_CASCADING); - sharing.setInheritedParentId(entityId); - sharing.setDomainId(domainId); - sharing.setCreatedTime(System.currentTimeMillis()); - sharing.setUpdatedTime(System.currentTimeMillis()); - sharings.add(sharing); - (new EntityRepository()) - .getChildEntities(domainId, childEntityId).stream() - .forEach(e -> temp.addLast(e)); - } - } - } - (new SharingRepository()).create(sharings); - - EntityPK entityPK = new EntityPK(); - entityPK.setDomainId(domainId); - entityPK.setEntityId(entityId); - Entity entity = (new EntityRepository()).get(entityPK); - entity.setSharedCount((new SharingRepository()).getSharedCount(domainId, entityId)); - (new EntityRepository()).update(entity); - return true; + return sharingRegistryService.shareEntityWithGroups(domainId, entityId, groupList, permissionTypeId, cascadePermission); + } catch (SharingRegistryException e) { + throw e; } catch (Throwable ex) { logger.error(ex.getMessage(), ex); throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex)); @@ -1193,10 +812,9 @@ public class SharingRegistryServerHandler implements SharingRegistryService.Ifac String domainId, String entityId, List<String> userList, String permissionTypeId) throws SharingRegistryException, TException { try { - if (permissionTypeId.equals((new PermissionTypeRepository()).getOwnerPermissionTypeIdForDomain(domainId))) { - throw new SharingRegistryException(OWNER_PERMISSION_NAME + " permission cannot be assigned or removed"); - } - return revokeEntitySharing(domainId, entityId, userList, permissionTypeId); + return sharingRegistryService.revokeEntitySharingFromUsers(domainId, entityId, userList, permissionTypeId); + } catch (SharingRegistryException e) { + throw e; } catch (Throwable ex) { logger.error(ex.getMessage(), ex); throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex)); @@ -1208,10 +826,9 @@ public class SharingRegistryServerHandler implements SharingRegistryService.Ifac String domainId, String entityId, List<String> groupList, String permissionTypeId) throws SharingRegistryException, TException { try { - if (permissionTypeId.equals((new PermissionTypeRepository()).getOwnerPermissionTypeIdForDomain(domainId))) { - throw new SharingRegistryException(OWNER_PERMISSION_NAME + " permission cannot be assigned or removed"); - } - return revokeEntitySharing(domainId, entityId, groupList, permissionTypeId); + return sharingRegistryService.revokeEntitySharingFromGroups(domainId, entityId, groupList, permissionTypeId); + } catch (SharingRegistryException e) { + throw e; } catch (Throwable ex) { logger.error(ex.getMessage(), ex); throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex)); @@ -1222,119 +839,12 @@ public class SharingRegistryServerHandler implements SharingRegistryService.Ifac public boolean userHasAccess(String domainId, String userId, String entityId, String permissionTypeId) throws SharingRegistryException, TException { try { - // check whether the user has permission directly or indirectly - List<GroupMembership> parentMemberships = - (new GroupMembershipRepository()).getAllParentMembershipsForChild(domainId, userId); - List<String> groupIds = new ArrayList<>(); - parentMemberships.stream().forEach(pm -> groupIds.add(pm.getParentId())); - groupIds.add(userId); - return (new SharingRepository()) - .hasAccess( - domainId, - entityId, - groupIds, - Arrays.asList( - permissionTypeId, - (new PermissionTypeRepository()).getOwnerPermissionTypeIdForDomain(domainId))); + return sharingRegistryService.userHasAccess(domainId, userId, entityId, permissionTypeId); + } catch (SharingRegistryException e) { + throw e; } catch (Throwable ex) { logger.error(ex.getMessage(), ex); throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex)); } } - - public boolean revokeEntitySharing( - String domainId, String entityId, List<String> groupOrUserList, String permissionTypeId) - throws SharingRegistryException { - try { - if (permissionTypeId.equals((new PermissionTypeRepository()).getOwnerPermissionTypeIdForDomain(domainId))) { - throw new SharingRegistryException(OWNER_PERMISSION_NAME + " permission cannot be removed"); - } - - // revoking permission for the entity - for (String groupId : groupOrUserList) { - SharingPK sharingPK = new SharingPK(); - sharingPK.setEntityId(entityId); - sharingPK.setGroupId(groupId); - sharingPK.setPermissionTypeId(permissionTypeId); - sharingPK.setInheritedParentId(entityId); - sharingPK.setDomainId(domainId); - - (new SharingRepository()).delete(sharingPK); - } - - // revoking permission from inheritance - List<Sharing> temp = new ArrayList<>(); - (new SharingRepository()) - .getIndirectSharedChildren(domainId, entityId, permissionTypeId).stream() - .forEach(s -> temp.add(s)); - for (Sharing sharing : temp) { - String childEntityId = sharing.getEntityId(); - for (String groupId : groupOrUserList) { - SharingPK sharingPK = new SharingPK(); - sharingPK.setEntityId(childEntityId); - sharingPK.setGroupId(groupId); - sharingPK.setPermissionTypeId(permissionTypeId); - sharingPK.setInheritedParentId(entityId); - sharingPK.setDomainId(domainId); - - (new SharingRepository()).delete(sharingPK); - } - } - - EntityPK entityPK = new EntityPK(); - entityPK.setDomainId(domainId); - entityPK.setEntityId(entityId); - Entity entity = (new EntityRepository()).get(entityPK); - entity.setSharedCount((new SharingRepository()).getSharedCount(domainId, entityId)); - (new EntityRepository()).update(entity); - return true; - } catch (Throwable ex) { - logger.error(ex.getMessage(), ex); - throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex)); - } - } - - private <T> T getUpdatedObject(T oldEntity, T newEntity) throws SharingRegistryException { - Field[] newEntityFields = newEntity.getClass().getDeclaredFields(); - Hashtable newHT = fieldsToHT(newEntityFields, newEntity); - - Class oldEntityClass = oldEntity.getClass(); - Field[] oldEntityFields = oldEntityClass.getDeclaredFields(); - - for (Field field : oldEntityFields) { - if (!Modifier.isFinal(field.getModifiers())) { - field.setAccessible(true); - Object o = newHT.get(field.getName()); - if (o != null) { - Field f = null; - try { - f = oldEntityClass.getDeclaredField(field.getName()); - f.setAccessible(true); - logger.debug("setting " + f.getName()); - f.set(oldEntity, o); - } catch (Exception e) { - throw new SharingRegistryException(e.getMessage()); - } - } - } - } - return oldEntity; - } - - private static Hashtable<String, Object> fieldsToHT(Field[] fields, Object obj) { - Hashtable<String, Object> hashtable = new Hashtable<>(); - for (Field field : fields) { - field.setAccessible(true); - try { - Object retrievedObject = field.get(obj); - if (retrievedObject != null) { - logger.debug("scanning " + field.getName()); - hashtable.put(field.getName(), field.get(obj)); - } - } catch (IllegalAccessException e) { - e.printStackTrace(); - } - } - return hashtable; - } }
