001    /**
002     * Copyright (c) 2000-2013 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.service.impl;
016    
017    import com.liferay.portal.kernel.dao.orm.ActionableDynamicQuery;
018    import com.liferay.portal.kernel.dao.orm.DynamicQuery;
019    import com.liferay.portal.kernel.dao.orm.Property;
020    import com.liferay.portal.kernel.dao.orm.PropertyFactoryUtil;
021    import com.liferay.portal.kernel.exception.PortalException;
022    import com.liferay.portal.kernel.exception.SystemException;
023    import com.liferay.portal.kernel.util.ListUtil;
024    import com.liferay.portal.kernel.workflow.WorkflowConstants;
025    import com.liferay.portlet.documentlibrary.NoSuchFileVersionException;
026    import com.liferay.portlet.documentlibrary.model.DLFileEntryConstants;
027    import com.liferay.portlet.documentlibrary.model.DLFileVersion;
028    import com.liferay.portlet.documentlibrary.service.base.DLFileVersionLocalServiceBaseImpl;
029    import com.liferay.portlet.documentlibrary.service.persistence.DLFileVersionActionableDynamicQuery;
030    import com.liferay.portlet.documentlibrary.util.comparator.FileVersionVersionComparator;
031    
032    import java.util.Collections;
033    import java.util.List;
034    
035    /**
036     * @author Brian Wing Shun Chan
037     */
038    public class DLFileVersionLocalServiceImpl
039            extends DLFileVersionLocalServiceBaseImpl {
040    
041            @Override
042            public DLFileVersion getFileVersion(long fileVersionId)
043                    throws PortalException, SystemException {
044    
045                    return dlFileVersionPersistence.findByPrimaryKey(fileVersionId);
046            }
047    
048            @Override
049            public DLFileVersion getFileVersion(long fileEntryId, String version)
050                    throws PortalException, SystemException {
051    
052                    return dlFileVersionPersistence.findByF_V(fileEntryId, version);
053            }
054    
055            @Override
056            public DLFileVersion getFileVersionByUuidAndGroupId(
057                            String uuid, long groupId)
058                    throws SystemException {
059    
060                    return dlFileVersionPersistence.fetchByUUID_G(uuid, groupId);
061            }
062    
063            @Override
064            public List<DLFileVersion> getFileVersions(long fileEntryId, int status)
065                    throws SystemException {
066    
067                    List<DLFileVersion> dlFileVersions = null;
068    
069                    if (status == WorkflowConstants.STATUS_ANY) {
070                            dlFileVersions = dlFileVersionPersistence.findByFileEntryId(
071                                    fileEntryId);
072                    }
073                    else {
074                            dlFileVersions = dlFileVersionPersistence.findByF_S(
075                                    fileEntryId, status);
076                    }
077    
078                    dlFileVersions = ListUtil.copy(dlFileVersions);
079    
080                    Collections.sort(dlFileVersions, new FileVersionVersionComparator());
081    
082                    return dlFileVersions;
083            }
084    
085            @Override
086            public int getFileVersionsCount(long fileEntryId, int status)
087                    throws SystemException {
088    
089                    return dlFileVersionPersistence.countByF_S(fileEntryId, status);
090            }
091    
092            @Override
093            public DLFileVersion getLatestFileVersion(
094                            long fileEntryId, boolean excludeWorkingCopy)
095                    throws PortalException, SystemException {
096    
097                    List<DLFileVersion> dlFileVersions =
098                            dlFileVersionPersistence.findByFileEntryId(fileEntryId);
099    
100                    if (dlFileVersions.isEmpty()) {
101                            throw new NoSuchFileVersionException(
102                                    "No file versions found for fileEntryId " + fileEntryId);
103                    }
104    
105                    dlFileVersions = ListUtil.copy(dlFileVersions);
106    
107                    Collections.sort(dlFileVersions, new FileVersionVersionComparator());
108    
109                    DLFileVersion dlFileVersion = dlFileVersions.get(0);
110    
111                    String version = dlFileVersion.getVersion();
112    
113                    if (excludeWorkingCopy &&
114                            version.equals(DLFileEntryConstants.PRIVATE_WORKING_COPY_VERSION)) {
115    
116                            return dlFileVersions.get(1);
117                    }
118    
119                    return dlFileVersion;
120            }
121    
122            @Override
123            public DLFileVersion getLatestFileVersion(long userId, long fileEntryId)
124                    throws PortalException, SystemException {
125    
126                    boolean excludeWorkingCopy = true;
127    
128                    if (dlFileEntryLocalService.isFileEntryCheckedOut(fileEntryId)) {
129                            excludeWorkingCopy = !dlFileEntryLocalService.hasFileEntryLock(
130                                    userId, fileEntryId);
131                    }
132    
133                    return getLatestFileVersion(fileEntryId, excludeWorkingCopy);
134            }
135    
136            @Override
137            public void rebuildTree(long companyId)
138                    throws PortalException, SystemException {
139    
140                    dlFolderLocalService.rebuildTree(companyId);
141            }
142    
143            @Override
144            public void setTreePaths(final long folderId, final String treePath)
145                    throws PortalException, SystemException {
146    
147                    ActionableDynamicQuery actionableDynamicQuery =
148                            new DLFileVersionActionableDynamicQuery() {
149    
150                            @Override
151                            protected void addCriteria(DynamicQuery dynamicQuery) {
152                                    Property folderIdProperty = PropertyFactoryUtil.forName(
153                                            "folderId");
154    
155                                    dynamicQuery.add(folderIdProperty.eq(folderId));
156    
157                                    Property treePathProperty = PropertyFactoryUtil.forName(
158                                            "treePath");
159    
160                                    dynamicQuery.add(treePathProperty.ne(treePath));
161                            }
162    
163                            @Override
164                            protected void performAction(Object object)
165                                    throws PortalException, SystemException {
166    
167                                    DLFileVersion version = (DLFileVersion)object;
168    
169                                    version.setTreePath(treePath);
170    
171                                    updateDLFileVersion(version);
172                            }
173    
174                    };
175    
176                    actionableDynamicQuery.performActions();
177            }
178    
179    }