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