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