001    /**
002     * Copyright (c) 2000-present 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.portal.repository.capabilities;
016    
017    import com.liferay.document.library.kernel.model.DLFileEntry;
018    import com.liferay.document.library.kernel.model.DLFileEntryConstants;
019    import com.liferay.document.library.kernel.model.DLFileVersion;
020    import com.liferay.document.library.kernel.model.DLSyncConstants;
021    import com.liferay.document.library.kernel.util.DLAppHelperThreadLocal;
022    import com.liferay.document.library.kernel.util.DLUtil;
023    import com.liferay.portal.kernel.exception.PortalException;
024    import com.liferay.portal.kernel.repository.LocalRepository;
025    import com.liferay.portal.kernel.repository.Repository;
026    import com.liferay.portal.kernel.repository.capabilities.WorkflowCapability;
027    import com.liferay.portal.kernel.repository.model.FileEntry;
028    import com.liferay.portal.kernel.repository.model.FileVersion;
029    import com.liferay.portal.kernel.service.ServiceContext;
030    import com.liferay.portal.kernel.workflow.WorkflowConstants;
031    import com.liferay.portal.kernel.workflow.WorkflowThreadLocal;
032    import com.liferay.portal.repository.capabilities.util.DLFileEntryServiceAdapter;
033    import com.liferay.portal.repository.capabilities.util.DLFileVersionServiceAdapter;
034    import com.liferay.portal.repository.liferayrepository.LiferayWorkflowLocalRepositoryWrapper;
035    import com.liferay.portal.repository.liferayrepository.LiferayWorkflowRepositoryWrapper;
036    import com.liferay.portal.repository.util.RepositoryWrapperAware;
037    
038    /**
039     * @author Adolfo P??rez
040     */
041    public class LiferayWorkflowCapability
042            implements RepositoryWrapperAware, WorkflowCapability, WorkflowSupport {
043    
044            public LiferayWorkflowCapability(
045                    DLFileEntryServiceAdapter dlFileEntryServiceAdapter,
046                    DLFileVersionServiceAdapter dlFileVersionServiceAdapter) {
047    
048                    _dlFileEntryServiceAdapter = dlFileEntryServiceAdapter;
049                    _dlFileVersionServiceAdapter = dlFileVersionServiceAdapter;
050            }
051    
052            @Override
053            public void addFileEntry(
054                            long userId, FileEntry fileEntry, ServiceContext serviceContext)
055                    throws PortalException {
056    
057                    boolean previousEnabled = WorkflowThreadLocal.isEnabled();
058    
059                    if (!DLAppHelperThreadLocal.isEnabled()) {
060                            WorkflowThreadLocal.setEnabled(false);
061                    }
062    
063                    try {
064                            FileVersion fileVersion = fileEntry.getFileVersion();
065    
066                            DLUtil.startWorkflowInstance(
067                                    userId, (DLFileVersion)fileVersion.getModel(),
068                                    DLSyncConstants.EVENT_ADD, serviceContext);
069                    }
070                    finally {
071                            if (!DLAppHelperThreadLocal.isEnabled()) {
072                                    WorkflowThreadLocal.setEnabled(previousEnabled);
073                            }
074                    }
075            }
076    
077            @Override
078            public void checkInFileEntry(
079                            long userId, FileEntry fileEntry, boolean majorVersion,
080                            ServiceContext serviceContext)
081                    throws PortalException {
082    
083                    boolean keepFileVersionLabel =
084                            _dlFileEntryServiceAdapter.isKeepFileVersionLabel(
085                                    fileEntry.getFileEntryId(), majorVersion, serviceContext);
086    
087                    if ((serviceContext.getWorkflowAction() ==
088                                    WorkflowConstants.ACTION_PUBLISH) &&
089                            !keepFileVersionLabel) {
090    
091                            DLFileVersion latestDLFileVersion =
092                                    _dlFileVersionServiceAdapter.getLatestFileVersion(
093                                            fileEntry.getFileEntryId(), false);
094    
095                            DLUtil.startWorkflowInstance(
096                                    userId, latestDLFileVersion, DLSyncConstants.EVENT_UPDATE,
097                                    serviceContext);
098                    }
099            }
100    
101            @Override
102            public int getStatus(FileEntry fileEntry) {
103                    DLFileEntry dlFileEntry = (DLFileEntry)fileEntry.getModel();
104    
105                    return dlFileEntry.getStatus();
106            }
107    
108            @Override
109            public void revertFileEntry(
110                            long userId, FileEntry fileEntry, ServiceContext serviceContext)
111                    throws PortalException {
112    
113                    _startWorkflowInstance(userId, fileEntry, serviceContext);
114            }
115    
116            @Override
117            public void updateFileEntry(
118                            long userId, FileEntry fileEntry, boolean majorVersion,
119                            ServiceContext serviceContext)
120                    throws PortalException {
121    
122                    _startWorkflowInstance(userId, fileEntry, serviceContext);
123            }
124    
125            @Override
126            public LocalRepository wrapLocalRepository(
127                    LocalRepository localRepository) {
128    
129                    return new LiferayWorkflowLocalRepositoryWrapper(localRepository, this);
130            }
131    
132            @Override
133            public Repository wrapRepository(Repository repository) {
134                    return new LiferayWorkflowRepositoryWrapper(repository, this);
135            }
136    
137            private DLFileVersion _getWorkflowDLFileVersion(
138                            long fileEntryId, ServiceContext serviceContext)
139                    throws PortalException {
140    
141                    DLFileEntry dlFileEntry = _dlFileEntryServiceAdapter.getDLFileEntry(
142                            fileEntryId);
143    
144                    if (dlFileEntry.isCheckedOut()) {
145                            return null;
146                    }
147    
148                    DLFileVersion dlFileVersion =
149                            _dlFileVersionServiceAdapter.getLatestFileVersion(
150                                    fileEntryId, true);
151    
152                    if (dlFileVersion.isApproved() ||
153                            (serviceContext.getWorkflowAction() ==
154                                    WorkflowConstants.ACTION_PUBLISH)) {
155    
156                            return dlFileVersion;
157                    }
158    
159                    return null;
160            }
161    
162            private void _startWorkflowInstance(
163                            long userId, DLFileVersion dlFileVersion,
164                            ServiceContext serviceContext)
165                    throws PortalException {
166    
167                    if (dlFileVersion == null) {
168                            return;
169                    }
170    
171                    String syncEvent = DLSyncConstants.EVENT_UPDATE;
172    
173                    if (dlFileVersion.getVersion().equals(
174                                    DLFileEntryConstants.VERSION_DEFAULT)) {
175    
176                            syncEvent = DLSyncConstants.EVENT_ADD;
177                    }
178    
179                    DLUtil.startWorkflowInstance(
180                            userId, dlFileVersion, syncEvent, serviceContext);
181            }
182    
183            private void _startWorkflowInstance(
184                            long userId, FileEntry fileEntry, ServiceContext serviceContext)
185                    throws PortalException {
186    
187                    DLFileVersion dlFileVersion = _getWorkflowDLFileVersion(
188                            fileEntry.getFileEntryId(), serviceContext);
189    
190                    _startWorkflowInstance(userId, dlFileVersion, serviceContext);
191            }
192    
193            private final DLFileEntryServiceAdapter _dlFileEntryServiceAdapter;
194            private final DLFileVersionServiceAdapter _dlFileVersionServiceAdapter;
195    
196    }