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