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.Map;
020import java.util.Optional;
021import java.util.regex.Matcher;
022import java.util.regex.Pattern;
023
024import org.apache.avalon.framework.component.Component;
025import org.apache.avalon.framework.service.ServiceException;
026import org.apache.avalon.framework.service.ServiceManager;
027import org.apache.avalon.framework.service.Serviceable;
028
029import org.ametys.core.ui.Callable;
030import org.ametys.plugins.forms.dao.FormDAO;
031import org.ametys.plugins.forms.repository.Form;
032import org.ametys.plugins.repository.AmetysObjectResolver;
033import org.ametys.runtime.config.Config;
034
035/**
036 * The helper for acknowledgement of receipts
037 */
038public class AcknowledgementOfReceiptsHelper implements Serviceable, Component
039{
040    /** Avalon ROLE. */
041    public static final String ROLE = AcknowledgementOfReceiptsHelper.class.getName();
042   
043    /** Pattern for senders mail */
044    public static final String REGEX_SENDER_MAIL = "^([a-zA-Z0-9_\\.\\-])+\\@(([a-zA-Z0-9\\-])+\\.)+([a-zA-Z0-9]{2,})$";
045
046    /** Ametys object resolver. */
047    protected AmetysObjectResolver _resolver;
048    
049    /** The form DAO */
050    protected FormDAO _formDAO;
051    
052    public void service(ServiceManager manager) throws ServiceException
053    {
054        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
055        _formDAO = (FormDAO) manager.lookup(FormDAO.ROLE);
056    }
057    
058    /**
059     * Get the form properties relative to the acknowledgement of receipt
060     * @param formId The id of the form
061     * @return  a map of the form acknowledgement of receipt properties 
062     */
063    @Callable
064    public Map<String, Object> getAcknowledgementReceiptProperties(String formId)
065    {
066        Map<String, Object> receiptProperties = new HashMap<> ();
067        
068        Form form = _resolver.resolveById(formId);
069        _formDAO.checkHandleFormRight(form);
070        
071        _addOptionalProperty(Form.RECEIPT_SENDER, form.getReceiptSender(), receiptProperties, Config.getInstance().getValue("smtp.mail.from"));
072        _addOptionalProperty(Form.RECEIPT_RECEIVER, form.getReceiptReceiver(), receiptProperties, FormMailHelper.RECEIVER_COMBOBOX_ENTRY_USER_VALUE);
073        _addOptionalProperty(Form.RECEIPT_SUBJECT, form.getReceiptSubject(), receiptProperties, null);
074        _addOptionalProperty(Form.RECEIPT_BODY, form.getReceiptBody(), receiptProperties, null);
075        
076        return receiptProperties;
077    }
078    
079    private void _addOptionalProperty(String propertyName, Optional<? extends Object> value, Map<String, Object> limitProperties, Object defaultValue)
080    {
081        if (value.isPresent())
082        {
083            limitProperties.put(propertyName, value.get());
084        }
085        else if (defaultValue != null)
086        {
087            limitProperties.put(propertyName, defaultValue);
088        }
089    }
090    
091    /**
092     * Set the properties relative to the acknowledgement of receipt 
093     * @param formId id of the form
094     * @param sender the sender email address
095     * @param receiver name for form of question where the receiver's email address will be entered
096     * @param subject The subject for the email 
097     * @param body The body of the email
098     * @return the map of results
099     */
100    @Callable
101    public Map<String, Object> setAcknowledgementReceiptProperties(String formId, String sender, String receiver, String subject, String body)
102    {
103        Map<String, Object> result = new HashMap<>();
104        
105        Form form = _resolver.resolveById(formId);
106        _formDAO.checkHandleFormRight(form);
107        
108        Pattern pattern = Pattern.compile(REGEX_SENDER_MAIL);
109        Matcher matcher = pattern.matcher(sender);
110        if (matcher.matches())
111        {
112            form.setReceiptSender(sender);
113            form.setReceiptReceiver(receiver);
114            form.setReceiptSubject(subject);
115            form.setReceiptBody(body);
116            
117            form.saveChanges();
118        }
119        
120        return result;
121    }
122    
123    /**
124     * Unset the properties relative to the acknowledgement of receipt 
125     * @param formId The id ofthe current form
126     * @return the map of results
127     */
128    @Callable
129    public Map<String, Object> removeEntriesReceipt(String formId)
130    {
131        Map<String, Object> result = new HashMap<>();
132        
133        Form form = _resolver.resolveById(formId);
134        _formDAO.checkHandleFormRight(form);
135        
136        if (form.getReceiptReceiver().isPresent())
137        {
138            form.removeValue(Form.RECEIPT_SENDER);
139            form.removeValue(Form.RECEIPT_RECEIVER);
140            form.removeValue(Form.RECEIPT_SUBJECT);
141            form.removeValue(Form.RECEIPT_BODY);
142
143            form.saveChanges();
144        }
145        
146        return result;
147    }
148}