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