View Javadoc
1   package org.kuali.ole.select.controller;
2   
3   import org.kuali.ole.OLEConstants;
4   import org.kuali.ole.select.OleSelectConstant;
5   import org.kuali.ole.select.document.OLEPurchaseOrderBatchDocument;
6   import org.kuali.ole.select.document.service.OLEPurchaseOrderBatchService;
7   import org.kuali.ole.select.form.OLEPurchaseOrderBatchForm;
8   import org.kuali.ole.sys.OLEKeyConstants;
9   import org.kuali.ole.sys.context.SpringContext;
10  import org.kuali.rice.core.api.config.property.ConfigContext;
11  import org.kuali.rice.krad.UserSession;
12  
13  import org.kuali.rice.krad.bo.Note;
14  import org.kuali.rice.krad.bo.PersistableBusinessObject;
15  import org.kuali.rice.krad.uif.UifConstants;
16  import org.kuali.rice.krad.uif.UifParameters;
17  import org.kuali.rice.krad.util.GlobalVariables;
18  import org.kuali.rice.krad.web.controller.TransactionalDocumentControllerBase;
19  import org.kuali.rice.krad.web.form.DocumentFormBase;
20  import org.kuali.rice.krad.web.form.UifFormBase;
21  import org.kuali.rice.ksb.messaging.threadpool.KSBThreadPool;
22  import org.kuali.rice.ksb.service.KSBServiceLocator;
23  import org.springframework.stereotype.Controller;
24  import org.springframework.validation.BindingResult;
25  import org.springframework.web.bind.annotation.ModelAttribute;
26  import org.springframework.web.bind.annotation.RequestMapping;
27  import org.springframework.web.multipart.MultipartFile;
28  import org.springframework.web.servlet.ModelAndView;
29  
30  import javax.servlet.http.HttpServletRequest;
31  import javax.servlet.http.HttpServletResponse;
32  import java.io.*;
33  import java.text.SimpleDateFormat;
34  import java.util.*;
35  
36  /**
37   * Created with IntelliJ IDEA.
38   * User: gopalp
39   * Date: 5/15/15
40   * Time: 1:21 PM
41   * To change this template use File | Settings | File Templates.
42   */
43  
44  @Controller
45  @RequestMapping(value = "/olePurchaseOrderBatchController")
46  public class OLEPurchaseOrderBatchController extends TransactionalDocumentControllerBase {
47  
48      OLEPurchaseOrderBatchService olePurchaseOrderBatchService = (OLEPurchaseOrderBatchService)SpringContext.getService("olePurchaseOrderBatchService");
49      private OLEPurchaseOrderBatchDocument purchaseOrderBatchDocument;
50      private String principalName;
51  
52      @Override
53      protected DocumentFormBase createInitialForm(HttpServletRequest request) {
54          OLEPurchaseOrderBatchForm olePurchaseOrderBatchForm = new OLEPurchaseOrderBatchForm();
55          return olePurchaseOrderBatchForm;
56      }
57  
58      @RequestMapping(params = "methodToCall=docHandler")
59      public ModelAndView docHandler(@ModelAttribute("KualiForm") DocumentFormBase form, BindingResult result,
60                                     HttpServletRequest request, HttpServletResponse response) throws Exception {
61          OLEPurchaseOrderBatchForm olePurchaseOrderBatchForm = (OLEPurchaseOrderBatchForm)form;
62          ModelAndView modelAndView = super.docHandler(olePurchaseOrderBatchForm,result,request,response);
63          return modelAndView;
64      }
65  
66      @RequestMapping(params = "methodToCall=upload")
67      public ModelAndView createFileForPOBA(@ModelAttribute("KualiForm") DocumentFormBase form, BindingResult result,
68                                HttpServletRequest request, HttpServletResponse response)   {
69          OLEPurchaseOrderBatchForm olePurchaseOrderBatchForm = (OLEPurchaseOrderBatchForm)form;
70          OLEPurchaseOrderBatchDocument olePurchaseOrderBatchDocument = (OLEPurchaseOrderBatchDocument)olePurchaseOrderBatchForm.getDocument();
71          if(olePurchaseOrderBatchDocument.getDocIdIngestFile() == null) {
72              GlobalVariables.getMessageMap().putError(OleSelectConstant.POBA_VIEW, OLEKeyConstants.INPUT_FILE_REQ);
73              return getUIFModelAndView(olePurchaseOrderBatchForm);
74          }
75          try {
76              olePurchaseOrderBatchService.downloadCSV(olePurchaseOrderBatchService.createFileForPOBA(olePurchaseOrderBatchDocument));
77              olePurchaseOrderBatchDocument.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.SUCCESSFULLEY_UPLOADED));
78  
79  
80          } catch(Exception e) {
81              GlobalVariables.getMessageMap().putError(OleSelectConstant.POBA_VIEW, OLEKeyConstants.INPUT_FILE_REQ);
82              return getUIFModelAndView(olePurchaseOrderBatchForm);
83          }
84          return getUIFModelAndView(olePurchaseOrderBatchForm);
85  
86      }
87  
88  
89      @RequestMapping(params = "methodToCall=route")
90      public ModelAndView route(@ModelAttribute("KualiForm") DocumentFormBase form, BindingResult result,
91                                     HttpServletRequest request, HttpServletResponse response)   {
92          OLEPurchaseOrderBatchForm olePurchaseOrderBatchForm = (OLEPurchaseOrderBatchForm) form;
93          OLEPurchaseOrderBatchDocument olePurchaseOrderBatchDocument = (OLEPurchaseOrderBatchDocument) olePurchaseOrderBatchForm.getDocument();
94          olePurchaseOrderBatchDocument.setMessage("");
95          principalName = GlobalVariables.getUserSession().getPrincipalName();
96          final UserSession userSession = new UserSession(principalName);
97  
98          olePurchaseOrderBatchDocument.getDocumentHeader().setDocumentDescription(OLEConstants.OLEPurchaseOrderBulkAmendment.DOC_DESC);
99          if(GlobalVariables.getUserSession().getPrincipalName() != null) {
100             olePurchaseOrderBatchDocument.setPrincipalName(GlobalVariables.getUserSession().getPrincipalName());
101         }
102         if (olePurchaseOrderBatchDocument.getIngestedFile() == null) {
103             GlobalVariables.getMessageMap().putError(OleSelectConstant.POBA_VIEW, OLEKeyConstants.INPUT_FILE_REQ);
104             return getUIFModelAndView(olePurchaseOrderBatchForm);
105         }
106         if (olePurchaseOrderBatchDocument.getType() == null) {
107             GlobalVariables.getMessageMap().putError(OleSelectConstant.POBA_VIEW, OLEKeyConstants.SELECT_PROCESS);
108             return getUIFModelAndView(olePurchaseOrderBatchForm);
109         }
110         if (olePurchaseOrderBatchDocument.getType().equals(OLEConstants.OLEPurchaseOrderBulkAmendment.SPEC_TIME) && (olePurchaseOrderBatchDocument.getBatchStartDate() == null || olePurchaseOrderBatchDocument.getTime() == null)) {
111             GlobalVariables.getMessageMap().putError(OleSelectConstant.POBA_VIEW, OLEKeyConstants.ERROR_DATE);
112             return getUIFModelAndView(olePurchaseOrderBatchForm);
113         }
114         if(olePurchaseOrderBatchDocument.getType().equals(OLEConstants.OLEPurchaseOrderBulkAmendment.SPEC_TIME) && !(olePurchaseOrderBatchDocument.getTime().length() == 8)) {
115             GlobalVariables.getMessageMap().putError(OleSelectConstant.POBA_VIEW, OLEKeyConstants.ERROR_TIME_LENGTH);
116             return getUIFModelAndView(olePurchaseOrderBatchForm);
117         }
118         olePurchaseOrderBatchDocument.setUploadFileName(olePurchaseOrderBatchDocument.getDocumentNumber()+"_"+olePurchaseOrderBatchDocument.getIngestedFile().getOriginalFilename());
119         if (olePurchaseOrderBatchDocument.getType().equals(OLEConstants.OLEPurchaseOrderBulkAmendment.IMMEDIATE)) {
120             KSBThreadPool threadPool = KSBServiceLocator.getThreadPool();
121             try {
122                 File file = new File(System.getProperty("java.io.tmpdir") + "/" + olePurchaseOrderBatchDocument.getIngestedFile().getOriginalFilename());
123                 olePurchaseOrderBatchDocument.getIngestedFile().transferTo(file);
124                 threadPool.submit(new POBACallable(olePurchaseOrderBatchDocument, userSession, file));
125             } catch (IOException e) {
126                 e.printStackTrace();
127             }
128         } else {
129             try {
130                 String jobId = olePurchaseOrderBatchDocument.getDocumentNumber();
131                 String fileDirectory = ConfigContext.getCurrentContextConfig().getProperty(org.kuali.ole.OLEConstants.STAGING_DIRECTORY)+"/"+olePurchaseOrderBatchDocument.getUploadFileName();
132                 MultipartFile multipartFile = olePurchaseOrderBatchDocument.getIngestedFile();
133                 multipartFile.transferTo(new File(fileDirectory));
134                 String date = new SimpleDateFormat(OLEConstants.OLEPurchaseOrderBulkAmendment.FORMAT).format(olePurchaseOrderBatchDocument.getBatchStartDate());
135                 date += " " + olePurchaseOrderBatchDocument.getTime();
136                 purchaseOrderBatchDocument = olePurchaseOrderBatchDocument;
137                 Timer t = new Timer();
138                 t.schedule(new TimerTask() {
139                     public void run() {
140                         GlobalVariables.setUserSession(new UserSession(principalName));
141                         getOlePurchaseOrderBatchService().readFile(purchaseOrderBatchDocument, userSession, null);
142                     }
143                 }, new SimpleDateFormat(OLEConstants.OLEPurchaseOrderBulkAmendment.TIMESTAMP_FORMAT).parse(date));
144             } catch (Exception e) {
145                 e.printStackTrace();
146             }
147         }
148         return super.route(olePurchaseOrderBatchForm,result,request,response);
149     }
150 
151     @RequestMapping(params = "methodToCall=blanketApprove")
152     public ModelAndView blanketApprove(@ModelAttribute("KualiForm") DocumentFormBase form, BindingResult result,
153                                        HttpServletRequest request, HttpServletResponse response) throws Exception {
154 
155         OLEPurchaseOrderBatchForm olePurchaseOrderBatchForm = (OLEPurchaseOrderBatchForm) form;
156         OLEPurchaseOrderBatchDocument olePurchaseOrderBatchDocument = (OLEPurchaseOrderBatchDocument) olePurchaseOrderBatchForm.getDocument();
157         olePurchaseOrderBatchDocument.setMessage("");
158         principalName = GlobalVariables.getUserSession().getPrincipalName();
159         final UserSession userSession = new UserSession(principalName);
160 
161         olePurchaseOrderBatchDocument.getDocumentHeader().setDocumentDescription(OLEConstants.OLEPurchaseOrderBulkAmendment.DOC_DESC);
162         if(GlobalVariables.getUserSession().getPrincipalName() != null) {
163             olePurchaseOrderBatchDocument.setPrincipalName(GlobalVariables.getUserSession().getPrincipalName());
164         }
165         if (olePurchaseOrderBatchDocument.getIngestedFile() == null) {
166             GlobalVariables.getMessageMap().putError(OleSelectConstant.POBA_VIEW, OLEKeyConstants.INPUT_FILE_REQ);
167             return getUIFModelAndView(olePurchaseOrderBatchForm);
168         }
169         if (olePurchaseOrderBatchDocument.getType() == null) {
170             GlobalVariables.getMessageMap().putError(OleSelectConstant.POBA_VIEW, OLEKeyConstants.SELECT_PROCESS);
171             return getUIFModelAndView(olePurchaseOrderBatchForm);
172         }
173         if (olePurchaseOrderBatchDocument.getType().equals(OLEConstants.OLEPurchaseOrderBulkAmendment.SPEC_TIME) && (olePurchaseOrderBatchDocument.getBatchStartDate() == null || olePurchaseOrderBatchDocument.getTime() == null)) {
174             GlobalVariables.getMessageMap().putError(OleSelectConstant.POBA_VIEW, OLEKeyConstants.ERROR_DATE);
175             return getUIFModelAndView(olePurchaseOrderBatchForm);
176         }
177         if(olePurchaseOrderBatchDocument.getType().equals(OLEConstants.OLEPurchaseOrderBulkAmendment.SPEC_TIME) && !(olePurchaseOrderBatchDocument.getTime().length() == 8)) {
178             GlobalVariables.getMessageMap().putError(OleSelectConstant.POBA_VIEW, OLEKeyConstants.ERROR_TIME_LENGTH);
179             return getUIFModelAndView(olePurchaseOrderBatchForm);
180         }
181         olePurchaseOrderBatchDocument.setUploadFileName(olePurchaseOrderBatchDocument.getDocumentNumber()+"_"+olePurchaseOrderBatchDocument.getIngestedFile().getOriginalFilename());
182         if (olePurchaseOrderBatchDocument.getType().equals(OLEConstants.OLEPurchaseOrderBulkAmendment.IMMEDIATE)) {
183             getOlePurchaseOrderBatchService().readFile(olePurchaseOrderBatchDocument, userSession, null);
184         } else {
185             try {
186                 String jobId = olePurchaseOrderBatchDocument.getDocumentNumber();
187                 String fileDirectory = ConfigContext.getCurrentContextConfig().getProperty(org.kuali.ole.OLEConstants.STAGING_DIRECTORY)+"/"+olePurchaseOrderBatchDocument.getUploadFileName();
188                 MultipartFile multipartFile = olePurchaseOrderBatchDocument.getIngestedFile();
189                 multipartFile.transferTo(new File(fileDirectory));
190                 String date = new SimpleDateFormat(OLEConstants.OLEPurchaseOrderBulkAmendment.FORMAT).format(olePurchaseOrderBatchDocument.getBatchStartDate());
191                 date += " " + olePurchaseOrderBatchDocument.getTime();
192                 purchaseOrderBatchDocument = olePurchaseOrderBatchDocument;
193                 Timer t = new Timer();
194                 t.schedule(new TimerTask() {
195                     public void run() {
196                         GlobalVariables.setUserSession(new UserSession(principalName));
197                         getOlePurchaseOrderBatchService().readFile(purchaseOrderBatchDocument, userSession, null);
198                     }
199                 }, new SimpleDateFormat(OLEConstants.OLEPurchaseOrderBulkAmendment.TIMESTAMP_FORMAT).parse(date));
200             } catch (Exception e) {
201                 e.printStackTrace();
202             }
203         }
204         return super.blanketApprove(olePurchaseOrderBatchForm, result, request, response);
205     }
206 
207 
208     @RequestMapping(params = "methodToCall=approveDocument")
209     public ModelAndView approveDocument(@ModelAttribute("KualiForm") DocumentFormBase form, BindingResult result,
210                                        HttpServletRequest request, HttpServletResponse response) throws Exception {
211 
212         principalName = GlobalVariables.getUserSession().getPrincipalName();
213         final UserSession userSession = new UserSession(principalName);
214 
215         OLEPurchaseOrderBatchForm olePurchaseOrderBatchForm = (OLEPurchaseOrderBatchForm)form;
216         OLEPurchaseOrderBatchDocument olePurchaseOrderBatchDocument = (OLEPurchaseOrderBatchDocument)olePurchaseOrderBatchForm.getDocument();
217         olePurchaseOrderBatchDocument.getDocumentHeader().setDocumentDescription(OLEConstants.OLEPurchaseOrderBulkAmendment.DOC_DESC);
218         GlobalVariables.setUserSession(new UserSession(olePurchaseOrderBatchDocument.getPrincipalName()));
219         if(olePurchaseOrderBatchDocument.getType().equals(OLEConstants.OLEPurchaseOrderBulkAmendment.IMMEDIATE)) {
220             getOlePurchaseOrderBatchService().readFile(olePurchaseOrderBatchDocument, userSession, null);
221         } else {
222             try {
223                 String date = new SimpleDateFormat(OLEConstants.OLEPurchaseOrderBulkAmendment.FORMAT).format(olePurchaseOrderBatchDocument.getBatchStartDate());
224                 date += " "+olePurchaseOrderBatchDocument.getTime();
225                 purchaseOrderBatchDocument =  olePurchaseOrderBatchDocument;
226                 Timer t=new Timer();
227                 t.schedule(new TimerTask() {
228                     public void run() {
229                         GlobalVariables.setUserSession(new UserSession(purchaseOrderBatchDocument.getPrincipalName()));
230                         getOlePurchaseOrderBatchService().readFile(purchaseOrderBatchDocument, userSession, null);
231                     }
232                 }, new SimpleDateFormat(OLEConstants.OLEPurchaseOrderBulkAmendment.TIMESTAMP_FORMAT).parse(date));
233             } catch (Exception e) {
234                 e.printStackTrace();
235             }
236         }
237 
238         super.approve(olePurchaseOrderBatchForm, result, request, response);
239         return closeDocument(olePurchaseOrderBatchForm, result, request, response);
240 
241     }
242 
243     @RequestMapping(params = "methodToCall=" + "closeDocument")
244     public ModelAndView closeDocument(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
245                                       HttpServletRequest request, HttpServletResponse response) throws Exception {
246 
247         String baseUrl = ConfigContext.getCurrentContextConfig().getProperty(org.kuali.ole.OLEPropertyConstants.OLE_URL_BASE);
248         String url = baseUrl + "/portal.do";
249         Properties props = new Properties();
250         props.put(UifParameters.METHOD_TO_CALL, UifConstants.MethodToCallNames.REFRESH);
251         if (org.apache.commons.lang.StringUtils.isNotBlank(form.getReturnFormKey())) {
252             props.put(UifParameters.FORM_KEY, form.getReturnFormKey());
253         }
254         GlobalVariables.getUifFormManager().removeSessionForm(form);
255         return performRedirect(form, url, props);
256     }
257 
258     public OLEPurchaseOrderBatchService getOlePurchaseOrderBatchService() {
259         if(olePurchaseOrderBatchService == null) {
260             olePurchaseOrderBatchService = (OLEPurchaseOrderBatchService)SpringContext.getService("olePurchaseOrderBatchService");
261         }
262         return olePurchaseOrderBatchService;
263     }
264 
265     @RequestMapping(params = "methodToCall=disapprove")
266     public ModelAndView disapprove(@ModelAttribute("KualiForm") DocumentFormBase form, BindingResult result,
267                                    HttpServletRequest request, HttpServletResponse response) throws Exception {
268         OLEPurchaseOrderBatchForm olePurchaseOrderBatchForm = (OLEPurchaseOrderBatchForm) form;
269         OLEPurchaseOrderBatchDocument olePurchaseOrderBatchDocument = (OLEPurchaseOrderBatchDocument) olePurchaseOrderBatchForm.getDocument();
270 
271         Note noteObj = getDocumentService().createNoteFromDocument(olePurchaseOrderBatchDocument, "Dispproved at Exceptione By : " + GlobalVariables.getUserSession().getPerson().getName());
272         PersistableBusinessObject noteParent = olePurchaseOrderBatchDocument.getNoteTarget();
273         List<Note> noteList = getNoteService().getByRemoteObjectId(noteParent.getObjectId());
274         noteList.add(noteObj);
275         getNoteService().saveNoteList(noteList);
276         getNoteService().save(noteObj);
277         olePurchaseOrderBatchDocument.setNotes(noteList);
278         getDocumentService().saveDocument(olePurchaseOrderBatchDocument);
279 
280         performWorkflowAction(olePurchaseOrderBatchForm, UifConstants.WorkflowAction.DISAPPROVE, true);
281         return returnToPrevious(olePurchaseOrderBatchForm);
282 
283     }
284 
285 }