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