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