001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portlet.documentlibrary.store;
016    
017    import com.liferay.portal.jcr.JCRConstants;
018    import com.liferay.portal.jcr.JCRFactory;
019    import com.liferay.portal.jcr.JCRFactoryUtil;
020    import com.liferay.portal.kernel.exception.PortalException;
021    import com.liferay.portal.kernel.exception.SystemException;
022    import com.liferay.portal.kernel.util.GetterUtil;
023    import com.liferay.portal.kernel.util.StringPool;
024    import com.liferay.portal.kernel.util.StringUtil;
025    import com.liferay.portal.kernel.util.Validator;
026    import com.liferay.portal.util.PropsValues;
027    import com.liferay.portlet.documentlibrary.DuplicateDirectoryException;
028    import com.liferay.portlet.documentlibrary.DuplicateFileException;
029    import com.liferay.portlet.documentlibrary.NoSuchDirectoryException;
030    import com.liferay.portlet.documentlibrary.NoSuchFileException;
031    
032    import java.io.InputStream;
033    
034    import java.util.ArrayList;
035    import java.util.Calendar;
036    import java.util.List;
037    import java.util.Map;
038    
039    import javax.jcr.Binary;
040    import javax.jcr.Node;
041    import javax.jcr.NodeIterator;
042    import javax.jcr.PathNotFoundException;
043    import javax.jcr.Property;
044    import javax.jcr.RepositoryException;
045    import javax.jcr.Session;
046    import javax.jcr.Value;
047    import javax.jcr.ValueFactory;
048    import javax.jcr.Workspace;
049    import javax.jcr.nodetype.NodeType;
050    import javax.jcr.version.Version;
051    import javax.jcr.version.VersionHistory;
052    import javax.jcr.version.VersionIterator;
053    import javax.jcr.version.VersionManager;
054    
055    import org.apache.commons.lang.StringUtils;
056    
057    /**
058     * @author Michael Young
059     * @author Brian Wing Shun Chan
060     * @author Edward Han
061     */
062    public class JCRStore extends BaseStore {
063    
064            @Override
065            public void addDirectory(long companyId, long repositoryId, String dirName)
066                    throws PortalException, SystemException {
067    
068                    Session session = null;
069    
070                    try {
071                            session = JCRFactoryUtil.createSession();
072    
073                            Node rootNode = getRootNode(session, companyId);
074    
075                            Node repositoryNode = getFolderNode(rootNode, repositoryId);
076    
077                            if (repositoryNode.hasNode(dirName)) {
078                                    throw new DuplicateDirectoryException(dirName);
079                            }
080                            else {
081                                    String[] dirNameArray = StringUtil.split(dirName, '/');
082    
083                                    Node dirNode = repositoryNode;
084    
085                                    for (int i = 0; i < dirNameArray.length; i++) {
086                                            if (Validator.isNotNull(dirNameArray[i])) {
087                                                    if (dirNode.hasNode(dirNameArray[i])) {
088                                                            dirNode = dirNode.getNode(dirNameArray[i]);
089                                                    }
090                                                    else {
091                                                            dirNode = dirNode.addNode(
092                                                                    dirNameArray[i], JCRConstants.NT_FOLDER);
093                                                    }
094                                            }
095                                    }
096    
097                                    session.save();
098                            }
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                            else {
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, "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                    }
169                    catch (RepositoryException re) {
170                            throw new SystemException(re);
171                    }
172                    finally {
173                            JCRFactoryUtil.closeSession(session);
174                    }
175            }
176    
177            @Override
178            public void checkRoot(long companyId) throws SystemException {
179                    Session session = null;
180    
181                    try {
182                            session = JCRFactoryUtil.createSession();
183    
184                            getRootNode(session, companyId);
185    
186                            session.save();
187                    }
188                    catch (RepositoryException re) {
189                            throw new SystemException(re);
190                    }
191                    finally {
192                            JCRFactoryUtil.closeSession(session);
193                    }
194            }
195    
196            @Override
197            public void deleteDirectory(
198                            long companyId, long repositoryId, String dirName)
199                    throws PortalException {
200    
201                    Session session = null;
202    
203                    try {
204                            session = JCRFactoryUtil.createSession();
205    
206                            Node rootNode = getRootNode(session, companyId);
207    
208                            Node repositoryNode = getFolderNode(rootNode, repositoryId);
209    
210                            Node dirNode = repositoryNode.getNode(dirName);
211    
212                            dirNode.remove();
213    
214                            session.save();
215                    }
216                    catch (PathNotFoundException pnfe) {
217                            throw new NoSuchDirectoryException(dirName);
218                    }
219                    catch (RepositoryException re) {
220                            String message = GetterUtil.getString(re.getMessage());
221    
222                            if (message.contains("failed to resolve path")) {
223                                    throw new NoSuchDirectoryException(dirName);
224                            }
225                            else {
226                                    throw new PortalException(re);
227                            }
228                    }
229                    finally {
230                            JCRFactoryUtil.closeSession(session);
231                    }
232            }
233    
234            @Override
235            public void deleteFile(long companyId, long repositoryId, String fileName)
236                    throws PortalException, SystemException {
237    
238                    Session session = null;
239    
240                    // A bug in Jackrabbit requires us to create a dummy node and delete the
241                    // version tree manually to successfully delete a file
242    
243                    // Create a dummy node
244    
245                    try {
246                            session = JCRFactoryUtil.createSession();
247    
248                            Workspace workspace = session.getWorkspace();
249    
250                            VersionManager versionManager = workspace.getVersionManager();
251    
252                            Node rootNode = getRootNode(session, companyId);
253    
254                            Node repositoryNode = getFolderNode(rootNode, repositoryId);
255    
256                            Node fileNode = repositoryNode.getNode(fileName);
257    
258                            Node contentNode = fileNode.getNode(JCRConstants.JCR_CONTENT);
259    
260                            versionManager.checkout(contentNode.getPath());
261    
262                            contentNode.setProperty(JCRConstants.JCR_MIME_TYPE, "text/plain");
263                            contentNode.setProperty(JCRConstants.JCR_DATA, "");
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(
535                            long companyId, long repositoryId, String fileName)
536                    throws PortalException, SystemException {
537    
538                    long size;
539    
540                    Session session = null;
541    
542                    try {
543                            session = JCRFactoryUtil.createSession();
544    
545                            Node contentNode = getFileContentNode(
546                                    session, companyId, repositoryId, fileName, StringPool.BLANK);
547    
548                            size = contentNode.getProperty(JCRConstants.JCR_DATA).getLength();
549                    }
550                    catch (RepositoryException re) {
551                            throw new SystemException(re);
552                    }
553                    finally {
554                            JCRFactoryUtil.closeSession(session);
555                    }
556    
557                    return size;
558            }
559    
560            @Override
561            public boolean hasDirectory(
562                            long companyId, long repositoryId, String dirName)
563                    throws SystemException {
564    
565                    Session session = null;
566    
567                    try {
568                            session = JCRFactoryUtil.createSession();
569    
570                            Node rootNode = getRootNode(session, companyId);
571    
572                            Node repositoryNode = getFolderNode(rootNode, repositoryId);
573    
574                            repositoryNode.getNode(dirName);
575    
576                            return true;
577                    }
578                    catch (PathNotFoundException pnfe) {
579                            return false;
580                    }
581                    catch (RepositoryException re) {
582                            throw new SystemException(re);
583                    }
584                    finally {
585                            JCRFactoryUtil.closeSession(session);
586                    }
587            }
588    
589            @Override
590            public boolean hasFile(
591                            long companyId, long repositoryId, String fileName,
592                            String versionLabel)
593                    throws PortalException, SystemException {
594    
595                    try {
596                            getFileContentNode(
597                                    companyId, repositoryId, fileName, versionLabel);
598                    }
599                    catch (NoSuchFileException nsfe) {
600                            return false;
601                    }
602    
603                    return true;
604            }
605    
606            @Override
607            public void move(String srcDir, String destDir) throws SystemException {
608                    Session session = null;
609    
610                    try {
611                            session = JCRFactoryUtil.createSession();
612    
613                            session.move(srcDir, destDir);
614    
615                            session.save();
616                    }
617                    catch (RepositoryException re) {
618                            throw new SystemException(re);
619                    }
620                    finally {
621                            JCRFactoryUtil.closeSession(session);
622                    }
623            }
624    
625            @Override
626            public void updateFile(
627                            long companyId, long repositoryId, long newRepositoryId,
628                            String fileName)
629                    throws PortalException, SystemException {
630    
631                    Session session = null;
632    
633                    try {
634                            session = JCRFactoryUtil.createSession();
635    
636                            Workspace workspace = session.getWorkspace();
637    
638                            VersionManager versionManager = workspace.getVersionManager();
639    
640                            Node rootNode = getRootNode(session, companyId);
641    
642                            Node repositoryNode = getFolderNode(rootNode, repositoryId);
643    
644                            if (fileName.contains(StringPool.SLASH)) {
645                                    String path = fileName.substring(
646                                            0, fileName.lastIndexOf(StringPool.SLASH));
647    
648                                    fileName = fileName.substring(path.length() + 1);
649    
650                                    repositoryNode = getFolderNode(repositoryNode, path);
651                            }
652    
653                            Node fileNode = repositoryNode.getNode(fileName);
654    
655                            Node contentNode = fileNode.getNode(JCRConstants.JCR_CONTENT);
656    
657                            Node newRepositoryNode = getFolderNode(rootNode, newRepositoryId);
658    
659                            if (newRepositoryNode.hasNode(fileName)) {
660                                    throw new DuplicateFileException(fileName);
661                            }
662                            else {
663                                    Node newFileNode = newRepositoryNode.addNode(
664                                            fileName, JCRConstants.NT_FILE);
665    
666                                    Node newContentNode = newFileNode.addNode(
667                                            JCRConstants.JCR_CONTENT, JCRConstants.NT_RESOURCE);
668    
669                                    VersionHistory versionHistory =
670                                            versionManager.getVersionHistory(contentNode.getPath());
671    
672                                    String[] versionLabels = versionHistory.getVersionLabels();
673    
674                                    for (int i = (versionLabels.length - 1); i >= 0; i--) {
675                                            Version version = versionHistory.getVersionByLabel(
676                                                    versionLabels[i]);
677    
678                                            Node frozenContentNode = version.getNode(
679                                                    JCRConstants.JCR_FROZEN_NODE);
680    
681                                            if (i == (versionLabels.length - 1)) {
682                                                    newContentNode.addMixin(JCRConstants.MIX_VERSIONABLE);
683                                            }
684                                            else {
685                                                    versionManager.checkout(newContentNode.getPath());
686                                            }
687    
688                                            newContentNode.setProperty(
689                                                    JCRConstants.JCR_MIME_TYPE, "text/plain");
690    
691                                            copyBinaryProperty(
692                                                    frozenContentNode, newContentNode,
693                                                    JCRConstants.JCR_DATA);
694    
695                                            newContentNode.setProperty(
696                                                    JCRConstants.JCR_LAST_MODIFIED, Calendar.getInstance());
697    
698                                            session.save();
699    
700                                            Version newVersion = versionManager.checkin(
701                                                    newContentNode.getPath());
702    
703                                            VersionHistory newVersionHistory =
704                                                    versionManager.getVersionHistory(
705                                                            newContentNode.getPath());
706    
707                                            newVersionHistory.addVersionLabel(
708                                                    newVersion.getName(), versionLabels[i],
709                                                    PropsValues.DL_STORE_JCR_MOVE_VERSION_LABELS);
710                                    }
711    
712                                    fileNode.remove();
713    
714                                    session.save();
715                            }
716                    }
717                    catch (PathNotFoundException pnfe) {
718                            throw new NoSuchFileException(fileName);
719                    }
720                    catch (RepositoryException re) {
721                            throw new SystemException(re);
722                    }
723                    finally {
724                            JCRFactoryUtil.closeSession(session);
725                    }
726            }
727    
728            public void updateFile(
729                            long companyId, long repositoryId, String fileName,
730                            String newFileName)
731                    throws PortalException, SystemException {
732    
733                    Session session = null;
734    
735                    try {
736                            session = JCRFactoryUtil.createSession();
737    
738                            Workspace workspace = session.getWorkspace();
739    
740                            VersionManager versionManager = workspace.getVersionManager();
741    
742                            Node rootNode = getRootNode(session, companyId);
743    
744                            Node repositoryNode = getFolderNode(rootNode, repositoryId);
745    
746                            if (fileName.contains(StringPool.SLASH)) {
747                                    String path = fileName.substring(
748                                            0, fileName.lastIndexOf(StringPool.SLASH));
749    
750                                    fileName = fileName.substring(path.length() + 1);
751    
752                                    repositoryNode = getFolderNode(repositoryNode, path);
753                            }
754    
755                            Node fileNode = repositoryNode.getNode(fileName);
756    
757                            Node contentNode = fileNode.getNode(JCRConstants.JCR_CONTENT);
758    
759                            Node newFileNode = repositoryNode.addNode(
760                                    newFileName, JCRConstants.NT_FILE);
761    
762                            Node newContentNode = newFileNode.addNode(
763                                    JCRConstants.JCR_CONTENT, JCRConstants.NT_RESOURCE);
764    
765                            VersionHistory versionHistory = versionManager.getVersionHistory(
766                                    contentNode.getPath());
767    
768                            String[] versionLabels = versionHistory.getVersionLabels();
769    
770                            for (int i = (versionLabels.length - 1); i >= 0; i--) {
771                                    Version version = versionHistory.getVersionByLabel(
772                                            versionLabels[i]);
773    
774                                    Node frozenContentNode = version.getNode(
775                                            JCRConstants.JCR_FROZEN_NODE);
776    
777                                    if (i == (versionLabels.length - 1)) {
778                                            newContentNode.addMixin(JCRConstants.MIX_VERSIONABLE);
779                                    }
780                                    else {
781                                            versionManager.checkout(newContentNode.getPath());
782                                    }
783    
784                                    newContentNode.setProperty(
785                                            JCRConstants.JCR_MIME_TYPE, "text/plain");
786    
787                                    copyBinaryProperty(
788                                            frozenContentNode, newContentNode,
789                                            JCRConstants.JCR_DATA);
790    
791                                    newContentNode.setProperty(
792                                            JCRConstants.JCR_LAST_MODIFIED, Calendar.getInstance());
793    
794                                    session.save();
795    
796                                    Version newVersion = versionManager.checkin(
797                                            newContentNode.getPath());
798    
799                                    VersionHistory newVersionHistory =
800                                            versionManager.getVersionHistory(newContentNode.getPath());
801    
802                                    newVersionHistory.addVersionLabel(
803                                            newVersion.getName(), versionLabels[i],
804                                            PropsValues.DL_STORE_JCR_MOVE_VERSION_LABELS);
805                            }
806    
807                            fileNode.remove();
808    
809                            session.save();
810                    }
811                    catch (PathNotFoundException pnfe) {
812                            throw new NoSuchFileException(fileName);
813                    }
814                    catch (RepositoryException re) {
815                            throw new SystemException(re);
816                    }
817                    finally {
818                            JCRFactoryUtil.closeSession(session);
819                    }
820            }
821    
822            @Override
823            public void updateFile(
824                            long companyId, long repositoryId, String fileName,
825                            String versionLabel, InputStream is)
826                    throws PortalException, SystemException {
827    
828                    Session session = null;
829    
830                    try {
831                            session = JCRFactoryUtil.createSession();
832    
833                            Workspace workspace = session.getWorkspace();
834    
835                            VersionManager versionManager = workspace.getVersionManager();
836    
837                            Node rootNode = getRootNode(session, companyId);
838    
839                            Node repositoryNode = getFolderNode(rootNode, repositoryId);
840    
841                            if (fileName.contains(StringPool.SLASH)) {
842                                    String path = fileName.substring(
843                                            0, fileName.lastIndexOf(StringPool.SLASH));
844    
845                                    fileName = fileName.substring(path.length() + 1);
846    
847                                    repositoryNode = getFolderNode(repositoryNode, path);
848                            }
849    
850                            Node fileNode = repositoryNode.getNode(fileName);
851    
852                            Node contentNode = fileNode.getNode(JCRConstants.JCR_CONTENT);
853    
854                            versionManager.checkout(contentNode.getPath());
855    
856                            contentNode.setProperty(JCRConstants.JCR_MIME_TYPE, "text/plain");
857    
858                            ValueFactory valueFactory = session.getValueFactory();
859    
860                            Binary binary = valueFactory.createBinary(is);
861    
862                            contentNode.setProperty(JCRConstants.JCR_DATA, binary);
863    
864                            contentNode.setProperty(
865                                    JCRConstants.JCR_LAST_MODIFIED, Calendar.getInstance());
866    
867                            session.save();
868    
869                            Version version = versionManager.checkin(contentNode.getPath());
870    
871                            VersionHistory versionHistory =
872                                            versionManager.getVersionHistory(contentNode.getPath());
873    
874                            versionHistory.addVersionLabel(
875                                    version.getName(), versionLabel,
876                                    PropsValues.DL_STORE_JCR_MOVE_VERSION_LABELS);
877                    }
878                    catch (PathNotFoundException pnfe) {
879                            throw new NoSuchFileException(
880                                    "{fileName=" + fileName + ", versionLabel=" + versionLabel +
881                                            "}");
882                    }
883                    catch (RepositoryException re) {
884                            throw new SystemException(re);
885                    }
886                    finally {
887                            JCRFactoryUtil.closeSession(session);
888                    }
889            }
890    
891            protected void copyBinaryProperty(Node fromNode, Node toNode, String name)
892                    throws RepositoryException {
893    
894                    Property property = fromNode.getProperty(name);
895    
896                    Binary binary = property.getBinary();
897    
898                    toNode.setProperty(name, binary);
899            }
900    
901            protected Node getFileContentNode(
902                            long companyId, long repositoryId, String fileName,
903                            String versionLabel)
904                    throws PortalException, SystemException {
905    
906                    Node contentNode = null;
907    
908                    Session session = null;
909    
910                    try {
911                            session = JCRFactoryUtil.createSession();
912    
913                            contentNode = getFileContentNode(
914                                    session, companyId, repositoryId, fileName, versionLabel);
915                    }
916                    catch (RepositoryException re) {
917                            throw new SystemException(re);
918                    }
919                    finally {
920                            JCRFactoryUtil.closeSession(session);
921                    }
922    
923                    return contentNode;
924            }
925    
926            protected Node getFileContentNode(
927                            Session session, long companyId, long repositoryId, String fileName,
928                            String versionLabel)
929                    throws PortalException, SystemException {
930    
931                    Node contentNode = null;
932    
933                    try {
934                            Workspace workspace = session.getWorkspace();
935    
936                            VersionManager versionManager = workspace.getVersionManager();
937    
938                            Node rootNode = getRootNode(session, companyId);
939    
940                            Node repositoryNode = getFolderNode(rootNode, repositoryId);
941    
942                            Node fileNode = repositoryNode.getNode(fileName);
943    
944                            contentNode = fileNode.getNode(JCRConstants.JCR_CONTENT);
945    
946                            if (Validator.isNotNull(versionLabel)) {
947                                    VersionHistory versionHistory =
948                                            versionManager.getVersionHistory(contentNode.getPath());
949    
950                                    if (!versionHistory.hasVersionLabel(versionLabel)) {
951                                            throw new NoSuchFileException(
952                                                    "{fileName=" + fileName + ", versionLabel=" +
953                                                            versionLabel + "}");
954                                    }
955    
956                                    Version version = versionHistory.getVersionByLabel(
957                                            versionLabel);
958    
959                                    contentNode = version.getNode(JCRConstants.JCR_FROZEN_NODE);
960                            }
961                    }
962                    catch (PathNotFoundException pnfe) {
963                            throw new NoSuchFileException(
964                                    "{fileName=" + fileName + ", versionLabel=" +
965                                            versionLabel + "}");
966                    }
967                    catch (RepositoryException re) {
968                            throw new SystemException(re);
969                    }
970    
971                    return contentNode;
972            }
973    
974            protected Node getFolderNode(Node node, long name)
975                    throws RepositoryException {
976    
977                    return getFolderNode(node, String.valueOf(name));
978            }
979    
980            protected Node getFolderNode(Node node, String name)
981                    throws RepositoryException {
982    
983                    if (name.contains(StringPool.SLASH)) {
984                            String[] nameParts = name.split(StringPool.SLASH, 2);
985    
986                            node = getFolderNode(node, nameParts[0]);
987    
988                            return getFolderNode(node, nameParts[1]);
989                    }
990    
991                    Node folderNode = null;
992    
993                    if (node.hasNode(name)) {
994                            folderNode = node.getNode(name);
995                    }
996                    else {
997                            folderNode = node.addNode(name, JCRConstants.NT_FOLDER);
998                    }
999    
1000                    return folderNode;
1001            }
1002    
1003            protected Node getRootNode(Session session, long companyId)
1004                    throws RepositoryException {
1005    
1006                    Node companyNode = getFolderNode(session.getRootNode(), companyId);
1007    
1008                    return getFolderNode(companyNode, JCRFactory.NODE_DOCUMENTLIBRARY);
1009            }
1010    
1011    }