1 | package clarin.cmdi.componentregistry.persistence.impl; |
---|
2 | |
---|
3 | import java.util.ArrayList; |
---|
4 | import java.util.HashMap; |
---|
5 | import java.util.List; |
---|
6 | import java.util.Map; |
---|
7 | |
---|
8 | import org.slf4j.Logger; |
---|
9 | import org.slf4j.LoggerFactory; |
---|
10 | import org.springframework.beans.factory.annotation.Autowired; |
---|
11 | import org.springframework.dao.DataAccessException; |
---|
12 | import org.springframework.stereotype.Repository; |
---|
13 | |
---|
14 | import clarin.cmdi.componentregistry.impl.ComponentUtils; |
---|
15 | import clarin.cmdi.componentregistry.model.BaseDescription; |
---|
16 | import clarin.cmdi.componentregistry.model.ComponentDescription; |
---|
17 | import clarin.cmdi.componentregistry.model.ProfileDescription; |
---|
18 | import clarin.cmdi.componentregistry.model.RegistryUser; |
---|
19 | import clarin.cmdi.componentregistry.persistence.ComponentDao; |
---|
20 | import clarin.cmdi.componentregistry.persistence.jpa.CommentsDao; |
---|
21 | import clarin.cmdi.componentregistry.persistence.jpa.JpaComponentDao; |
---|
22 | import clarin.cmdi.componentregistry.persistence.jpa.UserDao; |
---|
23 | |
---|
24 | /** |
---|
25 | * Base DAO which can be extended to serve {@link ComponentDescription}s and |
---|
26 | * {@link ProfileDescription}s |
---|
27 | * |
---|
28 | * @author Twan Goosen <twan.goosen@mpi.nl> |
---|
29 | * @author George.Georgovassilis@mpi.nl |
---|
30 | */ |
---|
31 | @Repository |
---|
32 | public class ComponentDaoImpl implements ComponentDao { |
---|
33 | |
---|
34 | @Autowired |
---|
35 | private JpaComponentDao jpaComponentDao; |
---|
36 | @Autowired |
---|
37 | private CommentsDao commentsDao; |
---|
38 | @Autowired |
---|
39 | private UserDao userDao; |
---|
40 | private final static Logger LOG = LoggerFactory |
---|
41 | .getLogger(ComponentDaoImpl.class); |
---|
42 | |
---|
43 | /** |
---|
44 | * Copy a set of properties which are allowed to change from "from" to "to" |
---|
45 | * thus making sure that only those properties are updated and not other |
---|
46 | * ones. |
---|
47 | * |
---|
48 | * @param from |
---|
49 | * @param to |
---|
50 | */ |
---|
51 | protected void copyPersistentProperties(BaseDescription from, BaseDescription to) { |
---|
52 | to.setName(from.getName()); |
---|
53 | to.setDescription(from.getDescription()); |
---|
54 | to.setRegistrationDate(from.getRegistrationDate()); |
---|
55 | to.setCreatorName(from.getCreatorName()); |
---|
56 | to.setDomainName(from.getDomainName()); |
---|
57 | to.setGroupName(from.getGroupName()); |
---|
58 | to.setHref(from.getHref()); |
---|
59 | } |
---|
60 | |
---|
61 | protected String getTableName() { |
---|
62 | return "persistentcomponents"; |
---|
63 | } |
---|
64 | |
---|
65 | ; |
---|
66 | |
---|
67 | private boolean compare(String s, Number n) { |
---|
68 | return ("" + s).equals("" + n); |
---|
69 | } |
---|
70 | |
---|
71 | private String toString(Number n) { |
---|
72 | if (n == null) { |
---|
73 | return null; |
---|
74 | } |
---|
75 | return n.toString(); |
---|
76 | } |
---|
77 | |
---|
78 | private BaseDescription castDown(BaseDescription c) { |
---|
79 | // JPA will save only BaseComponent instances, not derived classes |
---|
80 | if (c instanceof ComponentDescription |
---|
81 | || c instanceof ProfileDescription) { |
---|
82 | BaseDescription copy = new BaseDescription(); |
---|
83 | ComponentUtils.copyPropertiesFrom(c, copy); |
---|
84 | return copy; |
---|
85 | } |
---|
86 | return c; |
---|
87 | } |
---|
88 | |
---|
89 | /** |
---|
90 | * Update {@link BaseDescription#getCommentsCount()} (which is a transient |
---|
91 | * property) with the number of comments for that component and load |
---|
92 | * {@link BaseDescription#getContent()} |
---|
93 | * |
---|
94 | * @param baseDescription |
---|
95 | */ |
---|
96 | private void augment(BaseDescription baseDescription) { |
---|
97 | if (baseDescription == null) { |
---|
98 | return; |
---|
99 | } |
---|
100 | int count = (int) commentsDao.findCommentCountForComponent(baseDescription |
---|
101 | .getId()); |
---|
102 | baseDescription.setCommentsCount(count); |
---|
103 | baseDescription.setContent(jpaComponentDao.findContentByComponentId(baseDescription |
---|
104 | .getId())); |
---|
105 | } |
---|
106 | |
---|
107 | /** |
---|
108 | * Update {@link BaseDescription#getCommentsCount() |
---|
109 | * |
---|
110 | * @param baseDescription |
---|
111 | */ |
---|
112 | private List<BaseDescription> augment(List<BaseDescription> baseDescription) { |
---|
113 | if (baseDescription.isEmpty()) { |
---|
114 | return baseDescription; |
---|
115 | } |
---|
116 | Map<String, BaseDescription> map = new HashMap<String, BaseDescription>(); |
---|
117 | List<String> idlist = new ArrayList<String>(); |
---|
118 | for (BaseDescription c : baseDescription) { |
---|
119 | if (c != null) { |
---|
120 | idlist.add(c.getId()); |
---|
121 | map.put(c.getId(), c); |
---|
122 | } |
---|
123 | } |
---|
124 | List<Object[]> tuples = commentsDao |
---|
125 | .findCommentCountForComponents(idlist); |
---|
126 | for (Object[] tuple : tuples) { |
---|
127 | String id = tuple[0].toString(); |
---|
128 | int count = Integer.parseInt(tuple[1].toString()); |
---|
129 | map.get(id).setCommentsCount(count); |
---|
130 | } |
---|
131 | return baseDescription; |
---|
132 | } |
---|
133 | |
---|
134 | /** |
---|
135 | * |
---|
136 | * @param cmdId CMD id |
---|
137 | * @return Whether the specified item is in the public space |
---|
138 | */ |
---|
139 | @Override |
---|
140 | public boolean isPublic(String cmdId) { |
---|
141 | BaseDescription baseDescription = jpaComponentDao.findByComponentId(cmdId); |
---|
142 | return baseDescription != null && baseDescription.isPublic(); |
---|
143 | } |
---|
144 | |
---|
145 | /** |
---|
146 | * |
---|
147 | * @param cmdId CMD id |
---|
148 | * @param userId User db id of workspace owner |
---|
149 | * @return Whether the specified item is in the specified user's workspace |
---|
150 | */ |
---|
151 | @Override |
---|
152 | public boolean isInUserSpace(String cmdId, Number userId) { |
---|
153 | BaseDescription baseDescription = jpaComponentDao.findByComponentId(cmdId); |
---|
154 | boolean b = baseDescription != null && !baseDescription.isPublic() |
---|
155 | && compare(baseDescription.getUserId(), userId); |
---|
156 | return b; |
---|
157 | } |
---|
158 | |
---|
159 | /** |
---|
160 | * |
---|
161 | * @param cmdId CMD id |
---|
162 | * @param userId User db id of workspace owner, null for public registry |
---|
163 | * @return Whether the specified item is in the specified workspace (user or |
---|
164 | * public) |
---|
165 | */ |
---|
166 | // @Override |
---|
167 | // public boolean isAccessible(String cmdId, Number userId) { |
---|
168 | // if (userId == null) { |
---|
169 | // return this.isPublic(cmdId); |
---|
170 | // } else { |
---|
171 | // return this.isInUserSpace(cmdId, userId); |
---|
172 | // } |
---|
173 | // } |
---|
174 | /** |
---|
175 | * |
---|
176 | * @param cmdId Profile or component Id (not primary key) |
---|
177 | * @return String value of XML content for profile or component |
---|
178 | */ |
---|
179 | @Override |
---|
180 | public String getContent(boolean isDeleted, String cmdId) { |
---|
181 | return jpaComponentDao.findContentByComponentId(cmdId, isDeleted); |
---|
182 | } |
---|
183 | |
---|
184 | /** |
---|
185 | * @param description Description to insert |
---|
186 | * @param content Content to insert and refer to from description |
---|
187 | * @return Id of newly inserted description |
---|
188 | */ |
---|
189 | @Override |
---|
190 | public Number insertDescription(BaseDescription description, String content, |
---|
191 | boolean isPublic, Number userId) { |
---|
192 | |
---|
193 | if (description.getId() != null |
---|
194 | && jpaComponentDao.findByComponentId(description.getId()) != null) { |
---|
195 | throw new IllegalArgumentException("Component " |
---|
196 | + description.getId() + " already in DB"); |
---|
197 | } |
---|
198 | if (content == null) { |
---|
199 | content = ""; |
---|
200 | } |
---|
201 | BaseDescription copy = castDown(description); |
---|
202 | copy.setContent(content); |
---|
203 | copy.setPublic(isPublic); |
---|
204 | copy.setUserId(toString(userId)); |
---|
205 | copy.setCreatorName(description.getCreatorName()); |
---|
206 | copy = jpaComponentDao.saveAndFlush(copy); |
---|
207 | jpaComponentDao.updateContent(copy.getId(), content); |
---|
208 | return copy.getDbId(); |
---|
209 | } |
---|
210 | |
---|
211 | /** |
---|
212 | * Updates a description by database id |
---|
213 | * |
---|
214 | * @param id Id (key) of description record |
---|
215 | * @param description New values for description (leave null to not change) |
---|
216 | * @param content New content for description (leave null to not change) |
---|
217 | */ |
---|
218 | @Override |
---|
219 | public void updateDescription(Number id, BaseDescription description, |
---|
220 | String content) { |
---|
221 | if (content == null) { |
---|
222 | content = ""; |
---|
223 | } |
---|
224 | if (description != null) { |
---|
225 | // Update description |
---|
226 | BaseDescription copy = jpaComponentDao.findByComponentId(description |
---|
227 | .getId()); |
---|
228 | copyPersistentProperties(description, copy); |
---|
229 | jpaComponentDao.saveAndFlush(copy); |
---|
230 | jpaComponentDao.updateContent(copy.getId(), content); |
---|
231 | } |
---|
232 | |
---|
233 | if (content != null) { |
---|
234 | BaseDescription copy = jpaComponentDao.findOne(id.longValue()); |
---|
235 | copy.setContent(content); |
---|
236 | jpaComponentDao.saveAndFlush(copy); |
---|
237 | jpaComponentDao.updateContent(copy.getId(), content); |
---|
238 | } |
---|
239 | } |
---|
240 | |
---|
241 | /** |
---|
242 | * Retrieves description by it's primary key Id |
---|
243 | * |
---|
244 | * @param id Description key |
---|
245 | * @return The description, if it exists; null otherwise |
---|
246 | */ |
---|
247 | @Override |
---|
248 | public BaseDescription getById(Number id) { |
---|
249 | BaseDescription baseDescription = jpaComponentDao.findOne(id.longValue()); |
---|
250 | if (baseDescription != null && !baseDescription.isDeleted()) { |
---|
251 | augment(baseDescription); |
---|
252 | return baseDescription; |
---|
253 | } |
---|
254 | return null; |
---|
255 | } |
---|
256 | |
---|
257 | /** |
---|
258 | * Get by ComponentId / ProfileId, whether in userspace or public |
---|
259 | * |
---|
260 | * @param id Full component id |
---|
261 | * @return The description, if it exists; null otherwise |
---|
262 | */ |
---|
263 | @Override |
---|
264 | public BaseDescription getByCmdId(String id) { |
---|
265 | BaseDescription baseDescription = jpaComponentDao.findByComponentId(id); |
---|
266 | if (baseDescription != null && !baseDescription.isDeleted()) { |
---|
267 | augment(baseDescription); |
---|
268 | return baseDescription; |
---|
269 | } |
---|
270 | return null; |
---|
271 | } |
---|
272 | |
---|
273 | /** |
---|
274 | * |
---|
275 | * @param cmdId CMD Id of description |
---|
276 | * @return Database id for description record |
---|
277 | */ |
---|
278 | @Override |
---|
279 | public Number getDbId(String cmdId) { |
---|
280 | Number id = null; |
---|
281 | BaseDescription c = jpaComponentDao.findByComponentId(cmdId); |
---|
282 | if (c != null) { |
---|
283 | id = c.getDbId(); |
---|
284 | } |
---|
285 | return id; |
---|
286 | } |
---|
287 | |
---|
288 | /** |
---|
289 | * |
---|
290 | * @return All descriptions in the public space. Won't include xml content |
---|
291 | * and comments count. |
---|
292 | */ |
---|
293 | @Override |
---|
294 | public List<BaseDescription> getPublicDescriptions() { |
---|
295 | return augment(jpaComponentDao.findPublicItems()); |
---|
296 | } |
---|
297 | |
---|
298 | /** |
---|
299 | * @return List of deleted descriptions in user space or in public when |
---|
300 | * userId=null. Won't include xml content and comments count. |
---|
301 | * @param userId |
---|
302 | */ |
---|
303 | @Override |
---|
304 | public List<BaseDescription> getDeletedDescriptions(Number userId) { |
---|
305 | List<BaseDescription> list = null; |
---|
306 | if (userId != null) { |
---|
307 | list = jpaComponentDao.findDeletedItemsForUser(userId.longValue()); |
---|
308 | } else { |
---|
309 | list = jpaComponentDao.findPublicDeletedItems(); |
---|
310 | } |
---|
311 | list = augment(list); |
---|
312 | return list; |
---|
313 | } |
---|
314 | |
---|
315 | /** |
---|
316 | * |
---|
317 | * @return All the user's descriptions not in the public space .Won't |
---|
318 | * include xml content and comments count. |
---|
319 | */ |
---|
320 | @Override |
---|
321 | public List<BaseDescription> getPrivateBaseDescriptions(Number userId, String prefix) { |
---|
322 | return augment(jpaComponentDao.findItemsForUserThatAreNotInGroups(userId.longValue(), prefix + "%")); |
---|
323 | //return augment(jpaComponentDao.findNotPublishedUserItems(userId.longValue(),prefix + "%")); |
---|
324 | } |
---|
325 | |
---|
326 | @Override |
---|
327 | public void setDeleted(BaseDescription desc, boolean isDeleted) { |
---|
328 | BaseDescription copy = jpaComponentDao.findByComponentId(desc.getId()); |
---|
329 | copy.setDeleted(isDeleted); |
---|
330 | jpaComponentDao.saveAndFlush(copy); |
---|
331 | } |
---|
332 | |
---|
333 | @Override |
---|
334 | public void setPublished(Number id, boolean published) { |
---|
335 | BaseDescription copy = jpaComponentDao.findOne(id.longValue()); |
---|
336 | copy.setPublic(published); |
---|
337 | jpaComponentDao.saveAndFlush(copy); |
---|
338 | } |
---|
339 | |
---|
340 | /** |
---|
341 | * |
---|
342 | * @param id Id of description record |
---|
343 | * @return Principal name of description's owner, if any. Otherwise, null. |
---|
344 | */ |
---|
345 | @Override |
---|
346 | public String getOwnerPrincipalName(Number id) { |
---|
347 | BaseDescription baseDescription = getById(id); |
---|
348 | if (baseDescription == null) { |
---|
349 | return null; |
---|
350 | } |
---|
351 | |
---|
352 | long userId = baseDescription.getDbUserId(); |
---|
353 | RegistryUser user = userDao.findOne(userId); |
---|
354 | if (user == null) { |
---|
355 | return null; |
---|
356 | } |
---|
357 | |
---|
358 | return user.getPrincipalName(); |
---|
359 | } |
---|
360 | |
---|
361 | @Override |
---|
362 | public List<BaseDescription> getPublicBaseDescriptions(String prefix) { |
---|
363 | return augment(jpaComponentDao.findPublishedItems(prefix + "%")); |
---|
364 | } |
---|
365 | |
---|
366 | @Override |
---|
367 | public List<String> getAllNonDeletedProfileIds() { |
---|
368 | return jpaComponentDao.findNonDeletedItemIds(ProfileDescription.PROFILE_PREFIX + "%"); |
---|
369 | } |
---|
370 | |
---|
371 | @Override |
---|
372 | public List<String> getAllNonDeletedComponentIds() { |
---|
373 | return jpaComponentDao.findNonDeletedItemIds(ComponentDescription.COMPONENT_PREFIX + "%"); |
---|
374 | } |
---|
375 | |
---|
376 | @Override |
---|
377 | public List<BaseDescription> getAllNonDeletedDescriptions() { |
---|
378 | return jpaComponentDao.findNonDeletedDescriptions(); |
---|
379 | } |
---|
380 | |
---|
381 | // Olha was here |
---|
382 | @Override |
---|
383 | public List<String> getAllItemIdsInGroup(String prefix, Long groupId) { |
---|
384 | // we are ineterested only in non-published components in the group |
---|
385 | return jpaComponentDao.findAllItemIdsInGroup(false, prefix + "%", groupId); |
---|
386 | } |
---|
387 | } |
---|