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