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.Map;
019import java.util.Optional;
020
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023
024import org.ametys.cms.workflow.AmetysObjectCheckRightsCondition;
025import org.ametys.plugins.forms.helper.FormMailHelper;
026import org.ametys.plugins.forms.helper.FormMailHelper.LimitationMailType;
027import org.ametys.plugins.forms.helper.LimitedEntriesHelper;
028import org.ametys.plugins.forms.repository.Form;
029import org.ametys.plugins.forms.repository.FormEntry;
030import org.ametys.plugins.workflow.AbstractWorkflowComponent;
031import org.ametys.runtime.i18n.I18nizableText;
032
033import com.opensymphony.module.propertyset.PropertySet;
034import com.opensymphony.workflow.FunctionProvider;
035import com.opensymphony.workflow.InvalidActionException;
036import com.opensymphony.workflow.WorkflowException;
037
038/**
039 * OS workflow function to active the entry
040 */
041public class ActiveEntryFunction extends AbstractWorkflowComponent implements FunctionProvider
042{
043    /** The limited entries helper */
044    protected LimitedEntriesHelper _limitedEntriesHelper;
045    
046    /** The form mail helper */
047    protected FormMailHelper _formMailHelper;
048    
049    @Override
050    public void service(ServiceManager manager) throws ServiceException
051    {
052        super.service(manager);
053        _limitedEntriesHelper = (LimitedEntriesHelper) manager.lookup(LimitedEntriesHelper.ROLE);
054        _formMailHelper = (FormMailHelper) manager.lookup(FormMailHelper.ROLE);
055    }
056    
057    @Override
058    public void execute(Map transientVars, Map args, PropertySet ps) throws WorkflowException
059    {
060        FormEntry entry = (FormEntry) transientVars.get(AmetysObjectCheckRightsCondition.AMETYS_OBJECT_KEY);
061        if (!entry.isActive())
062        {
063            if (_limitedEntriesHelper.isFormLimitIsReached(entry.getForm()))
064            {
065                addWorkflowError(transientVars, new I18nizableText("Le formulaire '" + entry.getForm().getTitle() + "' est clos car sa limite est atteinte. Impossible de réactiver l'entrée."));
066                throw new InvalidActionException("Can activate the entry because the form (" + entry.getForm().getId() + ") limit is reach");
067            }
068            else
069            {   
070                entry.setActive(true);
071                Form form = entry.getForm();
072                
073                Optional<String[]> emailsAsArrayOpt = form.getAdminEmails();
074                if (emailsAsArrayOpt.isPresent())
075                {
076                    String[] emailsAsArray = emailsAsArrayOpt.get();
077                    if (form.isEntriesLimited())
078                    {
079                        int totalSubmissions = form.getActiveEntries().size();
080                        Long maxEntries = form.getMaxEntries().get();
081                        if (maxEntries == totalSubmissions)
082                        {
083                            _formMailHelper.sendLimitationReachedMailForAdmin(entry, emailsAsArray, LimitationMailType.LIMIT);
084                        }
085                        else if (form.isQueueEnabled() && form.getQueueSize().isPresent() && form.getQueueSize().get() + maxEntries == totalSubmissions)
086                        {
087                            _formMailHelper.sendLimitationReachedMailForAdmin(entry, emailsAsArray, LimitationMailType.QUEUE);
088                        }
089                    }
090                }
091            }
092        }
093    }
094
095}