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.workflow;
017
018import java.util.ArrayList;
019import java.util.List;
020import java.util.Map;
021import java.util.Optional;
022
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025
026import org.ametys.cms.workflow.AmetysObjectCheckRightsCondition;
027import org.ametys.plugins.forms.helper.FormMailHelper;
028import org.ametys.plugins.forms.helper.FormMailHelper.LimitationMailType;
029import org.ametys.plugins.forms.helper.LimitedEntriesHelper;
030import org.ametys.plugins.forms.repository.Form;
031import org.ametys.plugins.forms.repository.FormEntry;
032import org.ametys.plugins.workflow.AbstractWorkflowComponent;
033import org.ametys.plugins.workflow.EnhancedFunction;
034import org.ametys.plugins.workflow.support.WorkflowHelper.WorkflowVisibility;
035import org.ametys.runtime.i18n.I18nizableText;
036
037import com.opensymphony.module.propertyset.PropertySet;
038import com.opensymphony.workflow.InvalidActionException;
039import com.opensymphony.workflow.WorkflowException;
040
041/**
042 * OS workflow function to active the entry
043 */
044public class ActiveEntryFunction extends AbstractWorkflowComponent implements EnhancedFunction
045{
046    /** The limited entries helper */
047    protected LimitedEntriesHelper _limitedEntriesHelper;
048    
049    /** The form mail helper */
050    protected FormMailHelper _formMailHelper;
051    
052    @Override
053    public void service(ServiceManager manager) throws ServiceException
054    {
055        super.service(manager);
056        _limitedEntriesHelper = (LimitedEntriesHelper) manager.lookup(LimitedEntriesHelper.ROLE);
057        _formMailHelper = (FormMailHelper) manager.lookup(FormMailHelper.ROLE);
058    }
059    
060    @Override
061    public void execute(Map transientVars, Map args, PropertySet ps) throws WorkflowException
062    {
063        FormEntry entry = (FormEntry) transientVars.get(AmetysObjectCheckRightsCondition.AMETYS_OBJECT_KEY);
064        if (!entry.isActive())
065        {
066            if (_limitedEntriesHelper.isFormLimitIsReached(entry.getForm()))
067            {
068                List<String> i18nParams = new ArrayList<>();
069                i18nParams.add(entry.getForm().getTitle());
070                
071                addWorkflowError(transientVars, new I18nizableText("plugin.forms", "PLUGINS_FORMS_ACTIVE_ENTRY_LIMIT_CONDITION_FAILED", i18nParams));
072                
073                throw new InvalidActionException("Can activate the entry because the form (" + entry.getForm().getId() + ") limit is reach");
074            }
075            else
076            {   
077                entry.setActive(true);
078                Form form = entry.getForm();
079                
080                Optional<String[]> emailsAsArrayOpt = form.getAdminEmails();
081                if (emailsAsArrayOpt.isPresent())
082                {
083                    String[] emailsAsArray = emailsAsArrayOpt.get();
084                    if (form.isEntriesLimited())
085                    {
086                        int totalSubmissions = form.getActiveEntries().size();
087                        Long maxEntries = form.getMaxEntries().get();
088                        if (maxEntries == totalSubmissions)
089                        {
090                            _formMailHelper.sendLimitationReachedMailForAdmin(entry, emailsAsArray, LimitationMailType.LIMIT);
091                        }
092                        else if (form.isQueueEnabled() && form.getQueueSize().isPresent() && form.getQueueSize().get() + maxEntries == totalSubmissions)
093                        {
094                            _formMailHelper.sendLimitationReachedMailForAdmin(entry, emailsAsArray, LimitationMailType.QUEUE);
095                        }
096                    }
097                }
098            }
099        }
100    }
101
102    @Override
103    public FunctionType getFunctionExecType()
104    {
105        return FunctionType.PRE;
106    }
107    
108    @Override
109    public I18nizableText getLabel()
110    {
111        return new I18nizableText("plugin.forms", "PLUGINS_FORMS_ACTIVE_ENTRY_FUNCTION_LABEL");
112    }
113    
114    @Override
115    public List<WorkflowVisibility> getVisibilities()
116    {
117        List<WorkflowVisibility> visibilities = EnhancedFunction.super.getVisibilities();
118        visibilities.add(WorkflowVisibility.USER);
119        return visibilities;
120    }
121}