001/* 002 * Copyright 2012 The Kuali Foundation. 003 * 004 * Licensed under the Educational Community License, Version 1.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl1.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.kuali.ole.select.service.impl; 017 018import org.kuali.ole.select.OleSelectNotificationConstant; 019import org.kuali.ole.select.service.OleGenericService; 020import org.kuali.ole.select.service.OleNotifyService; 021import org.kuali.ole.sys.context.SpringContext; 022import org.kuali.rice.core.api.membership.MemberType; 023import org.kuali.rice.core.impl.persistence.dao.GenericDaoOjb; 024import org.kuali.rice.ken.bo.*; 025import org.kuali.rice.ken.document.kew.NotificationWorkflowDocument; 026import org.kuali.rice.ken.service.NotificationChannelService; 027import org.kuali.rice.ken.service.impl.NotificationContentTypeServiceImpl; 028import org.kuali.rice.ken.service.impl.NotificationMessageContentServiceImpl; 029import org.kuali.rice.ken.util.NotificationConstants; 030import org.kuali.rice.ken.util.Util; 031import org.kuali.rice.kew.api.WorkflowDocument; 032import org.kuali.rice.kew.api.document.DocumentContent; 033import org.kuali.rice.kew.api.document.DocumentContent.Builder; 034import org.kuali.rice.kew.api.exception.WorkflowException; 035import org.kuali.rice.kew.api.identity.PrincipalId; 036import org.kuali.rice.kew.identity.service.IdentityHelperService; 037import org.kuali.rice.kew.rule.GenericAttributeContent; 038import org.kuali.rice.kim.api.identity.Person; 039import org.kuali.rice.kim.api.identity.PersonService; 040import org.kuali.rice.krad.util.GlobalVariables; 041 042import java.lang.reflect.Proxy; 043import java.sql.Timestamp; 044import java.text.ParseException; 045import java.util.HashMap; 046import java.util.List; 047import java.util.Map; 048 049 050public class OleNotifyServiceImpl implements OleNotifyService { 051 052 private OleInvocationHandler oleInvocationHandler; 053 054 //private GenericDao genericDaoOjb; 055 056 057 /** 058 * Gets the genericDaoOjb attribute. 059 * @return Returns the genericDaoOjb. 060 */ 061 /*public GenericDao getGenericDaoOjb() { 062 return genericDaoOjb; 063 }*/ 064 065 066 /** 067 * Sets the genericDaoOjb attribute value. 068 * @param genericDaoOjb The genericDaoOjb to set. 069 */ 070 /*public void setGenericDaoOjb(GenericDao genericDaoOjb) { 071 this.genericDaoOjb = genericDaoOjb; 072 }*/ 073 074 075 /** 076 * Gets the oleInvocationHandler attribute. 077 * 078 * @return Returns the oleInvocationHandler. 079 */ 080 public OleInvocationHandler getOleInvocationHandler() { 081 return oleInvocationHandler; 082 } 083 084 085 /** 086 * Sets the oleInvocationHandler attribute value. 087 * 088 * @param oleInvocationHandler The oleInvocationHandler to set. 089 */ 090 public void setOleInvocationHandler(OleInvocationHandler oleInvocationHandler) { 091 this.oleInvocationHandler = oleInvocationHandler; 092 } 093 094 095 /** 096 * @see org.kuali.ole.select.service.OleNotifyService#notify(java.util.List, java.lang.String) 097 */ 098 public void notify(List<String> userRecipients, String message) throws WorkflowException { 099 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}