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         } catch (ClassNotPersistenceCapableException ex) {
139             if (!document.getOldMaintainableObject().isExternalBusinessObject()) {
140                 LOG.error("Data Object Class: "
141                         + getDataObjectClass()
142                         + " is not persistable and is not externalizable - configuration error", ex);
143                 throw new RuntimeException("Data Object Class: "
144                         + getDataObjectClass()
145                         + " is not persistable and is not externalizable - configuration error");
146             }
147         }
148         return olePatron;
149     }
150 
151     /**
152      * This method will set the default value for the name object and also set the description when a new patron document is created
153      *
154      * @param document
155      * @param requestParameters
156      */
157     @Override
158     public void processAfterNew(MaintenanceDocument document,
159                                 Map<String, String[]> requestParameters) {
160         LOG.debug("Inside processAfterNew()");
161         EntityNameTypeBo nameTypeBo = (EntityNameTypeBo) KRADServiceLocator.getBusinessObjectService().findBySinglePrimaryKey(EntityNameTypeBo.class, OLEConstants.OlePatron.ENTITY_NM_TYP_CD);
162         OlePatronDocument patronDocument = (OlePatronDocument) document.getNewMaintainableObject().getDataObject();
163         patronDocument.setActiveIndicator(true);
164         patronDocument.getName().setNameCode(OLEConstants.OlePatron.ENTITY_NM_TYP_CD);
165         patronDocument.getName().setActive(true);
166         patronDocument.getName().setDefaultValue(true);
167         patronDocument.getName().setNameType(nameTypeBo);
168         document.getNewMaintainableObject().setDataObject(patronDocument);
169         super.processAfterNew(document, requestParameters);
170         document.getDocumentHeader().setDocumentDescription("New Patron Document");
171 
172     }
173 
174     /**
175      * This method will set the description when copy is selected
176      *
177      * @param document
178      * @param requestParameters
179      */
180     @Override
181     public void processAfterCopy(MaintenanceDocument document, Map<String, String[]> requestParameters) {
182         LOG.debug("Inside processAfterCopy()");
183         super.processAfterCopy(document, requestParameters);
184         document.getDocumentHeader().setDocumentDescription("Copied Patron Document");
185     }
186 
187     /**
188      * This method will set the description for edit operation
189      *
190      * @param document
191      * @param requestParameters
192      */
193     @Override
194     public void processAfterEdit(MaintenanceDocument document, Map<String, String[]> requestParameters) {
195         LOG.debug("Inside processAfterEdit()");
196         super.processAfterEdit(document, requestParameters);
197         document.getDocumentHeader().setDocumentDescription("Edited Patron Document");
198     }
199 
200     private List<OlePatronAffiliation> getPatronAffiliationFromEntity(List<EntityAffiliationBo> affiliations,
201                                                                       List<EntityEmploymentBo> employeeDetails) {
202         List<OlePatronAffiliation> patronAffiliations = new ArrayList<OlePatronAffiliation>();
203         for (EntityAffiliationBo entityAffiliationBo : affiliations) {
204             OlePatronAffiliation patronAffiliation = new OlePatronAffiliation(entityAffiliationBo);
205             List<EntityEmploymentBo> employmentBos = new ArrayList<EntityEmploymentBo>();
206             for (EntityEmploymentBo entityEmploymentBo : employeeDetails) {
207                 if (patronAffiliation.getEntityAffiliationId().equalsIgnoreCase(entityEmploymentBo.getEntityAffiliationId())) {
208                     employmentBos.add(entityEmploymentBo);
209                 }
210                 patronAffiliation.setEmployments(employmentBos);
211             }
212             patronAffiliations.add(patronAffiliation);
213         }
214         return patronAffiliations;
215     }
216 
217     public String getTempCircRecords(String olePatronId) {
218         String url = baseUrl + "/portal.do?channelTitle=Patron&channelUrl=" + baseUrl + "/ole-kr-krad/temporaryCirculationRecord?viewId=OleTemporaryCirculationHistoryRecordView&amp;methodToCall=viewTempCircRecords&amp;patronId=" + olePatronId;
219         return url;
220     }
221 
222     public String getLoanedRecords(String olePatronId) {
223         String url = baseUrl + "/portal.do?channelTitle=Patron&channelUrl=" + baseUrl + "/ole-kr-krad/patronLoanedRecord?viewId=OlePatronLoanedRecordView&amp;methodToCall=viewLoanedRecords&amp;patronId=" + olePatronId;
224         return url;
225     }
226 
227     public String getRequestedRecords(String olePatronId) {
228         String url = baseUrl + "/portal.do?channelTitle=Patron&channelUrl=" + baseUrl + "/ole-kr-krad/patronRequestedRecord?viewId=OlePatronRequestedRecordView&amp;methodToCall=viewRequestedRecords&amp;patronId=" + olePatronId;
229         return url;
230     }
231 
232     public String getCountOfPendingRequests(String itemId) {
233         Map itemMap = new HashMap();
234         itemMap.put(OLEConstants.OleDeliverRequest.ITEM_ID, itemId);
235         List<OleDeliverRequestBo> oleDeliverRequestBoList = (List<OleDeliverRequestBo>) getBusinessObjectService().findMatching(OleDeliverRequestBo.class, itemMap);
236         if (oleDeliverRequestBoList != null && oleDeliverRequestBoList.size() > 0) {
237 
238             return "View all requests";
239         } else
240             return " ";
241     }
242 }