View Javadoc
1   package org.kuali.ole.deliver.maintenance;
2   
3   import org.apache.commons.collections.CollectionUtils;
4   import org.apache.ojb.broker.metadata.ClassNotPersistenceCapableException;
5   import org.kuali.ole.OLEConstants;
6   import org.kuali.ole.OLEPropertyConstants;
7   import org.kuali.ole.deliver.bo.*;
8   import org.kuali.ole.deliver.processor.LoanProcessor;
9   import org.kuali.ole.service.OlePatronService;
10  import org.kuali.ole.service.OlePatronServiceImpl;
11  import org.kuali.rice.core.api.config.property.ConfigContext;
12  import org.kuali.rice.kim.impl.identity.address.EntityAddressBo;
13  import org.kuali.rice.kim.impl.identity.affiliation.EntityAffiliationBo;
14  import org.kuali.rice.kim.impl.identity.email.EntityEmailBo;
15  import org.kuali.rice.kim.impl.identity.employment.EntityEmploymentBo;
16  import org.kuali.rice.kim.impl.identity.entity.EntityBo;
17  import org.kuali.rice.kim.impl.identity.name.EntityNameBo;
18  import org.kuali.rice.kim.impl.identity.name.EntityNameTypeBo;
19  import org.kuali.rice.kim.impl.identity.phone.EntityPhoneBo;
20  import org.kuali.rice.krad.maintenance.MaintainableImpl;
21  import org.kuali.rice.krad.maintenance.MaintenanceDocument;
22  import org.kuali.rice.krad.service.KRADServiceLocator;
23  
24  import java.util.*;
25  
26  /**
27   * OlePatronMaintenanceImpl performs save ,edit,copy  and create new operation for OlePatron.
28   */
29  public class OlePatronMaintenanceImpl extends MaintainableImpl {
30      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OlePatronMaintenanceImpl.class);
31      OlePatronService olePatronService = new OlePatronServiceImpl();
32  
33      String baseUrl = ConfigContext.getCurrentContextConfig().getProperty(OLEPropertyConstants.OLE_URL_BASE);
34      /**
35       * This method populate the patron object used for edit and copy
36       *
37       * @param document
38       * @param dataObjectKeys
39       * @return olePatron(Object)
40       */
41      @Override
42      public Object retrieveObjectForEditOrCopy(MaintenanceDocument document, Map<String, String> dataObjectKeys) {
43          LOG.debug("Inside retrieveObjectForEditOrCopy()");
44          Object dataObject = null;
45          OlePatronDocument olePatron = new OlePatronDocument();
46          OleEntityAddressBo entityAddressBo = null;
47          OleEntityPhoneBo entityPhoneBo = null;
48          OleEntityEmailBo entityEmailBo = null;
49          List<OleAddressBo> oleAddressBos = new ArrayList<>();
50          List<OlePhoneBo> olePhoneBos = new ArrayList<>();
51          List<OleEmailBo> oleEmailBos = new ArrayList<>();
52          List<OleEntityAddressBo> oleEntityAddressList = new ArrayList<OleEntityAddressBo>();
53          List<OleEntityPhoneBo> oleEntityPhoneBoList = new ArrayList<>();
54          List<OleEntityEmailBo> oleEntityEmailBoList = new ArrayList<>();
55          try {
56              dataObject = getLookupService().findObjectBySearch(getDataObjectClass(), dataObjectKeys);
57              olePatron = (OlePatronDocument) dataObject;
58              EntityBo entity = olePatron.getEntity();
59              if (entity.getNames().size() > 0) {
60                  EntityNameBo name = entity.getNames().get(0);
61                  olePatron.setName(name);
62              }
63              if (!entity.getEntityTypeContactInfos().isEmpty()) {
64                  List<EntityAddressBo> entityAddressList = entity.getEntityTypeContactInfos().get(0).getAddresses();
65                  for (EntityAddressBo entityAdd : entityAddressList) {
66                      entityAddressBo = new OleEntityAddressBo();
67                      Map addMap = new HashMap();
68                      addMap.put(OLEConstants.OlePatron.ENTITY_BO_ID, entityAdd.getId());
69                      addMap.put(OLEConstants.OlePatron.PATRON_ID, olePatron.getId());
70                      oleAddressBos = (List<OleAddressBo>) getBusinessObjectService().findMatching(OleAddressBo.class, addMap);
71                      if(CollectionUtils.isNotEmpty(oleAddressBos)){
72                          entityAddressBo.setOleAddressBo(oleAddressBos.get(0));
73                      }
74                      entityAddressBo.setEntityAddressBo(entityAdd);
75                      oleEntityAddressList.add(entityAddressBo);
76                  }
77                  olePatron.setOleEntityAddressBo(oleEntityAddressList);
78                  List<EntityPhoneBo> entityPhoneBoList = entity.getEntityTypeContactInfos().get(0).getPhoneNumbers();
79                  for(EntityPhoneBo entityPhone : entityPhoneBoList) {
80                      entityPhoneBo = new OleEntityPhoneBo();
81                      Map map = new HashMap();
82                      map.put(OLEConstants.OlePatron.ENTITY_BO_ID, entityPhone.getId());
83                      map.put(OLEConstants.OlePatron.PATRON_ID, olePatron.getId());
84                      olePhoneBos = (List<OlePhoneBo>) getBusinessObjectService().findMatching(OlePhoneBo.class, map);
85                      if(CollectionUtils.isNotEmpty(olePhoneBos)) {
86                          entityPhoneBo.setOlePhoneBo(olePhoneBos.get(0));
87                      }
88                      entityPhoneBo.setEntityPhoneBo(entityPhone);
89                      oleEntityPhoneBoList.add(entityPhoneBo);
90                  }
91                  olePatron.setOleEntityPhoneBo(oleEntityPhoneBoList);
92                  List<EntityEmailBo> entityEmailBoList = entity.getEntityTypeContactInfos().get(0).getEmailAddresses();
93                  for(EntityEmailBo entityEmail : entityEmailBoList) {
94                      entityEmailBo = new OleEntityEmailBo();
95                      Map map = new HashMap();
96                      map.put(OLEConstants.OlePatron.ENTITY_BO_ID, entityEmail.getId());
97                      map.put(OLEConstants.OlePatron.PATRON_ID, olePatron.getId());
98                      oleEmailBos = (List<OleEmailBo>) getBusinessObjectService().findMatching(OleEmailBo.class, map);
99                      if(CollectionUtils.isNotEmpty(oleEmailBos)) {
100                         entityEmailBo.setOleEmailBo(oleEmailBos.get(0));
101                     }
102                     entityEmailBo.setEntityEmailBo(entityEmail);
103                     oleEntityEmailBoList.add(entityEmailBo);
104                 }
105                 olePatron.setOleEntityEmailBo(oleEntityEmailBoList);
106             }
107             
108             olePatron.setEmployments(entity.getEmploymentInformation());
109             List<OlePatronAffiliation> patronAffiliations = new ArrayList<OlePatronAffiliation>();
110             olePatron.setPatronAffiliations(getPatronAffiliationFromEntity(entity.getAffiliations(), entity.getEmploymentInformation()));
111 
112             if (olePatron.getOlePatronId() != null) {
113                 LoanProcessor loanProcessor = new LoanProcessor();
114                /* OleDeliverRequestDocumentHelperServiceImpl requestService = new OleDeliverRequestDocumentHelperServiceImpl();
115                 List<OleDeliverRequestBo> oleDeliverRequestBoList = olePatron.getOleDeliverRequestBos();
116                 if (oleDeliverRequestBoList.size() > 0) {
117                     for (int i = 0; i < oleDeliverRequestBoList.size(); i++) {
118                         OleItemSearch oleItemSearch = requestService.getItemDetailsForPatron(oleDeliverRequestBoList.get(i).getItemUuid());
119                         if (oleItemSearch != null && oleItemSearch.getItemBarCode() != null) {
120                             oleDeliverRequestBoList.get(i).setTitle(oleItemSearch.getTitle());
121                             oleDeliverRequestBoList.get(i).setCallNumber(oleItemSearch.getCallNumber());
122                         }
123                     }
124                 }
125                 try {
126                     olePatron.setOleLoanDocuments(loanProcessor.getPatronLoanedItemBySolr(olePatron.getOlePatronId()));
127                     olePatron.setOleTemporaryCirculationHistoryRecords(loanProcessor.getPatronTemporaryCirculationHistoryRecords(olePatron.getOlePatronId()));
128                     olePatron.setOleDeliverRequestBos(loanProcessor.getPatronRequestRecords(olePatron.getOlePatronId()));
129                 } catch (Exception e) {
130                     LOG.error("Exception", e);
131                 }*/
132                 if (olePatron.getOleLoanDocuments().size() > 0) {
133                     olePatron.setLoanFlag(true);
134                 }
135                 if (olePatron.getOleTemporaryCirculationHistoryRecords().size() > 0) {
136                     olePatron.setTempCircHistoryFlag(true);
137                 }
138                 if (olePatron.getOleDeliverRequestBos().size() > 0) {
139                     olePatron.setRequestFlag(true);
140                 }
141                 OlePatronDocument olePatronDocument;
142                 OleProxyPatronDocument proxyPatronDocument = null;
143                 List<OleProxyPatronDocument> proxyPatronDocuments = new ArrayList<OleProxyPatronDocument>();
144                 Map proxyMap = new HashMap();
145                 proxyMap.put(OLEConstants.OlePatron.PROXY_PATRON_ID, olePatron.getOlePatronId());
146                 List<OleProxyPatronDocument> oleProxyPatronDocumentList = (List<OleProxyPatronDocument>) getBusinessObjectService().findMatching(OleProxyPatronDocument.class, proxyMap);
147                 if (oleProxyPatronDocumentList.size() > 0) {
148                     for (OleProxyPatronDocument oleProxyPatronDocument : oleProxyPatronDocumentList) {
149                         proxyMap.remove(OLEConstants.OlePatron.PROXY_PATRON_ID);
150                         proxyMap.put(OLEConstants.OlePatron.PATRON_ID, oleProxyPatronDocument.getOlePatronId());
151                         List<OlePatronDocument> olePatronDocumentList = (List<OlePatronDocument>) getBusinessObjectService().findMatching(OlePatronDocument.class, proxyMap);
152                         if (olePatronDocumentList.size() > 0) {
153                             olePatronDocument = olePatronDocumentList.get(0);
154                             proxyPatronDocument = new OleProxyPatronDocument();
155                            // if (olePatronDocument.isActiveIndicator()) {
156                                 proxyPatronDocument.setOlePatronId(olePatronDocument.getOlePatronId());
157                                 proxyPatronDocument.setProxyForPatronFirstName(olePatronDocument.getEntity().getNames().get(0).getFirstName());
158                                 proxyPatronDocument.setProxyForPatronLastName(olePatronDocument.getEntity().getNames().get(0).getLastName());
159                                 proxyPatronDocument.setProxyPatronActivationDate(oleProxyPatronDocument.getProxyPatronActivationDate());
160                                 proxyPatronDocument.setProxyPatronExpirationDate(oleProxyPatronDocument.getProxyPatronExpirationDate());
161 
162                          //   }
163                             proxyPatronDocuments.add(proxyPatronDocument);
164                         }
165                     }
166                     olePatron.setOleProxyPatronDocumentList(proxyPatronDocuments);
167                 }
168 
169                 if(olePatron.getOleLoanDocuments()!=null){
170                     olePatron.setLoanCount(olePatron.getOleLoanDocuments().size());
171                 }
172                 if(olePatron.getOleTemporaryCirculationHistoryRecords()!=null){
173                     olePatron.setTempCirculationHistoryCount(olePatron.getOleTemporaryCirculationHistoryRecords().size());
174                 }
175                 if(olePatron.getOleDeliverRequestBos()!=null){
176                     olePatron.setRequestedItemRecordsCount(olePatron.getOleDeliverRequestBos().size());
177                 }
178                 if(olePatron.getLostBarcodes()!=null){
179                   Collections.sort(olePatron.getLostBarcodes(), new Comparator<OlePatronLostBarcode>() {
180                       @Override
181                       public int compare(OlePatronLostBarcode o1, OlePatronLostBarcode o2) {
182 
183                           return  Integer.parseInt(o2.getId()) - Integer.parseInt(o1.getId());
184                       }
185                   });
186 
187                 }
188             }
189         } catch (ClassNotPersistenceCapableException ex) {
190             if (!document.getOldMaintainableObject().isExternalBusinessObject()) {
191                 LOG.error("Data Object Class: "
192                         + getDataObjectClass()
193                         + " is not persistable and is not externalizable - configuration error", ex);
194                 throw new RuntimeException("Data Object Class: "
195                         + getDataObjectClass()
196                         + " is not persistable and is not externalizable - configuration error");
197             }
198         }
199         return olePatron;
200     }
201 
202     /**
203      * This method will set the default value for the name object and also set the description when a new patron document is created
204      *
205      * @param document
206      * @param requestParameters
207      */
208     @Override
209     public void processAfterNew(MaintenanceDocument document,
210                                 Map<String, String[]> requestParameters) {
211         LOG.debug("Inside processAfterNew()");
212         EntityNameTypeBo nameTypeBo = (EntityNameTypeBo) KRADServiceLocator.getBusinessObjectService().findBySinglePrimaryKey(EntityNameTypeBo.class, OLEConstants.OlePatron.ENTITY_NM_TYP_CD);
213         OlePatronDocument patronDocument = (OlePatronDocument) document.getNewMaintainableObject().getDataObject();
214         patronDocument.setActiveIndicator(true);
215         patronDocument.getName().setNameCode(OLEConstants.OlePatron.ENTITY_NM_TYP_CD);
216         patronDocument.getName().setActive(true);
217         patronDocument.getName().setDefaultValue(true);
218         patronDocument.getName().setNameType(nameTypeBo);
219         document.getNewMaintainableObject().setDataObject(patronDocument);
220         super.processAfterNew(document, requestParameters);
221         document.getDocumentHeader().setDocumentDescription("New Patron Document");
222 
223     }
224 
225     /**
226      * This method will set the description when copy is selected
227      *
228      * @param document
229      * @param requestParameters
230      */
231     @Override
232     public void processAfterCopy(MaintenanceDocument document, Map<String, String[]> requestParameters) {
233         LOG.debug("Inside processAfterCopy()");
234         super.processAfterCopy(document, requestParameters);
235         document.getDocumentHeader().setDocumentDescription("Copied Patron Document");
236     }
237 
238     /**
239      * This method will set the description for edit operation
240      *
241      * @param document
242      * @param requestParameters
243      */
244     @Override
245     public void processAfterEdit(MaintenanceDocument document, Map<String, String[]> requestParameters) {
246         LOG.debug("Inside processAfterEdit()");
247         super.processAfterEdit(document, requestParameters);
248         document.getDocumentHeader().setDocumentDescription("Edited Patron Document");
249     }
250 
251     private List<OlePatronAffiliation> getPatronAffiliationFromEntity(List<EntityAffiliationBo> affiliations,
252                                                                       List<EntityEmploymentBo> employeeDetails) {
253         List<OlePatronAffiliation> patronAffiliations = new ArrayList<OlePatronAffiliation>();
254         for (EntityAffiliationBo entityAffiliationBo : affiliations) {
255             OlePatronAffiliation patronAffiliation = new OlePatronAffiliation(entityAffiliationBo);
256             List<EntityEmploymentBo> employmentBos = new ArrayList<EntityEmploymentBo>();
257             for (EntityEmploymentBo entityEmploymentBo : employeeDetails) {
258                 if (patronAffiliation.getEntityAffiliationId().equalsIgnoreCase(entityEmploymentBo.getEntityAffiliationId())) {
259                     employmentBos.add(entityEmploymentBo);
260                 }
261                 patronAffiliation.setEmployments(employmentBos);
262             }
263             patronAffiliations.add(patronAffiliation);
264         }
265         return patronAffiliations;
266     }
267 
268     public String getTempCircRecords(String olePatronId) {
269         String url = baseUrl + "/portal.do?channelTitle=Patron&channelUrl=" + baseUrl + "/ole-kr-krad/temporaryCirculationRecord?viewId=OleTemporaryCirculationHistoryRecordView&amp;methodToCall=viewTempCircRecords&amp;patronId=" + olePatronId;
270         return url;
271     }
272 
273     public String getLoanedRecords(String olePatronId) {
274         String url = baseUrl + "/portal.do?channelTitle=Patron&channelUrl=" + baseUrl + "/ole-kr-krad/patronLoanedRecord?viewId=OlePatronLoanedRecordView&amp;methodToCall=viewLoanedRecords&amp;patronId=" + olePatronId;
275         return url;
276     }
277 
278     public String getRequestedRecords(String olePatronId) {
279         String url = baseUrl + "/portal.do?channelTitle=Patron&channelUrl=" + baseUrl + "/ole-kr-krad/patronRequestedRecord?viewId=OlePatronRequestedRecordView&amp;methodToCall=viewRequestedRecords&amp;patronId=" + olePatronId;
280         return url;
281     }
282 
283     public String getCountOfPendingRequests(String itemId) {
284         Map itemMap = new HashMap();
285         itemMap.put(OLEConstants.OleDeliverRequest.ITEM_ID, itemId);
286         List<OleDeliverRequestBo> oleDeliverRequestBoList = (List<OleDeliverRequestBo>) getBusinessObjectService().findMatching(OleDeliverRequestBo.class, itemMap);
287         if (oleDeliverRequestBoList != null && oleDeliverRequestBoList.size() > 0) {
288 
289             return "View all requests";
290         } else
291             return " ";
292     }
293 }