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