1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
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.Document;
35  import com.liferay.portal.kernel.search.Field;
36  import com.liferay.portal.kernel.search.SearchEngineUtil;
37  import com.liferay.portal.kernel.search.SearchException;
38  import com.liferay.portal.kernel.util.GetterUtil;
39  import com.liferay.portal.kernel.util.StringUtil;
40  import com.liferay.portal.kernel.util.Validator;
41  
42  import java.io.BufferedInputStream;
43  import java.io.InputStream;
44  
45  import java.util.ArrayList;
46  import java.util.Calendar;
47  import java.util.List;
48  
49  import javax.jcr.Node;
50  import javax.jcr.NodeIterator;
51  import javax.jcr.PathNotFoundException;
52  import javax.jcr.Property;
53  import javax.jcr.RepositoryException;
54  import javax.jcr.Session;
55  import javax.jcr.version.Version;
56  import javax.jcr.version.VersionHistory;
57  import javax.jcr.version.VersionIterator;
58  
59  import org.apache.commons.lang.StringUtils;
60  import org.apache.commons.logging.Log;
61  import org.apache.commons.logging.LogFactory;
62  
63  /**
64   * <a href="JCRHook.java.html"><b><i>View Source</i></b></a>
65   *
66   * @author Michael Young
67   * @author Brian Wing Shun Chan
68   *
69   */
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, String[] tagsEntries,
119             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, 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         // A bug in Jackrabbit requires us to create a dummy node and delete the
235         // version tree manually to successfully delete a file
236 
237         // Create a dummy node
238 
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         // Delete version tree
274 
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         // Delete file
317 
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, String[] tagsEntries, InputStream is)
577         throws PortalException, SystemException {
578 
579         String versionLabel = String.valueOf(versionNumber);
580 
581         Session session = null;
582 
583         try {
584             session = JCRFactoryUtil.createSession();
585 
586             Node rootNode = getRootNode(session, companyId);
587             Node repositoryNode = getFolderNode(rootNode, repositoryId);
588             Node fileNode = repositoryNode.getNode(fileName);
589             Node contentNode = fileNode.getNode(JCRConstants.JCR_CONTENT);
590 
591             contentNode.checkout();
592 
593             contentNode.setProperty(JCRConstants.JCR_MIME_TYPE, "text/plain");
594             contentNode.setProperty(JCRConstants.JCR_DATA, is);
595             contentNode.setProperty(
596                 JCRConstants.JCR_LAST_MODIFIED, Calendar.getInstance());
597 
598             session.save();
599 
600             Version version = contentNode.checkin();
601 
602             contentNode.getVersionHistory().addVersionLabel(
603                 version.getName(), versionLabel, false);
604 
605             Indexer.updateFile(
606                 companyId, portletId, groupId, repositoryId, fileName,
607                 properties, tagsEntries);
608         }
609         catch (PathNotFoundException pnfe) {
610             throw new NoSuchFileException(fileName);
611         }
612         catch (RepositoryException re) {
613             throw new SystemException(re);
614         }
615         catch (SearchException se) {
616             throw new SystemException(se);
617         }
618         finally {
619             if (session != null) {
620                 session.logout();
621             }
622         }
623     }
624 
625     public void updateFile(
626             long companyId, String portletId, long groupId, long repositoryId,
627             long newRepositoryId, String fileName)
628         throws PortalException, SystemException {
629 
630         Session session = null;
631 
632         try {
633             session = JCRFactoryUtil.createSession();
634 
635             Node rootNode = getRootNode(session, companyId);
636             Node repositoryNode = getFolderNode(rootNode, repositoryId);
637             Node fileNode = repositoryNode.getNode(fileName);
638             Node contentNode = fileNode.getNode(JCRConstants.JCR_CONTENT);
639 
640             Node newRepositoryNode = getFolderNode(rootNode, newRepositoryId);
641 
642             if (newRepositoryNode.hasNode(fileName)) {
643                 throw new DuplicateFileException(fileName);
644             }
645             else {
646                 Node newFileNode = newRepositoryNode.addNode(
647                     fileName, JCRConstants.NT_FILE);
648 
649                 Node newContentNode = newFileNode.addNode(
650                     JCRConstants.JCR_CONTENT, JCRConstants.NT_RESOURCE);
651 
652                 VersionHistory versionHistory = contentNode.getVersionHistory();
653 
654                 String[] versionLabels = versionHistory.getVersionLabels();
655 
656                 for (int i = (versionLabels.length - 1); i >= 0; i--) {
657                     Version version = versionHistory.getVersionByLabel(
658                         versionLabels[i]);
659 
660                     Node frozenContentNode = version.getNode(
661                         JCRConstants.JCR_FROZEN_NODE);
662 
663                     if (i == (versionLabels.length - 1)) {
664                         newContentNode.addMixin(JCRConstants.MIX_VERSIONABLE);
665                     }
666                     else {
667                         newContentNode.checkout();
668                     }
669 
670                     newContentNode.setProperty(
671                         JCRConstants.JCR_MIME_TYPE, "text/plain");
672                     newContentNode.setProperty(
673                         JCRConstants.JCR_DATA,
674                         frozenContentNode.getProperty(
675                             JCRConstants.JCR_DATA).getStream());
676                     newContentNode.setProperty(
677                         JCRConstants.JCR_LAST_MODIFIED, Calendar.getInstance());
678 
679                     session.save();
680 
681                     Version newVersion = newContentNode.checkin();
682 
683                     newContentNode.getVersionHistory().addVersionLabel(
684                         newVersion.getName(), versionLabels[i], false);
685                 }
686 
687                 fileNode.remove();
688 
689                 session.save();
690 
691                 try {
692                     Indexer.deleteFile(
693                         companyId, portletId, repositoryId, fileName);
694                 }
695                 catch (SearchException se) {
696                 }
697 
698                 Indexer.addFile(
699                     companyId, portletId, groupId, newRepositoryId, fileName);
700             }
701         }
702         catch (PathNotFoundException pnfe) {
703             throw new NoSuchFileException(fileName);
704         }
705         catch (RepositoryException re) {
706             throw new SystemException(re);
707         }
708         catch (SearchException se) {
709             throw new SystemException(se);
710         }
711         finally {
712             if (session != null) {
713                 session.logout();
714             }
715         }
716     }
717 
718     protected void deleteDirectory(
719             long companyId, String portletId, long repositoryId, Node dirNode)
720         throws SearchException {
721 
722         try {
723             NodeIterator itr = dirNode.getNodes();
724 
725             while (itr.hasNext()) {
726                 Node node = (Node)itr.next();
727 
728                 String primaryNodeTypeName =
729                     node.getPrimaryNodeType().getName();
730 
731                 if (primaryNodeTypeName.equals(JCRConstants.NT_FOLDER)) {
732                     deleteDirectory(companyId, portletId, repositoryId, node);
733                 }
734                 else if (primaryNodeTypeName.equals(JCRConstants.NT_FILE)) {
735                     Indexer.deleteFile(
736                         companyId, portletId, repositoryId, node.getName());
737                 }
738             }
739 
740             Indexer.deleteFile(
741                 companyId, portletId, repositoryId, dirNode.getName());
742         }
743         catch (RepositoryException e) {
744             _log.error(e);
745         }
746     }
747 
748     protected Node getFileContentNode(
749             long companyId, long repositoryId, String fileName,
750             double versionNumber)
751         throws PortalException, SystemException {
752 
753         Node contentNode = null;
754 
755         Session session = null;
756 
757         try {
758             session = JCRFactoryUtil.createSession();
759 
760             contentNode = getFileContentNode(
761                 session, companyId, repositoryId, fileName, versionNumber);
762         }
763         catch (RepositoryException re) {
764             throw new SystemException(re);
765         }
766         finally {
767             if (session != null) {
768                 session.logout();
769             }
770         }
771 
772         return contentNode;
773     }
774 
775     protected Node getFileContentNode(
776             Session session, long companyId, long repositoryId,
777             String fileName, double versionNumber)
778         throws PortalException, SystemException {
779 
780         String versionLabel = String.valueOf(versionNumber);
781 
782         Node contentNode = null;
783 
784         try {
785             Node rootNode = getRootNode(session, companyId);
786             Node repositoryNode = getFolderNode(rootNode, repositoryId);
787             Node fileNode = repositoryNode.getNode(fileName);
788             contentNode = fileNode.getNode(JCRConstants.JCR_CONTENT);
789 
790             if (versionNumber > 0) {
791                 VersionHistory versionHistory =
792                     contentNode.getVersionHistory();
793 
794                 Version version = versionHistory.getVersionByLabel(
795                     versionLabel);
796 
797                 contentNode = version.getNode(JCRConstants.JCR_FROZEN_NODE);
798             }
799         }
800         catch (PathNotFoundException pnfe) {
801             throw new NoSuchFileException(fileName);
802         }
803         catch (RepositoryException re) {
804             throw new SystemException(re);
805         }
806 
807         return contentNode;
808     }
809 
810     protected Node getFolderNode(Node node, long name)
811         throws RepositoryException {
812 
813         return getFolderNode(node, String.valueOf(name));
814     }
815 
816     protected Node getFolderNode(Node node, String name)
817         throws RepositoryException {
818 
819         Node folderNode = null;
820 
821         if (node.hasNode(name)) {
822             folderNode = node.getNode(name);
823         }
824         else {
825             folderNode = node.addNode(name, JCRConstants.NT_FOLDER);
826         }
827 
828         return folderNode;
829     }
830 
831     protected Node getRootNode(Session session, long companyId)
832         throws RepositoryException {
833 
834         Node companyNode = getFolderNode(session.getRootNode(), companyId);
835 
836         return getFolderNode(companyNode, JCRFactory.NODE_DOCUMENTLIBRARY);
837     }
838 
839     private static Log _log = LogFactory.getLog(JCRHook.class);
840 
841 }