001: /*
002: Licensed to the Apache Software Foundation (ASF) under one
003: or more contributor license agreements. See the NOTICE file
004: distributed with this work for additional information
005: regarding copyright ownership. The ASF licenses this file
006: to you under the Apache License, Version 2.0 (the
007: "License"); you may not use this file except in compliance
008: with the License. You may obtain a copy of the License at
009:
010: http://www.apache.org/licenses/LICENSE-2.0
011:
012: Unless required by applicable law or agreed to in writing,
013: software distributed under the License is distributed on an
014: "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: KIND, either express or implied. See the License for the
016: specific language governing permissions and limitations
017: under the License.
018: */
019:
020: package org.ofbiz.service;
021:
022: import org.ofbiz.service.config.ServiceConfigUtil;
023: import org.ofbiz.base.util.StringUtil;
024: import org.ofbiz.base.util.Debug;
025:
026: import java.util.List;
027: import java.util.Map;
028:
029: import javolution.util.FastMap;
030:
031: /**
032: * ModelNotification
033: */
034: public class ModelNotification {
035:
036: public static final String module = ModelNotification.class
037: .getName();
038:
039: public String notificationGroupName;
040: public String notificationEvent;
041: public String notificationMode;
042:
043: public void callNotify(DispatchContext dctx, ModelService model,
044: Map context, Map result) {
045: String this Event = (String) result
046: .get(ModelService.RESPONSE_MESSAGE);
047: if (notificationEvent.equals(this Event)) {
048: String notificationService = this .getService();
049: if (notificationService != null) {
050: try {
051: Map notifyContext = this .buildContext(context,
052: result, model);
053: dctx.getDispatcher().runSync(getService(),
054: notifyContext, 90, true);
055: } catch (GenericServiceException e) {
056: Debug.logError(e, module);
057: }
058: }
059: }
060: }
061:
062: public Map buildContext(Map context, Map result, ModelService model)
063: throws GenericServiceException {
064: Map userLogin = (Map) context.get("userLogin");
065: String partyId = null;
066: if (userLogin != null) {
067: partyId = (String) userLogin.get("partyId");
068: }
069:
070: String screen = getScreen();
071: if (screen == null) {
072: throw new GenericServiceException(
073: "SCREEN is a required attribute; check serviceengine.xml group definition; cannot generate notification");
074: }
075:
076: String subject = getSubject();
077: String from = buildFrom();
078: String bcc = buildBcc();
079: String cc = buildCc();
080: String to = buildTo();
081: if (subject == null || from == null || to == null) {
082: throw new GenericServiceException(
083: "TO, FROM and SUBJECT are required for notifications; check serviceengine.xml group definition");
084: }
085:
086: // template context
087: Map notifyContext = FastMap.newInstance();
088: Map bodyParams = FastMap.newInstance();
089: bodyParams.put("serviceContext", context);
090: bodyParams.put("serviceResult", result);
091: bodyParams.put("service", model);
092:
093: // notification context
094: notifyContext.put("bodyParameters", bodyParams);
095:
096: notifyContext.put("sendFrom", from);
097: notifyContext.put("sendBcc", bcc);
098: notifyContext.put("sendCc", cc);
099: notifyContext.put("sendTo", to);
100: notifyContext.put("subject", subject);
101: notifyContext.put("partyId", partyId);
102:
103: notifyContext.put("bodyScreenUri", screen);
104:
105: return notifyContext;
106: }
107:
108: public String buildTo() {
109: ServiceConfigUtil.NotificationGroup group = ServiceConfigUtil
110: .getNotificationGroup(notificationGroupName);
111: if (group != null) {
112: List addr = group.getAddress("to");
113: if (addr != null && addr.size() > 0) {
114: return StringUtil.join(addr, ",");
115: }
116: }
117: return null;
118: }
119:
120: public String buildCc() {
121: ServiceConfigUtil.NotificationGroup group = ServiceConfigUtil
122: .getNotificationGroup(notificationGroupName);
123: if (group != null) {
124: List addr = group.getAddress("cc");
125: if (addr != null) {
126: return StringUtil.join(addr, ",");
127: }
128: }
129: return null;
130: }
131:
132: public String buildBcc() {
133: ServiceConfigUtil.NotificationGroup group = ServiceConfigUtil
134: .getNotificationGroup(notificationGroupName);
135: if (group != null) {
136: List addr = group.getAddress("bcc");
137: if (addr != null && addr.size() > 0) {
138: return StringUtil.join(addr, ",");
139: }
140: }
141: return null;
142: }
143:
144: public String buildFrom() {
145: ServiceConfigUtil.NotificationGroup group = ServiceConfigUtil
146: .getNotificationGroup(notificationGroupName);
147: if (group != null) {
148: List addr = group.getAddress("from");
149: if (addr != null && addr.size() > 0) {
150: return (String) addr.get(0);
151: }
152: }
153: return null;
154: }
155:
156: public String getSubject() {
157: ServiceConfigUtil.NotificationGroup group = ServiceConfigUtil
158: .getNotificationGroup(notificationGroupName);
159: if (group != null) {
160: return group.getSubject();
161: }
162: return null;
163: }
164:
165: public String getScreen() {
166: ServiceConfigUtil.NotificationGroup group = ServiceConfigUtil
167: .getNotificationGroup(notificationGroupName);
168: if (group != null) {
169: return group.getScreen();
170: }
171: return null;
172: }
173:
174: public String getService() {
175: ServiceConfigUtil.NotificationGroup group = ServiceConfigUtil
176: .getNotificationGroup(notificationGroupName);
177: if (group != null) {
178: // only service supported at this time
179: return "sendMailFromScreen";
180: }
181: return null;
182: }
183: }
|