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

Last change on this file since 4098 was 4098, checked in by George.Georgovassilis@mpi.nl, 10 years ago

#360, #431, #432: JPA and unified component entities

File size: 5.9 KB
Line 
1package clarin.cmdi.componentregistry.impl.database;
2
3import clarin.cmdi.componentregistry.ComponentRegistry;
4import clarin.cmdi.componentregistry.ComponentRegistryFactory;
5import clarin.cmdi.componentregistry.ComponentStatus;
6import clarin.cmdi.componentregistry.Configuration;
7import clarin.cmdi.componentregistry.Owner;
8import clarin.cmdi.componentregistry.OwnerUser;
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(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        }
73    }
74
75    private ComponentRegistry getPrivateRegistry(Owner owner,
76            UserCredentials credentials) throws IllegalArgumentException,
77            DataAccessException, UserUnauthorizedException {
78        if (owner == null || owner instanceof OwnerUser) {
79            RegistryUser user = getOrCreateUser(credentials);
80            if (user != null) {
81                if (owner != null && !user.getId().equals(owner.getId())) {
82                    throw new UserUnauthorizedException(
83                            "User cannot access other user's private registry");
84                }
85
86                try {
87                    return getNewComponentRegistryForUser(user.getId());
88                } catch (DataAccessException ex) {
89                    LOG.error("Could not retrieve or create user", ex);
90                    throw ex;
91                }
92            } else {
93                throw new UserUnauthorizedException(
94                        "No user credentials available cannot load userspace.");
95            }
96        } else {
97            // TODO: Support group owners
98            throw new UnsupportedOperationException(
99                    "Group owners not supported");
100        }
101    }
102
103    @Override
104    public ComponentRegistry getOtherUserComponentRegistry(
105            Principal adminPrincipal, ComponentStatus status, Owner owner) {
106        try {
107            RegistryUser user;
108            if (owner instanceof OwnerUser) {
109                user = userDao.findOne(owner.getId().longValue());
110            } else {
111                // TODO: Implement for groups
112                throw new UnsupportedOperationException(
113                        "Groups not implemented yet");
114            }
115            ComponentRegistry result = null;
116            if (user != null) {
117                if (configuration.isAdminUser(adminPrincipal)) {
118                    result = getNewComponentRegistryForUser(user.getId());
119                } else {
120                    LOG.info("{} not found in list of {}",
121                            adminPrincipal.getName(),
122                            configuration.getAdminUsersArray().length);
123                    throw new IllegalArgumentException("User "
124                            + adminPrincipal.getName()
125                            + " is not admin user cannot load userspace.");
126                }
127            }
128            return result;
129        } catch (DataAccessException ex) {
130            LOG.error("Could not retrieve user by id", ex);
131            throw ex;
132        }
133    }
134
135    @Override
136    public ComponentRegistry getPublicRegistry() {
137        if (publicComponentRegistry == null) {
138            publicComponentRegistry = getNewComponentRegistryForUser(null);
139        }
140        return publicComponentRegistry;
141    }
142
143    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;
153    }
154
155    @Override
156    public RegistryUser getOrCreateUser(UserCredentials credentials) {
157        if (credentials != null
158                && !ANONYMOUS_USER.equals(credentials.getPrincipalName())) {
159            String principalName = credentials.getPrincipalName();
160            return getOrCreateUser(principalName, credentials.getDisplayName());
161        }
162        return null;
163    }
164
165    private synchronized RegistryUser getOrCreateUser(String principalName,
166            String displayName) {
167        // Try getting it from db
168        RegistryUser user = userDao.getByPrincipalName(principalName);
169        if (user == null) {
170            LOG.info(
171                    "Request to create user with principal name {} and display name {}",
172                    new Object[] { principalName, displayName });
173            // Create the new user
174            user = new RegistryUser();
175            user.setPrincipalName(principalName);
176            user.setName(displayName);
177            userDao.saveAndFlush(user);
178            // Retrieve from db
179            user = userDao.getByPrincipalName(principalName);
180        }
181        return user;
182    }
183}
Note: See TracBrowser for help on using the repository browser.