Add project url to email template keywords, allow url keyword in timesheet template

This commit is contained in:
lmann99 2017-01-01 08:42:21 -05:00
parent 02800f97db
commit 1e58368f3e
4 changed files with 255 additions and 166 deletions

View file

@ -19,6 +19,8 @@
package org.libreplan.importers.notifications;
//import javax.annotation.Resource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.libreplan.business.common.entities.ConnectorProperty;
@ -33,9 +35,20 @@ import org.libreplan.web.email.IEmailTemplateModel;
import org.libreplan.web.planner.tabs.MultipleTabsPlannerController;
import org.libreplan.web.resources.worker.IWorkerModel;
import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.ApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.core.env.AbstractEnvironment;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import javax.servlet.ServletContext;
import org.zkoss.zul.Messagebox;
import javax.mail.Message;
@ -45,19 +58,45 @@ import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.management.AttributeNotFoundException;
import javax.management.InstanceNotFoundException;
import javax.management.MBeanException;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.Query;
import javax.management.ReflectionException;
import javax.naming.InitialContext;
import javax.naming.Context;
import java.util.List;
import java.util.Locale;
import java.util.Properties;
import java.util.Set;
import java.util.Iterator;
import java.util.Map;
import java.util.ArrayList;
import java.util.HashMap;
import java.lang.String;
import java.lang.management.ManagementFactory;
import java.io.File;
import java.net.InetAddress;
import java.net.URLDecoder;
import java.net.UnknownHostException;
import java.io.UnsupportedEncodingException;
import org.springframework.core.env.MapPropertySource;
import static org.libreplan.web.I18nHelper._;
/**
* Sends E-mail to users with data that storing in notification_queue table
* and that are treat to incoming {@link EmailNotification}.
* Sends E-mail to users with data that storing in notification_queue table and
* that are treat to incoming {@link EmailNotification}.
*
* @author Vova Perebykivskyi <vova@libreplan-enterprise.com>
*/
@Configuration
@Component
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class ComposeMessage {
@ -71,6 +110,15 @@ public class ComposeMessage {
@Autowired
private EmailConnectionValidator emailConnectionValidator;
// @Autowired
// private Environment environment;
@Autowired
private WebApplicationContext webApplicationContext;
// @Autowired
// private ServletContext servletContext;
private String protocol;
private String host;
@ -87,7 +135,6 @@ public class ComposeMessage {
private static final Log LOG = LogFactory.getLog(ComposeMessage.class);
public boolean composeMessageForUser(EmailNotification notification) {
// Gather data about EmailTemplate needs to be used
Resource resource = notification.getResource();
@ -122,7 +169,8 @@ public class ComposeMessage {
final String username = usrnme;
final String password = psswrd;
// It is very important to use Session.getInstance() instead of Session.getDefaultInstance()
// It is very important to use Session.getInstance() instead of
// Session.getDefaultInstance()
Session mailSession = Session.getInstance(properties, new javax.mail.Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
@ -150,8 +198,7 @@ public class ComposeMessage {
throw new RuntimeException(e);
} catch (NullPointerException e) {
if (receiver == null) {
Messagebox.show(
_(currentWorker.getUser().getLoginName() + " - this user have not filled E-mail"),
Messagebox.show(_(currentWorker.getUser().getLoginName() + " - this user have not filled E-mail"),
_("Error"), Messagebox.OK, Messagebox.ERROR);
}
}
@ -188,23 +235,52 @@ public class ComposeMessage {
private String replaceKeywords(String text, Worker currentWorker, EmailNotification notification) {
String newText = text;
// replace {url} in all messages even timesheet reminder emails
// as a link may be helpful
newText = newText.replaceAll("\\{url\\}", MultipleTabsPlannerController.WELCOME_URL);
if (notification.getType().equals(EmailTemplateEnum.TEMPLATE_ENTER_DATA_IN_TIMESHEET)) {
// It is because there is no other data for
// EmailNotification of TEMPLATE_ENTER_DATA_IN_TIMESHEET notification type
// EmailNotification of TEMPLATE_ENTER_DATA_IN_TIMESHEET
// notification type
newText = newText.replaceAll("\\{resource\\}", notification.getResource().getName());
}
else {
} else {
newText = newText.replaceAll("\\{username\\}", currentWorker.getUser().getLoginName());
newText = newText.replaceAll("\\{firstname\\}", currentWorker.getUser().getFirstName());
newText = newText.replaceAll("\\{lastname\\}", currentWorker.getUser().getLastName());
newText = newText.replaceAll("\\{project\\}", notification.getProject().getName());
newText = newText.replaceAll("\\{resource\\}", notification.getResource().getName());
newText = newText.replaceAll("\\{task\\}", notification.getTask().getName());
newText = newText.replaceAll("\\{url\\}", MultipleTabsPlannerController.WELCOME_URL);
newText = newText.replaceAll("\\{projecturl\\}", MultipleTabsPlannerController.WELCOME_URL+ ";order=" + notification.getProject().getProjectCode());
}
return newText;
}
List<String> getEndPoints() throws MalformedObjectNameException, NullPointerException, UnknownHostException,
AttributeNotFoundException, InstanceNotFoundException, MBeanException, ReflectionException {
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
Set<ObjectName> objs = mbs.queryNames(new ObjectName("*:type=Connector,*"),
Query.match(Query.attr("protocol"), Query.value("HTTP/1.1")));
String hostname = InetAddress.getLocalHost().getHostName();
InetAddress[] addresses = InetAddress.getAllByName(hostname);
ArrayList<String> endPoints = new ArrayList<String>();
for (Iterator<ObjectName> i = objs.iterator(); i.hasNext();) {
ObjectName obj = i.next();
String scheme = mbs.getAttribute(obj, "scheme").toString();
String port = obj.getKeyProperty("port");
for (InetAddress addr : addresses) {
String host = addr.getHostAddress();
String ep = scheme + "://" + host + ":" + port;
endPoints.add(ep);
}
}
return endPoints;
}
private void setupConnectionProperties() {
List<ConnectorProperty> emailConnectorProperties = emailConnectionValidator.getEmailConnectorProperties();
@ -250,8 +326,7 @@ public class ComposeMessage {
properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.port", port);
}
else if ( "SMTP".equals(protocol) ) {
} else if ("SMTP".equals(protocol)) {
properties.put("mail.smtp.host", host);
properties.put("mail.smtp.port", port);
}
@ -283,5 +358,4 @@ public class ComposeMessage {
return null;
}
}
}

View file

@ -9291,6 +9291,10 @@ msgstr ""
msgid "Welcome page"
msgstr ""
#: libreplan-webapp/src/main/webapp/email/email_templates.zul:124
msgid "Project details page"
msgstr ""
#: libreplan-business/src/main/java/org/libreplan/business/orders/entities/OrderStatusEnum.java:43
msgid "ARCHIVED"
msgstr ""

View file

@ -118,6 +118,11 @@
<label value="{url}"/>
<label value="${i18n:_('Welcome page')}"/>
</row>
<row>
<label value="{projecturl}"/>
<label value="${i18n:_('Project details page')}"/>
</row>
</rows>
</grid>

View file

@ -866,6 +866,12 @@
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.21</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>