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