View Javadoc

1   package org.kuali.ole.deliver.controller;
2   
3   import org.apache.log4j.Logger;
4   import org.kuali.ole.OLEConstants;
5   import org.kuali.ole.deliver.bo.*;
6   import org.kuali.ole.deliver.form.OlePatronMergeForm;
7   import org.kuali.ole.ingest.FileUtil;
8   import org.kuali.rice.kim.impl.identity.name.EntityNameBo;
9   import org.kuali.rice.krad.service.BusinessObjectService;
10  import org.kuali.rice.krad.service.KRADServiceLocator;
11  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
12  import org.kuali.rice.krad.service.LookupService;
13  import org.kuali.rice.krad.util.GlobalVariables;
14  import org.kuali.rice.krad.util.KRADConstants;
15  import org.kuali.rice.krad.web.controller.UifControllerBase;
16  import org.kuali.rice.krad.web.form.UifFormBase;
17  import org.springframework.stereotype.Controller;
18  import org.springframework.validation.BindingResult;
19  import org.springframework.web.bind.annotation.ModelAttribute;
20  import org.springframework.web.bind.annotation.RequestMapping;
21  import org.springframework.web.servlet.ModelAndView;
22  
23  import javax.servlet.http.HttpServletRequest;
24  import javax.servlet.http.HttpServletResponse;
25  import java.io.File;
26  import java.net.URL;
27  import java.util.ArrayList;
28  import java.util.HashMap;
29  import java.util.List;
30  import java.util.Map;
31  
32  /**
33   * Created with IntelliJ IDEA.
34   * User: ?
35   * Date: 10/11/12
36   * Time: 12:54 PM
37   * To change this template use File | Settings | File Templates.
38   */
39  @Controller
40  @RequestMapping(value = "/patronMergeController")
41  public class OlePatronMergeController extends UifControllerBase {
42  
43      private static final Logger LOG = Logger.getLogger(OlePatronMergeController.class);
44      private BusinessObjectService businessObjectService;
45  
46      @Override
47      protected UifFormBase createInitialForm(HttpServletRequest request) {
48          return new OlePatronMergeForm();
49      }
50  
51      @Override
52      public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result, HttpServletRequest request, HttpServletResponse response) {
53          return super.start(form, result, request, response);    //To change body of overridden methods use File | Settings | File Templates.
54      }
55  
56      @RequestMapping(params = "methodToCall=searchPatronMerge")
57      public ModelAndView searchPatron(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
58                                       HttpServletRequest request, HttpServletResponse response) {
59          LOG.debug("inside searchPatronMerge method");
60          OlePatronMergeForm patronMergeForm = (OlePatronMergeForm) form;
61          List<OlePatronMergeDocument> patronMergeDocumentList = new ArrayList<OlePatronMergeDocument>();
62          String firstName = patronMergeForm.getFirstName();
63          String lastName = patronMergeForm.getLastName();
64          String patronType = patronMergeForm.getPatronType();
65          Map barMap = new HashMap();
66          List<OlePatronDocument> olePatronDocuments = new ArrayList<OlePatronDocument>();
67          boolean checkName = false;
68          boolean checkPatronType = false;
69          if (!firstName.isEmpty()) {
70              barMap.put(OLEConstants.OlePatron.PATRON_FIRST_NAME, firstName);
71          }
72          if (!lastName.isEmpty()) {
73              barMap.put(OLEConstants.OlePatron.PATRON_LAST_NAME, lastName);
74          }
75          if (!firstName.isEmpty() || !lastName.isEmpty()) {
76              List<EntityNameBo> entityNameBoList = (List<EntityNameBo>) getLookupService().findCollectionBySearchHelper(EntityNameBo.class, barMap, true);
77              if (entityNameBoList != null && entityNameBoList.size() > 0) {
78                  checkName = true;
79              }
80              List<EntityNameBo> entityNameBos = (List<EntityNameBo>) getLookupService().findCollectionBySearchHelper(EntityNameBo.class, barMap, true);
81              if (entityNameBos.size() > 0) {
82                  checkPatronType = true;
83              }
84              entityNameBos = entityNameBos != null ? entityNameBos : new ArrayList<EntityNameBo>();
85              for (EntityNameBo entityNameBo : entityNameBos) {
86                  barMap = new HashMap();
87                  barMap.put(OLEConstants.OlePatron.PATRON_ID, entityNameBo.getEntityId());
88                  OlePatronDocument olePatronDocument = getBusinessObjectService().findByPrimaryKey(OlePatronDocument.class, barMap);
89                  if (olePatronDocument != null) {
90                      olePatronDocuments.add(olePatronDocument);
91                  }
92              }
93          }
94          List<OlePatronDocument> newOlePatronDocuments = new ArrayList<OlePatronDocument>();
95          if (!patronType.isEmpty() && patronType != null && olePatronDocuments.size() > 0) {
96              for (OlePatronDocument olePatronDocument : olePatronDocuments) {
97                  if (olePatronDocument != null) {
98                      if (olePatronDocument.getBorrowerTypeName().equalsIgnoreCase(patronType)) {
99                          newOlePatronDocuments.add(olePatronDocument);
100                     }
101                 }
102             }
103             olePatronDocuments = newOlePatronDocuments;
104         } else if (!patronType.isEmpty()) {
105             Map barTypeMap = new HashMap();
106             barTypeMap.put(OLEConstants.BORROWER_TYPE_NAME, patronType);
107             List<OleBorrowerType> oleBorrowerTypeList = (List<OleBorrowerType>) getLookupService().findCollectionBySearchHelper(OleBorrowerType.class, barTypeMap, true);
108             if (oleBorrowerTypeList != null && oleBorrowerTypeList.size() > 0) {
109                 barTypeMap.put(OLEConstants.BORROWER_TYPE_NAME, oleBorrowerTypeList.get(0).getBorrowerTypeName());
110             }
111             if (barTypeMap.size() == 0) {
112                 barTypeMap.put(OLEConstants.BORROWER_TYPE_NAME, "");
113             }
114             List<OleBorrowerType> oleBorrowerTypes = (List<OleBorrowerType>) getBusinessObjectService().findMatching(OleBorrowerType.class, barTypeMap);
115             if (oleBorrowerTypes != null && oleBorrowerTypes.size() > 0) {
116                 barMap.put(OLEConstants.BORROWER_TYPE, oleBorrowerTypes.get(0).getBorrowerTypeId());
117             } else {
118                 barMap.put(OLEConstants.BORROWER_TYPE, "");
119             }
120             barMap.remove(OLEConstants.OlePatron.PATRON_FIRST_NAME);
121             barMap.remove(OLEConstants.OlePatron.PATRON_LAST_NAME);
122             if ((!checkName && !firstName.isEmpty()) || (!checkName && !lastName.isEmpty()) || !checkPatronType) {
123                 if (firstName.isEmpty() && lastName.isEmpty()) {
124                     olePatronDocuments = (List<OlePatronDocument>) getBusinessObjectService().findMatching(OlePatronDocument.class, barMap);
125                 } else {
126                     olePatronDocuments = new ArrayList<OlePatronDocument>();
127                 }
128             }
129         } else if (firstName.isEmpty() && lastName.isEmpty() && patronType.isEmpty()) {
130             olePatronDocuments = (List<OlePatronDocument>) getBusinessObjectService().findAll(OlePatronDocument.class);
131         }
132         olePatronDocuments = olePatronDocuments != null ? olePatronDocuments : new ArrayList<OlePatronDocument>();
133         for (OlePatronDocument olePatronDocument : olePatronDocuments) {
134             if (olePatronDocument != null) {
135                 if (olePatronDocument.isActiveIndicator()) {
136                     patronMergeDocumentList.add(new OlePatronMergeDocument(olePatronDocument));
137                 }
138             }
139         }
140         patronMergeForm.setPatronList(patronMergeDocumentList);
141         return getUIFModelAndView(patronMergeForm, OLEConstants.OlePatron.PATRON_MERGE_VIEW_PAGE);
142     }
143 
144     /**
145      * Gets the businessObjectService attribute.
146      *
147      * @return Returns the businessObjectService
148      */
149     private BusinessObjectService getBusinessObjectService() {
150         if (null == businessObjectService) {
151             businessObjectService = KRADServiceLocator.getBusinessObjectService();
152         }
153         return businessObjectService;
154     }
155 
156     @RequestMapping(params = "methodToCall=saveMerge")
157     public ModelAndView saveMerge(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
158                                   HttpServletRequest request, HttpServletResponse response) throws Exception {
159         LOG.debug("inside saveMerge method");
160         OlePatronMergeForm patronMergeForm = (OlePatronMergeForm) form;
161         URL resource = getClass().getResource(OLEConstants.OlePatron.PATRON_MERGE_CONFIG_XML_FILE);
162         File file = new File(resource.toURI());
163         String patronConfigXML = new FileUtil().readFile(file);
164         OlePatronConfigObjectGeneratorFromXML patronConfigObjectGeneratorFromXML = new OlePatronConfigObjectGeneratorFromXML();
165         OlePatronConfig patronConfig = patronConfigObjectGeneratorFromXML.buildKrmsFromFileContent(patronConfigXML);
166         List<OleConfigDocument> configDocuments = patronConfig.getDocuments();
167         Map<String, Object> patronConfigDocuments = OLEConstants.OlePatron.getPatronConfigObject();
168         List<OlePatronMergeDocument> patronMergeDocumentList = patronMergeForm.getPatronList();
169         List<OlePatronMergeDocument> newPatronMergeDocumentList = new ArrayList<OlePatronMergeDocument>();
170         Map barMap = new HashMap();
171         List<OleLoanDocument> duplicatePatronLoanDocuments = null;
172         List<OleDeliverRequestBo> duplicatePatronRequests = null;
173         List<OleTemporaryCirculationHistory> duplicateTemporaryCirculationRecords = null;
174         List<PatronBillPayment> duplicatePatronBillPayments = null;
175         OlePatronDocument survivorPatronDocument = null;
176         int survivor = 0;
177         for (OlePatronMergeDocument patronMergeDocument : patronMergeDocumentList) {
178             if (patronMergeDocument.isCheckSurvivor()) {
179                 survivorPatronDocument = patronMergeDocument.getOlePatronDocument();
180                 survivor++;
181             }
182             if (patronMergeDocument.isCheckSurvivor() && patronMergeDocument.isCheckDuplicatePatron()) {
183                 GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.GLOBAL_ERRORS, OLEConstants.OlePatron.PATRON_MERGE_DUPLICATE_PATRON_SELECT, OLEConstants.OlePatron.PATRON_MERGE_DUPLICATE_PATRON_SELECT);
184                 return getUIFModelAndView(patronMergeForm, OLEConstants.OlePatron.PATRON_MERGE_VIEW_PAGE);
185             }
186         }
187         if (survivor > 1 || survivor < 1) {
188             GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.GLOBAL_ERRORS, OLEConstants.OlePatron.PATRON_MERGE_SURVIVOR_SELECT, OLEConstants.OlePatron.PATRON_MERGE_SURVIVOR_SELECT);
189             return getUIFModelAndView(patronMergeForm, OLEConstants.OlePatron.PATRON_MERGE_VIEW_PAGE);
190         }
191         for (OlePatronMergeDocument patronMergeDocument : patronMergeDocumentList) {
192             if (patronMergeDocument.isCheckDuplicatePatron()) {
193                 OlePatronDocument olePatronDocument = patronMergeDocument.getOlePatronDocument();
194                 for (OleConfigDocument configDocument : configDocuments) {
195                     Object object = patronConfigDocuments.get(configDocument.getDocumentName());
196                     if (object != null && object.equals(OleLoanDocument.class)) {
197                         barMap.put(configDocument.getAttribute(), olePatronDocument.getOlePatronId());
198                         duplicatePatronLoanDocuments = (List<OleLoanDocument>) getBusinessObjectService().findMatching(OleLoanDocument.class, barMap);
199                         if (duplicatePatronLoanDocuments.size() > 0) {
200                             for (OleLoanDocument oleLoanDocument : duplicatePatronLoanDocuments) {
201                                 oleLoanDocument.setPatronId(survivorPatronDocument.getOlePatronId());
202                                 oleLoanDocument.setOlePatron(survivorPatronDocument);
203                                 getBusinessObjectService().save(oleLoanDocument);
204                             }
205                         }
206                         barMap.remove(configDocument.getAttribute());
207                     }
208                     if (object != null && object.equals(OleDeliverRequestBo.class)) {
209                         barMap.put(configDocument.getAttribute(), olePatronDocument.getOlePatronId());
210                         duplicatePatronRequests = (List<OleDeliverRequestBo>) getBusinessObjectService().findMatching(OleDeliverRequestBo.class, barMap);
211                         if (duplicatePatronRequests.size() > 0) {
212                             for (OleDeliverRequestBo oleDeliverRequestBo : duplicatePatronRequests) {
213                                 oleDeliverRequestBo.setBorrowerId(survivorPatronDocument.getOlePatronId());
214                                 oleDeliverRequestBo.setOlePatron(survivorPatronDocument);
215                                 getBusinessObjectService().save(oleDeliverRequestBo);
216                             }
217                         }
218                         barMap.remove(configDocument.getAttribute());
219                     }
220                     if (object != null && object.equals(OleTemporaryCirculationHistory.class)) {
221                         barMap.put(configDocument.getAttribute(), olePatronDocument.getOlePatronId());
222                         duplicateTemporaryCirculationRecords = (List<OleTemporaryCirculationHistory>) getBusinessObjectService().findMatching(OleTemporaryCirculationHistory.class, barMap);
223                         if (duplicateTemporaryCirculationRecords.size() > 0) {
224                             for (OleTemporaryCirculationHistory oleTemporaryCirculationHistory : duplicateTemporaryCirculationRecords) {
225                                 oleTemporaryCirculationHistory.setOlePatronId(survivorPatronDocument.getOlePatronId());
226                                 getBusinessObjectService().save(oleTemporaryCirculationHistory);
227                             }
228                         }
229                         barMap.remove(configDocument.getAttribute());
230                     }
231                     if (object != null && object.equals(PatronBillPayment.class)) {
232                         barMap.put(configDocument.getAttribute(), olePatronDocument.getOlePatronId());
233                         duplicatePatronBillPayments = (List<PatronBillPayment>) getBusinessObjectService().findMatching(PatronBillPayment.class, barMap);
234                         if (duplicatePatronBillPayments.size() > 0) {
235                             for (PatronBillPayment patronBillPayment : duplicatePatronBillPayments) {
236                                 patronBillPayment.setPatronId(survivorPatronDocument.getOlePatronId());
237                                 getBusinessObjectService().save(patronBillPayment);
238                             }
239                         }
240                         barMap.remove(configDocument.getAttribute());
241                     }
242                 }
243                 olePatronDocument.setActiveIndicator(false);
244                 getBusinessObjectService().linkAndSave(olePatronDocument);
245 
246             } else {
247                 newPatronMergeDocumentList.add(patronMergeDocument);
248             }
249         }
250         patronMergeForm.setPatronList(newPatronMergeDocumentList);
251         return getUIFModelAndView(patronMergeForm, OLEConstants.OlePatron.PATRON_MERGE_VIEW_PAGE);
252     }
253 
254 
255     private LookupService getLookupService() {
256         return KRADServiceLocatorWeb.getLookupService();
257     }
258 }