1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
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.documentlibrary.model.FileModel;
22  import com.liferay.portal.jcr.JCRConstants;
23  import com.liferay.portal.jcr.JCRFactory;
24  import com.liferay.portal.jcr.JCRFactoryUtil;
25  import com.liferay.portal.kernel.exception.PortalException;
26  import com.liferay.portal.kernel.exception.SystemException;
27  import com.liferay.portal.kernel.io.unsync.UnsyncBufferedInputStream;
28  import com.liferay.portal.kernel.log.Log;
29  import com.liferay.portal.kernel.log.LogFactoryUtil;
30  import com.liferay.portal.kernel.search.Document;
31  import com.liferay.portal.kernel.search.Field;
32  import com.liferay.portal.kernel.search.Indexer;
33  import com.liferay.portal.kernel.search.IndexerRegistryUtil;
34  import com.liferay.portal.kernel.search.SearchEngineUtil;
35  import com.liferay.portal.kernel.search.SearchException;
36  import com.liferay.portal.kernel.util.GetterUtil;
37  import com.liferay.portal.kernel.util.StringPool;
38  import com.liferay.portal.kernel.util.StringUtil;
39  import com.liferay.portal.kernel.util.Validator;
40  import com.liferay.portal.service.ServiceContext;
41  
42  import java.io.InputStream;
43  
44  import java.util.ArrayList;
45  import java.util.Calendar;
46  import java.util.Date;
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  
61  /**
62   * <a href="JCRHook.java.html"><b><i>View Source</i></b></a>
63   *
64   * @author Michael Young
65   * @author Brian Wing Shun Chan
66   */
67  public class JCRHook extends BaseHook {
68  
69      public void addDirectory(long companyId, long repositoryId, String dirName)
70          throws PortalException, SystemException {
71  
72          Session session = null;
73  
74          try {
75              session = JCRFactoryUtil.createSession();
76  
77              Node rootNode = getRootNode(session, companyId);
78              Node repositoryNode = getFolderNode(rootNode, repositoryId);
79  
80              if (repositoryNode.hasNode(dirName)) {
81                  throw new DuplicateDirectoryException(dirName);
82              }
83              else {
84                  String[] dirNameArray = StringUtil.split(dirName, "/");
85  
86                  Node dirNode = repositoryNode;
87  
88                  for (int i = 0; i < dirNameArray.length; i++) {
89                      if (Validator.isNotNull(dirNameArray[i])) {
90                          if (dirNode.hasNode(dirNameArray[i])) {
91                              dirNode = dirNode.getNode(dirNameArray[i]);
92                          }
93                          else {
94                              dirNode = dirNode.addNode(
95                                  dirNameArray[i], JCRConstants.NT_FOLDER);
96                          }
97                      }
98                  }
99  
100                 session.save();
101             }
102         }
103         catch (RepositoryException re) {
104             throw new SystemException(re);
105         }
106         finally {
107             if (session != null) {
108                 session.logout();
109             }
110         }
111     }
112 
113     public void addFile(
114             long companyId, String portletId, long groupId, long repositoryId,
115             String fileName, long fileEntryId, String properties,
116             Date modifiedDate, ServiceContext serviceContext, InputStream is)
117         throws PortalException, SystemException {
118 
119         Session session = null;
120 
121         try {
122             session = JCRFactoryUtil.createSession();
123 
124             Node rootNode = getRootNode(session, companyId);
125             Node repositoryNode = getFolderNode(rootNode, repositoryId);
126 
127             if (repositoryNode.hasNode(fileName)) {
128                 throw new DuplicateFileException(fileName);
129             }
130             else {
131                 Node fileNode = repositoryNode.addNode(
132                     fileName, JCRConstants.NT_FILE);
133 
134                 Node contentNode = fileNode.addNode(
135                     JCRConstants.JCR_CONTENT, JCRConstants.NT_RESOURCE);
136 
137                 contentNode.addMixin(JCRConstants.MIX_VERSIONABLE);
138                 contentNode.setProperty(
139                     JCRConstants.JCR_MIME_TYPE, "text/plain");
140                 contentNode.setProperty(JCRConstants.JCR_DATA, is);
141                 contentNode.setProperty(
142                     JCRConstants.JCR_LAST_MODIFIED, Calendar.getInstance());
143 
144                 session.save();
145 
146                 Version version = contentNode.checkin();
147 
148                 contentNode.getVersionHistory().addVersionLabel(
149                     version.getName(), DEFAULT_VERSION, false);
150 
151                 Indexer indexer = IndexerRegistryUtil.getIndexer(
152                     FileModel.class);
153 
154                 FileModel fileModel = new FileModel();
155 
156                 fileModel.setAssetCategoryIds(
157                     serviceContext.getAssetCategoryIds());
158                 fileModel.setAssetTagNames(serviceContext.getAssetTagNames());
159                 fileModel.setCompanyId(companyId);
160                 fileModel.setFileEntryId(fileEntryId);
161                 fileModel.setFileName(fileName);
162                 fileModel.setGroupId(groupId);
163                 fileModel.setModifiedDate(modifiedDate);
164                 fileModel.setPortletId(portletId);
165                 fileModel.setProperties(properties);
166                 fileModel.setRepositoryId(repositoryId);
167 
168                 indexer.reindex(fileModel);
169             }
170         }
171         catch (RepositoryException re) {
172             throw new SystemException(re);
173         }
174         finally {
175             if (session != null) {
176                 session.logout();
177             }
178         }
179     }
180 
181     public void checkRoot(long companyId) throws SystemException {
182         Session session = null;
183 
184         try {
185             session = JCRFactoryUtil.createSession();
186 
187             getRootNode(session, companyId);
188 
189             session.save();
190         }
191         catch (RepositoryException re) {
192             throw new SystemException(re);
193         }
194         finally {
195             if (session != null) {
196                 session.logout();
197             }
198         }
199     }
200 
201     public void deleteDirectory(
202             long companyId, String portletId, long repositoryId, String dirName)
203         throws PortalException {
204 
205         Session session = null;
206 
207         try {
208             session = JCRFactoryUtil.createSession();
209 
210             Node rootNode = getRootNode(session, companyId);
211             Node repositoryNode = getFolderNode(rootNode, repositoryId);
212             Node dirNode = repositoryNode.getNode(dirName);
213 
214             deleteDirectory(companyId, portletId, repositoryId, dirNode);
215 
216             dirNode.remove();
217 
218             session.save();
219         }
220         catch (PathNotFoundException pnfe) {
221             throw new NoSuchDirectoryException(dirName);
222         }
223         catch (RepositoryException re) {
224             String message = GetterUtil.getString(re.getMessage());
225 
226             if (message.contains("failed to resolve path")) {
227                 throw new NoSuchDirectoryException(dirName);
228             }
229             else {
230                 throw new PortalException(re);
231             }
232         }
233         finally {
234             if (session != null) {
235                 session.logout();
236             }
237         }
238     }
239 
240     public void deleteFile(
241             long companyId, String portletId, long repositoryId,
242             String fileName)
243         throws PortalException, SystemException {
244 
245         Session session = null;
246 
247         // A bug in Jackrabbit requires us to create a dummy node and delete the
248         // version tree manually to successfully delete a file
249 
250         // Create a dummy node
251 
252         try {
253             session = JCRFactoryUtil.createSession();
254 
255             Node rootNode = getRootNode(session, companyId);
256             Node repositoryNode = getFolderNode(rootNode, repositoryId);
257             Node fileNode = repositoryNode.getNode(fileName);
258             Node contentNode = fileNode.getNode(JCRConstants.JCR_CONTENT);
259 
260             contentNode.checkout();
261 
262             contentNode.setProperty(JCRConstants.JCR_MIME_TYPE, "text/plain");
263             contentNode.setProperty(JCRConstants.JCR_DATA, "");
264             contentNode.setProperty(
265                 JCRConstants.JCR_LAST_MODIFIED, Calendar.getInstance());
266 
267             session.save();
268 
269             Version version = contentNode.checkin();
270 
271             contentNode.getVersionHistory().addVersionLabel(
272                 version.getName(), "0.0", false);
273         }
274         catch (PathNotFoundException pnfe) {
275             throw new NoSuchFileException(fileName);
276         }
277         catch (RepositoryException re) {
278             throw new SystemException(re);
279         }
280         finally {
281             if (session != null) {
282                 session.logout();
283             }
284         }
285 
286         // Delete version tree
287 
288         try {
289             session = JCRFactoryUtil.createSession();
290 
291             Node rootNode = getRootNode(session, companyId);
292             Node repositoryNode = getFolderNode(rootNode, repositoryId);
293             Node fileNode = repositoryNode.getNode(fileName);
294             Node contentNode = fileNode.getNode(JCRConstants.JCR_CONTENT);
295 
296             VersionHistory versionHistory = contentNode.getVersionHistory();
297 
298             VersionIterator itr = versionHistory.getAllVersions();
299 
300             while (itr.hasNext()) {
301                 Version version = itr.nextVersion();
302 
303                 if (itr.getPosition() == itr.getSize()) {
304                     break;
305                 }
306                 else {
307                     if (!StringUtils.equals(
308                             JCRConstants.JCR_ROOT_VERSION, version.getName())) {
309 
310                         versionHistory.removeVersion(version.getName());
311                     }
312                 }
313             }
314 
315             session.save();
316         }
317         catch (PathNotFoundException pnfe) {
318             throw new NoSuchFileException(fileName);
319         }
320         catch (RepositoryException re) {
321             throw new SystemException(re);
322         }
323         finally {
324             if (session != null) {
325                 session.logout();
326             }
327         }
328 
329         // Delete file
330 
331         try {
332             session = JCRFactoryUtil.createSession();
333 
334             Node rootNode = getRootNode(session, companyId);
335             Node repositoryNode = getFolderNode(rootNode, repositoryId);
336             Node fileNode = repositoryNode.getNode(fileName);
337 
338             Indexer indexer = IndexerRegistryUtil.getIndexer(
339                 FileModel.class);
340 
341             FileModel fileModel = new FileModel();
342 
343             fileModel.setCompanyId(companyId);
344             fileModel.setFileName(fileName);
345             fileModel.setPortletId(portletId);
346             fileModel.setRepositoryId(repositoryId);
347 
348             indexer.delete(fileModel);
349 
350             fileNode.remove();
351 
352             session.save();
353         }
354         catch (PathNotFoundException pnfe) {
355             throw new NoSuchFileException(fileName);
356         }
357         catch (RepositoryException re) {
358             throw new SystemException(re);
359         }
360         finally {
361             if (session != null) {
362                 session.logout();
363             }
364         }
365     }
366 
367     public void deleteFile(
368             long companyId, String portletId, long repositoryId,
369             String fileName, String versionNumber)
370         throws PortalException, SystemException {
371 
372         String versionLabel = versionNumber;
373 
374         Session session = null;
375 
376         try {
377             session = JCRFactoryUtil.createSession();
378 
379             Node rootNode = getRootNode(session, companyId);
380             Node repositoryNode = getFolderNode(rootNode, repositoryId);
381             Node fileNode = repositoryNode.getNode(fileName);
382             Node contentNode = fileNode.getNode(JCRConstants.JCR_CONTENT);
383 
384             VersionHistory versionHistory = contentNode.getVersionHistory();
385 
386             Version version = versionHistory.getVersionByLabel(versionLabel);
387 
388             versionHistory.removeVersion(version.getName());
389 
390             session.save();
391         }
392         catch (PathNotFoundException pnfe) {
393             throw new NoSuchFileException(fileName);
394         }
395         catch (RepositoryException re) {
396             throw new SystemException(re);
397         }
398         finally {
399             if (session != null) {
400                 session.logout();
401             }
402         }
403     }
404 
405     public InputStream getFileAsStream(
406             long companyId, long repositoryId, String fileName,
407             String versionNumber)
408         throws PortalException, SystemException {
409 
410         InputStream is = null;
411 
412         Session session = null;
413 
414         try {
415             session = JCRFactoryUtil.createSession();
416 
417             Node contentNode = getFileContentNode(
418                 session, companyId, repositoryId, fileName, versionNumber);
419 
420             Property data = contentNode.getProperty(JCRConstants.JCR_DATA);
421 
422             is = new UnsyncBufferedInputStream(data.getStream());
423         }
424         catch (RepositoryException re) {
425             throw new SystemException(re);
426         }
427         finally {
428             if (session != null) {
429                 session.logout();
430             }
431         }
432 
433         return is;
434     }
435 
436     public String[] getFileNames(
437             long companyId, long repositoryId, String dirName)
438         throws PortalException, SystemException {
439 
440         List<String> fileNames = new ArrayList<String>();
441 
442         Session session = null;
443 
444         try {
445             session = JCRFactoryUtil.createSession();
446 
447             Node rootNode = getRootNode(session, companyId);
448             Node repositoryNode = getFolderNode(rootNode, repositoryId);
449             Node dirNode = repositoryNode.getNode(dirName);
450 
451             NodeIterator itr = dirNode.getNodes();
452 
453             while (itr.hasNext()) {
454                 Node node = (Node)itr.next();
455 
456                 if (node.getPrimaryNodeType().getName().equals(
457                         JCRConstants.NT_FILE)) {
458 
459                     fileNames.add(dirName + "/" + node.getName());
460                 }
461             }
462         }
463         catch (PathNotFoundException pnfe) {
464             throw new NoSuchDirectoryException(dirName);
465         }
466         catch (RepositoryException re) {
467             throw new SystemException(re);
468         }
469         finally {
470             if (session != null) {
471                 session.logout();
472             }
473         }
474 
475         return fileNames.toArray(new String[fileNames.size()]);
476     }
477 
478     public long getFileSize(
479             long companyId, long repositoryId, String fileName)
480         throws PortalException, SystemException {
481 
482         long size;
483 
484         Session session = null;
485 
486         try {
487             session = JCRFactoryUtil.createSession();
488 
489             Node contentNode = getFileContentNode(
490                 session, companyId, repositoryId, fileName, StringPool.BLANK);
491 
492             size = contentNode.getProperty(JCRConstants.JCR_DATA).getLength();
493         }
494         catch (RepositoryException re) {
495             throw new SystemException(re);
496         }
497         finally {
498             if (session != null) {
499                 session.logout();
500             }
501         }
502 
503         return size;
504     }
505 
506     public boolean hasFile(
507             long companyId, long repositoryId, String fileName,
508             String versionNumber)
509         throws PortalException, SystemException {
510 
511         try {
512             getFileContentNode(
513                 companyId, repositoryId, fileName, versionNumber);
514         }
515         catch (NoSuchFileException nsfe) {
516             return false;
517         }
518 
519         return true;
520     }
521 
522     public void move(String srcDir, String destDir) throws SystemException {
523         Session session = null;
524 
525         try {
526             session = JCRFactoryUtil.createSession();
527 
528             session.move(srcDir, destDir);
529 
530             session.save();
531         }
532         catch (RepositoryException re) {
533             throw new SystemException(re);
534         }
535         finally {
536             if (session != null) {
537                 session.logout();
538             }
539         }
540     }
541 
542     public void reindex(String[] ids) throws SearchException {
543         long companyId = GetterUtil.getLong(ids[0]);
544         String portletId = ids[1];
545         long groupId = GetterUtil.getLong(ids[2]);
546         long repositoryId = GetterUtil.getLong(ids[3]);
547 
548         Session session = null;
549 
550         try {
551             session = JCRFactoryUtil.createSession();
552 
553             Node rootNode = getRootNode(session, companyId);
554             Node repositoryNode = getFolderNode(rootNode, repositoryId);
555 
556             NodeIterator itr = repositoryNode.getNodes();
557 
558             while (itr.hasNext()) {
559                 Node node = (Node)itr.next();
560 
561                 if (node.getPrimaryNodeType().getName().equals(
562                         JCRConstants.NT_FILE)) {
563 
564                     try {
565                         Indexer indexer = IndexerRegistryUtil.getIndexer(
566                             FileModel.class);
567 
568                         FileModel fileModel = new FileModel();
569 
570                         fileModel.setCompanyId(companyId);
571                         fileModel.setFileName(node.getName());
572                         fileModel.setGroupId(groupId);
573                         fileModel.setPortletId(portletId);
574                         fileModel.setRepositoryId(repositoryId);
575 
576                         Document document = indexer.getDocument(fileModel);
577 
578                         SearchEngineUtil.updateDocument(
579                             companyId, document.get(Field.UID), document);
580                     }
581                     catch (Exception e2) {
582                         _log.error("Reindexing " + node.getName(), e2);
583                     }
584                 }
585             }
586         }
587         catch (Exception e1) {
588             throw new SearchException(e1);
589         }
590         finally {
591             try {
592                 if (session != null) {
593                     session.logout();
594                 }
595             }
596             catch (Exception e) {
597                 _log.error(e);
598             }
599         }
600     }
601 
602     public void updateFile(
603             long companyId, String portletId, long groupId, long repositoryId,
604             long newRepositoryId, String fileName, long fileEntryId)
605         throws PortalException, SystemException {
606 
607         Session session = null;
608 
609         try {
610             session = JCRFactoryUtil.createSession();
611 
612             Node rootNode = getRootNode(session, companyId);
613             Node repositoryNode = getFolderNode(rootNode, repositoryId);
614             Node fileNode = repositoryNode.getNode(fileName);
615             Node contentNode = fileNode.getNode(JCRConstants.JCR_CONTENT);
616 
617             Node newRepositoryNode = getFolderNode(rootNode, newRepositoryId);
618 
619             if (newRepositoryNode.hasNode(fileName)) {
620                 throw new DuplicateFileException(fileName);
621             }
622             else {
623                 Node newFileNode = newRepositoryNode.addNode(
624                     fileName, JCRConstants.NT_FILE);
625 
626                 Node newContentNode = newFileNode.addNode(
627                     JCRConstants.JCR_CONTENT, JCRConstants.NT_RESOURCE);
628 
629                 VersionHistory versionHistory = contentNode.getVersionHistory();
630 
631                 String[] versionLabels = versionHistory.getVersionLabels();
632 
633                 for (int i = (versionLabels.length - 1); i >= 0; i--) {
634                     Version version = versionHistory.getVersionByLabel(
635                         versionLabels[i]);
636 
637                     Node frozenContentNode = version.getNode(
638                         JCRConstants.JCR_FROZEN_NODE);
639 
640                     if (i == (versionLabels.length - 1)) {
641                         newContentNode.addMixin(JCRConstants.MIX_VERSIONABLE);
642                     }
643                     else {
644                         newContentNode.checkout();
645                     }
646 
647                     newContentNode.setProperty(
648                         JCRConstants.JCR_MIME_TYPE, "text/plain");
649                     newContentNode.setProperty(
650                         JCRConstants.JCR_DATA,
651                         frozenContentNode.getProperty(
652                             JCRConstants.JCR_DATA).getStream());
653                     newContentNode.setProperty(
654                         JCRConstants.JCR_LAST_MODIFIED, Calendar.getInstance());
655 
656                     session.save();
657 
658                     Version newVersion = newContentNode.checkin();
659 
660                     newContentNode.getVersionHistory().addVersionLabel(
661                         newVersion.getName(), versionLabels[i], false);
662                 }
663 
664                 fileNode.remove();
665 
666                 session.save();
667 
668                 Indexer indexer = IndexerRegistryUtil.getIndexer(
669                     FileModel.class);
670 
671                 FileModel fileModel = new FileModel();
672 
673                 fileModel.setCompanyId(companyId);
674                 fileModel.setFileName(fileName);
675                 fileModel.setPortletId(portletId);
676                 fileModel.setRepositoryId(repositoryId);
677 
678                 indexer.delete(fileModel);
679 
680                 fileModel.setRepositoryId(newRepositoryId);
681                 fileModel.setGroupId(groupId);
682 
683                 indexer.reindex(fileModel);
684             }
685         }
686         catch (PathNotFoundException pnfe) {
687             throw new NoSuchFileException(fileName);
688         }
689         catch (RepositoryException re) {
690             throw new SystemException(re);
691         }
692         finally {
693             if (session != null) {
694                 session.logout();
695             }
696         }
697     }
698 
699     public void updateFile(
700             long companyId, String portletId, long groupId, long repositoryId,
701             String fileName, String versionNumber, String sourceFileName,
702             long fileEntryId, String properties, Date modifiedDate,
703             ServiceContext serviceContext, InputStream is)
704         throws PortalException, SystemException {
705 
706         String versionLabel = versionNumber;
707 
708         Session session = null;
709 
710         try {
711             session = JCRFactoryUtil.createSession();
712 
713             Node rootNode = getRootNode(session, companyId);
714             Node repositoryNode = getFolderNode(rootNode, repositoryId);
715             Node fileNode = repositoryNode.getNode(fileName);
716             Node contentNode = fileNode.getNode(JCRConstants.JCR_CONTENT);
717 
718             contentNode.checkout();
719 
720             contentNode.setProperty(JCRConstants.JCR_MIME_TYPE, "text/plain");
721             contentNode.setProperty(JCRConstants.JCR_DATA, is);
722             contentNode.setProperty(
723                 JCRConstants.JCR_LAST_MODIFIED, Calendar.getInstance());
724 
725             session.save();
726 
727             Version version = contentNode.checkin();
728 
729             contentNode.getVersionHistory().addVersionLabel(
730                 version.getName(), versionLabel, false);
731 
732             Indexer indexer = IndexerRegistryUtil.getIndexer(
733                 FileModel.class);
734 
735             FileModel fileModel = new FileModel();
736 
737             fileModel.setAssetCategoryIds(serviceContext.getAssetCategoryIds());
738             fileModel.setAssetTagNames(serviceContext.getAssetTagNames());
739             fileModel.setCompanyId(companyId);
740             fileModel.setFileEntryId(fileEntryId);
741             fileModel.setFileName(fileName);
742             fileModel.setGroupId(groupId);
743             fileModel.setModifiedDate(modifiedDate);
744             fileModel.setPortletId(portletId);
745             fileModel.setProperties(properties);
746             fileModel.setRepositoryId(repositoryId);
747 
748             indexer.reindex(fileModel);
749         }
750         catch (PathNotFoundException pnfe) {
751             throw new NoSuchFileException(fileName);
752         }
753         catch (RepositoryException re) {
754             throw new SystemException(re);
755         }
756         finally {
757             if (session != null) {
758                 session.logout();
759             }
760         }
761     }
762 
763     public void updateFile(
764             long companyId, String portletId, long groupId, long repositoryId,
765             String fileName, String newFileName, boolean reindex)
766         throws PortalException, SystemException {
767 
768         Session session = null;
769 
770         try {
771             session = JCRFactoryUtil.createSession();
772 
773             Node rootNode = getRootNode(session, companyId);
774             Node repositoryNode = getFolderNode(rootNode, repositoryId);
775             Node fileNode = repositoryNode.getNode(fileName);
776             Node contentNode = fileNode.getNode(JCRConstants.JCR_CONTENT);
777 
778             Node newFileNode = repositoryNode.addNode(
779                 newFileName, JCRConstants.NT_FILE);
780 
781             Node newContentNode = newFileNode.addNode(
782                 JCRConstants.JCR_CONTENT, JCRConstants.NT_RESOURCE);
783 
784             VersionHistory versionHistory = contentNode.getVersionHistory();
785 
786             String[] versionLabels = versionHistory.getVersionLabels();
787 
788             for (int i = (versionLabels.length - 1); i >= 0; i--) {
789                 Version version = versionHistory.getVersionByLabel(
790                     versionLabels[i]);
791 
792                 Node frozenContentNode = version.getNode(
793                     JCRConstants.JCR_FROZEN_NODE);
794 
795                 if (i == (versionLabels.length - 1)) {
796                     newContentNode.addMixin(JCRConstants.MIX_VERSIONABLE);
797                 }
798                 else {
799                     newContentNode.checkout();
800                 }
801 
802                 newContentNode.setProperty(
803                     JCRConstants.JCR_MIME_TYPE, "text/plain");
804                 newContentNode.setProperty(
805                     JCRConstants.JCR_DATA,
806                     frozenContentNode.getProperty(
807                         JCRConstants.JCR_DATA).getStream());
808                 newContentNode.setProperty(
809                     JCRConstants.JCR_LAST_MODIFIED, Calendar.getInstance());
810 
811                 session.save();
812 
813                 Version newVersion = newContentNode.checkin();
814 
815                 newContentNode.getVersionHistory().addVersionLabel(
816                     newVersion.getName(), versionLabels[i], false);
817             }
818 
819             fileNode.remove();
820 
821             session.save();
822 
823             if (reindex) {
824                 Indexer indexer = IndexerRegistryUtil.getIndexer(
825                     FileModel.class);
826 
827                 FileModel fileModel = new FileModel();
828 
829                 fileModel.setCompanyId(companyId);
830                 fileModel.setFileName(fileName);
831                 fileModel.setPortletId(portletId);
832                 fileModel.setRepositoryId(repositoryId);
833 
834                 indexer.delete(fileModel);
835 
836                 fileModel.setFileName(newFileName);
837                 fileModel.setGroupId(groupId);
838 
839                 indexer.reindex(fileModel);
840             }
841         }
842         catch (PathNotFoundException pnfe) {
843             throw new NoSuchFileException(fileName);
844         }
845         catch (RepositoryException re) {
846             throw new SystemException(re);
847         }
848         finally {
849             if (session != null) {
850                 session.logout();
851             }
852         }
853     }
854 
855     protected void deleteDirectory(
856             long companyId, String portletId, long repositoryId, Node dirNode)
857         throws SearchException {
858 
859         try {
860             NodeIterator itr = dirNode.getNodes();
861 
862             FileModel fileModel = new FileModel();
863 
864             fileModel.setCompanyId(companyId);
865             fileModel.setPortletId(portletId);
866             fileModel.setRepositoryId(repositoryId);
867 
868             Indexer indexer = IndexerRegistryUtil.getIndexer(FileModel.class);
869 
870             while (itr.hasNext()) {
871                 Node node = (Node)itr.next();
872 
873                 String primaryNodeTypeName =
874                     node.getPrimaryNodeType().getName();
875 
876                 if (primaryNodeTypeName.equals(JCRConstants.NT_FOLDER)) {
877                     deleteDirectory(companyId, portletId, repositoryId, node);
878                 }
879                 else if (primaryNodeTypeName.equals(JCRConstants.NT_FILE)) {
880                     fileModel.setFileName(node.getName());
881 
882                     indexer.delete(fileModel);
883                 }
884             }
885 
886             fileModel.setFileName(dirNode.getName());
887 
888             indexer.delete(fileModel);
889         }
890         catch (RepositoryException e) {
891             _log.error(e);
892         }
893     }
894 
895     protected Node getFileContentNode(
896             long companyId, long repositoryId, String fileName,
897             String versionNumber)
898         throws PortalException, SystemException {
899 
900         Node contentNode = null;
901 
902         Session session = null;
903 
904         try {
905             session = JCRFactoryUtil.createSession();
906 
907             contentNode = getFileContentNode(
908                 session, companyId, repositoryId, fileName, versionNumber);
909         }
910         catch (RepositoryException re) {
911             throw new SystemException(re);
912         }
913         finally {
914             if (session != null) {
915                 session.logout();
916             }
917         }
918 
919         return contentNode;
920     }
921 
922     protected Node getFileContentNode(
923             Session session, long companyId, long repositoryId,
924             String fileName, String versionNumber)
925         throws PortalException, SystemException {
926 
927         String versionLabel = versionNumber;
928 
929         Node contentNode = null;
930 
931         try {
932             Node rootNode = getRootNode(session, companyId);
933             Node repositoryNode = getFolderNode(rootNode, repositoryId);
934             Node fileNode = repositoryNode.getNode(fileName);
935             contentNode = fileNode.getNode(JCRConstants.JCR_CONTENT);
936 
937             if (Validator.isNotNull(versionNumber)) {
938                 VersionHistory versionHistory =
939                     contentNode.getVersionHistory();
940 
941                 Version version = versionHistory.getVersionByLabel(
942                     versionLabel);
943 
944                 contentNode = version.getNode(JCRConstants.JCR_FROZEN_NODE);
945             }
946         }
947         catch (PathNotFoundException pnfe) {
948             throw new NoSuchFileException(fileName);
949         }
950         catch (RepositoryException re) {
951             throw new SystemException(re);
952         }
953 
954         return contentNode;
955     }
956 
957     protected Node getFolderNode(Node node, long name)
958         throws RepositoryException {
959 
960         return getFolderNode(node, String.valueOf(name));
961     }
962 
963     protected Node getFolderNode(Node node, String name)
964         throws RepositoryException {
965 
966         Node folderNode = null;
967 
968         if (node.hasNode(name)) {
969             folderNode = node.getNode(name);
970         }
971         else {
972             folderNode = node.addNode(name, JCRConstants.NT_FOLDER);
973         }
974 
975         return folderNode;
976     }
977 
978     protected Node getRootNode(Session session, long companyId)
979         throws RepositoryException {
980 
981         Node companyNode = getFolderNode(session.getRootNode(), companyId);
982 
983         return getFolderNode(companyNode, JCRFactory.NODE_DOCUMENTLIBRARY);
984     }
985 
986     private static Log _log = LogFactoryUtil.getLog(JCRHook.class);
987 
988 }