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