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.core.impl.persistence.dao.GenericDaoOjb;
24  import org.kuali.rice.ken.bo.*;
25  import org.kuali.rice.ken.document.kew.NotificationWorkflowDocument;
26  import org.kuali.rice.ken.service.NotificationChannelService;
27  import org.kuali.rice.ken.service.impl.NotificationContentTypeServiceImpl;
28  import org.kuali.rice.ken.service.impl.NotificationMessageContentServiceImpl;
29  import org.kuali.rice.ken.util.NotificationConstants;
30  import org.kuali.rice.ken.util.Util;
31  import org.kuali.rice.kew.api.WorkflowDocument;
32  import org.kuali.rice.kew.api.document.DocumentContent;
33  import org.kuali.rice.kew.api.document.DocumentContent.Builder;
34  import org.kuali.rice.kew.api.exception.WorkflowException;
35  import org.kuali.rice.kew.api.identity.PrincipalId;
36  import org.kuali.rice.kew.identity.service.IdentityHelperService;
37  import org.kuali.rice.kew.rule.GenericAttributeContent;
38  import org.kuali.rice.kim.api.identity.Person;
39  import org.kuali.rice.kim.api.identity.PersonService;
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         GenericDaoOjb genericDaoOjb = new GenericDaoOjb();
101         if (GlobalVariables.getUserSession().getPrincipalName() != null) {
102             Person principalPerson1 = SpringContext.getBean(PersonService.class).getPerson(GlobalVariables.getUserSession().getPerson().getPrincipalId());
103             currentUser = principalPerson1.getPrincipalId();
104 
105         }
106         PrincipalId initiator = new PrincipalId(currentUser);
107         WorkflowDocument docs = NotificationWorkflowDocument.createNotificationDocument(initiator.toString(), NotificationConstants.KEW_CONSTANTS.SEND_NOTIFICATION_REQ_DOC_TYPE);
108         // // new NotificationWorkflowDocument();
109         ///  (initiator,NotificationConstants.KEW_CONSTANTS.SEND_NOTIFICATION_REQ_DOC_TYPE);
110         NotificationBo notification = populateNotificationInstance(currentUser, userRecipients, message);
111         NotificationContentTypeServiceImpl content = new NotificationContentTypeServiceImpl(genericDaoOjb);
112         NotificationMessageContentServiceImpl messageContentService = new NotificationMessageContentServiceImpl(genericDaoOjb, content);
113         String notificationAsXml = messageContentService.generateNotificationMessage(notification);
114         Map<String, String> attrFields = new HashMap<String, String>();
115         List<NotificationChannelReviewerBo> reviewers = notification.getChannel().getReviewers();
116         int ui = 0;
117         int gi = 0;
118         for (NotificationChannelReviewerBo reviewer : reviewers) {
119             String prefix;
120             int index;
121             if (MemberType.PRINCIPAL.equals(reviewer.getReviewerType())) {
122                 prefix = "user";
123                 index = ui;
124                 ui++;
125             } else if (MemberType.GROUP.equals(reviewer.getReviewerType())) {
126                 prefix = "group";
127                 index = gi;
128                 gi++;
129             } else {
130                 continue;
131             }
132             attrFields.put(prefix + index, reviewer.getReviewerId());
133         }
134         GenericAttributeContent gac = new GenericAttributeContent("channelReviewers");
135         DocumentContent documentContent = docs.getDocumentContent();
136         DocumentContent.Builder builder = Builder.create(docs.getDocumentId());
137         builder.setApplicationContent(notificationAsXml);
138         builder.setAttributeContent("<attributeContent>" + gac.generateContent(attrFields) + "</attributeContent>");
139         docs.setTitle(notification.getTitle());
140         docs.route("This message was submitted via the simple notification message submission form by user "
141                 + initiator.getPrincipalId());
142     }
143 
144 
145     private NotificationBo populateNotificationInstance(String currentUser, List<String> userRecipients, String message) {
146         OleGenericService generic = (OleGenericService) Proxy.newProxyInstance(OleGenericService.class.getClassLoader(),
147                 new Class[]{OleGenericService.class},
148                 oleInvocationHandler);
149         NotificationBo notification = new NotificationBo();
150         String channelName = OleSelectNotificationConstant.CHANNEL_NAME;
151         String priorityName = OleSelectNotificationConstant.PRIORITY_NAME;
152         String senderNames = currentUser;
153         String deliveryType = NotificationConstants.DELIVERY_TYPES.FYI;
154         String sendDateTime = Util.getCurrentDateTime();
155         String title = OleSelectNotificationConstant.NOTIFICATION_TITLE;
156         NotificationChannelService notify = (NotificationChannelService) SpringContext.getBean(NotificationChannelService.class);
157         NotificationChannelBo channel = notify.getNotificationChannel(new String(OleSelectNotificationConstant.NOTIFICATION_CHANNEL_ID));
158         NotificationPriorityBo priority = (NotificationPriorityBo) generic.getObject(OleSelectNotificationConstant.NOTIFICATION_NAME, priorityName, NotificationPriorityBo.class);
159         notification.setPriority(priority);
160         NotificationContentTypeBo contentType = (NotificationContentTypeBo) generic.getObject(OleSelectNotificationConstant.NOTIFICATION_NAME, NotificationConstants.CONTENT_TYPES.SIMPLE_CONTENT_TYPE, NotificationContentTypeBo.class);
161         notification.setContentType(contentType);
162         Map producerMap = new HashMap();
163         producerMap.put(OleSelectNotificationConstant.NOTIFICATION_NAME, OleSelectNotificationConstant.NOTIFICATION_SYSTEM);
164         NotificationProducerBo producer = (NotificationProducerBo) generic.getObject(OleSelectNotificationConstant.NOTIFICATION_NAME, OleSelectNotificationConstant.NOTIFICATION_SYSTEM, NotificationProducerBo.class);
165         notification.setProducer(producer);
166         NotificationSenderBo ns = new NotificationSenderBo();
167         ns.setSenderName(senderNames.trim());
168         notification.addSender(ns);
169         notification.setChannel(channel);
170         for (String userRecipient : userRecipients) {
171             NotificationRecipientBo recipient = new NotificationRecipientBo();
172             recipient.setRecipientType(MemberType.PRINCIPAL.getCode());
173             String userRecipientId = SpringContext.getBean(IdentityHelperService.class).getPrincipalByPrincipalName(userRecipient).getPrincipalId();
174             StringBuffer buffer = new StringBuffer();
175             recipient.setRecipientId(userRecipient);
176             notification.addRecipient(recipient);
177         }
178         notification.setTitle(title);
179         notification.setDeliveryType(deliveryType);
180         try {
181             notification.setSendDateTimeValue(new Timestamp(Util.parseUIDateTime(sendDateTime).getTime()));
182         } catch (ParseException e) {
183             throw new RuntimeException(e);
184         }
185         notification
186                 .setContent(NotificationConstants.XML_MESSAGE_CONSTANTS.CONTENT_SIMPLE_OPEN
187                         + NotificationConstants.XML_MESSAGE_CONSTANTS.MESSAGE_OPEN
188                         + message
189                         + NotificationConstants.XML_MESSAGE_CONSTANTS.MESSAGE_CLOSE
190                         + NotificationConstants.XML_MESSAGE_CONSTANTS.CONTENT_CLOSE);
191 
192         return notification;
193     }
194 
195 
196 }