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.Indexer;
32  import com.liferay.portal.kernel.search.IndexerRegistryUtil;
33  import com.liferay.portal.kernel.search.SearchEngineUtil;
34  import com.liferay.portal.kernel.search.SearchException;
35  import com.liferay.portal.kernel.util.GetterUtil;
36  import com.liferay.portal.kernel.util.StringPool;
37  import com.liferay.portal.kernel.util.StringUtil;
38  import com.liferay.portal.kernel.util.Validator;
39  import com.liferay.portal.service.ServiceContext;
40  
41  import java.io.InputStream;
42  
43  import java.util.ArrayList;
44  import java.util.Calendar;
45  import java.util.Collection;
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         Collection<Document> documents = new ArrayList<Document>();
549 
550         Session session = null;
551 
552         try {
553             session = JCRFactoryUtil.createSession();
554 
555             Node rootNode = getRootNode(session, companyId);
556             Node repositoryNode = getFolderNode(rootNode, repositoryId);
557 
558             NodeIterator itr = repositoryNode.getNodes();
559 
560             while (itr.hasNext()) {
561                 Node node = (Node)itr.next();
562 
563                 if (node.getPrimaryNodeType().getName().equals(
564                         JCRConstants.NT_FILE)) {
565 
566                     Indexer indexer = IndexerRegistryUtil.getIndexer(
567                         FileModel.class);
568 
569                     FileModel fileModel = new FileModel();
570 
571                     fileModel.setCompanyId(companyId);
572                     fileModel.setFileName(node.getName());
573                     fileModel.setGroupId(groupId);
574                     fileModel.setPortletId(portletId);
575                     fileModel.setRepositoryId(repositoryId);
576 
577                     Document document = indexer.getDocument(fileModel);
578 
579                     if (document == null) {
580                         continue;
581                     }
582 
583                     documents.add(document);
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         SearchEngineUtil.updateDocuments(companyId, documents);
602     }
603 
604     public void updateFile(
605             long companyId, String portletId, long groupId, long repositoryId,
606             long newRepositoryId, String fileName, long fileEntryId)
607         throws PortalException, SystemException {
608 
609         Session session = null;
610 
611         try {
612             session = JCRFactoryUtil.createSession();
613 
614             Node rootNode = getRootNode(session, companyId);
615             Node repositoryNode = getFolderNode(rootNode, repositoryId);
616             Node fileNode = repositoryNode.getNode(fileName);
617             Node contentNode = fileNode.getNode(JCRConstants.JCR_CONTENT);
618 
619             Node newRepositoryNode = getFolderNode(rootNode, newRepositoryId);
620 
621             if (newRepositoryNode.hasNode(fileName)) {
622                 throw new DuplicateFileException(fileName);
623             }
624             else {
625                 Node newFileNode = newRepositoryNode.addNode(
626                     fileName, JCRConstants.NT_FILE);
627 
628                 Node newContentNode = newFileNode.addNode(
629                     JCRConstants.JCR_CONTENT, JCRConstants.NT_RESOURCE);
630 
631                 VersionHistory versionHistory = contentNode.getVersionHistory();
632 
633                 String[] versionLabels = versionHistory.getVersionLabels();
634 
635                 for (int i = (versionLabels.length - 1); i >= 0; i--) {
636                     Version version = versionHistory.getVersionByLabel(
637                         versionLabels[i]);
638 
639                     Node frozenContentNode = version.getNode(
640                         JCRConstants.JCR_FROZEN_NODE);
641 
642                     if (i == (versionLabels.length - 1)) {
643                         newContentNode.addMixin(JCRConstants.MIX_VERSIONABLE);
644                     }
645                     else {
646                         newContentNode.checkout();
647                     }
648 
649                     newContentNode.setProperty(
650                         JCRConstants.JCR_MIME_TYPE, "text/plain");
651                     newContentNode.setProperty(
652                         JCRConstants.JCR_DATA,
653                         frozenContentNode.getProperty(
654                             JCRConstants.JCR_DATA).getStream());
655                     newContentNode.setProperty(
656                         JCRConstants.JCR_LAST_MODIFIED, Calendar.getInstance());
657 
658                     session.save();
659 
660                     Version newVersion = newContentNode.checkin();
661 
662                     newContentNode.getVersionHistory().addVersionLabel(
663                         newVersion.getName(), versionLabels[i], false);
664                 }
665 
666                 fileNode.remove();
667 
668                 session.save();
669 
670                 Indexer indexer = IndexerRegistryUtil.getIndexer(
671                     FileModel.class);
672 
673                 FileModel fileModel = new FileModel();
674 
675                 fileModel.setCompanyId(companyId);
676                 fileModel.setFileName(fileName);
677                 fileModel.setPortletId(portletId);
678                 fileModel.setRepositoryId(repositoryId);
679 
680                 indexer.delete(fileModel);
681 
682                 fileModel.setRepositoryId(newRepositoryId);
683                 fileModel.setGroupId(groupId);
684 
685                 indexer.reindex(fileModel);
686             }
687         }
688         catch (PathNotFoundException pnfe) {
689             throw new NoSuchFileException(fileName);
690         }
691         catch (RepositoryException re) {
692             throw new SystemException(re);
693         }
694         finally {
695             if (session != null) {
696                 session.logout();
697             }
698         }
699     }
700 
701     public void updateFile(
702             long companyId, String portletId, long groupId, long repositoryId,
703             String fileName, String newFileName, boolean reindex)
704         throws PortalException, SystemException {
705 
706         Session session = null;
707 
708         try {
709             session = JCRFactoryUtil.createSession();
710 
711             Node rootNode = getRootNode(session, companyId);
712             Node repositoryNode = getFolderNode(rootNode, repositoryId);
713             Node fileNode = repositoryNode.getNode(fileName);
714             Node contentNode = fileNode.getNode(JCRConstants.JCR_CONTENT);
715 
716             Node newFileNode = repositoryNode.addNode(
717                 newFileName, JCRConstants.NT_FILE);
718 
719             Node newContentNode = newFileNode.addNode(
720                 JCRConstants.JCR_CONTENT, JCRConstants.NT_RESOURCE);
721 
722             VersionHistory versionHistory = contentNode.getVersionHistory();
723 
724             String[] versionLabels = versionHistory.getVersionLabels();
725 
726             for (int i = (versionLabels.length - 1); i >= 0; i--) {
727                 Version version = versionHistory.getVersionByLabel(
728                     versionLabels[i]);
729 
730                 Node frozenContentNode = version.getNode(
731                     JCRConstants.JCR_FROZEN_NODE);
732 
733                 if (i == (versionLabels.length - 1)) {
734                     newContentNode.addMixin(JCRConstants.MIX_VERSIONABLE);
735                 }
736                 else {
737                     newContentNode.checkout();
738                 }
739 
740                 newContentNode.setProperty(
741                     JCRConstants.JCR_MIME_TYPE, "text/plain");
742                 newContentNode.setProperty(
743                     JCRConstants.JCR_DATA,
744                     frozenContentNode.getProperty(
745                         JCRConstants.JCR_DATA).getStream());
746                 newContentNode.setProperty(
747                     JCRConstants.JCR_LAST_MODIFIED, Calendar.getInstance());
748 
749                 session.save();
750 
751                 Version newVersion = newContentNode.checkin();
752 
753                 newContentNode.getVersionHistory().addVersionLabel(
754                     newVersion.getName(), versionLabels[i], false);
755             }
756 
757             fileNode.remove();
758 
759             session.save();
760 
761             if (reindex) {
762                 Indexer indexer = IndexerRegistryUtil.getIndexer(
763                     FileModel.class);
764 
765                 FileModel fileModel = new FileModel();
766 
767                 fileModel.setCompanyId(companyId);
768                 fileModel.setFileName(fileName);
769                 fileModel.setPortletId(portletId);
770                 fileModel.setRepositoryId(repositoryId);
771 
772                 indexer.delete(fileModel);
773 
774                 fileModel.setFileName(newFileName);
775                 fileModel.setGroupId(groupId);
776 
777                 indexer.reindex(fileModel);
778             }
779         }
780         catch (PathNotFoundException pnfe) {
781             throw new NoSuchFileException(fileName);
782         }
783         catch (RepositoryException re) {
784             throw new SystemException(re);
785         }
786         finally {
787             if (session != null) {
788                 session.logout();
789             }
790         }
791     }
792 
793     public void updateFile(
794             long companyId, String portletId, long groupId, long repositoryId,
795             String fileName, String versionNumber, String sourceFileName,
796             long fileEntryId, String properties, Date modifiedDate,
797             ServiceContext serviceContext, InputStream is)
798         throws PortalException, SystemException {
799 
800         String versionLabel = versionNumber;
801 
802         Session session = null;
803 
804         try {
805             session = JCRFactoryUtil.createSession();
806 
807             Node rootNode = getRootNode(session, companyId);
808             Node repositoryNode = getFolderNode(rootNode, repositoryId);
809             Node fileNode = repositoryNode.getNode(fileName);
810             Node contentNode = fileNode.getNode(JCRConstants.JCR_CONTENT);
811 
812             contentNode.checkout();
813 
814             contentNode.setProperty(JCRConstants.JCR_MIME_TYPE, "text/plain");
815             contentNode.setProperty(JCRConstants.JCR_DATA, is);
816             contentNode.setProperty(
817                 JCRConstants.JCR_LAST_MODIFIED, Calendar.getInstance());
818 
819             session.save();
820 
821             Version version = contentNode.checkin();
822 
823             contentNode.getVersionHistory().addVersionLabel(
824                 version.getName(), versionLabel, false);
825 
826             Indexer indexer = IndexerRegistryUtil.getIndexer(
827                 FileModel.class);
828 
829             FileModel fileModel = new FileModel();
830 
831             fileModel.setAssetCategoryIds(serviceContext.getAssetCategoryIds());
832             fileModel.setAssetTagNames(serviceContext.getAssetTagNames());
833             fileModel.setCompanyId(companyId);
834             fileModel.setFileEntryId(fileEntryId);
835             fileModel.setFileName(fileName);
836             fileModel.setGroupId(groupId);
837             fileModel.setModifiedDate(modifiedDate);
838             fileModel.setPortletId(portletId);
839             fileModel.setProperties(properties);
840             fileModel.setRepositoryId(repositoryId);
841 
842             indexer.reindex(fileModel);
843         }
844         catch (PathNotFoundException pnfe) {
845             throw new NoSuchFileException(fileName);
846         }
847         catch (RepositoryException re) {
848             throw new SystemException(re);
849         }
850         finally {
851             if (session != null) {
852                 session.logout();
853             }
854         }
855     }
856 
857     protected void deleteDirectory(
858             long companyId, String portletId, long repositoryId, Node dirNode)
859         throws SearchException {
860 
861         try {
862             NodeIterator itr = dirNode.getNodes();
863 
864             FileModel fileModel = new FileModel();
865 
866             fileModel.setCompanyId(companyId);
867             fileModel.setPortletId(portletId);
868             fileModel.setRepositoryId(repositoryId);
869 
870             Indexer indexer = IndexerRegistryUtil.getIndexer(FileModel.class);
871 
872             while (itr.hasNext()) {
873                 Node node = (Node)itr.next();
874 
875                 String primaryNodeTypeName =
876                     node.getPrimaryNodeType().getName();
877 
878                 if (primaryNodeTypeName.equals(JCRConstants.NT_FOLDER)) {
879                     deleteDirectory(companyId, portletId, repositoryId, node);
880                 }
881                 else if (primaryNodeTypeName.equals(JCRConstants.NT_FILE)) {
882                     fileModel.setFileName(node.getName());
883 
884                     indexer.delete(fileModel);
885                 }
886             }
887 
888             fileModel.setFileName(dirNode.getName());
889 
890             indexer.delete(fileModel);
891         }
892         catch (RepositoryException e) {
893             _log.error(e);
894         }
895     }
896 
897     protected Node getFileContentNode(
898             long companyId, long repositoryId, String fileName,
899             String versionNumber)
900         throws PortalException, SystemException {
901 
902         Node contentNode = null;
903 
904         Session session = null;
905 
906         try {
907             session = JCRFactoryUtil.createSession();
908 
909             contentNode = getFileContentNode(
910                 session, companyId, repositoryId, fileName, versionNumber);
911         }
912         catch (RepositoryException re) {
913             throw new SystemException(re);
914         }
915         finally {
916             if (session != null) {
917                 session.logout();
918             }
919         }
920 
921         return contentNode;
922     }
923 
924     protected Node getFileContentNode(
925             Session session, long companyId, long repositoryId,
926             String fileName, String versionNumber)
927         throws PortalException, SystemException {
928 
929         String versionLabel = versionNumber;
930 
931         Node contentNode = null;
932 
933         try {
934             Node rootNode = getRootNode(session, companyId);
935             Node repositoryNode = getFolderNode(rootNode, repositoryId);
936             Node fileNode = repositoryNode.getNode(fileName);
937             contentNode = fileNode.getNode(JCRConstants.JCR_CONTENT);
938 
939             if (Validator.isNotNull(versionNumber)) {
940                 VersionHistory versionHistory =
941                     contentNode.getVersionHistory();
942 
943                 Version version = versionHistory.getVersionByLabel(
944                     versionLabel);
945 
946                 contentNode = version.getNode(JCRConstants.JCR_FROZEN_NODE);
947             }
948         }
949         catch (PathNotFoundException pnfe) {
950             throw new NoSuchFileException(fileName);
951         }
952         catch (RepositoryException re) {
953             throw new SystemException(re);
954         }
955 
956         return contentNode;
957     }
958 
959     protected Node getFolderNode(Node node, long name)
960         throws RepositoryException {
961 
962         return getFolderNode(node, String.valueOf(name));
963     }
964 
965     protected Node getFolderNode(Node node, String name)
966         throws RepositoryException {
967 
968         Node folderNode = null;
969 
970         if (node.hasNode(name)) {
971             folderNode = node.getNode(name);
972         }
973         else {
974             folderNode = node.addNode(name, JCRConstants.NT_FOLDER);
975         }
976 
977         return folderNode;
978     }
979 
980     protected Node getRootNode(Session session, long companyId)
981         throws RepositoryException {
982 
983         Node companyNode = getFolderNode(session.getRootNode(), companyId);
984 
985         return getFolderNode(companyNode, JCRFactory.NODE_DOCUMENTLIBRARY);
986     }
987 
988     private static Log _log = LogFactoryUtil.getLog(JCRHook.class);
989 
990 }