Rev 1310 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
215 | jmachado | 1 | package pt.estgp.estgweb.services.managedidentifers; |
2 | |||
3 | import pt.estgp.estgweb.domain.*; |
||
1830 | jmachado | 4 | import pt.estgp.estgweb.domain.dao.DaoFactory; |
215 | jmachado | 5 | import pt.estgp.estgweb.domain.views.ManagedIdentifierCollectionView; |
6 | import pt.estgp.estgweb.domain.views.ManagedIdentifierView; |
||
7 | import pt.estgp.estgweb.domain.views.utils.ManagedIdentifierCollectionComparator; |
||
1830 | jmachado | 8 | import pt.utl.ist.berserk.logic.serviceManager.IService; |
215 | jmachado | 9 | |
1830 | jmachado | 10 | import java.util.Collections; |
11 | import java.util.HashSet; |
||
12 | import java.util.List; |
||
13 | import java.util.Set; |
||
215 | jmachado | 14 | |
15 | /** |
||
16 | * @author Jorge Machado |
||
17 | * @date 4/Jun/2008 |
||
18 | * @see pt.estgp.estgweb.services.data |
||
19 | */ |
||
20 | public class ManagedIdentifiersService implements IService |
||
21 | { |
||
22 | |||
23 | |||
24 | public List<ManagedIdentifierCollectionView> loadCollections(UserSession userSession) |
||
25 | { |
||
26 | Set<ManagedIdentifierCollection> managedIdentifierCollections = userSession.getUser().getManagedIdentifierCollectionsManager(); |
||
27 | Set<ManagedIdentifierCollection> ownerIdentifierCollections = userSession.getUser().getManagedIdentifierCollectionsOwner(); |
||
28 | Set<ManagedIdentifierCollection> mergeList = managedIdentifierCollections; |
||
29 | if(mergeList == null) |
||
30 | { |
||
31 | if(ownerIdentifierCollections == null) |
||
32 | return null; |
||
33 | else |
||
34 | { |
||
35 | mergeList = new HashSet<ManagedIdentifierCollection>(); |
||
36 | mergeList.removeAll(ownerIdentifierCollections); |
||
37 | } |
||
38 | } |
||
39 | mergeList.addAll(ownerIdentifierCollections); |
||
40 | |||
41 | List<ManagedIdentifierCollectionView> views = ManagedIdentifierCollectionView.loadViews(managedIdentifierCollections); |
||
42 | 1.5.0/docs/api/java/util/Collections.html">Collections.sort(views, ManagedIdentifierCollectionComparator.getInstance()); |
||
43 | return views; |
||
44 | } |
||
45 | |||
46 | public List<ManagedIdentifierCollectionView> createCollection(ManagedIdentifierCollectionView collection, UserSession userSession) |
||
47 | { |
||
48 | ManagedIdentifierCollection managedIdentifierCollection; |
||
49 | if(collection.getId() > 0) |
||
50 | managedIdentifierCollection = DaoFactory.getManagedIdentifierCollectionDaoImpl().get(collection.getId()); |
||
51 | else |
||
52 | { |
||
53 | managedIdentifierCollection = DomainObjectFactory.createManagedIdentifierCollectionImpl(); |
||
54 | managedIdentifierCollection.setOwner(userSession.getUser()); |
||
55 | DaoFactory.getManagedIdentifierCollectionDaoImpl().save(managedIdentifierCollection); |
||
56 | } |
||
57 | collection.persistViewInObject(managedIdentifierCollection); |
||
58 | return loadCollections(userSession); |
||
59 | } |
||
60 | |||
61 | public ManagedIdentifierCollectionView loadCollection(long collectionId, UserSession userSession) |
||
62 | { |
||
63 | ManagedIdentifierCollection collection = DaoFactory.getManagedIdentifierCollectionDaoImpl().get(collectionId); |
||
64 | return new ManagedIdentifierCollectionView(collection,true); |
||
65 | } |
||
66 | |||
67 | |||
68 | public List<ManagedIdentifierCollectionView> deleteCollection(long collectionId, UserSession userSession) |
||
69 | { |
||
70 | ManagedIdentifierCollection collection = DaoFactory.getManagedIdentifierCollectionDaoImpl().get(collectionId); |
||
71 | DaoFactory.getManagedIdentifierCollectionDaoImpl().delete(collection); |
||
72 | userSession.getUser().getManagedIdentifierCollectionsManager().remove(collection); |
||
73 | userSession.getUser().getManagedIdentifierCollectionsOwner().remove(collection); |
||
74 | return loadCollections(userSession); |
||
75 | } |
||
76 | |||
77 | public ManagedIdentifierCollectionView assignCollectionUser(long userId, long collectionId, UserSession userSession) |
||
78 | { |
||
79 | ManagedIdentifierCollection collection = DaoFactory.getManagedIdentifierCollectionDaoImpl().get(collectionId); |
||
80 | User user = DaoFactory.getUserDaoImpl().get(userId); |
||
81 | if(!collection.getManagers().contains(user)) |
||
82 | { |
||
83 | collection.getManagers().add(user); |
||
84 | } |
||
85 | return new ManagedIdentifierCollectionView(collection,true); |
||
86 | } |
||
87 | |||
88 | public ManagedIdentifierCollectionView deleteCollectionUser(long userId, long collectionId, UserSession userSession) |
||
89 | { |
||
90 | ManagedIdentifierCollection collection = DaoFactory.getManagedIdentifierCollectionDaoImpl().get(collectionId); |
||
91 | User user = DaoFactory.getUserDaoImpl().get(userId); |
||
92 | collection.getManagers().remove(user); |
||
93 | return new ManagedIdentifierCollectionView(collection,true); |
||
94 | } |
||
95 | |||
96 | public ManagedIdentifierView createIdentifier(ManagedIdentifierView managedIdentifierView, long collectionId, UserSession userSession) |
||
97 | { |
||
98 | ManagedIdentifier managedIdentifier; |
||
99 | if(managedIdentifierView.getId() <= 0) |
||
100 | { |
||
101 | managedIdentifier = DomainObjectFactory.createManagedIdentifierImpl(); |
||
102 | ManagedIdentifierCollection managedIdentifierCollection = DaoFactory.getManagedIdentifierCollectionDaoImpl().load(collectionId); |
||
103 | managedIdentifier.setCollection(managedIdentifierCollection); |
||
104 | managedIdentifier.setOwner(userSession.getUser()); |
||
105 | managedIdentifierCollection.setSequenceNumber(managedIdentifierCollection.getSequenceNumber() + 1); |
||
106 | managedIdentifier.setSequenceNumber(managedIdentifierCollection.getSequenceNumber()); |
||
107 | } |
||
108 | else |
||
109 | managedIdentifier = DaoFactory.getManagedIdentifierDaoImpl().get(managedIdentifierView.getId()); |
||
110 | managedIdentifierView.persistViewInObject(managedIdentifier); |
||
111 | DaoFactory.getManagedIdentifierDaoImpl().save(managedIdentifier); |
||
112 | return new ManagedIdentifierView(managedIdentifier,true); |
||
113 | } |
||
114 | |||
115 | public ManagedIdentifierView findIdentifier(long identifier, long collectionId, UserSession userSession) |
||
116 | { |
||
117 | ManagedIdentifier managedIdentifier = DaoFactory.getManagedIdentifierDaoImpl().findBySequence(identifier,collectionId); |
||
118 | if(managedIdentifier != null) |
||
119 | return new ManagedIdentifierView(managedIdentifier,true); |
||
120 | else return null; |
||
121 | } |
||
122 | } |