001/*
002 *  Copyright 2022 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.plugins.forms.helper;
017
018import java.util.ArrayList;
019import java.util.HashMap;
020import java.util.List;
021import java.util.Map;
022
023import org.apache.avalon.framework.component.Component;
024import org.apache.avalon.framework.service.ServiceException;
025import org.apache.avalon.framework.service.ServiceManager;
026import org.apache.avalon.framework.service.Serviceable;
027import org.apache.commons.lang3.StringUtils;
028
029import org.ametys.cms.workflow.AmetysObjectCheckRightsCondition;
030import org.ametys.core.right.RightManager;
031import org.ametys.core.user.UserIdentity;
032import org.ametys.plugins.forms.dao.FormDAO;
033import org.ametys.plugins.forms.dao.FormEntryDAO;
034import org.ametys.plugins.forms.repository.Form;
035import org.ametys.plugins.forms.repository.FormEntry;
036import org.ametys.plugins.workflow.support.WorkflowHelper;
037import org.ametys.plugins.workflow.support.WorkflowProvider;
038import org.ametys.runtime.plugin.component.AbstractLogEnabled;
039
040import com.opensymphony.workflow.Workflow;
041import com.opensymphony.workflow.loader.ActionDescriptor;
042import com.opensymphony.workflow.loader.WorkflowDescriptor;
043
044/**
045 * The helper to handle admin dashboard
046 */
047public class FormAdminDashboardHelper extends AbstractLogEnabled implements Serviceable, Component
048{
049    /** Avalon ROLE. */
050    public static final String ROLE = FormAdminDashboardHelper.class.getName();
051    
052    /** The workflow provider */
053    protected WorkflowProvider _workflowProvider;
054    
055    /** The form DAO */
056    protected FormDAO _formDAO;
057    
058    /** The right manager */
059    protected RightManager _rightManager;
060    
061    /** The form entry DAO */
062    protected FormEntryDAO _formEntryDAO;
063    
064    /** The workflow helper component */
065    protected WorkflowHelper _workflowHelper;
066    
067    public void service(ServiceManager manager) throws ServiceException
068    {
069        _workflowProvider = (WorkflowProvider) manager.lookup(WorkflowProvider.ROLE);
070        _formDAO = (FormDAO) manager.lookup(FormDAO.ROLE);
071        _rightManager = (RightManager) manager.lookup(RightManager.ROLE);
072        _formEntryDAO = (FormEntryDAO) manager.lookup(FormEntryDAO.ROLE);
073        _workflowHelper = (WorkflowHelper) manager.lookup(WorkflowHelper.ROLE);
074    }
075    
076    /**
077     * The list of form to admin for current user
078     * @param siteName the sitename
079     * @param currentUser the current user
080     * @return the list of form to admin for current user
081     */
082    public List<Form> getFormToAdmin(String siteName, UserIdentity currentUser)
083    {
084        return _formDAO.getForms(siteName)
085                .stream()
086                .filter(Form::hasWorkflow)
087                .filter(f -> _formEntryDAO.hasHandleDataRightOnForm(currentUser, f))
088                .filter(f -> this._hasAtLeastOneEntryToDoAction(f, currentUser))
089                .toList();
090    }
091    
092    /**
093     * <code>true</code> if the form has an entry that the user can do action
094     * @param form the form
095     * @param user the user
096     * @return <code>true</code> if the form has an entry that the user can do action
097     */
098    protected boolean _hasAtLeastOneEntryToDoAction(Form form, UserIdentity user)
099    {
100        return form.getEntries()
101                .stream()
102                // filter entry that have been anonymized
103                .filter(entry -> !entry.hasValue(FormEntry.ATTRIBUTE_ANONYMIZATION_DATE))
104                .anyMatch(this::hasAvailableActions);
105    }
106    
107    /**
108     * <code>true</code> if the entry has available actions for current user
109     * @param entry the entry
110     * @return <code>true</code> if the entry has available actions for current user
111     */
112    public boolean hasAvailableActions(FormEntry entry)
113    {
114        return _getAvailableActions(entry).length > 0;
115    }
116    
117    /**
118     * Get available actions for current user filter by its type
119     * @param entry the entry
120     * @param types the types of actions. Empty to get all available actions
121     * @return the available actions for current user
122     */
123    public List<ActionDescriptor> getAvailableActions(FormEntry entry, List<String> types)
124    {
125        List<ActionDescriptor> actions = new ArrayList<>();
126        
127        WorkflowDescriptor workflowDescriptor = _workflowHelper.getWorkflowDescriptor(entry.getForm().getWorkflowName());
128        for (int actionId : _getAvailableActions(entry))
129        {
130            ActionDescriptor action = workflowDescriptor.getAction(actionId);
131            Map<String, Object> metaAttributes = action.getMetaAttributes();
132            String actionType = (String) metaAttributes.getOrDefault("action-type", StringUtils.EMPTY);
133            if (types.isEmpty() || types.contains(actionType))
134            {
135                actions.add(action);
136            }
137            
138        }
139        
140        return actions;
141    }
142    
143    private int[] _getAvailableActions(FormEntry entry)
144    {
145        Map<String, Object> inputs = new HashMap<>();
146        inputs.put(AmetysObjectCheckRightsCondition.AMETYS_OBJECT_KEY, entry);
147     
148        Workflow workflow = _workflowProvider.getAmetysObjectWorkflow(entry);
149        return workflow.getAvailableActions(entry.getWorkflowId(), inputs);
150    }
151}