001/* 002 * Copyright 2016 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.Arrays; 020import java.util.HashMap; 021import java.util.List; 022import java.util.Map; 023import java.util.regex.Pattern; 024 025import org.apache.avalon.framework.component.Component; 026import org.apache.avalon.framework.service.ServiceException; 027import org.apache.avalon.framework.service.ServiceManager; 028import org.apache.avalon.framework.service.Serviceable; 029 030import org.ametys.core.ui.Callable; 031import org.ametys.core.user.CurrentUserProvider; 032import org.ametys.core.user.User; 033import org.ametys.core.user.UserIdentity; 034import org.ametys.core.user.UserManager; 035import org.ametys.core.util.mail.SendMailHelper; 036import org.ametys.plugins.forms.Field; 037import org.ametys.plugins.forms.Field.FieldType; 038import org.ametys.plugins.forms.Form; 039import org.ametys.plugins.forms.FormsException; 040import org.ametys.plugins.forms.data.FieldValue; 041import org.ametys.plugins.forms.data.UserEntry; 042import org.ametys.plugins.forms.jcr.FormPropertiesManager; 043import org.ametys.plugins.forms.table.FormTableManager; 044import org.ametys.runtime.plugin.component.AbstractLogEnabled; 045 046/** 047 * Helper component allowing to retrieve information in order to send emails 048 */ 049public class MailInformationHelper extends AbstractLogEnabled implements Component, Serviceable 050{ 051 /** The Avalon role */ 052 public static final String ROLE = MailInformationHelper.class.getName(); 053 054 /** The email validation pattern. */ 055 protected static final Pattern _EMAIL_PATTERN = SendMailHelper.EMAIL_VALIDATION; 056 057 /** The current user provider */ 058 private CurrentUserProvider _currentUserProvider; 059 060 /** The user manager */ 061 private UserManager _userManager; 062 063 /** The manager for the JCR representation of forms */ 064 private FormPropertiesManager _formPropertiesManager; 065 066 /** The manager for the form entries */ 067 private FormTableManager _formTableManager; 068 069 public void service(ServiceManager serviceManager) throws ServiceException 070 { 071 _currentUserProvider = (CurrentUserProvider) serviceManager.lookup(CurrentUserProvider.ROLE); 072 _userManager = (UserManager) serviceManager.lookup(UserManager.ROLE); 073 _formPropertiesManager = (FormPropertiesManager) serviceManager.lookup(FormPropertiesManager.ROLE); 074 _formTableManager = (FormTableManager) serviceManager.lookup(FormTableManager.ROLE); 075 } 076 077 /** 078 * Get information on the given entry of the given form 079 * @param formId the id of the form 080 * @param entryId the id of the entry 081 * @return a map of information on the current user and on the email fields filled in the entry 082 */ 083 @Callable 084 public Map<String, Object> getMailInfo(String formId, Integer entryId) 085 { 086 Map<String, Object> result = new HashMap<> (); 087 088 // Retrieve the current user name and email 089 UserIdentity userIdentity = _currentUserProvider.getUser(); 090 User user = _userManager.getUser(userIdentity.getPopulationId(), userIdentity.getLogin()); 091 092 Map<String, Object> currentUser = new HashMap<> (); 093 currentUser.put("email", user.getEmail()); 094 currentUser.put("fullname", user.getFullName()); 095 096 Map<String, Object> sendMail = new HashMap<> (); 097 sendMail.put("current-user", currentUser); 098 099 // Retrieve the email fields 100 List<UserEntry> submission = null; 101 try 102 { 103 Form form = _formPropertiesManager.getForm(formId); 104 Map<String, FieldValue> columns = _formTableManager.getColumns(form); 105 106 submission = _formTableManager.getSubmissions(form, columns, 0, Integer.MAX_VALUE, new ArrayList<>(Arrays.asList(Integer.valueOf(entryId)))); 107 } 108 catch (FormsException e) 109 { 110 throw new IllegalArgumentException("An error occured while trying to recover the user entry '" + entryId + "' of the form of id '" + formId + "'."); 111 } 112 113 List<Map<String, Object>> emails = new ArrayList<> (); 114 115 List<FieldValue> entries = submission.get(0).getValues(); 116 for (FieldValue entry : entries) 117 { 118 Field field = entry.getField(); 119 if (FieldType.TEXT.equals(field.getType())) 120 { 121 Map<String, String> properties = field.getProperties(); 122 if ("email".equals(properties.get("regexptype")) && entry.getValue() != null && _EMAIL_PATTERN.matcher((String) entry.getValue()).matches()) 123 { 124 Map<String, Object> email = new HashMap<> (); 125 126 email.put("displayValue", field.getLabel() + ": " + entry.getValue()); 127 email.put("value", entry.getValue()); 128 129 emails.add(email); 130 } 131 } 132 } 133 134 sendMail.put("emails", emails); 135 result.put("send-mail", sendMail); 136 return result; 137 } 138}