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.HashMap;
019import java.util.List;
020import java.util.Map;
021
022import org.apache.avalon.framework.component.Component;
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025import org.apache.avalon.framework.service.Serviceable;
026
027import org.ametys.cms.workflow.AmetysObjectCheckRightsCondition;
028import org.ametys.core.right.RightManager;
029import org.ametys.core.right.RightManager.RightResult;
030import org.ametys.core.user.UserIdentity;
031import org.ametys.plugins.forms.dao.FormDAO;
032import org.ametys.plugins.forms.dao.FormEntryDAO;
033import org.ametys.plugins.forms.repository.Form;
034import org.ametys.plugins.forms.repository.FormEntry;
035import org.ametys.plugins.workflow.support.WorkflowProvider;
036import org.ametys.runtime.plugin.component.AbstractLogEnabled;
037
038import com.opensymphony.workflow.Workflow;
039
040/**
041 * The helper to handle admin dashboard
042 */
043public class FormAdminDashboardHelper extends AbstractLogEnabled implements Serviceable, Component
044{
045    /** Avalon ROLE. */
046    public static final String ROLE = FormAdminDashboardHelper.class.getName();
047    
048    /** The workflow provider */
049    protected WorkflowProvider _workflowProvider;
050    
051    /** The form DAO */
052    protected FormDAO _formDAO;
053    
054    /** The right manager */
055    protected RightManager _rightManager;
056    
057    public void service(ServiceManager manager) throws ServiceException
058    {
059        _workflowProvider = (WorkflowProvider) manager.lookup(WorkflowProvider.ROLE);
060        _formDAO = (FormDAO) manager.lookup(FormDAO.ROLE);
061        _rightManager = (RightManager) manager.lookup(RightManager.ROLE);
062    }
063    
064    /**
065     * The list of form to admin for current user
066     * @param siteName the sitename
067     * @param currentUser the current user
068     * @return the list of form to admin for current user
069     */
070    public List<Form> getFormToAdmin(String siteName, UserIdentity currentUser)
071    {
072        return _formDAO.getForms(siteName)
073                .stream()
074                .filter(Form::hasWorkflow)
075                .filter(f -> _rightManager.currentUserHasRight(FormEntryDAO.HANDLE_FORMS_ENTRIES_RIGHT_ID, f) == RightResult.RIGHT_ALLOW)
076                .filter(f -> this._hasAtLeastOneEntryToDoAction(f, currentUser))
077                .toList();
078    }
079    
080    /**
081     * <code>true</code> if the form has an entry that the user can do action
082     * @param form the form
083     * @param user the user
084     * @return <code>true</code> if the form has an entry that the user can do action
085     */
086    protected boolean _hasAtLeastOneEntryToDoAction(Form form, UserIdentity user)
087    {
088        return form.getEntries()
089                .stream()
090                .anyMatch(this::hasAvailableActions);
091    }
092    
093    /**
094     * <code>true</code> if the entry has available actions for current user
095     * @param entry the entry
096     * @return <code>true</code> if the entry has available actions for current user
097     */
098    public boolean hasAvailableActions(FormEntry entry)
099    {
100        Map<String, Object> inputs = new HashMap<>();
101        inputs.put(AmetysObjectCheckRightsCondition.AMETYS_OBJECT_KEY, entry);
102     
103        Workflow workflow = _workflowProvider.getAmetysObjectWorkflow(entry);
104        int[] availableActions = workflow.getAvailableActions(entry.getWorkflowId(), inputs);
105        
106        return availableActions.length > 0;
107    }
108}