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.workflow;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.util.GetterUtil;
020    import com.liferay.portal.kernel.workflow.BaseWorkflowHandler;
021    import com.liferay.portal.kernel.workflow.WorkflowConstants;
022    import com.liferay.portal.model.WorkflowDefinitionLink;
023    import com.liferay.portal.security.permission.ResourceActionsUtil;
024    import com.liferay.portal.service.ServiceContext;
025    import com.liferay.portal.service.WorkflowDefinitionLinkLocalServiceUtil;
026    import com.liferay.portal.theme.ThemeDisplay;
027    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
028    import com.liferay.portlet.documentlibrary.model.DLFileEntryTypeConstants;
029    import com.liferay.portlet.documentlibrary.model.DLFileVersion;
030    import com.liferay.portlet.documentlibrary.model.DLFolder;
031    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
032    import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
033    import com.liferay.portlet.documentlibrary.service.DLFileVersionLocalServiceUtil;
034    import com.liferay.portlet.documentlibrary.service.DLFolderLocalServiceUtil;
035    
036    import java.io.Serializable;
037    
038    import java.util.Locale;
039    import java.util.Map;
040    
041    /**
042     * @author Bruno Farache
043     * @author Jorge Ferrer
044     * @author Alexander Chow
045     */
046    public class DLFileEntryWorkflowHandler extends BaseWorkflowHandler {
047    
048            @Override
049            public String getClassName() {
050                    return DLFileEntry.class.getName();
051            }
052    
053            @Override
054            public String getType(Locale locale) {
055                    return ResourceActionsUtil.getModelResource(locale, getClassName());
056            }
057    
058            @Override
059            public WorkflowDefinitionLink getWorkflowDefinitionLink(
060                            long companyId, long groupId, long classPK)
061                    throws PortalException, SystemException {
062    
063                    DLFileVersion dlFileVersion =
064                            DLFileVersionLocalServiceUtil.getFileVersion(classPK);
065    
066                    long folderId = dlFileVersion.getFolderId();
067    
068                    while (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
069                            DLFolder dlFolder = DLFolderLocalServiceUtil.getFolder(folderId);
070    
071                            if (dlFolder.isOverrideFileEntryTypes()) {
072                                    break;
073                            }
074    
075                            folderId = dlFolder.getParentFolderId();
076                    }
077    
078                    WorkflowDefinitionLink workflowDefinitionLink =
079                            WorkflowDefinitionLinkLocalServiceUtil.fetchWorkflowDefinitionLink(
080                                    companyId, groupId, DLFolder.class.getName(), folderId,
081                                    dlFileVersion.getFileEntryTypeId(), true);
082    
083                    if (workflowDefinitionLink == null) {
084                            workflowDefinitionLink =
085                                    WorkflowDefinitionLinkLocalServiceUtil.
086                                            fetchWorkflowDefinitionLink(
087                                                    companyId, groupId, DLFolder.class.getName(), folderId,
088                                                    DLFileEntryTypeConstants.FILE_ENTRY_TYPE_ID_ALL, true);
089                    }
090    
091                    return workflowDefinitionLink;
092            }
093    
094            @Override
095            public boolean isVisible() {
096                    return _VISIBLE;
097            }
098    
099            @Override
100            public DLFileEntry updateStatus(
101                            int status, Map<String, Serializable> workflowContext)
102                    throws PortalException, SystemException {
103    
104                    long userId = GetterUtil.getLong(
105                            (String)workflowContext.get(WorkflowConstants.CONTEXT_USER_ID));
106                    long classPK = GetterUtil.getLong(
107                            (String)workflowContext.get(
108                                    WorkflowConstants.CONTEXT_ENTRY_CLASS_PK));
109    
110                    ServiceContext serviceContext = (ServiceContext)workflowContext.get(
111                            "serviceContext");
112    
113                    return DLFileEntryLocalServiceUtil.updateStatus(
114                            userId, classPK, status, workflowContext, serviceContext);
115            }
116    
117            @Override
118            protected String getIconPath(ThemeDisplay themeDisplay) {
119                    return themeDisplay.getPathThemeImages() + "/common/clip.png";
120            }
121    
122            private static final boolean _VISIBLE = false;
123    
124    }