001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
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                    Session session = null;
647    
648                    try {
649                            session = JCRFactoryUtil.createSession();
650    
651                            Node rootNode = getRootNode(session, companyId);
652    
653                            Node repositoryNode = getFolderNode(rootNode, repositoryId);
654    
655                            if (fileName.contains(StringPool.SLASH)) {
656                                    String path = fileName.substring(
657                                            0, fileName.lastIndexOf(StringPool.SLASH));
658    
659                                    fileName = fileName.substring(path.length() + 1);
660    
661                                    repositoryNode = getFolderNode(repositoryNode, path);
662                            }
663    
664                            Node newRepositoryNode = getFolderNode(rootNode, newRepositoryId);
665    
666                            if (newRepositoryNode.hasNode(fileName)) {
667                                    throw new DuplicateFileException(fileName);
668                            }
669    
670                            Node fileNode = repositoryNode.getNode(fileName);
671    
672                            Node contentNode = fileNode.getNode(JCRConstants.JCR_CONTENT);
673    
674                            String contentNodePath = contentNode.getPath();
675    
676                            Node newFileNode = newRepositoryNode.addNode(
677                                    fileName, JCRConstants.NT_FILE);
678    
679                            String newContentNodePath = newFileNode.getPath().concat(
680                                    StringPool.SLASH).concat(JCRConstants.JCR_CONTENT);
681    
682                            session.move(contentNodePath, newContentNodePath);
683    
684                            fileNode.remove();
685    
686                            session.save();
687                    }
688                    catch (PathNotFoundException pnfe) {
689                            throw new NoSuchFileException(fileName);
690                    }
691                    catch (RepositoryException re) {
692                            throw new SystemException(re);
693                    }
694                    finally {
695                            JCRFactoryUtil.closeSession(session);
696                    }
697            }
698    
699            @Override
700            public void updateFile(
701                            long companyId, long repositoryId, String fileName,
702                            String newFileName)
703                    throws PortalException, SystemException {
704    
705                    Session session = null;
706    
707                    try {
708                            session = JCRFactoryUtil.createSession();
709    
710                            Node rootNode = getRootNode(session, companyId);
711    
712                            Node repositoryNode = getFolderNode(rootNode, repositoryId);
713    
714                            if (fileName.contains(StringPool.SLASH)) {
715                                    String path = fileName.substring(
716                                            0, fileName.lastIndexOf(StringPool.SLASH));
717    
718                                    fileName = fileName.substring(path.length() + 1);
719    
720                                    repositoryNode = getFolderNode(repositoryNode, path);
721                            }
722    
723                            if (repositoryNode.hasNode(newFileName)) {
724                                    throw new DuplicateFileException(newFileName);
725                            }
726    
727                            Node fileNode = repositoryNode.getNode(fileName);
728    
729                            Node contentNode = fileNode.getNode(JCRConstants.JCR_CONTENT);
730    
731                            String contentNodePath = contentNode.getPath();
732    
733                            Node newFileNode = repositoryNode.addNode(
734                                    newFileName, JCRConstants.NT_FILE);
735    
736                            String newContentNodePath = newFileNode.getPath().concat(
737                                    StringPool.SLASH).concat(JCRConstants.JCR_CONTENT);
738    
739                            session.move(contentNodePath, newContentNodePath);
740    
741                            fileNode.remove();
742    
743                            session.save();
744                    }
745                    catch (PathNotFoundException pnfe) {
746                            throw new NoSuchFileException(fileName);
747                    }
748                    catch (RepositoryException re) {
749                            throw new SystemException(re);
750                    }
751                    finally {
752                            JCRFactoryUtil.closeSession(session);
753                    }
754            }
755    
756            @Override
757            public void updateFile(
758                            long companyId, long repositoryId, String fileName,
759                            String versionLabel, InputStream is)
760                    throws PortalException, SystemException {
761    
762                    Session session = null;
763    
764                    try {
765                            session = JCRFactoryUtil.createSession();
766    
767                            Workspace workspace = session.getWorkspace();
768    
769                            VersionManager versionManager = workspace.getVersionManager();
770    
771                            Node rootNode = getRootNode(session, companyId);
772    
773                            Node repositoryNode = getFolderNode(rootNode, repositoryId);
774    
775                            if (fileName.contains(StringPool.SLASH)) {
776                                    String path = fileName.substring(
777                                            0, fileName.lastIndexOf(StringPool.SLASH));
778    
779                                    fileName = fileName.substring(path.length() + 1);
780    
781                                    repositoryNode = getFolderNode(repositoryNode, path);
782                            }
783    
784                            Node fileNode = repositoryNode.getNode(fileName);
785    
786                            Node contentNode = fileNode.getNode(JCRConstants.JCR_CONTENT);
787    
788                            versionManager.checkout(contentNode.getPath());
789    
790                            contentNode.setProperty(
791                                    JCRConstants.JCR_MIME_TYPE, ContentTypes.TEXT_PLAIN);
792    
793                            ValueFactory valueFactory = session.getValueFactory();
794    
795                            Binary binary = valueFactory.createBinary(is);
796    
797                            contentNode.setProperty(JCRConstants.JCR_DATA, binary);
798    
799                            contentNode.setProperty(
800                                    JCRConstants.JCR_LAST_MODIFIED, Calendar.getInstance());
801    
802                            session.save();
803    
804                            Version version = versionManager.checkin(contentNode.getPath());
805    
806                            VersionHistory versionHistory = versionManager.getVersionHistory(
807                                    contentNode.getPath());
808    
809                            versionHistory.addVersionLabel(
810                                    version.getName(), versionLabel,
811                                    PropsValues.DL_STORE_JCR_MOVE_VERSION_LABELS);
812                    }
813                    catch (PathNotFoundException pnfe) {
814                            throw new NoSuchFileException(
815                                    "{fileName=" + fileName + ", versionLabel=" + versionLabel +
816                                            "}");
817                    }
818                    catch (RepositoryException re) {
819                            throw new SystemException(re);
820                    }
821                    finally {
822                            JCRFactoryUtil.closeSession(session);
823                    }
824            }
825    
826            protected Node getFileContentNode(
827                            long companyId, long repositoryId, String fileName,
828                            String versionLabel)
829                    throws PortalException, SystemException {
830    
831                    Node contentNode = null;
832    
833                    Session session = null;
834    
835                    try {
836                            session = JCRFactoryUtil.createSession();
837    
838                            contentNode = getFileContentNode(
839                                    session, companyId, repositoryId, fileName, versionLabel);
840                    }
841                    catch (RepositoryException re) {
842                            throw new SystemException(re);
843                    }
844                    finally {
845                            JCRFactoryUtil.closeSession(session);
846                    }
847    
848                    return contentNode;
849            }
850    
851            protected Node getFileContentNode(
852                            Session session, long companyId, long repositoryId, String fileName,
853                            String versionLabel)
854                    throws PortalException, SystemException {
855    
856                    Node contentNode = null;
857    
858                    try {
859                            Workspace workspace = session.getWorkspace();
860    
861                            VersionManager versionManager = workspace.getVersionManager();
862    
863                            Node rootNode = getRootNode(session, companyId);
864    
865                            Node repositoryNode = getFolderNode(rootNode, repositoryId);
866    
867                            Node fileNode = repositoryNode.getNode(fileName);
868    
869                            contentNode = fileNode.getNode(JCRConstants.JCR_CONTENT);
870    
871                            if (Validator.isNotNull(versionLabel)) {
872                                    VersionHistory versionHistory =
873                                            versionManager.getVersionHistory(contentNode.getPath());
874    
875                                    if (!versionHistory.hasVersionLabel(versionLabel)) {
876                                            throw new NoSuchFileException(
877                                                    "{fileName=" + fileName + ", versionLabel=" +
878                                                            versionLabel + "}");
879                                    }
880    
881                                    Version version = versionHistory.getVersionByLabel(
882                                            versionLabel);
883    
884                                    contentNode = version.getNode(JCRConstants.JCR_FROZEN_NODE);
885                            }
886                    }
887                    catch (PathNotFoundException pnfe) {
888                            throw new NoSuchFileException(
889                                    "{fileName=" + fileName + ", versionLabel=" +
890                                            versionLabel + "}");
891                    }
892                    catch (RepositoryException re) {
893                            throw new SystemException(re);
894                    }
895    
896                    return contentNode;
897            }
898    
899            protected Node getFolderNode(Node node, long name)
900                    throws RepositoryException {
901    
902                    return getFolderNode(node, String.valueOf(name));
903            }
904    
905            protected Node getFolderNode(Node node, String name)
906                    throws RepositoryException {
907    
908                    if (name.contains(StringPool.SLASH)) {
909                            String[] nameParts = name.split(StringPool.SLASH, 2);
910    
911                            node = getFolderNode(node, nameParts[0]);
912    
913                            return getFolderNode(node, nameParts[1]);
914                    }
915    
916                    Node folderNode = null;
917    
918                    if (node.hasNode(name)) {
919                            folderNode = node.getNode(name);
920                    }
921                    else {
922                            folderNode = node.addNode(name, JCRConstants.NT_FOLDER);
923                    }
924    
925                    return folderNode;
926            }
927    
928            protected Node getRootNode(Session session, long companyId)
929                    throws RepositoryException {
930    
931                    Node companyNode = getFolderNode(session.getRootNode(), companyId);
932    
933                    return getFolderNode(companyNode, JCRFactory.NODE_DOCUMENTLIBRARY);
934            }
935    
936    }