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