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.search.SearchException;
35 import com.liferay.portal.kernel.util.GetterUtil;
36 import com.liferay.portal.kernel.util.StringUtil;
37 import com.liferay.portal.kernel.util.Validator;
38 import com.liferay.portal.lucene.LuceneUtil;
39
40 import java.io.BufferedInputStream;
41 import java.io.IOException;
42 import java.io.InputStream;
43
44 import java.util.ArrayList;
45 import java.util.Calendar;
46 import java.util.List;
47
48 import javax.jcr.Node;
49 import javax.jcr.NodeIterator;
50 import javax.jcr.PathNotFoundException;
51 import javax.jcr.Property;
52 import javax.jcr.RepositoryException;
53 import javax.jcr.Session;
54 import javax.jcr.version.Version;
55 import javax.jcr.version.VersionHistory;
56 import javax.jcr.version.VersionIterator;
57
58 import org.apache.commons.lang.StringUtils;
59 import org.apache.commons.logging.Log;
60 import org.apache.commons.logging.LogFactory;
61 import org.apache.lucene.document.Document;
62 import org.apache.lucene.index.IndexWriter;
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, String properties, 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);
157 }
158 }
159 catch (IOException ioe) {
160 throw new SystemException(ioe);
161 }
162 catch (RepositoryException re) {
163 throw new SystemException(re);
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 (IOException ioe) {
212 throw new SystemException(ioe);
213 }
214 catch (PathNotFoundException pnfe) {
215 throw new NoSuchDirectoryException(dirName);
216 }
217 catch (RepositoryException e) {
218 throw new PortalException(e);
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 (IOException ioe) {
332 throw new SystemException(ioe);
333 }
334 catch (PathNotFoundException pnfe) {
335 throw new NoSuchFileException(fileName);
336 }
337 catch (RepositoryException re) {
338 throw new SystemException(re);
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 fileNames = new ArrayList();
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 (String[])fileNames.toArray(new String[0]);
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 IndexWriter writer = null;
529
530 Session session = null;
531
532 try {
533 writer = LuceneUtil.getWriter(companyId);
534
535 session = JCRFactoryUtil.createSession();
536
537 Node rootNode = getRootNode(session, companyId);
538 Node repositoryNode = getFolderNode(rootNode, repositoryId);
539
540 NodeIterator itr = repositoryNode.getNodes();
541
542 while (itr.hasNext()) {
543 Node node = (Node)itr.next();
544
545 if (node.getPrimaryNodeType().getName().equals(
546 JCRConstants.NT_FILE)) {
547
548 try {
549 Document doc = IndexerImpl.getAddFileDocument(
550 companyId, portletId, groupId, repositoryId,
551 node.getName());
552
553 writer.addDocument(doc);
554 }
555 catch (Exception e1) {
556 _log.error("Reindexing " + node.getName(), e1);
557 }
558 }
559 }
560 }
561 catch (Exception e2) {
562 throw new SearchException(e2);
563 }
564 finally {
565 try {
566 if (session != null) {
567 session.logout();
568 }
569 }
570 catch (Exception e) {
571 _log.error(e);
572 }
573
574 try {
575 if (writer != null) {
576 LuceneUtil.write(companyId);
577 }
578 }
579 catch (Exception e) {
580 _log.error(e);
581 }
582 }
583 }
584
585 public void updateFile(
586 long companyId, String portletId, long groupId, long repositoryId,
587 String fileName, double versionNumber, String sourceFileName,
588 String properties, InputStream is)
589 throws PortalException, SystemException {
590
591 String versionLabel = String.valueOf(versionNumber);
592
593 Session session = null;
594
595 try {
596 session = JCRFactoryUtil.createSession();
597
598 Node rootNode = getRootNode(session, companyId);
599 Node repositoryNode = getFolderNode(rootNode, repositoryId);
600 Node fileNode = repositoryNode.getNode(fileName);
601 Node contentNode = fileNode.getNode(JCRConstants.JCR_CONTENT);
602
603 contentNode.checkout();
604
605 contentNode.setProperty(JCRConstants.JCR_MIME_TYPE, "text/plain");
606 contentNode.setProperty(JCRConstants.JCR_DATA, is);
607 contentNode.setProperty(
608 JCRConstants.JCR_LAST_MODIFIED, Calendar.getInstance());
609
610 session.save();
611
612 Version version = contentNode.checkin();
613
614 contentNode.getVersionHistory().addVersionLabel(
615 version.getName(), versionLabel, false);
616
617 Indexer.updateFile(
618 companyId, portletId, groupId, repositoryId, fileName,
619 properties);
620 }
621 catch (IOException ioe) {
622 throw new SystemException(ioe);
623 }
624 catch (PathNotFoundException pnfe) {
625 throw new NoSuchFileException(fileName);
626 }
627 catch (RepositoryException re) {
628 throw new SystemException(re);
629 }
630 finally {
631 if (session != null) {
632 session.logout();
633 }
634 }
635 }
636
637 public void updateFile(
638 long companyId, String portletId, long groupId, long repositoryId,
639 long newRepositoryId, String fileName)
640 throws PortalException, SystemException {
641
642 Session session = null;
643
644 try {
645 session = JCRFactoryUtil.createSession();
646
647 Node rootNode = getRootNode(session, companyId);
648 Node repositoryNode = getFolderNode(rootNode, repositoryId);
649 Node fileNode = repositoryNode.getNode(fileName);
650 Node contentNode = fileNode.getNode(JCRConstants.JCR_CONTENT);
651
652 Node newRepositoryNode = getFolderNode(rootNode, newRepositoryId);
653
654 if (newRepositoryNode.hasNode(fileName)) {
655 throw new DuplicateFileException(fileName);
656 }
657 else {
658 Node newFileNode = newRepositoryNode.addNode(
659 fileName, JCRConstants.NT_FILE);
660
661 Node newContentNode = newFileNode.addNode(
662 JCRConstants.JCR_CONTENT, JCRConstants.NT_RESOURCE);
663
664 VersionHistory versionHistory = contentNode.getVersionHistory();
665
666 String[] versionLabels = versionHistory.getVersionLabels();
667
668 for (int i = (versionLabels.length - 1); i >= 0; i--) {
669 Version version = versionHistory.getVersionByLabel(
670 versionLabels[i]);
671
672 Node frozenContentNode = version.getNode(
673 JCRConstants.JCR_FROZEN_NODE);
674
675 if (i == (versionLabels.length - 1)) {
676 newContentNode.addMixin(JCRConstants.MIX_VERSIONABLE);
677 }
678 else {
679 newContentNode.checkout();
680 }
681
682 newContentNode.setProperty(
683 JCRConstants.JCR_MIME_TYPE, "text/plain");
684 newContentNode.setProperty(
685 JCRConstants.JCR_DATA,
686 frozenContentNode.getProperty(
687 JCRConstants.JCR_DATA).getStream());
688 newContentNode.setProperty(
689 JCRConstants.JCR_LAST_MODIFIED, Calendar.getInstance());
690
691 session.save();
692
693 Version newVersion = newContentNode.checkin();
694
695 newContentNode.getVersionHistory().addVersionLabel(
696 newVersion.getName(), versionLabels[i], false);
697 }
698
699 fileNode.remove();
700
701 session.save();
702
703 try {
704 Indexer.deleteFile(
705 companyId, portletId, repositoryId, fileName);
706 }
707 catch (IOException ioe) {
708 }
709
710 Indexer.addFile(
711 companyId, portletId, groupId, newRepositoryId, fileName);
712 }
713 }
714 catch (IOException ioe) {
715 throw new SystemException(ioe);
716 }
717 catch (PathNotFoundException pnfe) {
718 throw new NoSuchFileException(fileName);
719 }
720 catch (RepositoryException re) {
721 throw new SystemException(re);
722 }
723 finally {
724 if (session != null) {
725 session.logout();
726 }
727 }
728 }
729
730 protected void deleteDirectory(
731 long companyId, String portletId, long repositoryId, Node dirNode)
732 throws IOException {
733
734 try {
735 NodeIterator itr = dirNode.getNodes();
736
737 while (itr.hasNext()) {
738 Node node = (Node)itr.next();
739
740 String primaryNodeTypeName =
741 node.getPrimaryNodeType().getName();
742
743 if (primaryNodeTypeName.equals(JCRConstants.NT_FOLDER)) {
744 deleteDirectory(companyId, portletId, repositoryId, node);
745 }
746 else if (primaryNodeTypeName.equals(JCRConstants.NT_FILE)) {
747 Indexer.deleteFile(
748 companyId, portletId, repositoryId, node.getName());
749 }
750 }
751
752 Indexer.deleteFile(
753 companyId, portletId, repositoryId, dirNode.getName());
754 }
755 catch (RepositoryException e) {
756 _log.error(e);
757 }
758 }
759
760 protected Node getFileContentNode(
761 long companyId, long repositoryId, String fileName,
762 double versionNumber)
763 throws PortalException, SystemException {
764
765 Node contentNode = null;
766
767 Session session = null;
768
769 try {
770 session = JCRFactoryUtil.createSession();
771
772 contentNode = getFileContentNode(
773 session, companyId, repositoryId, fileName, versionNumber);
774 }
775 catch (RepositoryException re) {
776 throw new SystemException(re);
777 }
778 finally {
779 if (session != null) {
780 session.logout();
781 }
782 }
783
784 return contentNode;
785 }
786
787 protected Node getFileContentNode(
788 Session session, long companyId, long repositoryId,
789 String fileName, double versionNumber)
790 throws PortalException, SystemException {
791
792 String versionLabel = String.valueOf(versionNumber);
793
794 Node contentNode = null;
795
796 try {
797 Node rootNode = getRootNode(session, companyId);
798 Node repositoryNode = getFolderNode(rootNode, repositoryId);
799 Node fileNode = repositoryNode.getNode(fileName);
800 contentNode = fileNode.getNode(JCRConstants.JCR_CONTENT);
801
802 if (versionNumber > 0) {
803 VersionHistory versionHistory =
804 contentNode.getVersionHistory();
805
806 Version version = versionHistory.getVersionByLabel(
807 versionLabel);
808
809 contentNode = version.getNode(JCRConstants.JCR_FROZEN_NODE);
810 }
811 }
812 catch (PathNotFoundException pnfe) {
813 throw new NoSuchFileException(fileName);
814 }
815 catch (RepositoryException re) {
816 throw new SystemException(re);
817 }
818
819 return contentNode;
820 }
821
822 protected Node getFolderNode(Node node, long name)
823 throws RepositoryException {
824
825 return getFolderNode(node, String.valueOf(name));
826 }
827
828 protected Node getFolderNode(Node node, String name)
829 throws RepositoryException {
830
831 Node folderNode = null;
832
833 if (node.hasNode(name)) {
834 folderNode = node.getNode(name);
835 }
836 else {
837 folderNode = node.addNode(name, JCRConstants.NT_FOLDER);
838 }
839
840 return folderNode;
841 }
842
843 protected Node getRootNode(Session session, long companyId)
844 throws RepositoryException {
845
846 Node companyNode = getFolderNode(session.getRootNode(), companyId);
847
848 return getFolderNode(companyNode, JCRFactory.NODE_DOCUMENTLIBRARY);
849 }
850
851 private static Log _log = LogFactory.getLog(JCRHook.class);
852
853 }