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