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

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

Fixed issued arising after testing on localhost tomcat (not fully)

File size: 8.3 KB
RevLine 
[1289]1package clarin.cmdi.componentregistry.impl.database;
2
[1993]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;
[5549]8import clarin.cmdi.componentregistry.RegistrySpace;
[1993]9import clarin.cmdi.componentregistry.UserCredentials;
10import clarin.cmdi.componentregistry.UserUnauthorizedException;
11import clarin.cmdi.componentregistry.model.RegistryUser;
[4098]12import clarin.cmdi.componentregistry.persistence.jpa.UserDao;
[3451]13
[1289]14import java.security.Principal;
[1322]15import java.util.ArrayList;
[1289]16import java.util.List;
[3451]17
[1329]18import org.slf4j.Logger;
19import org.slf4j.LoggerFactory;
[1291]20import org.springframework.beans.factory.annotation.Autowired;
[1329]21import org.springframework.dao.DataAccessException;
[1289]22
23/**
[1357]24 * Implementation of ComponentRegistryFactory that uses the
25 * ComponentRegistryDbImpl implementation of ComponentRegistry for accessing the
[1293]26 * registry
[5549]27 *
[1289]28 * @author Twan Goosen <twan.goosen@mpi.nl>
[3449]29 * @author George.Georgovassilis@mpi.nl
[1289]30 */
31public class ComponentRegistryFactoryDbImpl implements ComponentRegistryFactory {
[1324]32
[3449]33    private final static Logger LOG = LoggerFactory
[5549]34            .getLogger(ComponentRegistryFactoryDbImpl.class);
[1291]35    @Autowired
[1329]36    private Configuration configuration;
37    @Autowired
[1322]38    private ComponentRegistryBeanFactory componentRegistryBeanFactory;
39    @Autowired
[3451]40    private UserDao userDao;
[1360]41    private ComponentRegistry publicComponentRegistry = null;
[1293]42
[1289]43    @Override
44    public List<ComponentRegistry> getAllUserRegistries() {
[5549]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        }
[1289]57    }
58
59    @Override
[5549]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        }
[1993]76    }
77
[5549]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
[5551]84        cr.setRegistrySpace(RegistrySpace.GROUP);
[5549]85        cr.setRegistryOwner(owner);
86        cr.setGroupId(groupId);
87        return cr;
88    }
89
[3449]90    private ComponentRegistry getPrivateRegistry(Owner owner,
[5549]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                }
[1993]100
[5549]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        }
[1289]116    }
117
118    @Override
[3449]119    public ComponentRegistry getOtherUserComponentRegistry(
[5549]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        }
[1289]148    }
149
150    @Override
151    public ComponentRegistry getPublicRegistry() {
[5549]152        if (publicComponentRegistry == null) {
153            publicComponentRegistry = this.getNewComponentRegistryForUser(null);
154        }
155        return publicComponentRegistry;
[1289]156    }
157
[1360]158    private ComponentRegistry getNewComponentRegistryForUser(Number userId) {
[5549]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;
[1293]167    }
[1324]168
[1698]169    @Override
[5549]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
[1698]184    public RegistryUser getOrCreateUser(UserCredentials credentials) {
[5549]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;
[1698]191    }
192
[3449]193    private synchronized RegistryUser getOrCreateUser(String principalName,
[5549]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;
[1324]210    }
[1289]211}
Note: See TracBrowser for help on using the repository browser.