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.portlet.journal.workflow;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.util.GetterUtil;
019    import com.liferay.portal.kernel.workflow.BaseWorkflowHandler;
020    import com.liferay.portal.kernel.workflow.WorkflowConstants;
021    import com.liferay.portal.model.WorkflowDefinitionLink;
022    import com.liferay.portal.security.permission.ResourceActionsUtil;
023    import com.liferay.portal.service.ServiceContext;
024    import com.liferay.portal.service.WorkflowDefinitionLinkLocalServiceUtil;
025    import com.liferay.portal.theme.ThemeDisplay;
026    import com.liferay.portal.util.PortalUtil;
027    import com.liferay.portal.util.PortletKeys;
028    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
029    import com.liferay.portlet.dynamicdatamapping.service.DDMStructureLocalServiceUtil;
030    import com.liferay.portlet.journal.model.JournalArticle;
031    import com.liferay.portlet.journal.model.JournalArticleConstants;
032    import com.liferay.portlet.journal.model.JournalFolder;
033    import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
034    import com.liferay.portlet.journal.service.JournalFolderLocalServiceUtil;
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 Marcellus Tavares
044     * @author Juan Fern??ndez
045     * @author Julio Camarero
046     */
047    public class JournalArticleWorkflowHandler
048            extends BaseWorkflowHandler<JournalArticle> {
049    
050            @Override
051            public String getClassName() {
052                    return JournalArticle.class.getName();
053            }
054    
055            @Override
056            public String getType(Locale locale) {
057                    return ResourceActionsUtil.getModelResource(locale, getClassName());
058            }
059    
060            @Override
061            public WorkflowDefinitionLink getWorkflowDefinitionLink(
062                            long companyId, long groupId, long classPK)
063                    throws PortalException {
064    
065                    JournalArticle article = JournalArticleLocalServiceUtil.getArticle(
066                            classPK);
067    
068                    long folderId =
069                            JournalFolderLocalServiceUtil.getInheritedWorkflowFolderId(
070                                    article.getFolderId());
071    
072                    DDMStructure ddmStructure = DDMStructureLocalServiceUtil.getStructure(
073                            article.getGroupId(),
074                            PortalUtil.getClassNameId(JournalArticle.class),
075                            article.getDDMStructureKey(), true);
076    
077                    WorkflowDefinitionLink workflowDefinitionLink =
078                            WorkflowDefinitionLinkLocalServiceUtil.fetchWorkflowDefinitionLink(
079                                    companyId, groupId, JournalFolder.class.getName(), folderId,
080                                    ddmStructure.getStructureId(), true);
081    
082                    if (workflowDefinitionLink == null) {
083                            workflowDefinitionLink =
084                                    WorkflowDefinitionLinkLocalServiceUtil.
085                                            fetchWorkflowDefinitionLink(
086                                                    companyId, groupId, JournalFolder.class.getName(),
087                                                    folderId, JournalArticleConstants.DDM_STRUCTURE_ID_ALL,
088                                                    true);
089                    }
090    
091                    return workflowDefinitionLink;
092            }
093    
094            @Override
095            public boolean isVisible() {
096                    return _VISIBLE;
097            }
098    
099            @Override
100            public JournalArticle updateStatus(
101                            int status, Map<String, Serializable> workflowContext)
102                    throws PortalException {
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                    JournalArticle article = JournalArticleLocalServiceUtil.getArticle(
111                            classPK);
112    
113                    ServiceContext serviceContext = (ServiceContext)workflowContext.get(
114                            "serviceContext");
115    
116                    String articleURL = PortalUtil.getControlPanelFullURL(
117                            serviceContext.getScopeGroupId(), PortletKeys.JOURNAL, null);
118    
119                    return JournalArticleLocalServiceUtil.updateStatus(
120                            userId, article, status, articleURL, serviceContext,
121                            workflowContext);
122            }
123    
124            @Override
125            protected String getIconPath(ThemeDisplay themeDisplay) {
126                    return themeDisplay.getPathThemeImages() + "/common/history.png";
127            }
128    
129            private static final boolean _VISIBLE = false;
130    
131    }