source: ComponentRegistry/trunk/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/impl/database/ComponentRegistryFactoryDbImpl.java @ 5549

Last change on this file since 5549 was 5549, checked in by olhsha@mpi.nl, 10 years ago

Added group service. Tested via the tomcat on loclahots (test URI and postman), old unit tests are adjusted and work well. Todo: retest on localhost tomcat, look at run-time exceptions, add new unit tests, adjust front-end

File size: 8.3 KB
Line 
1package clarin.cmdi.componentregistry.impl.database;
2
3import clarin.cmdi.componentregistry.ComponentRegistry;
4import clarin.cmdi.componentregistry.ComponentRegistryFactory;
5import clarin.cmdi.componentregistry.Configuration;
6import clarin.cmdi.componentregistry.Owner;
7import clarin.cmdi.componentregistry.OwnerUser;
8import clarin.cmdi.componentregistry.RegistrySpace;
9import clarin.cmdi.componentregistry.UserCredentials;
10import clarin.cmdi.componentregistry.UserUnauthorizedException;
11import clarin.cmdi.componentregistry.model.RegistryUser;
12import clarin.cmdi.componentregistry.persistence.jpa.UserDao;
13
14import java.security.Principal;
15import java.util.ArrayList;
16import java.util.List;
17
18import org.slf4j.Logger;
19import org.slf4j.LoggerFactory;
20import org.springframework.beans.factory.annotation.Autowired;
21import org.springframework.dao.DataAccessException;
22
23/**
24 * Implementation of ComponentRegistryFactory that uses the
25 * ComponentRegistryDbImpl implementation of ComponentRegistry for accessing the
26 * registry
27 *
28 * @author Twan Goosen <twan.goosen@mpi.nl>
29 * @author George.Georgovassilis@mpi.nl
30 */
31public class ComponentRegistryFactoryDbImpl implements ComponentRegistryFactory {
32
33    private final static Logger LOG = LoggerFactory
34            .getLogger(ComponentRegistryFactoryDbImpl.class);
35    @Autowired
36    private Configuration configuration;
37    @Autowired
38    private ComponentRegistryBeanFactory componentRegistryBeanFactory;
39    @Autowired
40    private UserDao userDao;
41    private ComponentRegistry publicComponentRegistry = null;
42
43    @Override
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(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;
88    }
89
90    private ComponentRegistry getPrivateRegistry(Owner owner,
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        }
116    }
117
118    @Override
119    public ComponentRegistry getOtherUserComponentRegistry(
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        }
148    }
149
150    @Override
151    public ComponentRegistry getPublicRegistry() {
152        if (publicComponentRegistry == null) {
153            publicComponentRegistry = this.getNewComponentRegistryForUser(null);
154        }
155        return publicComponentRegistry;
156    }
157
158    private ComponentRegistry getNewComponentRegistryForUser(Number userId) {
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;
181    }
182
183    @Override
184    public RegistryUser getOrCreateUser(UserCredentials credentials) {
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;
191    }
192
193    private synchronized RegistryUser getOrCreateUser(String principalName,
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;
210    }
211}
Note: See TracBrowser for help on using the repository browser.