001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portlet.documentlibrary.store;
016    
017    import com.liferay.portal.jcr.JCRConstants;
018    import com.liferay.portal.jcr.JCRFactory;
019    import com.liferay.portal.jcr.JCRFactoryUtil;
020    import com.liferay.portal.kernel.exception.PortalException;
021    import com.liferay.portal.kernel.exception.SystemException;
022    import com.liferay.portal.kernel.util.ContentTypes;
023    import com.liferay.portal.kernel.util.GetterUtil;
024    import com.liferay.portal.kernel.util.StringPool;
025    import com.liferay.portal.kernel.util.StringUtil;
026    import com.liferay.portal.kernel.util.Validator;
027    import com.liferay.portal.util.PropsValues;
028    import com.liferay.portlet.documentlibrary.DuplicateDirectoryException;
029    import com.liferay.portlet.documentlibrary.DuplicateFileException;
030    import com.liferay.portlet.documentlibrary.NoSuchDirectoryException;
031    import com.liferay.portlet.documentlibrary.NoSuchFileException;
032    
033    import java.io.InputStream;
034    
035    import java.util.ArrayList;
036    import java.util.Calendar;
037    import java.util.List;
038    import java.util.Map;
039    
040    import javax.jcr.Binary;
041    import javax.jcr.Node;
042    import javax.jcr.NodeIterator;
043    import javax.jcr.PathNotFoundException;
044    import javax.jcr.Property;
045    import javax.jcr.RepositoryException;
046    import javax.jcr.Session;
047    import javax.jcr.Value;
048    import javax.jcr.ValueFactory;
049    import javax.jcr.Workspace;
050    import javax.jcr.nodetype.NodeType;
051    import javax.jcr.version.Version;
052    import javax.jcr.version.VersionHistory;
053    import javax.jcr.version.VersionIterator;
054    import javax.jcr.version.VersionManager;
055    
056    import org.apache.commons.lang.StringUtils;
057    
058    /**
059     * @author Michael Young
060     * @author Brian Wing Shun Chan
061     * @author Edward Han
062     */
063    public class JCRStore extends BaseStore {
064    
065            @Override
066            public void addDirectory(long companyId, long repositoryId, String dirName)
067                    throws PortalException, SystemException {
068    
069                    Session session = null;
070    
071                    try {
072                            session = JCRFactoryUtil.createSession();
073    
074                            Node rootNode = getRootNode(session, companyId);
075    
076                            Node repositoryNode = getFolderNode(rootNode, repositoryId);
077    
078                            if (repositoryNode.hasNode(dirName)) {
079                                    throw new DuplicateDirectoryException(dirName);
080                            }
081    
082                            String[] dirNameArray = StringUtil.split(dirName, '/');
083    
084                            Node dirNode = repositoryNode;
085    
086                            for (int i = 0; i < dirNameArray.length; i++) {
087                                    if (Validator.isNotNull(dirNameArray[i])) {
088                                            if (dirNode.hasNode(dirNameArray[i])) {
089                                                    dirNode = dirNode.getNode(dirNameArray[i]);
090                                            }
091                                            else {
092                                                    dirNode = dirNode.addNode(
093                                                            dirNameArray[i], JCRConstants.NT_FOLDER);
094                                            }
095                                    }
096                            }
097    
098                            session.save();
099                    }
100                    catch (RepositoryException re) {
101                            throw new SystemException(re);
102                    }
103                    finally {
104                            JCRFactoryUtil.closeSession(session);
105                    }
106            }
107    
108            @Override
109            public void addFile(
110                            long companyId, long repositoryId, String fileName, InputStream is)
111                    throws PortalException, SystemException {
112    
113                    Session session = null;
114    
115                    try {
116                            session = JCRFactoryUtil.createSession();
117    
118                            Workspace workspace = session.getWorkspace();
119    
120                            VersionManager versionManager = workspace.getVersionManager();
121    
122                            Node rootNode = getRootNode(session, companyId);
123    
124                            Node repositoryNode = getFolderNode(rootNode, repositoryId);
125    
126                            if (fileName.contains(StringPool.SLASH)) {
127                                    String path = fileName.substring(
128                                            0, fileName.lastIndexOf(StringPool.SLASH));
129    
130                                    fileName = fileName.substring(path.length() + 1);
131    
132                                    repositoryNode = getFolderNode(repositoryNode, path);
133                            }
134    
135                            if (repositoryNode.hasNode(fileName)) {
136                                    throw new DuplicateFileException(fileName);
137                            }
138    
139                            Node fileNode = repositoryNode.addNode(
140                                    fileName, JCRConstants.NT_FILE);
141    
142                            Node contentNode = fileNode.addNode(
143                                    JCRConstants.JCR_CONTENT, JCRConstants.NT_RESOURCE);
144    
145                            contentNode.addMixin(JCRConstants.MIX_VERSIONABLE);
146                            contentNode.setProperty(
147                                    JCRConstants.JCR_MIME_TYPE, ContentTypes.TEXT_PLAIN);
148    
149                            ValueFactory valueFactory = session.getValueFactory();
150    
151                            Binary binary = valueFactory.createBinary(is);
152    
153                            contentNode.setProperty(JCRConstants.JCR_DATA, binary);
154    
155                            contentNode.setProperty(
156                                    JCRConstants.JCR_LAST_MODIFIED, Calendar.getInstance());
157    
158                            session.save();
159    
160                            Version version = versionManager.checkin(contentNode.getPath());
161    
162                            VersionHistory versionHistory = versionManager.getVersionHistory(
163                                    contentNode.getPath());
164    
165                            versionHistory.addVersionLabel(
166                                    version.getName(), VERSION_DEFAULT, false);
167                    }
168                    catch (RepositoryException re) {
169                            throw new SystemException(re);
170                    }
171                    finally {
172                            JCRFactoryUtil.closeSession(session);
173                    }
174            }
175    
176            @Override
177            public void checkRoot(long companyId) throws SystemException {
178                    Session session = null;
179    
180                    try {
181                            session = JCRFactoryUtil.createSession();
182    
183                            getRootNode(session, companyId);
184    
185                            session.save();
186                    }
187                    catch (RepositoryException re) {
188                            throw new SystemException(re);
189                    }
190                    finally {
191                            JCRFactoryUtil.closeSession(session);
192                    }
193            }
194    
195            @Override
196            public void deleteDirectory(
197                            long companyId, long repositoryId, String dirName)
198                    throws PortalException {
199    
200                    Session session = null;
201    
202                    try {
203                            session = JCRFactoryUtil.createSession();
204    
205                            Node rootNode = getRootNode(session, companyId);
206    
207                            Node repositoryNode = getFolderNode(rootNode, repositoryId);
208    
209                            Node dirNode = repositoryNode.getNode(dirName);
210    
211                            dirNode.remove();
212    
213                            session.save();
214                    }
215                    catch (PathNotFoundException pnfe) {
216                            throw new NoSuchDirectoryException(dirName);
217                    }
218                    catch (RepositoryException re) {
219                            String message = GetterUtil.getString(re.getMessage());
220    
221                            if (message.contains("failed to resolve path")) {
222                                    throw new NoSuchDirectoryException(dirName);
223                            }
224                            else {
225                                    throw new PortalException(re);
226                            }
227                    }
228                    finally {
229                            JCRFactoryUtil.closeSession(session);
230                    }
231            }
232    
233            @Override
234            public void deleteFile(long companyId, long repositoryId, String fileName)
235                    throws PortalException, SystemException {
236    
237                    Session session = null;
238    
239                    // A bug in Jackrabbit requires us to create a dummy node and delete the
240                    // version tree manually to successfully delete a file
241    
242                    // Create a dummy node
243    
244                    try {
245                            session = JCRFactoryUtil.createSession();
246    
247                            Workspace workspace = session.getWorkspace();
248    
249                            VersionManager versionManager = workspace.getVersionManager();
250    
251                            Node rootNode = getRootNode(session, companyId);
252    
253                            Node repositoryNode = getFolderNode(rootNode, repositoryId);
254    
255                            Node fileNode = repositoryNode.getNode(fileName);
256    
257                            Node contentNode = fileNode.getNode(JCRConstants.JCR_CONTENT);
258    
259                            versionManager.checkout(contentNode.getPath());
260    
261                            contentNode.setProperty(
262                                    JCRConstants.JCR_MIME_TYPE, ContentTypes.TEXT_PLAIN);
263                            contentNode.setProperty(JCRConstants.JCR_DATA, StringPool.BLANK);
264                            contentNode.setProperty(
265                                    JCRConstants.JCR_LAST_MODIFIED, Calendar.getInstance());
266    
267                            session.save();
268    
269                            Version version = versionManager.checkin(contentNode.getPath());
270    
271                            VersionHistory versionHistory = versionManager.getVersionHistory(
272                                    contentNode.getPath());
273    
274                            versionHistory.addVersionLabel(version.getName(), "0.0", false);
275                    }
276                    catch (PathNotFoundException pnfe) {
277                            throw new NoSuchFileException(fileName);
278                    }
279                    catch (RepositoryException re) {
280                            throw new SystemException(re);
281                    }
282                    finally {
283                            JCRFactoryUtil.closeSession(session);
284                    }
285    
286                    // Delete version tree
287    
288                    try {
289                            session = JCRFactoryUtil.createSession();
290    
291                            Workspace workspace = session.getWorkspace();
292    
293                            VersionManager versionManager = workspace.getVersionManager();
294    
295                            Node rootNode = getRootNode(session, companyId);
296    
297                            Node repositoryNode = getFolderNode(rootNode, repositoryId);
298    
299                            Node fileNode = repositoryNode.getNode(fileName);
300    
301                            Node contentNode = fileNode.getNode(JCRConstants.JCR_CONTENT);
302    
303                            VersionHistory versionHistory = versionManager.getVersionHistory(
304                                    contentNode.getPath());
305    
306                            VersionIterator itr = versionHistory.getAllVersions();
307    
308                            while (itr.hasNext()) {
309                                    Version version = itr.nextVersion();
310    
311                                    if (itr.getPosition() == itr.getSize()) {
312                                            break;
313                                    }
314                                    else {
315                                            if (!StringUtils.equals(
316                                                            JCRConstants.JCR_ROOT_VERSION, version.getName())) {
317    
318                                                    versionHistory.removeVersion(version.getName());
319                                            }
320                                    }
321                            }
322    
323                            session.save();
324                    }
325                    catch (PathNotFoundException pnfe) {
326                            throw new NoSuchFileException(fileName);
327                    }
328                    catch (RepositoryException re) {
329                            throw new SystemException(re);
330                    }
331                    finally {
332                            JCRFactoryUtil.closeSession(session);
333                    }
334    
335                    // Delete file
336    
337                    try {
338                            session = JCRFactoryUtil.createSession();
339    
340                            Node rootNode = getRootNode(session, companyId);
341    
342                            Node repositoryNode = getFolderNode(rootNode, repositoryId);
343    
344                            Node fileNode = repositoryNode.getNode(fileName);
345    
346                            fileNode.remove();
347    
348                            session.save();
349                    }
350                    catch (PathNotFoundException pnfe) {
351                            throw new NoSuchFileException(fileName);
352                    }
353                    catch (RepositoryException re) {
354                            throw new SystemException(re);
355                    }
356                    finally {
357                            JCRFactoryUtil.closeSession(session);
358                    }
359            }
360    
361            @Override
362            public void deleteFile(
363                            long companyId, long repositoryId, String fileName,
364                            String versionLabel)
365                    throws PortalException, SystemException {
366    
367                    Session session = null;
368    
369                    try {
370                            session = JCRFactoryUtil.createSession();
371    
372                            Workspace workspace = session.getWorkspace();
373    
374                            VersionManager versionManager = workspace.getVersionManager();
375    
376                            Node rootNode = getRootNode(session, companyId);
377    
378                            Node repositoryNode = getFolderNode(rootNode, repositoryId);
379    
380                            Node fileNode = repositoryNode.getNode(fileName);
381    
382                            Node contentNode = fileNode.getNode(JCRConstants.JCR_CONTENT);
383    
384                            VersionHistory versionHistory = versionManager.getVersionHistory(
385                                    contentNode.getPath());
386    
387                            if (!versionHistory.hasVersionLabel(versionLabel)) {
388                                    throw new NoSuchFileException(
389                                            "{fileName=" + fileName + ", versionLabel=" +
390                                                    versionLabel + "}");
391                            }
392    
393                            Version version = versionHistory.getVersionByLabel(versionLabel);
394    
395                            Version linearPredecessorVersion = version.getLinearPredecessor();
396    
397                            if (version.getLinearSuccessor() == null) {
398                                    Version restoreVersion = linearPredecessorVersion;
399    
400                                    if (Validator.equals(
401                                                    JCRConstants.JCR_ROOT_VERSION,
402                                                    linearPredecessorVersion.getName())) {
403    
404                                            versionManager.checkout(contentNode.getPath());
405    
406                                            restoreVersion = versionManager.checkin(
407                                                    contentNode.getPath());
408                                    }
409    
410                                    versionManager.restore(restoreVersion, true);
411                            }
412    
413                            versionHistory.removeVersion(version.getName());
414    
415                            session.save();
416                    }
417                    catch (PathNotFoundException pnfe) {
418                            throw new NoSuchFileException(
419                                    "{fileName=" + fileName + ", versionLabel=" +
420                                            versionLabel + "}");
421                    }
422                    catch (RepositoryException re) {
423                            throw new SystemException(re);
424                    }
425                    finally {
426                            JCRFactoryUtil.closeSession(session);
427                    }
428            }
429    
430            @Override
431            public InputStream getFileAsStream(
432                            long companyId, long repositoryId, String fileName,
433                            String versionLabel)
434                    throws PortalException, SystemException {
435    
436                    Session session = null;
437    
438                    try {
439                            session = JCRFactoryUtil.createSession();
440    
441                            Node contentNode = getFileContentNode(
442                                    session, companyId, repositoryId, fileName, versionLabel);
443    
444                            Property property = contentNode.getProperty(JCRConstants.JCR_DATA);
445    
446                            Value value = property.getValue();
447    
448                            Binary binary = value.getBinary();
449    
450                            if (session instanceof Map) {
451                                    Map<String, Binary> mapSession = (Map<String, Binary>)session;
452    
453                                    mapSession.put(fileName, binary);
454                            }
455    
456                            return binary.getStream();
457                    }
458                    catch (RepositoryException re) {
459                            throw new SystemException(re);
460                    }
461                    finally {
462                            JCRFactoryUtil.closeSession(session);
463                    }
464            }
465    
466            @Override
467            public String[] getFileNames(long companyId, long repositoryId)
468                    throws SystemException {
469    
470                    List<String> fileNames = new ArrayList<String>();
471    
472                    Session session = null;
473    
474                    try {
475                            session = JCRFactoryUtil.createSession();
476    
477                            Node rootNode = getRootNode(session, companyId);
478    
479                            Node repositoryNode = getFolderNode(rootNode, repositoryId);
480    
481                            NodeIterator itr = repositoryNode.getNodes();
482    
483                            while (itr.hasNext()) {
484                                    Node node = (Node)itr.next();
485    
486                                    NodeType primaryNodeType = node.getPrimaryNodeType();
487    
488                                    String primaryNodeTypeName = primaryNodeType.getName();
489    
490                                    if (primaryNodeTypeName.equals(JCRConstants.NT_FILE)) {
491                                            fileNames.add(node.getName());
492                                    }
493                            }
494                    }
495                    catch (Exception e) {
496                            throw new SystemException(e);
497                    }
498                    finally {
499                            JCRFactoryUtil.closeSession(session);
500                    }
501    
502                    return fileNames.toArray(new String[fileNames.size()]);
503            }
504    
505            @Override
506            public String[] getFileNames(
507                            long companyId, long repositoryId, String dirName)
508                    throws PortalException, SystemException {
509    
510                    List<String> fileNames = new ArrayList<String>();
511    
512                    Session session = null;
513    
514                    try {
515                            session = JCRFactoryUtil.createSession();
516    
517                            Node rootNode = getRootNode(session, companyId);
518    
519                            Node repositoryNode = getFolderNode(rootNode, repositoryId);
520    
521                            Node dirNode = repositoryNode.getNode(dirName);
522    
523                            NodeIterator itr = dirNode.getNodes();
524    
525                            while (itr.hasNext()) {
526                                    Node node = (Node)itr.next();
527    
528                                    NodeType primaryNodeType = node.getPrimaryNodeType();
529    
530                                    String primaryNodeTypeName = primaryNodeType.getName();
531    
532                                    if (primaryNodeTypeName.equals(JCRConstants.NT_FILE)) {
533                                            fileNames.add(dirName + "/" + node.getName());
534                                    }
535                            }
536                    }
537                    catch (PathNotFoundException pnfe) {
538                            throw new NoSuchDirectoryException(dirName);
539                    }
540                    catch (RepositoryException re) {
541                            throw new SystemException(re);
542                    }
543                    finally {
544                            JCRFactoryUtil.closeSession(session);
545                    }
546    
547                    return fileNames.toArray(new String[fileNames.size()]);
548            }
549    
550            @Override
551            public long getFileSize(long companyId, long repositoryId, String fileName)
552                    throws PortalException, SystemException {
553    
554                    long size;
555    
556                    Session session = null;
557    
558                    try {
559                            session = JCRFactoryUtil.createSession();
560    
561                            Node contentNode = getFileContentNode(
562                                    session, companyId, repositoryId, fileName, StringPool.BLANK);
563    
564                            size = contentNode.getProperty(JCRConstants.JCR_DATA).getLength();
565                    }
566                    catch (RepositoryException re) {
567                            throw new SystemException(re);
568                    }
569                    finally {
570                            JCRFactoryUtil.closeSession(session);
571                    }
572    
573                    return size;
574            }
575    
576            @Override
577            public boolean hasDirectory(
578                            long companyId, long repositoryId, String dirName)
579                    throws SystemException {
580    
581                    Session session = null;
582    
583                    try {
584                            session = JCRFactoryUtil.createSession();
585    
586                            Node rootNode = getRootNode(session, companyId);
587    
588                            Node repositoryNode = getFolderNode(rootNode, repositoryId);
589    
590                            repositoryNode.getNode(dirName);
591    
592                            return true;
593                    }
594                    catch (PathNotFoundException pnfe) {
595                            return false;
596                    }
597                    catch (RepositoryException re) {
598                            throw new SystemException(re);
599                    }
600                    finally {
601                            JCRFactoryUtil.closeSession(session);
602                    }
603            }
604    
605            @Override
606            public boolean hasFile(
607                            long companyId, long repositoryId, String fileName,
608                            String versionLabel)
609                    throws PortalException, SystemException {
610    
611                    try {
612                            getFileContentNode(companyId, repositoryId, fileName, versionLabel);
613                    }
614                    catch (NoSuchFileException nsfe) {
615                            return false;
616                    }
617    
618                    return true;
619            }
620    
621            @Override
622            public void move(String srcDir, String destDir) throws SystemException {
623                    Session session = null;
624    
625                    try {
626                            session = JCRFactoryUtil.createSession();
627    
628                            session.move(srcDir, destDir);
629    
630                            session.save();
631                    }
632                    catch (RepositoryException re) {
633                            throw new SystemException(re);
634                    }
635                    finally {
636                            JCRFactoryUtil.closeSession(session);
637                    }
638            }
639    
640            @Override
641            public void updateFile(
642                            long companyId, long repositoryId, long newRepositoryId,
643                            String fileName)
644                    throws PortalException, SystemException {
645    
646                    if (repositoryId == newRepositoryId) {
647                            throw new DuplicateFileException(
648                                    String.format(
649                                            "{companyId=%s, fileName=%s, repositoryId=%s}", companyId,
650                                            fileName, repositoryId));
651                    }
652    
653                    Session session = null;
654    
655                    try {
656                            session = JCRFactoryUtil.createSession();
657    
658                            Node rootNode = getRootNode(session, companyId);
659    
660                            Node repositoryNode = getFolderNode(rootNode, repositoryId);
661    
662                            if (fileName.contains(StringPool.SLASH)) {
663                                    String path = fileName.substring(
664                                            0, fileName.lastIndexOf(StringPool.SLASH));
665    
666                                    fileName = fileName.substring(path.length() + 1);
667    
668                                    repositoryNode = getFolderNode(repositoryNode, path);
669                            }
670    
671                            Node newRepositoryNode = getFolderNode(rootNode, newRepositoryId);
672    
673                            if (newRepositoryNode.hasNode(fileName)) {
674                                    throw new DuplicateFileException(fileName);
675                            }
676    
677                            Node fileNode = repositoryNode.getNode(fileName);
678    
679                            Node contentNode = fileNode.getNode(JCRConstants.JCR_CONTENT);
680    
681                            String contentNodePath = contentNode.getPath();
682    
683                            Node newFileNode = newRepositoryNode.addNode(
684                                    fileName, JCRConstants.NT_FILE);
685    
686                            String newContentNodePath = newFileNode.getPath().concat(
687                                    StringPool.SLASH).concat(JCRConstants.JCR_CONTENT);
688    
689                            session.move(contentNodePath, newContentNodePath);
690    
691                            fileNode.remove();
692    
693                            session.save();
694                    }
695                    catch (PathNotFoundException pnfe) {
696                            throw new NoSuchFileException(fileName);
697                    }
698                    catch (RepositoryException re) {
699                            throw new SystemException(re);
700                    }
701                    finally {
702                            JCRFactoryUtil.closeSession(session);
703                    }
704            }
705    
706            @Override
707            public void updateFile(
708                            long companyId, long repositoryId, String fileName,
709                            String newFileName)
710                    throws PortalException, SystemException {
711    
712                    if (fileName.equals(newFileName)) {
713                            throw new DuplicateFileException(
714                                    String.format(
715                                            "{companyId=%s, fileName=%s, repositoryId=%s}", companyId,
716                                            fileName, repositoryId));
717                    }
718    
719                    Session session = null;
720    
721                    try {
722                            session = JCRFactoryUtil.createSession();
723    
724                            Node rootNode = getRootNode(session, companyId);
725    
726                            Node repositoryNode = getFolderNode(rootNode, repositoryId);
727    
728                            if (fileName.contains(StringPool.SLASH)) {
729                                    String path = fileName.substring(
730                                            0, fileName.lastIndexOf(StringPool.SLASH));
731    
732                                    fileName = fileName.substring(path.length() + 1);
733    
734                                    repositoryNode = getFolderNode(repositoryNode, path);
735                            }
736    
737                            if (repositoryNode.hasNode(newFileName)) {
738                                    throw new DuplicateFileException(newFileName);
739                            }
740    
741                            Node fileNode = repositoryNode.getNode(fileName);
742    
743                            Node contentNode = fileNode.getNode(JCRConstants.JCR_CONTENT);
744    
745                            String contentNodePath = contentNode.getPath();
746    
747                            Node newFileNode = repositoryNode.addNode(
748                                    newFileName, JCRConstants.NT_FILE);
749    
750                            String newContentNodePath = newFileNode.getPath().concat(
751                                    StringPool.SLASH).concat(JCRConstants.JCR_CONTENT);
752    
753                            session.move(contentNodePath, newContentNodePath);
754    
755                            fileNode.remove();
756    
757                            session.save();
758                    }
759                    catch (PathNotFoundException pnfe) {
760                            throw new NoSuchFileException(fileName);
761                    }
762                    catch (RepositoryException re) {
763                            throw new SystemException(re);
764                    }
765                    finally {
766                            JCRFactoryUtil.closeSession(session);
767                    }
768            }
769    
770            @Override
771            public void updateFile(
772                            long companyId, long repositoryId, String fileName,
773                            String versionLabel, InputStream is)
774                    throws PortalException, SystemException {
775    
776                    Session session = null;
777    
778                    try {
779                            session = JCRFactoryUtil.createSession();
780    
781                            Workspace workspace = session.getWorkspace();
782    
783                            VersionManager versionManager = workspace.getVersionManager();
784    
785                            Node rootNode = getRootNode(session, companyId);
786    
787                            Node repositoryNode = getFolderNode(rootNode, repositoryId);
788    
789                            if (fileName.contains(StringPool.SLASH)) {
790                                    String path = fileName.substring(
791                                            0, fileName.lastIndexOf(StringPool.SLASH));
792    
793                                    fileName = fileName.substring(path.length() + 1);
794    
795                                    repositoryNode = getFolderNode(repositoryNode, path);
796                            }
797    
798                            Node fileNode = repositoryNode.getNode(fileName);
799    
800                            Node contentNode = fileNode.getNode(JCRConstants.JCR_CONTENT);
801    
802                            versionManager.checkout(contentNode.getPath());
803    
804                            contentNode.setProperty(
805                                    JCRConstants.JCR_MIME_TYPE, ContentTypes.TEXT_PLAIN);
806    
807                            ValueFactory valueFactory = session.getValueFactory();
808    
809                            Binary binary = valueFactory.createBinary(is);
810    
811                            contentNode.setProperty(JCRConstants.JCR_DATA, binary);
812    
813                            contentNode.setProperty(
814                                    JCRConstants.JCR_LAST_MODIFIED, Calendar.getInstance());
815    
816                            session.save();
817    
818                            Version version = versionManager.checkin(contentNode.getPath());
819    
820                            VersionHistory versionHistory = versionManager.getVersionHistory(
821                                    contentNode.getPath());
822    
823                            versionHistory.addVersionLabel(
824                                    version.getName(), versionLabel,
825                                    PropsValues.DL_STORE_JCR_MOVE_VERSION_LABELS);
826                    }
827                    catch (PathNotFoundException pnfe) {
828                            throw new NoSuchFileException(
829                                    "{fileName=" + fileName + ", versionLabel=" + versionLabel +
830                                            "}");
831                    }
832                    catch (RepositoryException re) {
833                            throw new SystemException(re);
834                    }
835                    finally {
836                            JCRFactoryUtil.closeSession(session);
837                    }
838            }
839    
840            protected Node getFileContentNode(
841                            long companyId, long repositoryId, String fileName,
842                            String versionLabel)
843                    throws PortalException, SystemException {
844    
845                    Node contentNode = null;
846    
847                    Session session = null;
848    
849                    try {
850                            session = JCRFactoryUtil.createSession();
851    
852                            contentNode = getFileContentNode(
853                                    session, companyId, repositoryId, fileName, versionLabel);
854                    }
855                    catch (RepositoryException re) {
856                            throw new SystemException(re);
857                    }
858                    finally {
859                            JCRFactoryUtil.closeSession(session);
860                    }
861    
862                    return contentNode;
863            }
864    
865            protected Node getFileContentNode(
866                            Session session, long companyId, long repositoryId, String fileName,
867                            String versionLabel)
868                    throws PortalException, SystemException {
869    
870                    Node contentNode = null;
871    
872                    try {
873                            Workspace workspace = session.getWorkspace();
874    
875                            VersionManager versionManager = workspace.getVersionManager();
876    
877                            Node rootNode = getRootNode(session, companyId);
878    
879                            Node repositoryNode = getFolderNode(rootNode, repositoryId);
880    
881                            Node fileNode = repositoryNode.getNode(fileName);
882    
883                            contentNode = fileNode.getNode(JCRConstants.JCR_CONTENT);
884    
885                            if (Validator.isNotNull(versionLabel)) {
886                                    VersionHistory versionHistory =
887                                            versionManager.getVersionHistory(contentNode.getPath());
888    
889                                    if (!versionHistory.hasVersionLabel(versionLabel)) {
890                                            throw new NoSuchFileException(
891                                                    "{fileName=" + fileName + ", versionLabel=" +
892                                                            versionLabel + "}");
893                                    }
894    
895                                    Version version = versionHistory.getVersionByLabel(
896                                            versionLabel);
897    
898                                    contentNode = version.getNode(JCRConstants.JCR_FROZEN_NODE);
899                            }
900                    }
901                    catch (PathNotFoundException pnfe) {
902                            throw new NoSuchFileException(
903                                    "{fileName=" + fileName + ", versionLabel=" +
904                                            versionLabel + "}");
905                    }
906                    catch (RepositoryException re) {
907                            throw new SystemException(re);
908                    }
909    
910                    return contentNode;
911            }
912    
913            protected Node getFolderNode(Node node, long name)
914                    throws RepositoryException {
915    
916                    return getFolderNode(node, String.valueOf(name));
917            }
918    
919            protected Node getFolderNode(Node node, String name)
920                    throws RepositoryException {
921    
922                    if (name.contains(StringPool.SLASH)) {
923                            String[] nameParts = name.split(StringPool.SLASH, 2);
924    
925                            node = getFolderNode(node, nameParts[0]);
926    
927                            return getFolderNode(node, nameParts[1]);
928                    }
929    
930                    Node folderNode = null;
931    
932                    if (node.hasNode(name)) {
933                            folderNode = node.getNode(name);
934                    }
935                    else {
936                            folderNode = node.addNode(name, JCRConstants.NT_FOLDER);
937                    }
938    
939                    return folderNode;
940            }
941    
942            protected Node getRootNode(Session session, long companyId)
943                    throws RepositoryException {
944    
945                    Node companyNode = getFolderNode(session.getRootNode(), companyId);
946    
947                    return getFolderNode(companyNode, JCRFactory.NODE_DOCUMENTLIBRARY);
948            }
949    
950    }