Changeset 5549 for ComponentRegistry/trunk/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/impl/database/ComponentRegistryFactoryDbImpl.java
- Timestamp:
- 08/11/14 16:07:55 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
ComponentRegistry/trunk/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/impl/database/ComponentRegistryFactoryDbImpl.java
r4098 r5549 3 3 import clarin.cmdi.componentregistry.ComponentRegistry; 4 4 import clarin.cmdi.componentregistry.ComponentRegistryFactory; 5 import clarin.cmdi.componentregistry.ComponentStatus;6 5 import clarin.cmdi.componentregistry.Configuration; 7 6 import clarin.cmdi.componentregistry.Owner; 8 7 import clarin.cmdi.componentregistry.OwnerUser; 8 import clarin.cmdi.componentregistry.RegistrySpace; 9 9 import clarin.cmdi.componentregistry.UserCredentials; 10 10 import clarin.cmdi.componentregistry.UserUnauthorizedException; … … 25 25 * ComponentRegistryDbImpl implementation of ComponentRegistry for accessing the 26 26 * registry 27 * 27 * 28 28 * @author Twan Goosen <twan.goosen@mpi.nl> 29 29 * @author George.Georgovassilis@mpi.nl … … 32 32 33 33 private final static Logger LOG = LoggerFactory 34 34 .getLogger(ComponentRegistryFactoryDbImpl.class); 35 35 @Autowired 36 36 private Configuration configuration; … … 43 43 @Override 44 44 public List<ComponentRegistry> getAllUserRegistries() { 45 // TODO: this probably could use some caching 46 try { 47 List<RegistryUser> users = userDao.getAllUsers(); 48 List<ComponentRegistry> registries = new ArrayList<ComponentRegistry>(); 49 for (RegistryUser user : users) { 50 registries.add(getNewComponentRegistryForUser(user.getId())); 51 } 52 return registries; 53 } catch (DataAccessException ex) { 54 LOG.error("Could not retrieve users", ex); 55 throw ex; 56 } 57 } 58 59 @Override 60 public ComponentRegistry getComponentRegistry(ComponentStatus status, 61 Owner owner, UserCredentials credentials) 62 throws UserUnauthorizedException { 63 switch (status) { 64 case PRIVATE: 65 return getPrivateRegistry(owner, credentials); 66 case PUBLISHED: 67 return getPublicRegistry(); 68 default: 69 // TODO: Add support for other status types 70 throw new UnsupportedOperationException( 71 "Unsupported component status" + status); 72 } 45 // TODO: this probably could use some caching 46 try { 47 List<RegistryUser> users = userDao.getAllUsers(); 48 List<ComponentRegistry> registries = new ArrayList<ComponentRegistry>(); 49 for (RegistryUser user : users) { 50 registries.add(getNewComponentRegistryForUser(user.getId())); 51 } 52 return registries; 53 } catch (DataAccessException ex) { 54 LOG.error("Could not retrieve users", ex); 55 throw ex; 56 } 57 } 58 59 @Override 60 public ComponentRegistry getComponentRegistry(RegistrySpace space, 61 Owner owner, UserCredentials credentials, Number groupId) 62 throws UserUnauthorizedException { 63 64 switch (space) { 65 case PRIVATE: 66 return this.getPrivateRegistry(owner, credentials); 67 case PUBLISHED: 68 return this.getPublicRegistry(); 69 case GROUP: 70 return this.getGroupRegistry(groupId, owner, credentials); 71 default: 72 // TODO: Add support for "review" types 73 throw new UnsupportedOperationException( 74 "Unsupported component space" + space); 75 } 76 } 77 78 private ComponentRegistry getGroupRegistry(Number groupId, Owner owner, UserCredentials credentials) { 79 if (owner == null || owner instanceof OwnerUser) { 80 RegistryUser user = this.getOrCreateUser(credentials); 81 owner = new OwnerUser(user.getId()); 82 } 83 ComponentRegistryDbImpl cr = componentRegistryBeanFactory.getNewComponentRegistry();// default public registry 84 cr.setRegistrySpace(RegistrySpace.PRIVATE); 85 cr.setRegistryOwner(owner); 86 cr.setGroupId(groupId); 87 return cr; 73 88 } 74 89 75 90 private ComponentRegistry getPrivateRegistry(Owner owner, 76 77 78 79 80 81 82 83 84 85 86 87 returngetNewComponentRegistryForUser(user.getId());88 89 90 91 92 93 94 95 96 97 98 99 100 91 UserCredentials credentials) throws IllegalArgumentException, 92 DataAccessException, UserUnauthorizedException { 93 if (owner == null || owner instanceof OwnerUser) { 94 RegistryUser user = getOrCreateUser(credentials); 95 if (user != null) { 96 if (owner != null && !user.getId().equals(owner.getId())) { 97 throw new UserUnauthorizedException( 98 "User cannot access other user's private registry"); 99 } 100 101 try { 102 return this.getNewComponentRegistryForUser(user.getId()); 103 } catch (DataAccessException ex) { 104 LOG.error("Could not retrieve or create user", ex); 105 throw ex; 106 } 107 } else { 108 throw new UserUnauthorizedException( 109 "No user credentials available cannot load userspace."); 110 } 111 } else { 112 // TODO: Support group owners 113 throw new UnsupportedOperationException( 114 "Group owners not supported"); 115 } 101 116 } 102 117 103 118 @Override 104 119 public ComponentRegistry getOtherUserComponentRegistry( 105 Principal adminPrincipal, ComponentStatus status, Owner owner) {106 107 108 109 110 111 // TODO: Implement for groups 112 113 114 115 116 117 118 result =getNewComponentRegistryForUser(user.getId());119 120 121 122 123 124 125 126 127 128 129 130 131 132 120 Principal adminPrincipal, Owner owner) { 121 try { 122 RegistryUser user; 123 if (owner instanceof OwnerUser) { 124 user = userDao.findOne(owner.getId().longValue()); 125 } else { 126 // TODO: Implement for groups ??? Twan 127 throw new UnsupportedOperationException( 128 "Groups not implemented yet"); 129 } 130 ComponentRegistry result = null; 131 if (user != null) { 132 if (configuration.isAdminUser(adminPrincipal)) { 133 result = this.getNewComponentRegistryForUser(user.getId()); 134 } else { 135 LOG.info("{} not found in list of {}", 136 adminPrincipal.getName(), 137 configuration.getAdminUsersArray().length); 138 throw new IllegalArgumentException("User " 139 + adminPrincipal.getName() 140 + " is not admin user cannot load userspace."); 141 } 142 } 143 return result; 144 } catch (DataAccessException ex) { 145 LOG.error("Could not retrieve user by id", ex); 146 throw ex; 147 } 133 148 } 134 149 135 150 @Override 136 151 public ComponentRegistry getPublicRegistry() { 137 138 publicComponentRegistry =getNewComponentRegistryForUser(null);139 140 152 if (publicComponentRegistry == null) { 153 publicComponentRegistry = this.getNewComponentRegistryForUser(null); 154 } 155 return publicComponentRegistry; 141 156 } 142 157 143 158 private ComponentRegistry getNewComponentRegistryForUser(Number userId) { 144 ComponentRegistryDbImpl componentRegistry = componentRegistryBeanFactory 145 .getNewComponentRegistry(); 146 if (userId != null) { 147 // Null means public registry 148 // TODO: Make this more explicit 149 componentRegistry.setStatus(ComponentStatus.PRIVATE, new OwnerUser( 150 userId)); 151 } 152 return componentRegistry; 159 ComponentRegistryDbImpl componentRegistry = componentRegistryBeanFactory.getNewComponentRegistry(); 160 if (userId != null) { 161 OwnerUser ou = new OwnerUser(userId); 162 componentRegistry.setRegistrySpace(RegistrySpace.PRIVATE); 163 componentRegistry.setRegistryOwner(ou); 164 componentRegistry.setGroupId(null); 165 } 166 return componentRegistry; 167 } 168 169 @Override 170 public ComponentRegistryDbImpl getBaseRegistry(UserCredentials credentials) { 171 RegistryUser user = this.getOrCreateUser(credentials); 172 ComponentRegistryDbImpl componentRegistryDbImpl = componentRegistryBeanFactory.getNewComponentRegistry();// default public registry 173 if (user != null) { 174 Number userId = user.getId(); 175 Owner owner = new OwnerUser(userId); 176 componentRegistryDbImpl.setRegistrySpace(null); 177 componentRegistryDbImpl.setRegistryOwner(owner); 178 componentRegistryDbImpl.setGroupId(null); 179 } 180 return componentRegistryDbImpl; 153 181 } 154 182 155 183 @Override 156 184 public RegistryUser getOrCreateUser(UserCredentials credentials) { 157 158 159 160 returngetOrCreateUser(principalName, credentials.getDisplayName());161 162 185 if (credentials != null 186 && !ANONYMOUS_USER.equals(credentials.getPrincipalName())) { 187 String principalName = credentials.getPrincipalName(); 188 return this.getOrCreateUser(principalName, credentials.getDisplayName()); 189 } 190 return null; 163 191 } 164 192 165 193 private synchronized RegistryUser getOrCreateUser(String principalName, 166 167 168 169 170 171 172 new Object[] { principalName, displayName});173 174 175 176 177 178 179 180 181 194 String displayName) { 195 // Try getting it from db 196 RegistryUser user = userDao.getByPrincipalName(principalName); 197 if (user == null) { 198 LOG.info( 199 "Request to create user with principal name {} and display name {}", 200 new Object[]{principalName, displayName}); 201 // Create the new user 202 user = new RegistryUser(); 203 user.setPrincipalName(principalName); 204 user.setName(displayName); 205 userDao.saveAndFlush(user); 206 // Retrieve from db 207 user = userDao.getByPrincipalName(principalName); 208 } 209 return user; 182 210 } 183 211 }
Note: See TracChangeset
for help on using the changeset viewer.