⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 notificationservices.java

📁 国外的一套开源CRM
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            Debug.logError(serviceException, "Error sending email", module);
            result = ServiceUtil.returnError("Email delivery error, see error log");
        }

        return result;
    }

    /**
     * This will process the associated notification template definition
     * with all the fields contained in the given context and generate
     * the message body of the notification.
     * <p>
     * The result returned will contain the appropriate response
     * messages indicating succes or failure and the OUT parameter,
     * "body" containing the generated message.
     * 
     * @param ctx   The dispatching context of the service
     * @param context The map containing all the fields associated with
     * the sevice
     * @return A new Map indicating success or error containing the
     * body generated from the template and the input parameters. 
     */
    public static Map prepareNotification(DispatchContext ctx, Map context) {
        GenericDelegator delegator = ctx.getDelegator();
        String templateName = (String) context.get("templateName");
        Map templateData = (Map) context.get("templateData");
        String webSiteId = (String) context.get("webSiteId");
                        
        Map result = null;                               
        if (templateData == null) {
            templateData = new HashMap();
        }
               
        try {
            // ensure the baseURl is defined
            setBaseUrl(delegator, webSiteId, templateData);
                                   
            // initialize the template reader and processor
            URL templateUrl = UtilURL.fromResource(templateName);
            
            if (templateUrl == null) {
                Debug.logError("Problem getting the template URL: " + templateName + " not found", module);
                return ServiceUtil.returnError("Problem finding template; see logs");
            }
                                    
            InputStreamReader templateReader = new InputStreamReader(templateUrl.openStream());
                        
            // process the template with the given data and write
            // the email body to the String buffer
            Writer writer = new StringWriter();
            FreeMarkerWorker.renderTemplate(templateName, templateReader, templateData, writer);

            // extract the newly created body for the notification email
            String notificationBody = writer.toString();            
            
            // generate the successfull reponse
            result = ServiceUtil.returnSuccess("Message body generated successfully");
            result.put("body", notificationBody);
        } catch (IOException ie) {
            Debug.logError(ie, "Problems reading template", module);
            result = ServiceUtil.returnError("Template reading problem, see error logs");
        } catch (TemplateException te) {
            Debug.logError(te, "Problems processing template", module);
            result = ServiceUtil.returnError("Template processing problem, see error log");
        }

        return result;
    }

    /**
     * The expectation is that a lot of notification messages will include
     * a link back to one or more pages in the system, which will require knowledge
     * of the base URL to extrapolate. This method will ensure that the
     * <code>baseUrl</code> field is set in the given context.
     * <p>
     * If it has been specified a default <code>baseUrl</code> will be
     * set using a best effort approach. If it is specified in the 
     * url.properties configuration files of the system, that will be
     * used, otherwise it will attempt resolve the fully qualified 
     * local host name.
     * <p>
     * <i>Note:</i> I thought it might be useful to have some dynamic way
     * of extending the default properties provided by the NotificationService,
     * such as the <code>baseUrl</code>, perhaps using the standard 
     * <code>ResourceBundle</code> java approach so that both classes
     * and static files may be invoked.
     *  
     * @param context   The context to check and, if necessary, set the
     * <code>baseUrl</code>.
     */
    public static void setBaseUrl(GenericDelegator delegator, String webSiteId, Map context) {
        StringBuffer httpBase = null;
        StringBuffer httpsBase = null;
                
        String localServer = null;        
               
        String httpsPort = null;
        String httpsServer = null;
        String httpPort = null;
        String httpServer = null;
        Boolean enableHttps = null;        

        // If the baseUrl was not specified we can do a best effort instead
        if (!context.containsKey("baseUrl")) {
            try {
                // using just the IP address of localhost if we don't have a defined server
                InetAddress localHost = InetAddress.getLocalHost();
                localServer = localHost.getHostAddress();
            } catch (UnknownHostException hostException) {
                Debug.logWarning(hostException, "Could not determine localhost, using '127.0.0.1'", module);
                localServer = "127.0.0.1";
            }

            // load the properties from the website entity        
            GenericValue webSite = null;
            if (webSiteId != null) {
                try {
                    webSite = delegator.findByPrimaryKeyCache("WebSite", UtilMisc.toMap("webSiteId", webSiteId));
                    if (webSite != null) {
                        httpsPort = webSite.getString("httpsPort");
                        httpsServer = webSite.getString("httpsHost");
                        httpPort = webSite.getString("httpPort");
                        httpServer = webSite.getString("httpHost");
                        enableHttps = webSite.getBoolean("enableHttps");
                    }
                } catch (GenericEntityException e) {
                    Debug.logWarning(e, "Problems with WebSite entity; using global defaults", module);
                }
            }
            
            // fill in any missing properties with fields from the global file
            if (httpsPort == null) {            
                httpsPort = UtilProperties.getPropertyValue("url.properties", "port.https", "443");
            }
            if (httpServer == null) {            
                httpsServer = UtilProperties.getPropertyValue("url.properties", "force.https.host", localServer);
            }
            if (httpPort == null) {            
                httpPort = UtilProperties.getPropertyValue("url.properties", "port.http", "80");
            }
            if (httpServer == null) {
                httpServer = UtilProperties.getPropertyValue("url.properties", "force.http.host", localServer);
            }
            if (enableHttps == null) {
                enableHttps = new Boolean(UtilProperties.propertyValueEqualsIgnoreCase("url.properties", "port.https.enabled", "Y"));
            }
                                
            // prepare the (non-secure) URL
            httpBase = new StringBuffer("http://");
            httpBase.append(httpServer);
            if (!"80".equals(httpPort)) {
                httpBase.append(":");
                httpBase.append(httpPort);
            }

            // set the base (non-secure) URL for any messages requiring it
            context.put("baseUrl", httpBase.toString());
            
            if (enableHttps.booleanValue()) {
                // prepare the (secure) URL
                httpsBase = new StringBuffer("https://");
                httpsBase.append(httpsServer);
                if (!"443".equals(httpsPort)) {
                    httpsBase.append(":");
                    httpsBase.append(httpsPort);
                }
    
                // set the base (secure) URL for any messages requiring it
                context.put("baseSecureUrl", httpsBase.toString());
            } else {
                context.put("baseSecureUrl", httpBase.toString());
            }                        
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -