001    /**
002     * Copyright (c) 2000-present 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 {
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 {
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) {
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 {
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 {
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 {
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                    List<String> fileNames = new ArrayList<String>();
469    
470                    Session session = null;
471    
472                    try {
473                            session = JCRFactoryUtil.createSession();
474    
475                            Node rootNode = getRootNode(session, companyId);
476    
477                            Node repositoryNode = getFolderNode(rootNode, repositoryId);
478    
479                            NodeIterator itr = repositoryNode.getNodes();
480    
481                            while (itr.hasNext()) {
482                                    Node node = (Node)itr.next();
483    
484                                    NodeType primaryNodeType = node.getPrimaryNodeType();
485    
486                                    String primaryNodeTypeName = primaryNodeType.getName();
487    
488                                    if (primaryNodeTypeName.equals(JCRConstants.NT_FILE)) {
489                                            fileNames.add(node.getName());
490                                    }
491                            }
492                    }
493                    catch (Exception e) {
494                            throw new SystemException(e);
495                    }
496                    finally {
497                            JCRFactoryUtil.closeSession(session);
498                    }
499    
500                    return fileNames.toArray(new String[fileNames.size()]);
501            }
502    
503            @Override
504            public String[] getFileNames(
505                            long companyId, long repositoryId, String dirName)
506                    throws PortalException {
507    
508                    List<String> fileNames = new ArrayList<String>();
509    
510                    Session session = null;
511    
512                    try {
513                            session = JCRFactoryUtil.createSession();
514    
515                            Node rootNode = getRootNode(session, companyId);
516    
517                            Node repositoryNode = getFolderNode(rootNode, repositoryId);
518    
519                            Node dirNode = repositoryNode.getNode(dirName);
520    
521                            NodeIterator itr = dirNode.getNodes();
522    
523                            while (itr.hasNext()) {
524                                    Node node = (Node)itr.next();
525    
526                                    NodeType primaryNodeType = node.getPrimaryNodeType();
527    
528                                    String primaryNodeTypeName = primaryNodeType.getName();
529    
530                                    if (primaryNodeTypeName.equals(JCRConstants.NT_FILE)) {
531                                            fileNames.add(dirName + "/" + node.getName());
532                                    }
533                            }
534                    }
535                    catch (PathNotFoundException pnfe) {
536                            throw new NoSuchDirectoryException(dirName);
537                    }
538                    catch (RepositoryException re) {
539                            throw new SystemException(re);
540                    }
541                    finally {
542                            JCRFactoryUtil.closeSession(session);
543                    }
544    
545                    return fileNames.toArray(new String[fileNames.size()]);
546            }
547    
548            @Override
549            public long getFileSize(long companyId, long repositoryId, String fileName)
550                    throws PortalException {
551    
552                    long size;
553    
554                    Session session = null;
555    
556                    try {
557                            session = JCRFactoryUtil.createSession();
558    
559                            Node contentNode = getFileContentNode(
560                                    session, companyId, repositoryId, fileName, StringPool.BLANK);
561    
562                            size = contentNode.getProperty(JCRConstants.JCR_DATA).getLength();
563                    }
564                    catch (RepositoryException re) {
565                            throw new SystemException(re);
566                    }
567                    finally {
568                            JCRFactoryUtil.closeSession(session);
569                    }
570    
571                    return size;
572            }
573    
574            @Override
575            public boolean hasDirectory(
576                    long companyId, long repositoryId, String dirName) {
577    
578                    Session session = null;
579    
580                    try {
581                            session = JCRFactoryUtil.createSession();
582    
583                            Node rootNode = getRootNode(session, companyId);
584    
585                            Node repositoryNode = getFolderNode(rootNode, repositoryId);
586    
587                            repositoryNode.getNode(dirName);
588    
589                            return true;
590                    }
591                    catch (PathNotFoundException pnfe) {
592                            return false;
593                    }
594                    catch (RepositoryException re) {
595                            throw new SystemException(re);
596                    }
597                    finally {
598                            JCRFactoryUtil.closeSession(session);
599                    }
600            }
601    
602            @Override
603            public boolean hasFile(
604                            long companyId, long repositoryId, String fileName,
605                            String versionLabel)
606                    throws PortalException {
607    
608                    try {
609                            getFileContentNode(companyId, repositoryId, fileName, versionLabel);
610                    }
611                    catch (NoSuchFileException nsfe) {
612                            return false;
613                    }
614    
615                    return true;
616            }
617    
618            @Override
619            public void move(String srcDir, String destDir) {
620                    Session session = null;
621    
622                    try {
623                            session = JCRFactoryUtil.createSession();
624    
625                            session.move(srcDir, destDir);
626    
627                            session.save();
628                    }
629                    catch (RepositoryException re) {
630                            throw new SystemException(re);
631                    }
632                    finally {
633                            JCRFactoryUtil.closeSession(session);
634                    }
635            }
636    
637            @Override
638            public void updateFile(
639                            long companyId, long repositoryId, long newRepositoryId,
640                            String fileName)
641                    throws PortalException {
642    
643                    Session session = null;
644    
645                    try {
646                            session = JCRFactoryUtil.createSession();
647    
648                            Node rootNode = getRootNode(session, companyId);
649    
650                            Node repositoryNode = getFolderNode(rootNode, repositoryId);
651    
652                            if (fileName.contains(StringPool.SLASH)) {
653                                    String path = fileName.substring(
654                                            0, fileName.lastIndexOf(StringPool.SLASH));
655    
656                                    fileName = fileName.substring(path.length() + 1);
657    
658                                    repositoryNode = getFolderNode(repositoryNode, path);
659                            }
660    
661                            Node newRepositoryNode = getFolderNode(rootNode, newRepositoryId);
662    
663                            if (newRepositoryNode.hasNode(fileName)) {
664                                    throw new DuplicateFileException(fileName);
665                            }
666    
667                            Node fileNode = repositoryNode.getNode(fileName);
668    
669                            Node contentNode = fileNode.getNode(JCRConstants.JCR_CONTENT);
670    
671                            String contentNodePath = contentNode.getPath();
672    
673                            Node newFileNode = newRepositoryNode.addNode(
674                                    fileName, JCRConstants.NT_FILE);
675    
676                            String newContentNodePath = newFileNode.getPath().concat(
677                                    StringPool.SLASH).concat(JCRConstants.JCR_CONTENT);
678    
679                            session.move(contentNodePath, newContentNodePath);
680    
681                            fileNode.remove();
682    
683                            session.save();
684                    }
685                    catch (PathNotFoundException pnfe) {
686                            throw new NoSuchFileException(fileName);
687                    }
688                    catch (RepositoryException re) {
689                            throw new SystemException(re);
690                    }
691                    finally {
692                            JCRFactoryUtil.closeSession(session);
693                    }
694            }
695    
696            @Override
697            public void updateFile(
698                            long companyId, long repositoryId, String fileName,
699                            String newFileName)
700                    throws PortalException {
701    
702                    Session session = null;
703    
704                    try {
705                            session = JCRFactoryUtil.createSession();
706    
707                            Node rootNode = getRootNode(session, companyId);
708    
709                            Node repositoryNode = getFolderNode(rootNode, repositoryId);
710    
711                            if (fileName.contains(StringPool.SLASH)) {
712                                    String path = fileName.substring(
713                                            0, fileName.lastIndexOf(StringPool.SLASH));
714    
715                                    fileName = fileName.substring(path.length() + 1);
716    
717                                    repositoryNode = getFolderNode(repositoryNode, path);
718                            }
719    
720                            if (repositoryNode.hasNode(newFileName)) {
721                                    throw new DuplicateFileException(newFileName);
722                            }
723    
724                            Node fileNode = repositoryNode.getNode(fileName);
725    
726                            Node contentNode = fileNode.getNode(JCRConstants.JCR_CONTENT);
727    
728                            String contentNodePath = contentNode.getPath();
729    
730                            Node newFileNode = repositoryNode.addNode(
731                                    newFileName, JCRConstants.NT_FILE);
732    
733                            String newContentNodePath = newFileNode.getPath().concat(
734                                    StringPool.SLASH).concat(JCRConstants.JCR_CONTENT);
735    
736                            session.move(contentNodePath, newContentNodePath);
737    
738                            fileNode.remove();
739    
740                            session.save();
741                    }
742                    catch (PathNotFoundException pnfe) {
743                            throw new NoSuchFileException(fileName);
744                    }
745                    catch (RepositoryException re) {
746                            throw new SystemException(re);
747                    }
748                    finally {
749                            JCRFactoryUtil.closeSession(session);
750                    }
751            }
752    
753            @Override
754            public void updateFile(
755                            long companyId, long repositoryId, String fileName,
756                            String versionLabel, InputStream is)
757                    throws PortalException {
758    
759                    Session session = null;
760    
761                    try {
762                            session = JCRFactoryUtil.createSession();
763    
764                            Workspace workspace = session.getWorkspace();
765    
766                            VersionManager versionManager = workspace.getVersionManager();
767    
768                            Node rootNode = getRootNode(session, companyId);
769    
770                            Node repositoryNode = getFolderNode(rootNode, repositoryId);
771    
772                            if (fileName.contains(StringPool.SLASH)) {
773                                    String path = fileName.substring(
774                                            0, fileName.lastIndexOf(StringPool.SLASH));
775    
776                                    fileName = fileName.substring(path.length() + 1);
777    
778                                    repositoryNode = getFolderNode(repositoryNode, path);
779                            }
780    
781                            Node fileNode = repositoryNode.getNode(fileName);
782    
783                            Node contentNode = fileNode.getNode(JCRConstants.JCR_CONTENT);
784    
785                            versionManager.checkout(contentNode.getPath());
786    
787                            contentNode.setProperty(
788                                    JCRConstants.JCR_MIME_TYPE, ContentTypes.TEXT_PLAIN);
789    
790                            ValueFactory valueFactory = session.getValueFactory();
791    
792                            Binary binary = valueFactory.createBinary(is);
793    
794                            contentNode.setProperty(JCRConstants.JCR_DATA, binary);
795    
796                            contentNode.setProperty(
797                                    JCRConstants.JCR_LAST_MODIFIED, Calendar.getInstance());
798    
799                            session.save();
800    
801                            Version version = versionManager.checkin(contentNode.getPath());
802    
803                            VersionHistory versionHistory = versionManager.getVersionHistory(
804                                    contentNode.getPath());
805    
806                            versionHistory.addVersionLabel(
807                                    version.getName(), versionLabel,
808                                    PropsValues.DL_STORE_JCR_MOVE_VERSION_LABELS);
809                    }
810                    catch (PathNotFoundException pnfe) {
811                            throw new NoSuchFileException(
812                                    "{fileName=" + fileName + ", versionLabel=" + versionLabel +
813                                            "}");
814                    }
815                    catch (RepositoryException re) {
816                            throw new SystemException(re);
817                    }
818                    finally {
819                            JCRFactoryUtil.closeSession(session);
820                    }
821            }
822    
823            protected Node getFileContentNode(
824                            long companyId, long repositoryId, String fileName,
825                            String versionLabel)
826                    throws PortalException {
827    
828                    Node contentNode = null;
829    
830                    Session session = null;
831    
832                    try {
833                            session = JCRFactoryUtil.createSession();
834    
835                            contentNode = getFileContentNode(
836                                    session, companyId, repositoryId, fileName, versionLabel);
837                    }
838                    catch (RepositoryException re) {
839                            throw new SystemException(re);
840                    }
841                    finally {
842                            JCRFactoryUtil.closeSession(session);
843                    }
844    
845                    return contentNode;
846            }
847    
848            protected Node getFileContentNode(
849                            Session session, long companyId, long repositoryId, String fileName,
850                            String versionLabel)
851                    throws PortalException {
852    
853                    Node contentNode = null;
854    
855                    try {
856                            Workspace workspace = session.getWorkspace();
857    
858                            VersionManager versionManager = workspace.getVersionManager();
859    
860                            Node rootNode = getRootNode(session, companyId);
861    
862                            Node repositoryNode = getFolderNode(rootNode, repositoryId);
863    
864                            Node fileNode = repositoryNode.getNode(fileName);
865    
866                            contentNode = fileNode.getNode(JCRConstants.JCR_CONTENT);
867    
868                            if (Validator.isNotNull(versionLabel)) {
869                                    VersionHistory versionHistory =
870                                            versionManager.getVersionHistory(contentNode.getPath());
871    
872                                    if (!versionHistory.hasVersionLabel(versionLabel)) {
873                                            throw new NoSuchFileException(
874                                                    "{fileName=" + fileName + ", versionLabel=" +
875                                                            versionLabel + "}");
876                                    }
877    
878                                    Version version = versionHistory.getVersionByLabel(
879                                            versionLabel);
880    
881                                    contentNode = version.getNode(JCRConstants.JCR_FROZEN_NODE);
882                            }
883                    }
884                    catch (PathNotFoundException pnfe) {
885                            throw new NoSuchFileException(
886                                    "{fileName=" + fileName + ", versionLabel=" +
887                                            versionLabel + "}");
888                    }
889                    catch (RepositoryException re) {
890                            throw new SystemException(re);
891                    }
892    
893                    return contentNode;
894            }
895    
896            protected Node getFolderNode(Node node, long name)
897                    throws RepositoryException {
898    
899                    return getFolderNode(node, String.valueOf(name));
900            }
901    
902            protected Node getFolderNode(Node node, String name)
903                    throws RepositoryException {
904    
905                    if (name.contains(StringPool.SLASH)) {
906                            String[] nameParts = name.split(StringPool.SLASH, 2);
907    
908                            node = getFolderNode(node, nameParts[0]);
909    
910                            return getFolderNode(node, nameParts[1]);
911                    }
912    
913                    Node folderNode = null;
914    
915                    if (node.hasNode(name)) {
916                            folderNode = node.getNode(name);
917                    }
918                    else {
919                            folderNode = node.addNode(name, JCRConstants.NT_FOLDER);
920                    }
921    
922                    return folderNode;
923            }
924    
925            protected Node getRootNode(Session session, long companyId)
926                    throws RepositoryException {
927    
928                    Node companyNode = getFolderNode(session.getRootNode(), companyId);
929    
930                    return getFolderNode(companyNode, JCRFactory.NODE_DOCUMENTLIBRARY);
931            }
932    
933    }