From: <jt...@hy...> - 2007-07-13 17:24:26
|
Author: jtravis Date: 2007-07-13 10:24:21 -0700 (Fri, 13 Jul 2007) New Revision: 5197 URL: http://svn.hyperic.org/?view=rev&root=Hyperic+HQ&revision=5197 Modified: trunk/src/org/hyperic/hq/bizapp/server/action/email/EmailAction.java Log: Totally switch over to email templates Modified: trunk/src/org/hyperic/hq/bizapp/server/action/email/EmailAction.java =================================================================== --- trunk/src/org/hyperic/hq/bizapp/server/action/email/EmailAction.java 2007-07-13 17:16:49 UTC (rev 5196) +++ trunk/src/org/hyperic/hq/bizapp/server/action/email/EmailAction.java 2007-07-13 17:24:21 UTC (rev 5197) @@ -25,11 +25,11 @@ package org.hyperic.hq.bizapp.server.action.email; +import java.io.File; +import java.io.StringWriter; import java.io.UnsupportedEncodingException; -import java.text.MessageFormat; -import java.text.SimpleDateFormat; import java.util.ArrayList; -import java.util.Date; +import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; @@ -39,48 +39,31 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.hyperic.dao.DAOFactory; import org.hyperic.hq.appdef.shared.AppdefEntityID; +import org.hyperic.hq.application.HQApp; import org.hyperic.hq.authz.server.session.AuthzSubject; import org.hyperic.hq.authz.server.session.AuthzSubjectManagerEJBImpl; +import org.hyperic.hq.authz.server.session.ResourceDAO; import org.hyperic.hq.authz.shared.AuthzSubjectManagerLocal; import org.hyperic.hq.bizapp.shared.action.EmailActionConfig; -import org.hyperic.hq.common.server.session.ServerConfigManagerEJBImpl; -import org.hyperic.hq.common.shared.HQConstants; import org.hyperic.hq.escalation.server.session.Escalatable; -import org.hyperic.hq.escalation.server.session.EscalationManagerEJBImpl; import org.hyperic.hq.escalation.server.session.EscalationStateChange; import org.hyperic.hq.escalation.server.session.PerformsEscalations; import org.hyperic.hq.events.ActionExecuteException; import org.hyperic.hq.events.ActionExecutionInfo; import org.hyperic.hq.events.ActionInterface; -import org.hyperic.hq.events.AlertAuxLog; import org.hyperic.hq.events.AlertDefinitionInterface; import org.hyperic.hq.events.AlertInterface; -import org.hyperic.hq.events.EventConstants; import org.hyperic.hq.events.InvalidActionDataException; import org.hyperic.hq.events.Notify; -import org.hyperic.hq.measurement.MeasurementConstants; +import org.hyperic.hq.hqu.rendit.RenditServer; import org.hyperic.hq.measurement.MeasurementNotFoundException; -import org.hyperic.hq.measurement.UnitsConvert; -import org.hyperic.hq.measurement.server.session.DataManagerEJBImpl; -import org.hyperic.hq.measurement.server.session.DerivedMeasurement; -import org.hyperic.hq.measurement.server.session.DerivedMeasurementManagerEJBImpl; -import org.hyperic.hq.measurement.shared.DataManagerLocal; -import org.hyperic.hq.measurement.shared.DerivedMeasurementManagerLocal; -import org.hyperic.hq.product.MetricValue; -import org.hyperic.util.ConfigPropertyException; import org.hyperic.util.config.ConfigResponse; -import org.hyperic.util.units.FormattedNumber; public class EmailAction extends EmailActionConfig implements ActionInterface, Notify { - protected static final String LINK_FORMAT = - "alerts/Alerts.do?mode=viewAlert&eid={0,number,#}:{1,number,#}&a={2,number,#}"; - - private static final String SEPARATOR = - "\n\n------------------------------------------\n\n"; - public static final String RES_NAME_HOLDER = "RES_NAME_REPL"; public static final String RES_DESC_HOLDER = "RES_DESC_REPL"; @@ -119,180 +102,35 @@ return subj.toString(); } - private String getBaseURL() - throws ConfigPropertyException - { - if (baseUrl == null) { - baseUrl = ServerConfigManagerEJBImpl.getOne() - .getConfig().getProperty(HQConstants.BaseURL); - - // make sure no extra slashes (/) - if ( baseUrl.charAt(baseUrl.length() - 1) != '/') { - baseUrl += '/'; - } - } - return baseUrl; - } - - private String createLink(AppdefEntityID aeid, Integer aid) - throws ConfigPropertyException - { - StringBuffer text = new StringBuffer(); - - // Create link - Object[] args = { new Integer(aeid.getType()), aeid.getId(), aid }; - String alertUrl = MessageFormat.format(LINK_FORMAT, args); - text.append(getBaseURL()).append(alertUrl); - - return text.toString(); - } - private String createText(AlertDefinitionInterface alertdef, ActionExecutionInfo info, AppdefEntityID aeid, AlertInterface alert) throws MeasurementNotFoundException { - StringBuffer text = new StringBuffer("The ") - .append(RES_NAME_HOLDER).append(" ") - .append(aeid.getTypeName()) - .append(" has generated the following alert -\n") - .append(info.getShortReason()) - .append(SEPARATOR); - - text.append("ALERT DETAIL") - .append("\n- Resource Name: ").append(RES_NAME_HOLDER) - .append("\n- Resource Description: ").append(RES_DESC_HOLDER) - .append("\n- Alert Name: ").append(alertdef.getName()); + File templateDir = new File(HQApp.getInstance().getResourceDir(), + "alertTemplates"); + File templateFile = new File(templateDir, "text_email.gsp"); + StringWriter output = new StringWriter(); + Map params = new HashMap(); - if (alertdef.getDescription() != null && - alertdef.getDescription().length() > 0) { - text.append("\n- Description: ").append(alertdef.getDescription()); - } - - // Now go through the condition set - text.append("\n- Condition Set: "); - - // Get the conditions - text.append(info.getLongReason()); - - // XXX: Ashamed of myself, this is definitely not localizable - // um, when we figger out how we want to make the user's locale - // something that is accessible to the backend, this should be - // un-hardcoded - SimpleDateFormat dformat = new SimpleDateFormat("MM/dd/yyyy hh:mm aaa"); - String alertTime = dformat.format(new Date(alert.getTimestamp())); - - // The rest of the alert details - text.append("\n- Alert Severity: ") - .append(EventConstants.getPriority(alertdef.getPriority())) - .append("\n- Alert Date / Time: ").append(alertTime); - - if (!info.getAuxLogs().isEmpty()) { - text.append("\nAdditional information:\n"); - addAuxLogs(" ", info.getAuxLogs(), text); - } + ResourceDAO rDao = new ResourceDAO(DAOFactory.getDAOFactory()); - // See if we can get the previous fix - if (alertdef.performsEscalations()) { - String lastFix = EscalationManagerEJBImpl.getOne() - .getLastFix((PerformsEscalations) alertdef); - if (lastFix != null) { - text.append("\n- Previous Fix: ") - .append(lastFix); - } - } + params.put("alertDef", alertdef); + params.put("alert", alert); + params.put("action", info); + params.put("resource", rDao.findByInstanceId(aeid.getAuthzTypeId(), + aeid.getId())); - // See if we can get the indicator metrics - if (aeid.isPlatform() || aeid.isServer() || aeid.isService()) { - DerivedMeasurementManagerLocal dmMan = - DerivedMeasurementManagerEJBImpl.getOne(); - List designates = dmMan.findDesignatedMeasurements(aeid); - - // Gather the IDs to get the last data points - Integer[] mids = new Integer[designates.size()]; - int i = 0; - for (Iterator it = designates.iterator(); it.hasNext(); i++) { - DerivedMeasurement m = (DerivedMeasurement) it.next(); - mids[i] = m.getId(); - } - - DataManagerLocal dMan = DataManagerEJBImpl.getOne(); - Map values = - dMan.getLastDataPoints(mids, - MeasurementConstants.ACCEPTABLE_LIVE_MILLIS); - - // Now output the values - if (values.size() > 0) { - text.append("\n- Previous Indicator Metrics: "); - - for (Iterator it = designates.iterator(); it.hasNext(); ) { - DerivedMeasurement m = (DerivedMeasurement) it.next(); - if (values.containsKey(m.getId())) { - text.append("\n ") - .append(m.getTemplate().getName()) - .append(": "); - MetricValue val = (MetricValue) values.get(m.getId()); - FormattedNumber th = - UnitsConvert.convert(val.getValue(), - m.getTemplate().getUnits()); - text.append(th); - } - } - } - } - - text.append(SEPARATOR); - try { - // Create the links - text.append("\nFor additional detail about this alert, go to ") - .append(createLink(aeid, alert.getId())) - .append(SEPARATOR); - - // Public Service Announcement - text.append("This message was delivered to you by Hyperic HQ.") - .append("\nTo view the HQ Dashboard, go to ") - .append(getBaseURL()) - .append("Dashboard.do") - .append(SEPARATOR); - } catch (ConfigPropertyException e) { - _log.error("Error getting HQ config. Can't add link to email.", e); - } catch (ArrayIndexOutOfBoundsException e) { // Retarded ... XXX - _log.error("Error finding event id. Can't add link to email.", e); + RenditServer.getInstance().renderTemplate(templateFile, params, + output); + _log.info("Template rendered\n" + output.toString() + "\n"); + } catch(Exception e) { + _log.warn("Unable to render template", e); } - - return text.toString(); + return output.toString(); } - private void addAuxLogs(String prefix, List logs, StringBuffer text) { - String baseUrl; - - try { - baseUrl = getBaseURL(); - } catch(ConfigPropertyException e) { - _log.warn("Unable to get base url"); - return; - } - - for (Iterator i=logs.iterator(); i.hasNext(); ) { - AlertAuxLog a = (AlertAuxLog)i.next(); - - text.append(prefix) - .append(a.getDescription()) - .append("\n"); - - if (a.getURL() != null) { - text.append(prefix) - .append(" - ") - .append(baseUrl) - .append(a.getURL()) - .append("\n"); - } - - addAuxLogs(prefix + " ", a.getChildren(), text); - } - } - private AppdefEntityID getResource(AlertDefinitionInterface def) { return new AppdefEntityID(def.getAppdefType(), def.getAppdefId()); } |