1 | |
package org.jtheque.primary.services.impl; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
import org.jtheque.core.managers.Managers; |
20 | |
import org.jtheque.core.managers.language.ILanguageManager; |
21 | |
import org.jtheque.core.managers.persistence.able.DataListener; |
22 | |
import org.jtheque.primary.PrimaryUtils; |
23 | |
import org.jtheque.primary.dao.able.IDaoCollections; |
24 | |
import org.jtheque.primary.od.able.Collection; |
25 | |
import org.jtheque.primary.services.able.ICollectionsService; |
26 | |
import org.jtheque.utils.StringUtils; |
27 | |
import org.jtheque.utils.io.FileUtils; |
28 | |
import org.springframework.transaction.annotation.Transactional; |
29 | |
|
30 | |
import javax.annotation.Resource; |
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | 0 | public final class CollectionsService implements ICollectionsService { |
38 | |
@Resource |
39 | |
private IDaoCollections daoCollections; |
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
private Collection getEmptyCollection(){ |
47 | 0 | Collection emptyCollection = daoCollections.createCollection(); |
48 | |
|
49 | 0 | emptyCollection.setTitle(Managers.getManager(ILanguageManager.class).getMessage("generic.view.actions.new")); |
50 | 0 | emptyCollection.setProtection(false); |
51 | 0 | emptyCollection.setPassword(""); |
52 | |
|
53 | 0 | return emptyCollection; |
54 | |
} |
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
private boolean isLoginIncorrect(String title, String password){ |
65 | 0 | Collection collection = daoCollections.getCollection(title); |
66 | |
|
67 | 0 | if (collection == null){ |
68 | 0 | return true; |
69 | |
} |
70 | |
|
71 | 0 | if (collection.isProtection()){ |
72 | 0 | String encrypted = FileUtils.encryptKey(password); |
73 | |
|
74 | 0 | if (!encrypted.equals(collection.getPassword())){ |
75 | 0 | return true; |
76 | |
} |
77 | |
} |
78 | |
|
79 | 0 | return false; |
80 | |
} |
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
@Transactional |
89 | |
private void createCollection(String title, String password){ |
90 | 0 | Collection collection = getEmptyCollection(); |
91 | |
|
92 | 0 | collection.setPrimaryImpl(PrimaryUtils.getPrimaryImpl()); |
93 | 0 | collection.setTitle(title); |
94 | |
|
95 | 0 | if (StringUtils.isEmpty(password)){ |
96 | 0 | collection.setProtection(false); |
97 | |
} else { |
98 | 0 | collection.setProtection(true); |
99 | 0 | collection.setPassword(FileUtils.encryptKey(password)); |
100 | |
} |
101 | |
|
102 | 0 | daoCollections.create(collection); |
103 | 0 | } |
104 | |
|
105 | |
@Override |
106 | |
public void createCollectionAndUse(String title, String password){ |
107 | 0 | createCollection(title, password); |
108 | 0 | daoCollections.setCurrentCollection(daoCollections.getCollection(title)); |
109 | 0 | } |
110 | |
|
111 | |
@Override |
112 | |
public boolean login(String title, String password){ |
113 | 0 | if (isLoginIncorrect(title, password)){ |
114 | 0 | return false; |
115 | |
} |
116 | |
|
117 | 0 | daoCollections.setCurrentCollection(daoCollections.getCollection(title)); |
118 | |
|
119 | 0 | return true; |
120 | |
} |
121 | |
|
122 | |
@Override |
123 | |
public java.util.Collection<Collection> getDatas(){ |
124 | 0 | return daoCollections.getCollections(); |
125 | |
} |
126 | |
|
127 | |
@Override |
128 | |
public void addDataListener(DataListener listener){ |
129 | 0 | daoCollections.addDataListener(listener); |
130 | 0 | } |
131 | |
|
132 | |
@Override |
133 | |
@Transactional |
134 | |
public void clearAll(){ |
135 | 0 | daoCollections.clearAll(); |
136 | 0 | } |
137 | |
|
138 | |
@Override |
139 | |
public String getDataType(){ |
140 | 0 | return DATA_TYPE; |
141 | |
} |
142 | |
} |