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            public static final String CLASS_NAME = DLFileEntry.class.getName();
049    
050            @Override
051            public String getClassName() {
052                    return CLASS_NAME;
053            }
054    
055            @Override
056            public String getType(Locale locale) {
057                    return ResourceActionsUtil.getModelResource(locale, CLASS_NAME);
058            }
059    
060            @Override
061            public WorkflowDefinitionLink getWorkflowDefinitionLink(
062                            long companyId, long groupId, long classPK)
063                    throws PortalException, SystemException {
064    
065                    DLFileVersion dlFileVersion =
066                            DLFileVersionLocalServiceUtil.getFileVersion(classPK);
067    
068                    long folderId = dlFileVersion.getFolderId();
069    
070                    while (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
071                            DLFolder dlFolder = DLFolderLocalServiceUtil.getFolder(folderId);
072    
073                            if (dlFolder.isOverrideFileEntryTypes()) {
074                                    break;
075                            }
076    
077                            folderId = dlFolder.getParentFolderId();
078                    }
079    
080                    WorkflowDefinitionLink workflowDefinitionLink =
081                            WorkflowDefinitionLinkLocalServiceUtil.fetchWorkflowDefinitionLink(
082                                    companyId, groupId, DLFolder.class.getName(), folderId,
083                                    dlFileVersion.getFileEntryTypeId(), true);
084    
085                    if (workflowDefinitionLink == null) {
086                            workflowDefinitionLink =
087                                    WorkflowDefinitionLinkLocalServiceUtil.
088                                            fetchWorkflowDefinitionLink(
089                                                    companyId, groupId, DLFolder.class.getName(), folderId,
090                                                    DLFileEntryTypeConstants.FILE_ENTRY_TYPE_ID_ALL, true);
091                    }
092    
093                    return workflowDefinitionLink;
094            }
095    
096            @Override
097            public boolean isVisible() {
098                    return _VISIBLE;
099            }
100    
101            @Override
102            public DLFileEntry updateStatus(
103                            int status, Map<String, Serializable> workflowContext)
104                    throws PortalException, SystemException {
105    
106                    long userId = GetterUtil.getLong(
107                            (String)workflowContext.get(WorkflowConstants.CONTEXT_USER_ID));
108                    long classPK = GetterUtil.getLong(
109                            (String)workflowContext.get(
110                                    WorkflowConstants.CONTEXT_ENTRY_CLASS_PK));
111    
112                    ServiceContext serviceContext = (ServiceContext)workflowContext.get(
113                            "serviceContext");
114    
115                    return DLFileEntryLocalServiceUtil.updateStatus(
116                            userId, classPK, status, workflowContext, serviceContext);
117            }
118    
119            @Override
120            protected String getIconPath(ThemeDisplay themeDisplay) {
121                    return themeDisplay.getPathThemeImages() + "/common/clip.png";
122            }
123    
124            private static final boolean _VISIBLE = false;
125    
126    }