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