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