View Javadoc
1   /*
2    * Copyright 2012 The Kuali Foundation.
3    * 
4    * Licensed under the Educational Community License, Version 1.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl1.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.ole.select.service.impl;
17  
18  import org.kuali.ole.select.OleSelectNotificationConstant;
19  import org.kuali.ole.select.service.OleGenericService;
20  import org.kuali.ole.select.service.OleNotifyService;
21  import org.kuali.ole.sys.context.SpringContext;
22  import org.kuali.rice.core.api.membership.MemberType;
23  import org.kuali.rice.ken.bo.*;
24  import org.kuali.rice.ken.document.kew.NotificationWorkflowDocument;
25  import org.kuali.rice.ken.service.NotificationChannelService;
26  import org.kuali.rice.ken.service.impl.NotificationContentTypeServiceImpl;
27  import org.kuali.rice.ken.service.impl.NotificationMessageContentServiceImpl;
28  import org.kuali.rice.ken.util.NotificationConstants;
29  import org.kuali.rice.ken.util.Util;
30  import org.kuali.rice.kew.api.WorkflowDocument;
31  import org.kuali.rice.kew.api.document.DocumentContent;
32  import org.kuali.rice.kew.api.document.DocumentContent.Builder;
33  import org.kuali.rice.kew.api.exception.WorkflowException;
34  import org.kuali.rice.kew.api.identity.PrincipalId;
35  import org.kuali.rice.kew.identity.service.IdentityHelperService;
36  import org.kuali.rice.kew.rule.GenericAttributeContent;
37  import org.kuali.rice.kim.api.identity.Person;
38  import org.kuali.rice.kim.api.identity.PersonService;
39  import org.kuali.rice.krad.data.DataObjectService;
40  import org.kuali.rice.krad.util.GlobalVariables;
41  
42  import java.lang.reflect.Proxy;
43  import java.sql.Timestamp;
44  import java.text.ParseException;
45  import java.util.HashMap;
46  import java.util.List;
47  import java.util.Map;
48  
49  
50  public class OleNotifyServiceImpl implements OleNotifyService {
51  
52      private OleInvocationHandler oleInvocationHandler;
53  
54      //private GenericDao genericDaoOjb;
55  
56  
57      /**
58       * Gets the genericDaoOjb attribute. 
59       * @return Returns the genericDaoOjb.
60       */
61      /*public GenericDao getGenericDaoOjb() {
62          return genericDaoOjb;
63      }*/
64  
65  
66      /**
67       * Sets the genericDaoOjb attribute value.
68       * @param genericDaoOjb The genericDaoOjb to set.
69       */
70      /*public void setGenericDaoOjb(GenericDao genericDaoOjb) {
71          this.genericDaoOjb = genericDaoOjb;
72      }*/
73  
74  
75      /**
76       * Gets the oleInvocationHandler attribute.
77       *
78       * @return Returns the oleInvocationHandler.
79       */
80      public OleInvocationHandler getOleInvocationHandler() {
81          return oleInvocationHandler;
82      }
83  
84  
85      /**
86       * Sets the oleInvocationHandler attribute value.
87       *
88       * @param oleInvocationHandler The oleInvocationHandler to set.
89       */
90      public void setOleInvocationHandler(OleInvocationHandler oleInvocationHandler) {
91          this.oleInvocationHandler = oleInvocationHandler;
92      }
93  
94  
95      /**
96       * @see org.kuali.ole.select.service.OleNotifyService#notify(java.util.List, java.lang.String)
97       */
98      public void notify(List<String> userRecipients, String message) throws WorkflowException {
99          String currentUser = null;
100         if (GlobalVariables.getUserSession().getPrincipalName() != null) {
101             Person principalPerson1 = SpringContext.getBean(PersonService.class).getPerson(GlobalVariables.getUserSession().getPerson().getPrincipalId());
102             currentUser = principalPerson1.getPrincipalId();
103 
104         }
105         PrincipalId initiator = new PrincipalId(currentUser);
106         WorkflowDocument docs = NotificationWorkflowDocument.createNotificationDocument(initiator.toString(), NotificationConstants.KEW_CONSTANTS.SEND_NOTIFICATION_REQ_DOC_TYPE);
107         // // new NotificationWorkflowDocument();
108         ///  (initiator,NotificationConstants.KEW_CONSTANTS.SEND_NOTIFICATION_REQ_DOC_TYPE);
109         NotificationBo notification = populateNotificationInstance(currentUser, userRecipients, message);
110         NotificationContentTypeServiceImpl content = new NotificationContentTypeServiceImpl(SpringContext.getBean(DataObjectService.class));
111         NotificationMessageContentServiceImpl messageContentService = new NotificationMessageContentServiceImpl(SpringContext.getBean(DataObjectService.class), content);
112         String notificationAsXml = messageContentService.generateNotificationMessage(notification);
113         Map<String, String> attrFields = new HashMap<String, String>();
114         List<NotificationChannelReviewerBo> reviewers = notification.getChannel().getReviewers();
115         int ui = 0;
116         int gi = 0;
117         for (NotificationChannelReviewerBo reviewer : reviewers) {
118             String prefix;
119             int index;
120             if (MemberType.PRINCIPAL.equals(reviewer.getReviewerType())) {
121                 prefix = "user";
122                 index = ui;
123                 ui++;
124             } else if (MemberType.GROUP.equals(reviewer.getReviewerType())) {
125                 prefix = "group";
126                 index = gi;
127                 gi++;
128             } else {
129                 continue;
130             }
131             attrFields.put(prefix + index, reviewer.getReviewerId());
132         }
133         GenericAttributeContent gac = new GenericAttributeContent("channelReviewers");
134         DocumentContent documentContent = docs.getDocumentContent();
135         DocumentContent.Builder builder = Builder.create(docs.getDocumentId());
136         builder.setApplicationContent(notificationAsXml);
137         builder.setAttributeContent("<attributeContent>" + gac.generateContent(attrFields) + "</attributeContent>");
138         docs.setTitle(notification.getTitle());
139         docs.route("This message was submitted via the simple notification message submission form by user "
140                 + initiator.getPrincipalId());
141     }
142 
143 
144     private NotificationBo populateNotificationInstance(String currentUser, List<String> userRecipients, String message) {
145         OleGenericService generic = (OleGenericService) Proxy.newProxyInstance(OleGenericService.class.getClassLoader(),
146                 new Class[]{OleGenericService.class},
147                 oleInvocationHandler);
148         NotificationBo notification = new NotificationBo();
149         String channelName = OleSelectNotificationConstant.CHANNEL_NAME;
150         String priorityName = OleSelectNotificationConstant.PRIORITY_NAME;
151         String senderNames = currentUser;
152         String deliveryType = NotificationConstants.DELIVERY_TYPES.FYI;
153         String sendDateTime = Util.getCurrentDateTime();
154         String title = OleSelectNotificationConstant.NOTIFICATION_TITLE;
155         NotificationChannelService notify = (NotificationChannelService) SpringContext.getBean(NotificationChannelService.class);
156         NotificationChannelBo channel = notify.getNotificationChannel(new String(OleSelectNotificationConstant.NOTIFICATION_CHANNEL_ID));
157         NotificationPriorityBo priority = (NotificationPriorityBo) generic.getObject(OleSelectNotificationConstant.NOTIFICATION_NAME, priorityName, NotificationPriorityBo.class);
158         notification.setPriority(priority);
159         NotificationContentTypeBo contentType = (NotificationContentTypeBo) generic.getObject(OleSelectNotificationConstant.NOTIFICATION_NAME, NotificationConstants.CONTENT_TYPES.SIMPLE_CONTENT_TYPE, NotificationContentTypeBo.class);
160         notification.setContentType(contentType);
161         Map producerMap = new HashMap();
162         producerMap.put(OleSelectNotificationConstant.NOTIFICATION_NAME, OleSelectNotificationConstant.NOTIFICATION_SYSTEM);
163         NotificationProducerBo producer = (NotificationProducerBo) generic.getObject(OleSelectNotificationConstant.NOTIFICATION_NAME, OleSelectNotificationConstant.NOTIFICATION_SYSTEM, NotificationProducerBo.class);
164         notification.setProducer(producer);
165         NotificationSenderBo ns = new NotificationSenderBo();
166         ns.setSenderName(senderNames.trim());
167         notification.addSender(ns);
168         notification.setChannel(channel);
169         for (String userRecipient : userRecipients) {
170             NotificationRecipientBo recipient = new NotificationRecipientBo();
171             recipient.setRecipientType(MemberType.PRINCIPAL.getCode());
172             String userRecipientId = SpringContext.getBean(IdentityHelperService.class).getPrincipalByPrincipalName(userRecipient).getPrincipalId();
173             StringBuffer buffer = new StringBuffer();
174             recipient.setRecipientId(userRecipient);
175             notification.addRecipient(recipient);
176         }
177         notification.setTitle(title);
178         notification.setDeliveryType(deliveryType);
179         try {
180             notification.setSendDateTimeValue(new Timestamp(Util.parseUIDateTime(sendDateTime).getTime()));
181         } catch (ParseException e) {
182             throw new RuntimeException(e);
183         }
184         notification
185                 .setContent(NotificationConstants.XML_MESSAGE_CONSTANTS.CONTENT_SIMPLE_OPEN
186                         + NotificationConstants.XML_MESSAGE_CONSTANTS.MESSAGE_OPEN
187                         + message
188                         + NotificationConstants.XML_MESSAGE_CONSTANTS.MESSAGE_CLOSE
189                         + NotificationConstants.XML_MESSAGE_CONSTANTS.CONTENT_CLOSE);
190 
191         return notification;
192     }
193 
194 
195 }