1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package spcp7.imagegallery.impl.alfresco;
21
22 import java.rmi.RemoteException;
23 import java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.Comparator;
26 import java.util.List;
27 import java.util.Map;
28
29 import org.alfresco.webservice.authentication.AuthenticationFault;
30 import org.alfresco.webservice.repository.QueryResult;
31 import org.alfresco.webservice.repository.RepositoryFault;
32 import org.alfresco.webservice.repository.RepositoryServiceSoapBindingStub;
33 import org.alfresco.webservice.repository.UpdateResult;
34 import org.alfresco.webservice.types.CML;
35 import org.alfresco.webservice.types.CMLCreate;
36 import org.alfresco.webservice.types.CMLDelete;
37 import org.alfresco.webservice.types.NamedValue;
38 import org.alfresco.webservice.types.Node;
39 import org.alfresco.webservice.types.ParentReference;
40 import org.alfresco.webservice.types.Predicate;
41 import org.alfresco.webservice.types.Query;
42 import org.alfresco.webservice.types.Reference;
43 import org.alfresco.webservice.types.ResultSet;
44 import org.alfresco.webservice.types.ResultSetRow;
45 import org.alfresco.webservice.types.Store;
46 import org.alfresco.webservice.util.AuthenticationUtils;
47 import org.alfresco.webservice.util.Constants;
48 import org.alfresco.webservice.util.Utils;
49 import org.alfresco.webservice.util.WebServiceFactory;
50 import org.apache.commons.logging.Log;
51 import org.apache.commons.logging.LogFactory;
52 import org.springframework.beans.BeansException;
53 import org.springframework.context.ApplicationContext;
54 import org.springframework.context.ApplicationContextAware;
55
56 import spcp7.imagegallery.abstractionlayer.enums.DefaultProperties;
57 import spcp7.imagegallery.abstractionlayer.exception.ContentCouldNotBeAccessedException;
58 import spcp7.imagegallery.abstractionlayer.exception.ContentProviderAuthenticationException;
59 import spcp7.imagegallery.abstractionlayer.exception.NoContentFoundException;
60 import spcp7.imagegallery.abstractionlayer.exception.PropertyValidationException;
61 import spcp7.imagegallery.abstractionlayer.exception.RangeNotValidException;
62 import spcp7.imagegallery.abstractionlayer.face.ContentFace;
63 import spcp7.imagegallery.abstractionlayer.face.ImageFilterFace;
64 import spcp7.imagegallery.abstractionlayer.face.persistence.PropertyModelFace;
65 import spcp7.imagegallery.abstractionlayer.util.GeneralValidation;
66 import spcp7.imagegallery.abstractionlayer.util.StandardUtils;
67 import spcp7.imagegallery.contentprovider.face.ContentRetrievalFace;
68 import spcp7.imagegallery.contentprovider.face.ContentSubmitterFace;
69 import spcp7.imagegallery.impl.alfresco.utils.AlfrescoDefaultProperties;
70 import spcp7.imagegallery.impl.alfresco.utils.AlfrescoUtils;
71 import spcp7.imagegallery.impl.alfresco.utils.AlfrescoValidation;
72
73
74
75
76
77
78
79 public class AlfrescoContentRetrievalImpl implements ContentRetrievalFace,
80 ApplicationContextAware {
81 private Log log = LogFactory.getLog(AlfrescoContentRetrievalImpl.class);
82
83 private RepositoryServiceSoapBindingStub repositoryService;
84 private Comparator<ContentFace> contentComparator;
85 private ImageFilterFace imageFilterFace;
86 private AlfrescoValidation alfrescoValidation;
87
88 private AlfrescoUtils alfrescoUtils;
89
90 private GeneralValidation generalValidation;
91
92 private ApplicationContext applicationContext;
93
94
95
96
97
98
99 public AlfrescoContentRetrievalImpl() {
100 }
101
102
103
104
105
106
107 public void close() {
108
109 AuthenticationUtils.endSession();
110 }
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130 public synchronized String createFolder(String name,
131 Map<String, PropertyModelFace> properties)
132 throws ContentCouldNotBeAccessedException {
133
134 Store store = new Store(properties.get(
135 AlfrescoDefaultProperties.ALFRESCO_WORKSPACE_STORE.toString())
136 .getWert(), properties.get(
137 AlfrescoDefaultProperties.ALFRESCO_SPACES_STORE.toString())
138 .getWert());
139 String basepath = properties.get(
140 DefaultProperties.SPCP7_CURRENT_PATH.toString()).getWert();
141 String folderToCreateQName = Constants.createQNameString(
142 Constants.NAMESPACE_CONTENT_MODEL, name);
143 Reference folderToCreateReference = new Reference(store, null,
144 this.alfrescoUtils.normalizePathNamespace(basepath
145 + "/"
146 + Constants.createQNameString(
147 Constants.NAMESPACE_CONTENT_MODEL, name),
148 properties));
149
150 try {
151 @SuppressWarnings("unused")
152 Node[] node = WebServiceFactory.getRepositoryService().get(
153 new Predicate(new Reference[] { folderToCreateReference },
154 store, null));
155 } catch (Exception e1) {
156
157 UpdateResult[] results = null;
158
159 ParentReference parentReference = new ParentReference(store, null,
160 basepath, Constants.ASSOC_CONTAINS, folderToCreateQName);
161
162
163 NamedValue[] folderProperties = new NamedValue[] { Utils
164 .createNamedValue(Constants.PROP_NAME, name) };
165 CMLCreate create = new CMLCreate("1", parentReference, null, null,
166 null, Constants.TYPE_FOLDER, folderProperties);
167 CML cml = new CML();
168 cml.setCreate(new CMLCreate[] { create });
169 try {
170 results = this.repositoryService.update(cml);
171 return results[0].getDestination().getUuid();
172 } catch (RepositoryFault e) {
173
174
175 e.printStackTrace();
176 throw new ContentCouldNotBeAccessedException(
177 "The repository threw an exception when trying to create a folder!",
178 e);
179 } catch (RemoteException e) {
180 throw new ContentCouldNotBeAccessedException(
181 "The repository could not be reached when trying to create a folder!",
182 e);
183 }
184 }
185 throw new ContentCouldNotBeAccessedException(
186 "The folder which should be created already exists!");
187 }
188
189
190
191
192
193
194
195
196
197
198
199
200
201 public synchronized ContentFace getParent(String proprietaryId,
202 Map<String, PropertyModelFace> properties)
203 throws ContentCouldNotBeAccessedException, NoContentFoundException {
204 List<ContentFace> retList = null;
205 if (proprietaryId != null) {
206 String queryString = "+ISNODE:T +ID:\""
207 + properties.get(
208 AlfrescoDefaultProperties.ALFRESCO_WORKSPACE_STORE
209 .toString()).getWert()
210 + "://"
211 + properties.get(
212 AlfrescoDefaultProperties.ALFRESCO_SPACES_STORE
213 .toString()).getWert() + "/"
214 + proprietaryId + "\"";
215 Store store = new Store(properties.get(
216 AlfrescoDefaultProperties.ALFRESCO_WORKSPACE_STORE
217 .toString()).getWert(), properties.get(
218 AlfrescoDefaultProperties.ALFRESCO_SPACES_STORE.toString())
219 .getWert());
220 ResultSet resultSet = executeQuery(queryString, properties);
221 ResultSetRow[] rows = resultSet.getRows();
222 if (rows.length > 0) {
223
224
225 String firstResultId = rows[0].getNode().getId();
226 Reference reference = new Reference(store, firstResultId, null);
227
228
229 QueryResult parentQueryResult = null;
230 try {
231 parentQueryResult = this.repositoryService
232 .queryParents(reference);
233 } catch (RepositoryFault e) {
234 throw new ContentCouldNotBeAccessedException(
235 "The repository threw an exception when trying to get the parent!",
236 e);
237 } catch (RemoteException e) {
238 throw new ContentCouldNotBeAccessedException(
239 "The repository could not be reached when trying to get the parent!!",
240 e);
241 }
242 if (parentQueryResult != null) {
243 retList = transformResultSet(properties, parentQueryResult
244 .getResultSet(), 1, Integer.valueOf(properties.get(
245 AlfrescoDefaultProperties.ALFRESCO_MAX_RESULT_ROWS
246 .toString()).getWert()));
247 }
248 if (retList != null && retList.size() > 0) {
249 return retList.get(0);
250 }
251 } else {
252 throw new ContentCouldNotBeAccessedException(
253 "The specified parent could not be found");
254 }
255 }
256 return null;
257 }
258
259
260
261
262
263
264
265
266
267
268 public synchronized List<ContentFace> getVolumes(
269 Map<String, PropertyModelFace> properties, boolean isFolder)
270 throws PropertyValidationException,
271 ContentCouldNotBeAccessedException, NoContentFoundException {
272 List<ContentFace> retList = null;
273 try {
274 retList = getVolumesFromTo(1, Integer.valueOf(properties.get(
275 AlfrescoDefaultProperties.ALFRESCO_MAX_RESULT_ROWS
276 .toString()).getWert()), properties, isFolder);
277 } catch (NumberFormatException e) {
278 throw new ContentCouldNotBeAccessedException(
279 "Volumes could not be retrieved.", e);
280 } catch (RangeNotValidException e) {
281 throw new ContentCouldNotBeAccessedException(
282 "Volumes could not be retrieved.", e);
283 }
284 if (retList == null) {
285 throw new ContentCouldNotBeAccessedException(
286 "Volumes could not be retrieved.");
287 }
288 return retList;
289 }
290
291
292
293
294
295
296
297
298
299
300
301 public synchronized List<ContentFace> getVolumesFromTo(int lowerLimit,
302 int upperLimit, Map<String, PropertyModelFace> properties,
303 boolean isFolder) throws RangeNotValidException,
304 PropertyValidationException, ContentCouldNotBeAccessedException,
305 NoContentFoundException {
306 List<ContentFace> retList = null;
307 this.generalValidation.validateRange(lowerLimit, upperLimit);
308
309 this.alfrescoValidation.validateAlfrescoQueryProperties(properties);
310
311
312
313 while (properties.get(DefaultProperties.SPCP7_CURRENT_PATH.toString())
314 .getWert().endsWith("/")) {
315 PropertyModelFace prop = properties
316 .get(DefaultProperties.SPCP7_CURRENT_PATH.toString());
317 prop.setWert(prop.getWert().substring(0,
318 prop.getWert().length() - 1));
319 properties.put(DefaultProperties.SPCP7_CURRENT_PATH.toString(),
320 prop);
321 }
322 String encodedPath = this.alfrescoUtils
323 .encodeAlfrescoAbbreviatedPathISO9075(properties.get(
324 DefaultProperties.SPCP7_CURRENT_PATH.toString())
325 .getWert());
326 String queryString = "+PATH:\"" + encodedPath;
327 if (isFolder) {
328
329 queryString += "/*\"";
330 } else {
331
332 queryString += "\"";
333 }
334 ResultSet resultSet = executeQuery(queryString, properties);
335 retList = transformResultSet(properties, resultSet, lowerLimit,
336 upperLimit);
337 if (retList != null && this.contentComparator != null) {
338 Collections.sort(retList, this.contentComparator);
339 }
340 return retList;
341 }
342
343
344
345
346
347
348
349
350
351
352 public void open(Map<String, PropertyModelFace> properties)
353 throws PropertyValidationException,
354 ContentProviderAuthenticationException {
355 if (!properties.containsKey(AlfrescoDefaultProperties.ALFRESCO_USERNAME
356 .toString())) {
357 throw new PropertyValidationException("Property "
358 + AlfrescoDefaultProperties.ALFRESCO_USERNAME.toString()
359 + " not found", AlfrescoDefaultProperties.ALFRESCO_USERNAME
360 .toString(), "not set");
361
362 }
363 if (!properties.containsKey(AlfrescoDefaultProperties.ALFRESCO_PASSWORD
364 .toString())) {
365 throw new PropertyValidationException("Property "
366 + AlfrescoDefaultProperties.ALFRESCO_PASSWORD.toString()
367 + " not found", AlfrescoDefaultProperties.ALFRESCO_PASSWORD
368 .toString(), "not set");
369
370 }
371
372 try {
373 AuthenticationUtils.startSession(properties.get(
374 AlfrescoDefaultProperties.ALFRESCO_USERNAME.toString())
375 .getWert(), properties.get(
376 AlfrescoDefaultProperties.ALFRESCO_PASSWORD.toString())
377 .getWert());
378 } catch (AuthenticationFault e) {
379 throw new ContentProviderAuthenticationException(
380 "Authentication with the content provider failed! Maybe you provided wrong credentials. If so"
381 + " change property \""
382 + AlfrescoDefaultProperties.ALFRESCO_USERNAME
383 .toString()
384 + "\" and \""
385 + AlfrescoDefaultProperties.ALFRESCO_USERNAME
386 .toString() + "\" to correct values.", e,
387 AlfrescoContentProviderImpl.contentProviderTypeID);
388 }
389
390 this.repositoryService = WebServiceFactory.getRepositoryService();
391 }
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410 public synchronized void removeFolder(
411 Map<String, PropertyModelFace> properties)
412 throws ContentCouldNotBeAccessedException {
413 if (properties == null) {
414 throw new ContentCouldNotBeAccessedException(
415 "ProprietaryId and/or supplied properties must not be null!");
416 }
417 Store store = new Store(properties.get(
418 AlfrescoDefaultProperties.ALFRESCO_WORKSPACE_STORE.toString())
419 .getWert(), properties.get(
420 AlfrescoDefaultProperties.ALFRESCO_SPACES_STORE.toString())
421 .getWert());
422 PropertyModelFace currentPath = properties
423 .get(DefaultProperties.SPCP7_CURRENT_PATH.toString());
424 Reference folderToDeleteReference = new Reference(store, null,
425 currentPath.getWert());
426
427 Node[] node = null;
428 try {
429 node = WebServiceFactory.getRepositoryService().get(
430 new Predicate(new Reference[] { folderToDeleteReference },
431 store, null));
432 } catch (Exception e1) {
433
434
435 log.debug("We do nothing because the folder does not exist");
436 }
437 if (node != null) {
438
439 Predicate predicate = new Predicate();
440 predicate.setStore(store);
441
442
443
444 predicate.setNodes(new Reference[] { folderToDeleteReference });
445
446
447 CML cml = new CML();
448 CMLDelete cmldelete = new CMLDelete();
449 cmldelete.setWhere(predicate);
450 cml.setDelete(new CMLDelete[] { cmldelete });
451
452 try {
453 this.repositoryService.update(cml);
454 } catch (RepositoryFault e) {
455
456
457 e.printStackTrace();
458 throw new ContentCouldNotBeAccessedException(
459 "The repository threw an exception when trying to delete a folder!",
460 e);
461 } catch (RemoteException e) {
462 throw new ContentCouldNotBeAccessedException(
463 "The repository could not be reached when trying to delete a folder!",
464 e);
465 }
466 }
467
468 }
469
470
471
472
473
474
475 public void setApplicationContext(ApplicationContext arg0)
476 throws BeansException {
477 this.applicationContext = arg0;
478 }
479
480
481
482
483
484
485
486 public void setContentComparator(Comparator<ContentFace> contentComparator) {
487 this.contentComparator = contentComparator;
488 }
489
490
491
492
493
494
495
496 public void setImageFilter(ImageFilterFace imageFilterFace) {
497 this.imageFilterFace = imageFilterFace;
498 }
499
500 private ResultSet executeQuery(String queryString,
501 Map<String, PropertyModelFace> properties)
502 throws ContentCouldNotBeAccessedException, NoContentFoundException {
503 ResultSet resultSet = null;
504 Store store = new Store(properties.get(
505 AlfrescoDefaultProperties.ALFRESCO_WORKSPACE_STORE.toString())
506 .getWert(), properties.get(
507 AlfrescoDefaultProperties.ALFRESCO_SPACES_STORE.toString())
508 .getWert());
509
510 log.debug("getting volumes from query \"" + queryString + "\"");
511 Query query = new Query(Constants.QUERY_LANG_LUCENE, queryString);
512
513 QueryResult queryResult;
514 try {
515 queryResult = this.repositoryService.query(store, query, false);
516
517 resultSet = queryResult.getResultSet();
518 } catch (RepositoryFault e) {
519 throw new ContentCouldNotBeAccessedException(
520 "The repository threw an Exception when trying to execute a query!",
521 e);
522 } catch (RemoteException e) {
523 throw new ContentCouldNotBeAccessedException(
524 "The repository could not be reached when trying to execute a query!",
525 e);
526 }
527 if (resultSet.getRows() == null) {
528 throw new NoContentFoundException(
529 "No volumes at given path found!", queryString);
530 }
531 return resultSet;
532 }
533
534 private List<ContentFace> transformResultSet(
535 Map<String, PropertyModelFace> properties, ResultSet resultSet,
536 int lowerLimit, int upperLimit) {
537 List<ContentFace> retList = new ArrayList<ContentFace>();
538 ResultSetRow[] rows = resultSet.getRows();
539 if (rows == null) {
540
541
542 log.warn("No volumes for query found!");
543 } else {
544 if (rows.length < upperLimit) {
545 upperLimit = rows.length;
546 }
547 if (rows.length < lowerLimit) {
548 lowerLimit = rows.length;
549 }
550
551
552 for (int x = lowerLimit - 1; x < upperLimit; x++) {
553 ResultSetRow row = rows[x];
554 NamedValue[] columns = row.getColumns();
555 ContentFace cf = (ContentFace) this.applicationContext
556 .getBean("ContentDTO");
557 cf.setDirectory(true);
558 for (int y = 0; y < columns.length; y++) {
559 if (row.getColumns(y).getName().endsWith("node-uuid")) {
560 cf.setProprietaryId(row.getColumns(y).getValue());
561 } else if (row.getColumns(y).getName().endsWith("name")) {
562 cf.setName(row.getColumns(y).getValue());
563 } else if (row.getColumns(y).getName().endsWith("path")) {
564 cf.setPath(this.alfrescoUtils.normalizePathNamespace(
565 row.getColumns(y).getValue(), properties));
566 } else if (row.getColumns(y).getName().endsWith("content")) {
567 cf.setDirectory(false);
568 if (this.imageFilterFace != null) {
569 if (this.imageFilterFace.acceptImageMimetype(row
570 .getColumns(y).getValue())) {
571 cf.setImage(true);
572 } else {
573
574 if (this.imageFilterFace.accept(cf.getName())) {
575 cf.setImage(true);
576 }
577 }
578 }
579 }
580 cf.setParentContentFolderProperties(StandardUtils
581 .deepCopy(properties));
582 cf
583 .setContentProviderTypeId(AlfrescoContentProviderImpl.contentProviderTypeID);
584 }
585 cf.setUrl(properties.get(
586 AlfrescoDefaultProperties.ALFRESCO_BASE_URL.toString())
587 .getWert()
588 + "/" + cf.getProprietaryId() + "/" + cf.getName());
589 String[] splitName = cf.getName().split("\\.");
590 cf.setFileExtension(splitName[splitName.length - 1]);
591 retList.add(cf);
592
593 }
594 }
595 return retList;
596 }
597
598
599
600
601
602
603
604 public void setAlfrescoValidation(AlfrescoValidation alfrescoValidation) {
605 this.alfrescoValidation = alfrescoValidation;
606 }
607
608
609
610
611
612
613
614 public void setGeneralValidation(GeneralValidation generalValidation) {
615 this.generalValidation = generalValidation;
616 }
617
618
619
620
621
622
623
624 public void setAlfrescoUtils(AlfrescoUtils alfrescoUtils) {
625 this.alfrescoUtils = alfrescoUtils;
626 }
627
628 }