Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package NotificationSystem; import Configuration.AlarmConfig; import DataStructures.MailMessage; import DataStructures.TwitterMessage; import DataStructures.UserData; import DataStructures.WorkFlowInfo; import Model.TimeAlarm; import com.google.common.cache.CacheBuilder; import com.google.common.cache.CacheLoader; import com.google.common.cache.LoadingCache; import com.sun.jersey.api.client.Client; import com.sun.jersey.api.client.ClientResponse; import com.sun.jersey.api.client.WebResource; import com.sun.jersey.api.client.config.ClientConfig; import com.sun.jersey.api.client.config.DefaultClientConfig; import com.sun.jersey.api.json.JSONConfiguration; import com.sun.jersey.core.util.MultivaluedMapImpl; import java.text.DateFormat; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.TimeZone; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; import javax.ws.rs.core.MediaType; /** * * @author estevo * //por ahora lanza siempre el mismo mensaje ya que no hay info de los workflos */ public class MessageComposer implements GenericTemplates { private LoadingCache<String, WorkFlowInfo> workflowCache; private LoadingCache<String, UserData> userCache; public MessageComposer() { this.workflowCache = CacheBuilder.newBuilder().maximumSize(AlarmConfig.getInstance().getCacheSize()) .expireAfterAccess(AlarmConfig.getInstance().getCachePersistanceTime(), TimeUnit.MINUTES) .build(new CacheLoader<String, WorkFlowInfo>() { public WorkFlowInfo load(String key) { return retrieveWorkflowInfo(key); } }); this.userCache = CacheBuilder.newBuilder().maximumSize(1000).expireAfterAccess(10, TimeUnit.MINUTES) .build(new CacheLoader<String, UserData>() { public UserData load(String key) { return retrieveUserInfo(key); } }); } //devuelve las alarmas de correo para un usuario determinado public ArrayList<MailMessage> getMailNotifications(List<TimeAlarm> mailAlarm, String UserId) throws ExecutionException { HashMap<String, ArrayList<TimeAlarm>> buffer = new HashMap<String, ArrayList<TimeAlarm>>(); ArrayList<MailMessage> listOfMsg = new ArrayList<MailMessage>(); //ordenamos las alarmas por cuenta de envio for (TimeAlarm Talarm : mailAlarm) { if (!buffer.containsKey(Talarm.getDesigner())) { ArrayList<TimeAlarm> AlarmContainer = new ArrayList<TimeAlarm>(); AlarmContainer.add(Talarm); buffer.put(Talarm.getDesigner(), AlarmContainer); } else { ArrayList<TimeAlarm> AlarmContainer = buffer.get(Talarm.getDesigner()); AlarmContainer.add(Talarm); buffer.put(Talarm.getDesigner(), AlarmContainer); } } //crear los mensajes //informacion comun // UserData userData = retrieveUserInfo(UserId); UserData userData = this.userCache.get(UserId); //subjet String subject = String.format(SubjectMailTemplate, userData.getCompleteName()); for (Map.Entry<String, ArrayList<TimeAlarm>> entry : buffer.entrySet()) { String finalMsg; MailMessage message = new MailMessage(); ArrayList<TimeAlarm> mail = entry.getValue(); String NotificationAc = entry.getKey(); if (mail.size() > 1) { System.out.println("MENSAJE COMPUESTO______________________"); finalMsg = String.format(MailFirstLineMultipleAlarms, userData.getCompleteName()); for (TimeAlarm Talarm : mail) { // WorkFlowInfo workData = retrieveWorkflowInfo(Talarm.getIdWorkflow()); WorkFlowInfo workData = this.workflowCache.get(Talarm.getIdWorkflow()); String msg = String.format(AlarmDescriptionTemplate, getDateFromLong(Talarm.getDeadLine()), workData.getWorkflowName(), workData.getWorkflowDescription()); finalMsg = finalMsg.concat(msg); } } else //size =1 { System.out.println("MENSAJE UNICO______________________" + mail.size()); finalMsg = String.format(MailFirstLineMultipleAlarms, userData.getCompleteName()); TimeAlarm Talarm = mail.get(0); // System.out.println("id" + Talarm.getAlarmId()); //System.out.println("des" + Talarm.getDesigner()); WorkFlowInfo workData = retrieveWorkflowInfo(Talarm.getIdWorkflow()); String msg = String.format(AlarmDescriptionTemplate, getDateFromLong(Talarm.getDeadLine()), workData.getWorkflowName(), workData.getWorkflowDescription()); finalMsg = finalMsg.concat(msg); } //footer finalMsg = finalMsg.concat(FooterTemplate); message.setNotificationAcount(NotificationAc); message.setToEmail(userData.getEmail()); message.setSubject(subject); message.setMensaje(finalMsg); listOfMsg.add(message); System.out.println("MAIL_MENSAJE:___ " + finalMsg); System.out.println("MAIL_NOT-->" + NotificationAc); } return listOfMsg; } public ArrayList<TwitterMessage> getTwitterNotifications(List<TimeAlarm> twitterAlarm, String UserId) throws ExecutionException { ArrayList<TwitterMessage> listOfTweets = new ArrayList<TwitterMessage>(); //ordenamos las alarmas por cuenta de envio de twitter for (TimeAlarm Talarm : twitterAlarm) { TwitterMessage tweet = new TwitterMessage(); // WorkFlowInfo workData = retrieveWorkflowInfo(Talarm.getIdWorkflow()); WorkFlowInfo workData = this.workflowCache.get(Talarm.getIdWorkflow()); String msg = String.format(DeadLineTwitterTemplate, workData.getWorkflowName(), workData.getWorkflowDescription(), getDateFromLong(Talarm.getDeadLine())); tweet.setNotificationAccount(Talarm.getDesigner()); tweet.setMessage(msg); listOfTweets.add(tweet); System.out.println("TWITTER_MSG-->" + msg); System.out.println("TWITTER_NOT-->" + Talarm.getDesigner()); } return listOfTweets; } private UserData retrieveUserInfo(String userId) { UserData data = null; ClientConfig config = new DefaultClientConfig(); config.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE); Client client = Client.create(config); //cambiar esto para recogerlo desde la configuracion WebResource webResource = client.resource(AlarmConfig.getInstance().getUserInfoEndPoint()); MultivaluedMapImpl values = new MultivaluedMapImpl(); values.add("userId", userId); System.out.println(values.get("userId")); ClientResponse response = webResource.type(MediaType.APPLICATION_FORM_URLENCODED).post(ClientResponse.class, values); data = response.getEntity(UserData.class); return data; } private WorkFlowInfo retrieveWorkflowInfo(String idWorkflow) { //IMPORTANTE----> OJ0: seteado al mismo workflow para pruebas ClientConfig config = new DefaultClientConfig(); config.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE); Client client = Client.create(config); WebResource webResource = client.resource(AlarmConfig.getInstance().getWorkflowInfoEndPoint()); MultivaluedMapImpl values = new MultivaluedMapImpl(); values.add("workflowId", "03f69cc3-3583-4ce6-b788-5d706a06d299"); ClientResponse response = webResource.type(MediaType.APPLICATION_FORM_URLENCODED).post(ClientResponse.class, values); WorkFlowInfo data = response.getEntity(WorkFlowInfo.class); return data; } private String getDateFromLong(long date) { DateFormat df = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.DATE_FIELD); df.setTimeZone(TimeZone.getTimeZone("GMT+2")); return df.format(new Date(date)); } }