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