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