1
14
15 package com.liferay.documentlibrary.util;
16
17 import com.liferay.documentlibrary.DuplicateDirectoryException;
18 import com.liferay.documentlibrary.DuplicateFileException;
19 import com.liferay.documentlibrary.NoSuchDirectoryException;
20 import com.liferay.documentlibrary.NoSuchFileException;
21 import com.liferay.documentlibrary.model.FileModel;
22 import com.liferay.portal.jcr.JCRConstants;
23 import com.liferay.portal.jcr.JCRFactory;
24 import com.liferay.portal.jcr.JCRFactoryUtil;
25 import com.liferay.portal.kernel.exception.PortalException;
26 import com.liferay.portal.kernel.exception.SystemException;
27 import com.liferay.portal.kernel.io.unsync.UnsyncBufferedInputStream;
28 import com.liferay.portal.kernel.log.Log;
29 import com.liferay.portal.kernel.log.LogFactoryUtil;
30 import com.liferay.portal.kernel.search.Document;
31 import com.liferay.portal.kernel.search.Indexer;
32 import com.liferay.portal.kernel.search.IndexerRegistryUtil;
33 import com.liferay.portal.kernel.search.SearchEngineUtil;
34 import com.liferay.portal.kernel.search.SearchException;
35 import com.liferay.portal.kernel.util.GetterUtil;
36 import com.liferay.portal.kernel.util.StringPool;
37 import com.liferay.portal.kernel.util.StringUtil;
38 import com.liferay.portal.kernel.util.Validator;
39 import com.liferay.portal.service.ServiceContext;
40
41 import java.io.InputStream;
42
43 import java.util.ArrayList;
44 import java.util.Calendar;
45 import java.util.Collection;
46 import java.util.Date;
47 import java.util.List;
48
49 import javax.jcr.Node;
50 import javax.jcr.NodeIterator;
51 import javax.jcr.PathNotFoundException;
52 import javax.jcr.Property;
53 import javax.jcr.RepositoryException;
54 import javax.jcr.Session;
55 import javax.jcr.version.Version;
56 import javax.jcr.version.VersionHistory;
57 import javax.jcr.version.VersionIterator;
58
59 import org.apache.commons.lang.StringUtils;
60
61
67 public class JCRHook extends BaseHook {
68
69 public void addDirectory(long companyId, long repositoryId, String dirName)
70 throws PortalException, SystemException {
71
72 Session session = null;
73
74 try {
75 session = JCRFactoryUtil.createSession();
76
77 Node rootNode = getRootNode(session, companyId);
78 Node repositoryNode = getFolderNode(rootNode, repositoryId);
79
80 if (repositoryNode.hasNode(dirName)) {
81 throw new DuplicateDirectoryException(dirName);
82 }
83 else {
84 String[] dirNameArray = StringUtil.split(dirName, "/");
85
86 Node dirNode = repositoryNode;
87
88 for (int i = 0; i < dirNameArray.length; i++) {
89 if (Validator.isNotNull(dirNameArray[i])) {
90 if (dirNode.hasNode(dirNameArray[i])) {
91 dirNode = dirNode.getNode(dirNameArray[i]);
92 }
93 else {
94 dirNode = dirNode.addNode(
95 dirNameArray[i], JCRConstants.NT_FOLDER);
96 }
97 }
98 }
99
100 session.save();
101 }
102 }
103 catch (RepositoryException re) {
104 throw new SystemException(re);
105 }
106 finally {
107 if (session != null) {
108 session.logout();
109 }
110 }
111 }
112
113 public void addFile(
114 long companyId, String portletId, long groupId, long repositoryId,
115 String fileName, long fileEntryId, String properties,
116 Date modifiedDate, ServiceContext serviceContext, InputStream is)
117 throws PortalException, SystemException {
118
119 Session session = null;
120
121 try {
122 session = JCRFactoryUtil.createSession();
123
124 Node rootNode = getRootNode(session, companyId);
125 Node repositoryNode = getFolderNode(rootNode, repositoryId);
126
127 if (repositoryNode.hasNode(fileName)) {
128 throw new DuplicateFileException(fileName);
129 }
130 else {
131 Node fileNode = repositoryNode.addNode(
132 fileName, JCRConstants.NT_FILE);
133
134 Node contentNode = fileNode.addNode(
135 JCRConstants.JCR_CONTENT, JCRConstants.NT_RESOURCE);
136
137 contentNode.addMixin(JCRConstants.MIX_VERSIONABLE);
138 contentNode.setProperty(
139 JCRConstants.JCR_MIME_TYPE, "text/plain");
140 contentNode.setProperty(JCRConstants.JCR_DATA, is);
141 contentNode.setProperty(
142 JCRConstants.JCR_LAST_MODIFIED, Calendar.getInstance());
143
144 session.save();
145
146 Version version = contentNode.checkin();
147
148 contentNode.getVersionHistory().addVersionLabel(
149 version.getName(), DEFAULT_VERSION, false);
150
151 Indexer indexer = IndexerRegistryUtil.getIndexer(
152 FileModel.class);
153
154 FileModel fileModel = new FileModel();
155
156 fileModel.setAssetCategoryIds(
157 serviceContext.getAssetCategoryIds());
158 fileModel.setAssetTagNames(serviceContext.getAssetTagNames());
159 fileModel.setCompanyId(companyId);
160 fileModel.setFileEntryId(fileEntryId);
161 fileModel.setFileName(fileName);
162 fileModel.setGroupId(groupId);
163 fileModel.setModifiedDate(modifiedDate);
164 fileModel.setPortletId(portletId);
165 fileModel.setProperties(properties);
166 fileModel.setRepositoryId(repositoryId);
167
168 indexer.reindex(fileModel);
169 }
170 }
171 catch (RepositoryException re) {
172 throw new SystemException(re);
173 }
174 finally {
175 if (session != null) {
176 session.logout();
177 }
178 }
179 }
180
181 public void checkRoot(long companyId) throws SystemException {
182 Session session = null;
183
184 try {
185 session = JCRFactoryUtil.createSession();
186
187 getRootNode(session, companyId);
188
189 session.save();
190 }
191 catch (RepositoryException re) {
192 throw new SystemException(re);
193 }
194 finally {
195 if (session != null) {
196 session.logout();
197 }
198 }
199 }
200
201 public void deleteDirectory(
202 long companyId, String portletId, long repositoryId, String dirName)
203 throws PortalException {
204
205 Session session = null;
206
207 try {
208 session = JCRFactoryUtil.createSession();
209
210 Node rootNode = getRootNode(session, companyId);
211 Node repositoryNode = getFolderNode(rootNode, repositoryId);
212 Node dirNode = repositoryNode.getNode(dirName);
213
214 deleteDirectory(companyId, portletId, repositoryId, dirNode);
215
216 dirNode.remove();
217
218 session.save();
219 }
220 catch (PathNotFoundException pnfe) {
221 throw new NoSuchDirectoryException(dirName);
222 }
223 catch (RepositoryException re) {
224 String message = GetterUtil.getString(re.getMessage());
225
226 if (message.contains("failed to resolve path")) {
227 throw new NoSuchDirectoryException(dirName);
228 }
229 else {
230 throw new PortalException(re);
231 }
232 }
233 finally {
234 if (session != null) {
235 session.logout();
236 }
237 }
238 }
239
240 public void deleteFile(
241 long companyId, String portletId, long repositoryId,
242 String fileName)
243 throws PortalException, SystemException {
244
245 Session session = null;
246
247
250
252 try {
253 session = JCRFactoryUtil.createSession();
254
255 Node rootNode = getRootNode(session, companyId);
256 Node repositoryNode = getFolderNode(rootNode, repositoryId);
257 Node fileNode = repositoryNode.getNode(fileName);
258 Node contentNode = fileNode.getNode(JCRConstants.JCR_CONTENT);
259
260 contentNode.checkout();
261
262 contentNode.setProperty(JCRConstants.JCR_MIME_TYPE, "text/plain");
263 contentNode.setProperty(JCRConstants.JCR_DATA, "");
264 contentNode.setProperty(
265 JCRConstants.JCR_LAST_MODIFIED, Calendar.getInstance());
266
267 session.save();
268
269 Version version = contentNode.checkin();
270
271 contentNode.getVersionHistory().addVersionLabel(
272 version.getName(), "0.0", false);
273 }
274 catch (PathNotFoundException pnfe) {
275 throw new NoSuchFileException(fileName);
276 }
277 catch (RepositoryException re) {
278 throw new SystemException(re);
279 }
280 finally {
281 if (session != null) {
282 session.logout();
283 }
284 }
285
286
288 try {
289 session = JCRFactoryUtil.createSession();
290
291 Node rootNode = getRootNode(session, companyId);
292 Node repositoryNode = getFolderNode(rootNode, repositoryId);
293 Node fileNode = repositoryNode.getNode(fileName);
294 Node contentNode = fileNode.getNode(JCRConstants.JCR_CONTENT);
295
296 VersionHistory versionHistory = contentNode.getVersionHistory();
297
298 VersionIterator itr = versionHistory.getAllVersions();
299
300 while (itr.hasNext()) {
301 Version version = itr.nextVersion();
302
303 if (itr.getPosition() == itr.getSize()) {
304 break;
305 }
306 else {
307 if (!StringUtils.equals(
308 JCRConstants.JCR_ROOT_VERSION, version.getName())) {
309
310 versionHistory.removeVersion(version.getName());
311 }
312 }
313 }
314
315 session.save();
316 }
317 catch (PathNotFoundException pnfe) {
318 throw new NoSuchFileException(fileName);
319 }
320 catch (RepositoryException re) {
321 throw new SystemException(re);
322 }
323 finally {
324 if (session != null) {
325 session.logout();
326 }
327 }
328
329
331 try {
332 session = JCRFactoryUtil.createSession();
333
334 Node rootNode = getRootNode(session, companyId);
335 Node repositoryNode = getFolderNode(rootNode, repositoryId);
336 Node fileNode = repositoryNode.getNode(fileName);
337
338 Indexer indexer = IndexerRegistryUtil.getIndexer(
339 FileModel.class);
340
341 FileModel fileModel = new FileModel();
342
343 fileModel.setCompanyId(companyId);
344 fileModel.setFileName(fileName);
345 fileModel.setPortletId(portletId);
346 fileModel.setRepositoryId(repositoryId);
347
348 indexer.delete(fileModel);
349
350 fileNode.remove();
351
352 session.save();
353 }
354 catch (PathNotFoundException pnfe) {
355 throw new NoSuchFileException(fileName);
356 }
357 catch (RepositoryException re) {
358 throw new SystemException(re);
359 }
360 finally {
361 if (session != null) {
362 session.logout();
363 }
364 }
365 }
366
367 public void deleteFile(
368 long companyId, String portletId, long repositoryId,
369 String fileName, String versionNumber)
370 throws PortalException, SystemException {
371
372 String versionLabel = versionNumber;
373
374 Session session = null;
375
376 try {
377 session = JCRFactoryUtil.createSession();
378
379 Node rootNode = getRootNode(session, companyId);
380 Node repositoryNode = getFolderNode(rootNode, repositoryId);
381 Node fileNode = repositoryNode.getNode(fileName);
382 Node contentNode = fileNode.getNode(JCRConstants.JCR_CONTENT);
383
384 VersionHistory versionHistory = contentNode.getVersionHistory();
385
386 Version version = versionHistory.getVersionByLabel(versionLabel);
387
388 versionHistory.removeVersion(version.getName());
389
390 session.save();
391 }
392 catch (PathNotFoundException pnfe) {
393 throw new NoSuchFileException(fileName);
394 }
395 catch (RepositoryException re) {
396 throw new SystemException(re);
397 }
398 finally {
399 if (session != null) {
400 session.logout();
401 }
402 }
403 }
404
405 public InputStream getFileAsStream(
406 long companyId, long repositoryId, String fileName,
407 String versionNumber)
408 throws PortalException, SystemException {
409
410 InputStream is = null;
411
412 Session session = null;
413
414 try {
415 session = JCRFactoryUtil.createSession();
416
417 Node contentNode = getFileContentNode(
418 session, companyId, repositoryId, fileName, versionNumber);
419
420 Property data = contentNode.getProperty(JCRConstants.JCR_DATA);
421
422 is = new UnsyncBufferedInputStream(data.getStream());
423 }
424 catch (RepositoryException re) {
425 throw new SystemException(re);
426 }
427 finally {
428 if (session != null) {
429 session.logout();
430 }
431 }
432
433 return is;
434 }
435
436 public String[] getFileNames(
437 long companyId, long repositoryId, String dirName)
438 throws PortalException, SystemException {
439
440 List<String> fileNames = new ArrayList<String>();
441
442 Session session = null;
443
444 try {
445 session = JCRFactoryUtil.createSession();
446
447 Node rootNode = getRootNode(session, companyId);
448 Node repositoryNode = getFolderNode(rootNode, repositoryId);
449 Node dirNode = repositoryNode.getNode(dirName);
450
451 NodeIterator itr = dirNode.getNodes();
452
453 while (itr.hasNext()) {
454 Node node = (Node)itr.next();
455
456 if (node.getPrimaryNodeType().getName().equals(
457 JCRConstants.NT_FILE)) {
458
459 fileNames.add(dirName + "/" + node.getName());
460 }
461 }
462 }
463 catch (PathNotFoundException pnfe) {
464 throw new NoSuchDirectoryException(dirName);
465 }
466 catch (RepositoryException re) {
467 throw new SystemException(re);
468 }
469 finally {
470 if (session != null) {
471 session.logout();
472 }
473 }
474
475 return fileNames.toArray(new String[fileNames.size()]);
476 }
477
478 public long getFileSize(
479 long companyId, long repositoryId, String fileName)
480 throws PortalException, SystemException {
481
482 long size;
483
484 Session session = null;
485
486 try {
487 session = JCRFactoryUtil.createSession();
488
489 Node contentNode = getFileContentNode(
490 session, companyId, repositoryId, fileName, StringPool.BLANK);
491
492 size = contentNode.getProperty(JCRConstants.JCR_DATA).getLength();
493 }
494 catch (RepositoryException re) {
495 throw new SystemException(re);
496 }
497 finally {
498 if (session != null) {
499 session.logout();
500 }
501 }
502
503 return size;
504 }
505
506 public boolean hasFile(
507 long companyId, long repositoryId, String fileName,
508 String versionNumber)
509 throws PortalException, SystemException {
510
511 try {
512 getFileContentNode(
513 companyId, repositoryId, fileName, versionNumber);
514 }
515 catch (NoSuchFileException nsfe) {
516 return false;
517 }
518
519 return true;
520 }
521
522 public void move(String srcDir, String destDir) throws SystemException {
523 Session session = null;
524
525 try {
526 session = JCRFactoryUtil.createSession();
527
528 session.move(srcDir, destDir);
529
530 session.save();
531 }
532 catch (RepositoryException re) {
533 throw new SystemException(re);
534 }
535 finally {
536 if (session != null) {
537 session.logout();
538 }
539 }
540 }
541
542 public void reindex(String[] ids) throws SearchException {
543 long companyId = GetterUtil.getLong(ids[0]);
544 String portletId = ids[1];
545 long groupId = GetterUtil.getLong(ids[2]);
546 long repositoryId = GetterUtil.getLong(ids[3]);
547
548 Collection<Document> documents = new ArrayList<Document>();
549
550 Session session = null;
551
552 try {
553 session = JCRFactoryUtil.createSession();
554
555 Node rootNode = getRootNode(session, companyId);
556 Node repositoryNode = getFolderNode(rootNode, repositoryId);
557
558 NodeIterator itr = repositoryNode.getNodes();
559
560 while (itr.hasNext()) {
561 Node node = (Node)itr.next();
562
563 if (node.getPrimaryNodeType().getName().equals(
564 JCRConstants.NT_FILE)) {
565
566 try {
567 Indexer indexer = IndexerRegistryUtil.getIndexer(
568 FileModel.class);
569
570 FileModel fileModel = new FileModel();
571
572 fileModel.setCompanyId(companyId);
573 fileModel.setFileName(node.getName());
574 fileModel.setGroupId(groupId);
575 fileModel.setPortletId(portletId);
576 fileModel.setRepositoryId(repositoryId);
577
578 Document document = indexer.getDocument(fileModel);
579
580 if (document == null) {
581 continue;
582 }
583
584 documents.add(document);
585 }
586 catch (Exception e2) {
587 _log.error("Reindexing " + node.getName(), e2);
588 }
589 }
590 }
591 }
592 catch (Exception e1) {
593 throw new SearchException(e1);
594 }
595 finally {
596 try {
597 if (session != null) {
598 session.logout();
599 }
600 }
601 catch (Exception e) {
602 _log.error(e);
603 }
604 }
605
606 SearchEngineUtil.updateDocuments(companyId, documents);
607 }
608
609 public void updateFile(
610 long companyId, String portletId, long groupId, long repositoryId,
611 long newRepositoryId, String fileName, long fileEntryId)
612 throws PortalException, SystemException {
613
614 Session session = null;
615
616 try {
617 session = JCRFactoryUtil.createSession();
618
619 Node rootNode = getRootNode(session, companyId);
620 Node repositoryNode = getFolderNode(rootNode, repositoryId);
621 Node fileNode = repositoryNode.getNode(fileName);
622 Node contentNode = fileNode.getNode(JCRConstants.JCR_CONTENT);
623
624 Node newRepositoryNode = getFolderNode(rootNode, newRepositoryId);
625
626 if (newRepositoryNode.hasNode(fileName)) {
627 throw new DuplicateFileException(fileName);
628 }
629 else {
630 Node newFileNode = newRepositoryNode.addNode(
631 fileName, JCRConstants.NT_FILE);
632
633 Node newContentNode = newFileNode.addNode(
634 JCRConstants.JCR_CONTENT, JCRConstants.NT_RESOURCE);
635
636 VersionHistory versionHistory = contentNode.getVersionHistory();
637
638 String[] versionLabels = versionHistory.getVersionLabels();
639
640 for (int i = (versionLabels.length - 1); i >= 0; i--) {
641 Version version = versionHistory.getVersionByLabel(
642 versionLabels[i]);
643
644 Node frozenContentNode = version.getNode(
645 JCRConstants.JCR_FROZEN_NODE);
646
647 if (i == (versionLabels.length - 1)) {
648 newContentNode.addMixin(JCRConstants.MIX_VERSIONABLE);
649 }
650 else {
651 newContentNode.checkout();
652 }
653
654 newContentNode.setProperty(
655 JCRConstants.JCR_MIME_TYPE, "text/plain");
656 newContentNode.setProperty(
657 JCRConstants.JCR_DATA,
658 frozenContentNode.getProperty(
659 JCRConstants.JCR_DATA).getStream());
660 newContentNode.setProperty(
661 JCRConstants.JCR_LAST_MODIFIED, Calendar.getInstance());
662
663 session.save();
664
665 Version newVersion = newContentNode.checkin();
666
667 newContentNode.getVersionHistory().addVersionLabel(
668 newVersion.getName(), versionLabels[i], false);
669 }
670
671 fileNode.remove();
672
673 session.save();
674
675 Indexer indexer = IndexerRegistryUtil.getIndexer(
676 FileModel.class);
677
678 FileModel fileModel = new FileModel();
679
680 fileModel.setCompanyId(companyId);
681 fileModel.setFileName(fileName);
682 fileModel.setPortletId(portletId);
683 fileModel.setRepositoryId(repositoryId);
684
685 indexer.delete(fileModel);
686
687 fileModel.setRepositoryId(newRepositoryId);
688 fileModel.setGroupId(groupId);
689
690 indexer.reindex(fileModel);
691 }
692 }
693 catch (PathNotFoundException pnfe) {
694 throw new NoSuchFileException(fileName);
695 }
696 catch (RepositoryException re) {
697 throw new SystemException(re);
698 }
699 finally {
700 if (session != null) {
701 session.logout();
702 }
703 }
704 }
705
706 public void updateFile(
707 long companyId, String portletId, long groupId, long repositoryId,
708 String fileName, String newFileName, boolean reindex)
709 throws PortalException, SystemException {
710
711 Session session = null;
712
713 try {
714 session = JCRFactoryUtil.createSession();
715
716 Node rootNode = getRootNode(session, companyId);
717 Node repositoryNode = getFolderNode(rootNode, repositoryId);
718 Node fileNode = repositoryNode.getNode(fileName);
719 Node contentNode = fileNode.getNode(JCRConstants.JCR_CONTENT);
720
721 Node newFileNode = repositoryNode.addNode(
722 newFileName, JCRConstants.NT_FILE);
723
724 Node newContentNode = newFileNode.addNode(
725 JCRConstants.JCR_CONTENT, JCRConstants.NT_RESOURCE);
726
727 VersionHistory versionHistory = contentNode.getVersionHistory();
728
729 String[] versionLabels = versionHistory.getVersionLabels();
730
731 for (int i = (versionLabels.length - 1); i >= 0; i--) {
732 Version version = versionHistory.getVersionByLabel(
733 versionLabels[i]);
734
735 Node frozenContentNode = version.getNode(
736 JCRConstants.JCR_FROZEN_NODE);
737
738 if (i == (versionLabels.length - 1)) {
739 newContentNode.addMixin(JCRConstants.MIX_VERSIONABLE);
740 }
741 else {
742 newContentNode.checkout();
743 }
744
745 newContentNode.setProperty(
746 JCRConstants.JCR_MIME_TYPE, "text/plain");
747 newContentNode.setProperty(
748 JCRConstants.JCR_DATA,
749 frozenContentNode.getProperty(
750 JCRConstants.JCR_DATA).getStream());
751 newContentNode.setProperty(
752 JCRConstants.JCR_LAST_MODIFIED, Calendar.getInstance());
753
754 session.save();
755
756 Version newVersion = newContentNode.checkin();
757
758 newContentNode.getVersionHistory().addVersionLabel(
759 newVersion.getName(), versionLabels[i], false);
760 }
761
762 fileNode.remove();
763
764 session.save();
765
766 if (reindex) {
767 Indexer indexer = IndexerRegistryUtil.getIndexer(
768 FileModel.class);
769
770 FileModel fileModel = new FileModel();
771
772 fileModel.setCompanyId(companyId);
773 fileModel.setFileName(fileName);
774 fileModel.setPortletId(portletId);
775 fileModel.setRepositoryId(repositoryId);
776
777 indexer.delete(fileModel);
778
779 fileModel.setFileName(newFileName);
780 fileModel.setGroupId(groupId);
781
782 indexer.reindex(fileModel);
783 }
784 }
785 catch (PathNotFoundException pnfe) {
786 throw new NoSuchFileException(fileName);
787 }
788 catch (RepositoryException re) {
789 throw new SystemException(re);
790 }
791 finally {
792 if (session != null) {
793 session.logout();
794 }
795 }
796 }
797
798 public void updateFile(
799 long companyId, String portletId, long groupId, long repositoryId,
800 String fileName, String versionNumber, String sourceFileName,
801 long fileEntryId, String properties, Date modifiedDate,
802 ServiceContext serviceContext, InputStream is)
803 throws PortalException, SystemException {
804
805 String versionLabel = versionNumber;
806
807 Session session = null;
808
809 try {
810 session = JCRFactoryUtil.createSession();
811
812 Node rootNode = getRootNode(session, companyId);
813 Node repositoryNode = getFolderNode(rootNode, repositoryId);
814 Node fileNode = repositoryNode.getNode(fileName);
815 Node contentNode = fileNode.getNode(JCRConstants.JCR_CONTENT);
816
817 contentNode.checkout();
818
819 contentNode.setProperty(JCRConstants.JCR_MIME_TYPE, "text/plain");
820 contentNode.setProperty(JCRConstants.JCR_DATA, is);
821 contentNode.setProperty(
822 JCRConstants.JCR_LAST_MODIFIED, Calendar.getInstance());
823
824 session.save();
825
826 Version version = contentNode.checkin();
827
828 contentNode.getVersionHistory().addVersionLabel(
829 version.getName(), versionLabel, false);
830
831 Indexer indexer = IndexerRegistryUtil.getIndexer(
832 FileModel.class);
833
834 FileModel fileModel = new FileModel();
835
836 fileModel.setAssetCategoryIds(serviceContext.getAssetCategoryIds());
837 fileModel.setAssetTagNames(serviceContext.getAssetTagNames());
838 fileModel.setCompanyId(companyId);
839 fileModel.setFileEntryId(fileEntryId);
840 fileModel.setFileName(fileName);
841 fileModel.setGroupId(groupId);
842 fileModel.setModifiedDate(modifiedDate);
843 fileModel.setPortletId(portletId);
844 fileModel.setProperties(properties);
845 fileModel.setRepositoryId(repositoryId);
846
847 indexer.reindex(fileModel);
848 }
849 catch (PathNotFoundException pnfe) {
850 throw new NoSuchFileException(fileName);
851 }
852 catch (RepositoryException re) {
853 throw new SystemException(re);
854 }
855 finally {
856 if (session != null) {
857 session.logout();
858 }
859 }
860 }
861
862 protected void deleteDirectory(
863 long companyId, String portletId, long repositoryId, Node dirNode)
864 throws SearchException {
865
866 try {
867 NodeIterator itr = dirNode.getNodes();
868
869 FileModel fileModel = new FileModel();
870
871 fileModel.setCompanyId(companyId);
872 fileModel.setPortletId(portletId);
873 fileModel.setRepositoryId(repositoryId);
874
875 Indexer indexer = IndexerRegistryUtil.getIndexer(FileModel.class);
876
877 while (itr.hasNext()) {
878 Node node = (Node)itr.next();
879
880 String primaryNodeTypeName =
881 node.getPrimaryNodeType().getName();
882
883 if (primaryNodeTypeName.equals(JCRConstants.NT_FOLDER)) {
884 deleteDirectory(companyId, portletId, repositoryId, node);
885 }
886 else if (primaryNodeTypeName.equals(JCRConstants.NT_FILE)) {
887 fileModel.setFileName(node.getName());
888
889 indexer.delete(fileModel);
890 }
891 }
892
893 fileModel.setFileName(dirNode.getName());
894
895 indexer.delete(fileModel);
896 }
897 catch (RepositoryException e) {
898 _log.error(e);
899 }
900 }
901
902 protected Node getFileContentNode(
903 long companyId, long repositoryId, String fileName,
904 String versionNumber)
905 throws PortalException, SystemException {
906
907 Node contentNode = null;
908
909 Session session = null;
910
911 try {
912 session = JCRFactoryUtil.createSession();
913
914 contentNode = getFileContentNode(
915 session, companyId, repositoryId, fileName, versionNumber);
916 }
917 catch (RepositoryException re) {
918 throw new SystemException(re);
919 }
920 finally {
921 if (session != null) {
922 session.logout();
923 }
924 }
925
926 return contentNode;
927 }
928
929 protected Node getFileContentNode(
930 Session session, long companyId, long repositoryId,
931 String fileName, String versionNumber)
932 throws PortalException, SystemException {
933
934 String versionLabel = versionNumber;
935
936 Node contentNode = null;
937
938 try {
939 Node rootNode = getRootNode(session, companyId);
940 Node repositoryNode = getFolderNode(rootNode, repositoryId);
941 Node fileNode = repositoryNode.getNode(fileName);
942 contentNode = fileNode.getNode(JCRConstants.JCR_CONTENT);
943
944 if (Validator.isNotNull(versionNumber)) {
945 VersionHistory versionHistory =
946 contentNode.getVersionHistory();
947
948 Version version = versionHistory.getVersionByLabel(
949 versionLabel);
950
951 contentNode = version.getNode(JCRConstants.JCR_FROZEN_NODE);
952 }
953 }
954 catch (PathNotFoundException pnfe) {
955 throw new NoSuchFileException(fileName);
956 }
957 catch (RepositoryException re) {
958 throw new SystemException(re);
959 }
960
961 return contentNode;
962 }
963
964 protected Node getFolderNode(Node node, long name)
965 throws RepositoryException {
966
967 return getFolderNode(node, String.valueOf(name));
968 }
969
970 protected Node getFolderNode(Node node, String name)
971 throws RepositoryException {
972
973 Node folderNode = null;
974
975 if (node.hasNode(name)) {
976 folderNode = node.getNode(name);
977 }
978 else {
979 folderNode = node.addNode(name, JCRConstants.NT_FOLDER);
980 }
981
982 return folderNode;
983 }
984
985 protected Node getRootNode(Session session, long companyId)
986 throws RepositoryException {
987
988 Node companyNode = getFolderNode(session.getRootNode(), companyId);
989
990 return getFolderNode(companyNode, JCRFactory.NODE_DOCUMENTLIBRARY);
991 }
992
993 private static Log _log = LogFactoryUtil.getLog(JCRHook.class);
994
995 }