View Javadoc
1   package org.kuali.ole.alert.controller;
2   
3   import org.apache.commons.lang.ArrayUtils;
4   import org.apache.commons.lang.StringUtils;
5   import org.kuali.ole.OLEConstants;
6   import org.kuali.ole.alert.bo.AlertBo;
7   import org.kuali.ole.alert.document.OleTransactionalDocumentBase;
8   import org.kuali.ole.alert.service.impl.AlertHelperServiceImpl;
9   import org.kuali.ole.alert.service.impl.AlertServiceImpl;
10  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
11  import org.kuali.rice.kew.api.KewApiConstants;
12  import org.kuali.rice.kim.api.group.GroupService;
13  import org.kuali.rice.kim.api.role.Role;
14  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
15  import org.kuali.rice.krad.uif.UifParameters;
16  import org.kuali.rice.krad.util.GlobalVariables;
17  import org.kuali.rice.krad.web.controller.TransactionalDocumentControllerBase;
18  import org.kuali.rice.krad.web.form.DocumentFormBase;
19  import org.kuali.rice.krad.web.form.TransactionalDocumentFormBase;
20  import org.kuali.rice.krad.web.form.UifFormBase;
21  import org.springframework.validation.BindingResult;
22  import org.springframework.web.bind.annotation.ModelAttribute;
23  import org.springframework.web.bind.annotation.RequestMapping;
24  import org.springframework.web.bind.annotation.RequestMethod;
25  import org.springframework.web.servlet.ModelAndView;
26  
27  import javax.servlet.http.HttpServletRequest;
28  import javax.servlet.http.HttpServletResponse;
29  import java.sql.Date;
30  import java.util.ArrayList;
31  import java.util.Collection;
32  import java.util.HashMap;
33  import java.util.List;
34  
35  /**
36   * Created by maheswarang on 11/5/14.
37   */
38  public abstract class OleTransactionalDocumentControllerBase extends TransactionalDocumentControllerBase {
39  private AlertServiceImpl alertService = new AlertServiceImpl();
40      private GroupService groupService = KimApiServiceLocator.getGroupService();
41      private AlertHelperServiceImpl alertHelperService = new AlertHelperServiceImpl();
42      private org.kuali.rice.kim.api.role.RoleService roleService = KimApiServiceLocator.getRoleService();
43  
44  /*
45      @Override
46      @Autowired
47      @Qualifier("oleDocumentService")
48      public void setControllerService(ControllerService controllerService) {
49          super.setControllerService(controllerService);
50      }
51  */
52  
53  
54      @RequestMapping(params = "methodToCall=docHandler")
55      public ModelAndView docHandler(@ModelAttribute("KualiForm") DocumentFormBase form, BindingResult result,
56                                     HttpServletRequest request, HttpServletResponse response) throws Exception {
57          String command = form.getCommand();
58  
59          // in all of the following cases we want to load the document
60          if (ArrayUtils.contains(DOCUMENT_LOAD_COMMANDS, command) && form.getDocId() != null) {
61              loadDocument(form);
62              OleTransactionalDocumentBase oleTransactionalDocumentBase = (OleTransactionalDocumentBase)form.getDocument();
63              oleTransactionalDocumentBase.setAlertBoList(alertService.retrieveAlertList(form.getDocument().getDocumentNumber()));
64  
65          } else if (KewApiConstants.INITIATE_COMMAND.equals(command)) {
66              createDocument(form);
67          } else {
68              LOG.error("docHandler called with invalid parameters");
69              throw new IllegalArgumentException("docHandler called with invalid parameters");
70          }
71  
72          // TODO: authorization on document actions
73          // if (KEWConstants.SUPERUSER_COMMAND.equalsIgnoreCase(command)) {
74          // form.setSuppressAllButtons(true);
75          // }
76  
77          return getUIFModelAndView(form);
78      }
79  
80  
81      /**
82       * This method is to save alert
83       *
84       * @param uifForm
85       * @param result
86       * @param request
87       * @param response
88       * @return ModelAndView
89       */
90      @RequestMapping(params = "methodToCall=saveAlert")
91      public ModelAndView saveAlert(@ModelAttribute("KualiForm") UifFormBase uifForm, BindingResult result,
92                                       HttpServletRequest request, HttpServletResponse response) {
93          TransactionalDocumentFormBase transactionalDocumentFormBase = (TransactionalDocumentFormBase)uifForm;
94          OleTransactionalDocumentBase oleTransactionalDocumentBase = (OleTransactionalDocumentBase) transactionalDocumentFormBase.getDocument();
95          int index = Integer.parseInt(transactionalDocumentFormBase.getActionParamaterValue(UifParameters.SELECTED_LINE_INDEX));
96          oleTransactionalDocumentBase.getAlertBoList().get(index).setEditFlag(false);
97          AlertBo alertBo = oleTransactionalDocumentBase.getAlertBoList().get(index);
98          String status = null;
99          if(alertBo.getAlertDate()!=null){
100             Date alertDate = alertBo.getAlertDate();
101             if(alertDate.toString().equals(new Date(System.currentTimeMillis()).toString())){
102                 status = "Active";
103             }else{
104             int dateCompare= alertBo.getAlertDate().compareTo(new Date(System.currentTimeMillis()));
105             if(dateCompare>0){
106                 status = "Future";
107             }else if(dateCompare<0){
108                 status="Complete";
109             }
110             }
111         }
112         alertBo.setStatus(status);
113         return super.navigate(transactionalDocumentFormBase, result, request, response);
114     }
115 
116    /**
117      * This method is to edit alert
118      *
119      * @param uifForm
120      * @param result
121      * @param request
122      * @param response
123      * @return ModelAndView
124      */
125     @RequestMapping(params = "methodToCall=editAlert")
126     public ModelAndView editAlert(@ModelAttribute("KualiForm") UifFormBase uifForm, BindingResult result,
127                                      HttpServletRequest request, HttpServletResponse response) {
128         TransactionalDocumentFormBase transactionalDocumentFormBase = (TransactionalDocumentFormBase)uifForm;
129         OleTransactionalDocumentBase oleTransactionalDocumentBase = (OleTransactionalDocumentBase) transactionalDocumentFormBase.getDocument();
130         int index = Integer.parseInt(transactionalDocumentFormBase.getActionParamaterValue(UifParameters.SELECTED_LINE_INDEX));
131         AlertBo alertBo = oleTransactionalDocumentBase.getAlertBoList().get(index);
132         alertBo.setEditFlag(true);
133         alertBo.setAlertModifierId(GlobalVariables.getUserSession().getPrincipalId());
134         alertBo.setAlertModifiedDate(new Date(System.currentTimeMillis()));
135         alertBo.setAlertModifierName(alertService.getName(GlobalVariables.getUserSession().getPrincipalId()));
136         return super.navigate(transactionalDocumentFormBase, result, request, response);
137     }
138 
139 
140     /**
141      * This method is to delete alert
142      *
143      * @param uifForm
144      * @param result
145      * @param request
146      * @param response
147      * @return ModelAndView
148      */
149     @RequestMapping(params = "methodToCall=deleteAlert")
150     public ModelAndView deleteAlert(@ModelAttribute("KualiForm") UifFormBase uifForm, BindingResult result,
151                                        HttpServletRequest request, HttpServletResponse response) {
152         TransactionalDocumentFormBase transactionalDocumentFormBase = (TransactionalDocumentFormBase)uifForm;
153         OleTransactionalDocumentBase oleTransactionalDocumentBase = (OleTransactionalDocumentBase) transactionalDocumentFormBase.getDocument();
154         int index = Integer.parseInt(transactionalDocumentFormBase.getActionParamaterValue(UifParameters.SELECTED_LINE_INDEX));
155         if (oleTransactionalDocumentBase.getAlertBoList().size() > index) {
156             oleTransactionalDocumentBase.getAlertBoList().remove(index);
157         }
158         return super.navigate(uifForm, result, request, response);
159     }
160 
161 
162     /**
163      * This method is to delete alert
164      *
165      * @param uifForm
166      * @param result
167      * @param request
168      * @param response
169      * @return ModelAndView
170      */
171     @RequestMapping(params = "methodToCall=approveAlert")
172     public ModelAndView approveAlert(@ModelAttribute("KualiForm") UifFormBase uifForm, BindingResult result,
173                                     HttpServletRequest request, HttpServletResponse response) {
174         AlertBo alertBo = null;
175         TransactionalDocumentFormBase transactionalDocumentFormBase = (TransactionalDocumentFormBase)uifForm;
176         OleTransactionalDocumentBase oleTransactionalDocumentBase = (OleTransactionalDocumentBase) transactionalDocumentFormBase.getDocument();
177         int index = Integer.parseInt(transactionalDocumentFormBase.getActionParamaterValue(UifParameters.SELECTED_LINE_INDEX));
178         oleTransactionalDocumentBase.getAlertBoList().get(index).setAlertStatus(false);
179         oleTransactionalDocumentBase.getAlertBoList().get(index).setAlertModifierId(GlobalVariables.getUserSession().getPrincipalId());
180         if(oleTransactionalDocumentBase.getAlertBoList().get(index).isRepeatable()){
181         alertBo = alertHelperService.createNewAlertBo(oleTransactionalDocumentBase.getAlertBoList().get(index));
182             oleTransactionalDocumentBase.getAlertBoList().add(alertBo);
183         }
184         return super.navigate(uifForm, result, request, response);
185     }
186 
187 
188 
189     @RequestMapping(method = RequestMethod.POST, params = "methodToCall=addAlertLine")
190     public ModelAndView addAlertLine(@ModelAttribute("KualiForm") UifFormBase uifForm, BindingResult result,
191                                      HttpServletRequest request, HttpServletResponse response) {
192         TransactionalDocumentFormBase transactionalDocumentFormBase = (TransactionalDocumentFormBase)uifForm;
193         OleTransactionalDocumentBase oleTransactionalDocumentBase = (OleTransactionalDocumentBase) transactionalDocumentFormBase.getDocument();
194         oleTransactionalDocumentBase.getDocumentHeader().getWorkflowDocument().getDocument().getStatus().getCode();
195 /*        int index = Integer.parseInt(transactionalDocumentFormBase.getActionParamaterValue(UifParameters.SELECTED_LINE_INDEX));
196         oleTransactionalDocumentBase.getAlertBoList().get(index).setEditFlag(true);*/
197         ModelAndView modelAndView =  super.addLine(uifForm,result,request,response);
198         transactionalDocumentFormBase = (TransactionalDocumentFormBase)modelAndView.getModel().get("KualiForm");
199         List<String> principalIds = new ArrayList<String>();
200         AlertBo alertBo = oleTransactionalDocumentBase.getAlertBoList().get(0);
201         oleTransactionalDocumentBase.getAlertBoList().remove(0);
202         if(StringUtils.isBlank(alertBo.getReceivingGroupId()) && StringUtils.isBlank(alertBo.getReceivingUserId()) && StringUtils.isBlank(alertBo.getReceivingRoleId()) && StringUtils.isBlank(alertBo.getReceivingGroupId()) && StringUtils.isEmpty(alertBo.getReceivingUserName()) && StringUtils.isEmpty(alertBo.getReceivingRoleName()) && StringUtils.isEmpty(alertBo.getReceivingGroupName())){
203             GlobalVariables.getMessageMap().putErrorForSectionId("OLE-AlertSection", OLEConstants.SELECT_USER);
204             return modelAndView ;
205         } 
206         alertBo.setAlertCreateDate(new Date(System.currentTimeMillis()));
207         alertBo.setAlertInitiatorId(GlobalVariables.getUserSession().getPrincipalId());
208         alertBo.setAlertInitiatorName(alertService.getName(GlobalVariables.getUserSession().getPrincipalId()));
209        /* if(alertBo.getReceivingUserId()!=null){
210 //            principalIds.add(alertBo.getReceivingUserId());
211         alertBo.setReceivingUserName(alertService.getName(alertBo.getReceivingUserId()));
212         }
213         if(alertBo.getReceivingGroupId()!=null && !alertBo.getReceivingGroupId().trim().isEmpty()){
214         alertBo.setReceivingGroupName(alertService.getGroupName(alertBo.getReceivingGroupId()));
215         }
216 
217         if(alertBo.getReceivingGroupId()!=null){
218             List<String> memberIds = groupService.getMemberPrincipalIds(alertBo.getReceivingGroupId());
219             principalIds.addAll(memberIds);
220         }*/
221         String status = null;
222         if(alertBo.getAlertDate()!=null){
223             Date alertDate = alertBo.getAlertDate();
224             if(alertDate.toString().equals(new Date(System.currentTimeMillis()).toString())){
225                 status = "Active";
226             }else{
227                 int dateCompare= alertBo.getAlertDate().compareTo(new Date(System.currentTimeMillis()));
228                 if(dateCompare>0){
229                     status = "Future";
230                 }else if(dateCompare<0){
231                     status="Complete";
232                 }
233             }
234         }
235         alertBo.setStatus(status);
236         alertBo.setAlertStatus(true);
237         List<AlertBo> alerts = new ArrayList<AlertBo>();
238         if(StringUtils.isNotBlank(alertBo.getReceivingUserId()) && (alertBo.getReceivingUserName() == null || (alertBo.getReceivingUserName()!=null && alertBo.getReceivingUserName().trim().isEmpty()))){
239             alertBo.setReceivingUserName(alertService.getName(alertBo.getReceivingUserId()));
240         }
241         if(StringUtils.isBlank(alertBo.getReceivingUserId()) && (alertBo.getReceivingUserName() != null && !alertBo.getReceivingUserName().trim().isEmpty())){
242             alertBo.setReceivingUserId(alertService.getPersonId(alertBo.getReceivingUserName()));
243         }
244        if(StringUtils.isNotBlank(alertBo.getReceivingUserId())){
245         principalIds.add(alertBo.getReceivingUserId());
246        }
247         alerts.addAll(alertService.getAlertBo(alertBo,principalIds,false,false));
248         principalIds = new ArrayList<String>();
249 
250 
251 /*        if(alertBo.getReceivingUserId()!=null && !alertBo.getReceivingUserId().trim().isEmpty()){
252 //            principalIds.add(alertBo.getReceivingUserId());
253             alertBo.setReceivingUserName(alertService.getName(alertBo.getReceivingUserId()));
254         }*/
255         if(StringUtils.isNotBlank(alertBo.getReceivingGroupId()) && (alertBo.getReceivingGroupName() == null || (alertBo.getReceivingGroupName()!=null && alertBo.getReceivingGroupName().trim().isEmpty()))){
256             alertBo.setReceivingGroupName(alertService.getGroupName(alertBo.getReceivingGroupId()));
257         }
258         if(StringUtils.isBlank(alertBo.getReceivingGroupId()) && (alertBo.getReceivingGroupName() != null && !alertBo.getReceivingGroupName().trim().isEmpty())){
259             alertBo.setReceivingGroupId(alertService.getGroupId((alertBo.getReceivingGroupName())));
260         }
261 
262 /*        if(alertBo.getReceivingGroupId()!=null && !alertBo.getReceivingGroupId().trim().isEmpty()){
263             alertBo.setReceivingGroupName(alertService.getGroupName(alertBo.getReceivingGroupId()));
264         }*/
265 
266         if(StringUtils.isNotBlank(alertBo.getReceivingGroupId())){
267             List<String> memberIds = groupService.getMemberPrincipalIds(alertBo.getReceivingGroupId());
268             principalIds.addAll(memberIds);
269         }
270         alerts.addAll(alertService.getAlertBo(alertBo,principalIds,false,true));
271 
272 
273         principalIds = new ArrayList<String>();
274 
275         if(StringUtils.isNotBlank(alertBo.getReceivingRoleId()) && (alertBo.getReceivingRoleName() == null || (alertBo.getReceivingRoleName()!=null && alertBo.getReceivingRoleName().trim().isEmpty()))){
276             alertBo.setReceivingRoleName(alertService.getRoleName(alertBo.getReceivingRoleId()));
277         }
278         if(StringUtils.isBlank(alertBo.getReceivingRoleId()) && (alertBo.getReceivingRoleName() != null && !alertBo.getReceivingRoleName().trim().isEmpty())){
279             alertBo.setReceivingRoleId(alertService.getRoleId((alertBo.getReceivingRoleName())));
280         }
281 
282 
283         if(StringUtils.isNotBlank(alertBo.getReceivingRoleId())){
284             List<String> roleIds = new ArrayList<String>();
285             roleIds.add(alertBo.getReceivingRoleId());
286             Role role =  roleService.getRole(alertBo.getReceivingRoleId());
287             Collection collection  =  (Collection)roleService.getRoleMemberPrincipalIds(role.getNamespaceCode(),role.getName(),new HashMap<String, String>());
288             List<String> memberIds = new ArrayList<String>();
289             memberIds.addAll(collection);
290             principalIds.addAll(memberIds);
291         }
292 
293 
294         alerts.addAll(alertService.getAlertBo(alertBo,principalIds,true,false));
295 
296 
297         oleTransactionalDocumentBase.getAlertBoList().addAll(alerts);
298         if(StringUtils.isBlank(alertBo.getReceivingUserId()) && StringUtils.isNotBlank(alertBo.getReceivingGroupId())){
299             oleTransactionalDocumentBase.getAlertBoList().remove(0);
300         }
301         if(StringUtils.isNotBlank(alertBo.getReceivingUserId()) && StringUtils.isNotBlank(alertBo.getReceivingGroupId())){
302             alertBo.setReceivingGroupName(null);
303             alertBo.setReceivingGroupId(null);
304         }
305 
306         return modelAndView ;
307     }
308 
309 
310 
311     @Override
312     @RequestMapping(params = "methodToCall=save")
313     public ModelAndView save(@ModelAttribute("KualiForm") DocumentFormBase form, BindingResult result,
314     HttpServletRequest request, HttpServletResponse response) throws Exception {
315         return super.save(form,result,request,response);
316     }
317 
318 
319 }