View Javadoc

1   package org.kuali.ole.patron.maintenance;
2   
3   import org.apache.ojb.broker.metadata.ClassNotPersistenceCapableException;
4   import org.kuali.ole.OLEConstants;
5   import org.kuali.ole.deliver.loan.LoanProcessor;
6   import org.kuali.ole.deliver.loan.bo.OleItemSearch;
7   import org.kuali.ole.deliver.request.bo.OleDeliverRequestBo;
8   import org.kuali.ole.deliver.request.service.OleDeliverRequestDocumentHelperServiceImpl;
9   import org.kuali.ole.patron.bo.*;
10  import org.kuali.ole.service.OlePatronService;
11  import org.kuali.ole.service.OlePatronServiceImpl;
12  import org.kuali.rice.core.api.util.KeyValue;
13  import org.kuali.rice.kim.api.KimConstants;
14  import org.kuali.rice.kim.bo.options.NamePrefixValuesFinder;
15  import org.kuali.rice.kim.bo.options.NameSuffixValuesFinder;
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  
27  import java.util.*;
28  
29  /**
30   * OlePatronMaintenanceImpl performs save ,edit,copy  and create new operation for OlePatron.
31   *
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      /**
38       * This method populates the entity object and persist and as well as set that entity to that patron document
39       */
40      @Override
41      public void prepareForSave() {
42          LOG.debug("Inside prepareForSave method");
43          OlePatronDocument patronDocument = new OlePatronDocument();
44          try{
45              patronDocument = (OlePatronDocument)getDataObject();
46              EntityBo kimEntity;
47              List<EntityTypeContactInfoBo> entityTypes;
48              EntityTypeContactInfoBo entityType;
49              if(patronDocument.getEntity() == null) {
50                  kimEntity = new EntityBo();
51                  entityTypes = new ArrayList<EntityTypeContactInfoBo>();
52                  entityType = new EntityTypeContactInfoBo();
53                  kimEntity.setActive(true);
54  
55                  entityType.setEntityTypeCode(KimConstants.EntityTypes.PERSON);
56                  entityType.setActive(true);
57              }
58              else {
59                  kimEntity = patronDocument.getEntity();
60                  kimEntity.setActive(true);
61                  entityTypes = kimEntity.getEntityTypeContactInfos();
62                  if(!entityTypes.isEmpty()){
63                      entityType = entityTypes.get(0);
64                      entityType.setActive(true);
65                  }
66                  else {
67                      entityType = new EntityTypeContactInfoBo();
68                      entityType.setEntityTypeCode(KimConstants.EntityTypes.PERSON);
69                  }
70              }
71              List<EntityAffiliationBo> affiliationBoList = new ArrayList<EntityAffiliationBo>();
72              List<EntityEmploymentBo> employeeList = new ArrayList<EntityEmploymentBo>();
73              String affiliationSeq;
74              if(patronDocument.getPatronAffiliations().size() > 0) {
75                  affiliationSeq = KRADServiceLocator.getSequenceAccessorService().getNextAvailableSequenceNumber("krim_entity_afltn_id_s").toString();
76                  for(OlePatronAffiliation patronAffiliation : patronDocument.getPatronAffiliations()) {
77                      EntityAffiliationBo entityAffiliationBo = patronAffiliation.getEntityAffliationBo();
78                      if(entityAffiliationBo.getId() == null) {
79                          entityAffiliationBo.setId(affiliationSeq);
80                      }
81                      for(EntityEmploymentBo employmentBo : patronAffiliation.getEmployments()) {
82                          employmentBo.setEntityAffiliation(entityAffiliationBo);
83                          employmentBo.setEntityAffiliationId(entityAffiliationBo.getId());
84                          employeeList.add(employmentBo);
85                      }
86                      affiliationBoList.add(entityAffiliationBo);
87                  }
88              }
89              /* employeeList = patronDocument.getEmployments();*/
90              kimEntity.setAffiliations(affiliationBoList);
91              kimEntity.setEmploymentInformation(employeeList);
92              kimEntity.setNames(Arrays.asList(patronDocument.getName()));
93  
94              List<EntityAddressBo> addressList = new ArrayList<EntityAddressBo>();
95              if(patronDocument.getOleEntityAddressBo().size() > 0) {
96                  for(OleEntityAddressBo oleEntityAddress : patronDocument.getOleEntityAddressBo()) {
97                      addressList.add(oleEntityAddress.getEntityAddressBo());
98                          }
99                  }
100             entityType.setAddresses(addressList);
101             entityType.setEmailAddresses(patronDocument.getEmails());
102             entityType.setPhoneNumbers(patronDocument.getPhones());
103 
104             entityTypes.add(entityType);
105 
106             kimEntity.setEntityTypeContactInfos(entityTypes);
107 
108 
109             //EntityBo entity = getBusinessObjectService().save(kimEntity);
110             List<EntityAddressBo> addressBos = kimEntity.getEntityTypeContactInfos().get(0).getAddresses();
111             List<OleAddressBo> oleAddressBos = new ArrayList<OleAddressBo>();
112             for(int i=0;i<addressBos.size();i++){
113                 EntityAddressBo entityAddressBo = addressBos.get(i);
114                 Map<String, Object> criteria = new HashMap<String, Object>();
115                 criteria.put(OLEConstants.OlePatron.ENTITY_BO_ID,entityAddressBo.getId());
116                 OleAddressBo oleAddressBo =  KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(OleAddressBo.class,criteria);
117                 if(oleAddressBo==null){
118                     oleAddressBo = new OleAddressBo();
119                 }
120                 oleAddressBo.setId(entityAddressBo.getId());
121                 oleAddressBo.setOlePatronId(kimEntity.getId());
122                 oleAddressBo.setOleAddressId(KRADServiceLocator.getSequenceAccessorService().getNextAvailableSequenceNumber("OLE_DLVR_ADD_S").toString());
123                 oleAddressBo.setEntityAddress(entityAddressBo);
124                 oleAddressBo.setAddressValidFrom(patronDocument.getOleEntityAddressBo().get(i).getOleAddressBo().getAddressValidFrom());
125                 oleAddressBo.setAddressValidTo(patronDocument.getOleEntityAddressBo().get(i).getOleAddressBo().getAddressValidTo());
126                 oleAddressBo.setAddressVerified(patronDocument.getOleEntityAddressBo().get(i).getOleAddressBo().isAddressVerified());
127                 oleAddressBo.setAddressSource(patronDocument.getOleEntityAddressBo().get(i).getOleAddressBo().getAddressSource());
128                 //getBusinessObjectService().save(oleAddressBo);
129                 oleAddressBos.add(oleAddressBo);
130             }
131             patronDocument.setOleAddresses(oleAddressBos);
132             patronDocument.setEntity(kimEntity);
133             setDataObject(patronDocument);
134         } catch (Exception ex) {
135             LOG.error(ex.getMessage());
136         }
137     }
138 
139     /**
140      * This method populate the patron object used for edit and copy
141      * @param document
142      * @param dataObjectKeys
143      * @return olePatron(Object)
144      */
145     @Override
146     public Object retrieveObjectForEditOrCopy(MaintenanceDocument document, Map<String, String> dataObjectKeys) {
147         LOG.debug("Inside retrieveObjectForEditOrCopy()");
148         Object dataObject = null;
149         OlePatronDocument olePatron = new OlePatronDocument();
150         OleEntityAddressBo entityAddressBo = null;
151         OleAddressBo oleAddressBo = new OleAddressBo();
152         List<OleEntityAddressBo> oleEntityAddressList = new ArrayList<OleEntityAddressBo>();
153         try {
154             dataObject = getLookupService().findObjectBySearch(getDataObjectClass(), dataObjectKeys);
155             olePatron = (OlePatronDocument)dataObject;
156             EntityBo entity = olePatron.getEntity();
157             EntityNameBo name = entity.getNames().get(0);
158          /*   String namePrefix = name.getNamePrefix();
159             String nameSuffix = name.getNameSuffix();
160             NamePrefixValuesFinder namePrefixValue = new NamePrefixValuesFinder();
161             NameSuffixValuesFinder nameSuffixValue = new NameSuffixValuesFinder();
162             List<KeyValue> KeyValues = new ArrayList<KeyValue>();
163             if(namePrefix != null ) {
164                 KeyValues = namePrefixValue.getKeyValues();
165                 for (KeyValue keyValue : KeyValues) {
166                     if(!namePrefix.equals(keyValue)) {
167                         olePatron.setNamePrefixFlag(true);
168                         //namePrefix = "<div id='NameKeyValues'>"+namePrefix+"</div>";
169                     }
170                 }
171                 KeyValues.remove(namePrefixValue.getKeyValues());
172                 name.setNamePrefix(namePrefix);
173             }
174             if(nameSuffix != null ) {
175                 KeyValues = nameSuffixValue.getKeyValues();
176                 for (KeyValue keyValue : KeyValues) {
177                     if(!nameSuffix.equals(keyValue)) {
178                         olePatron.setNameSuffixFlag(true);
179                         //nameSuffix = "<div style=''>"+nameSuffix+"</div>";
180                     }
181                 }
182                 KeyValues.remove(nameSuffixValue.getKeyValues());
183                 name.setNameSuffix(nameSuffix);
184             }*/
185             olePatron.setName(name);
186             if(!entity.getEntityTypeContactInfos().isEmpty()){
187                 List<EntityAddressBo> entityAddressList = entity.getEntityTypeContactInfos().get(0).getAddresses();
188                 for (EntityAddressBo entityAdd : entityAddressList) {
189                     entityAddressBo = new OleEntityAddressBo();
190                     entityAddressBo.setEntityAddressBo(entityAdd);
191                     Map addMap = new HashMap();
192                     addMap.put(OLEConstants.OlePatron.ENTITY_BO_ID,entityAdd.getId());
193                     oleAddressBo =  getBusinessObjectService().findByPrimaryKey(OleAddressBo.class, addMap);
194                     entityAddressBo.setOleAddressBo(oleAddressBo);
195                     oleEntityAddressList.add(entityAddressBo);
196                 }
197                 olePatron.setOleEntityAddressBo(oleEntityAddressList);
198                 olePatron.setPhones(entity.getEntityTypeContactInfos().get(0).getPhoneNumbers());
199                 olePatron.setEmails(entity.getEntityTypeContactInfos().get(0).getEmailAddresses());
200             }
201             List<OleProxyPatronDocument> oleProxyPatronDocuments = olePatron.getOleProxyPatronDocuments();
202             List<OleProxyPatronDocument> proxyPatronDocumentList = new ArrayList<OleProxyPatronDocument>();
203             List<OlePatronDocument> olePatronDocuments = new ArrayList<OlePatronDocument>();
204             if (oleProxyPatronDocuments.size()>0) {
205                 for (Iterator<OleProxyPatronDocument> proxyPatronIterator = oleProxyPatronDocuments.iterator(); proxyPatronIterator.hasNext(); ) {
206                     OleProxyPatronDocument oleProxyPatronDocument = (OleProxyPatronDocument) proxyPatronIterator.next();
207                     Map map = new HashMap();
208                     map.put(OLEConstants.OlePatron.PATRON_ID,oleProxyPatronDocument.getProxyPatronId());
209                     OlePatronDocument olePatronDocument = (OlePatronDocument)getBusinessObjectService().findByPrimaryKey(OlePatronDocument.class, map);
210                     if (olePatronDocument.isActiveIndicator()) {
211                     oleProxyPatronDocument.setProxyPatronBarcode(olePatronDocument.getBarcode());
212                     oleProxyPatronDocument.setProxyPatronFirstName(olePatronDocument.getEntity().getNames().get(0).getFirstName());
213                     oleProxyPatronDocument.setProxyPatronLastName(olePatronDocument.getEntity().getNames().get(0).getLastName());
214                     proxyPatronDocumentList.add(oleProxyPatronDocument);
215                     }
216                 }
217                 olePatron.setOleProxyPatronDocuments(proxyPatronDocumentList);
218             }
219             olePatron.setEmployments(entity.getEmploymentInformation());
220             List<OlePatronAffiliation> patronAffiliations = new ArrayList<OlePatronAffiliation>();
221             olePatron.setPatronAffiliations(getPatronAffiliationFromEntity(entity.getAffiliations(),entity.getEmploymentInformation()));
222 
223             if(olePatron.getOlePatronId() != null) {
224                 LoanProcessor loanProcessor = new LoanProcessor();
225                 OleDeliverRequestDocumentHelperServiceImpl requestService =  new OleDeliverRequestDocumentHelperServiceImpl();
226                 List<OleDeliverRequestBo> oleDeliverRequestBoList = olePatron.getOleDeliverRequestBos();
227                 if(oleDeliverRequestBoList.size()>0){
228                     for(int i=0;i<oleDeliverRequestBoList.size();i++){
229                         OleItemSearch oleItemSearch= requestService.getItemDetailsForPatron(oleDeliverRequestBoList.get(i).getItemUuid());
230                         if(oleItemSearch!=null && oleItemSearch.getItemBarCode()!=null){
231                             oleDeliverRequestBoList.get(i).setTitle(oleItemSearch.getTitle());
232                             oleDeliverRequestBoList.get(i).setCallNumber(oleItemSearch.getCallNumber());
233                         }
234                     }
235                 }
236                 try {
237                     olePatron.setOleLoanDocuments(loanProcessor.getPatronLoanedItem(olePatron.getOlePatronId()));
238                     olePatron.setOleTemporaryCirculationHistoryRecords(loanProcessor.getPatronTemporaryCirculationHistoryRecords(olePatron.getOlePatronId()));
239                     olePatron.setOleDeliverRequestBos(loanProcessor.getPatronRequestRecords(olePatron.getOlePatronId()));
240                 } catch (Exception e) {
241                     e.printStackTrace();
242                 }
243                 if (olePatron.getOleLoanDocuments().size() > 0 ) {
244                     olePatron.setLoanFlag(true);
245                 }
246                 if (olePatron.getOleTemporaryCirculationHistoryRecords().size() > 0 ) {
247                     olePatron.setTempCircHistoryFlag(true);
248                 }
249                 if (olePatron.getOleDeliverRequestBos().size() > 0 ) {
250                     olePatron.setRequestFlag(true);
251                 }
252                 OlePatronDocument olePatronDocument;
253                 OleProxyPatronDocument proxyPatronDocument = null;
254                 List<OleProxyPatronDocument> proxyPatronDocuments = new ArrayList<OleProxyPatronDocument>();
255                 Map proxyMap = new HashMap();
256                 proxyMap.put(OLEConstants.OlePatron.PROXY_PATRON_ID,olePatron.getOlePatronId());
257                 List<OleProxyPatronDocument> oleProxyPatronDocumentList = (List<OleProxyPatronDocument>)getBusinessObjectService().findMatching(OleProxyPatronDocument.class, proxyMap);
258                 if ( oleProxyPatronDocumentList.size() > 0 ) {
259                     for (OleProxyPatronDocument oleProxyPatronDocument : oleProxyPatronDocumentList) {
260                         proxyMap.remove(OLEConstants.OlePatron.PROXY_PATRON_ID);
261                         proxyMap.put(OLEConstants.OlePatron.PATRON_ID,oleProxyPatronDocument.getOlePatronId());
262                         List<OlePatronDocument> olePatronDocumentList = (List<OlePatronDocument>) getBusinessObjectService().findMatching(OlePatronDocument.class, proxyMap);
263                         if ( olePatronDocumentList.size() > 0 ) {
264                             olePatronDocument = olePatronDocumentList.get(0);
265                             proxyPatronDocument = new OleProxyPatronDocument();
266                             if ( olePatronDocument.isActiveIndicator() ) {
267                                 proxyPatronDocument.setOlePatronId(olePatronDocument.getOlePatronId());
268                                 proxyPatronDocument.setProxyForPatronFirstName(olePatronDocument.getEntity().getNames().get(0).getFirstName());
269                                 proxyPatronDocument.setProxyForPatronLastName(olePatronDocument.getEntity().getNames().get(0).getLastName());
270                                 List<OleProxyPatronDocument> proxyPatronList = (List<OleProxyPatronDocument>)getBusinessObjectService().findMatching(OleProxyPatronDocument.class, proxyMap);
271                                 if ( proxyPatronList.size() > 0 ) {
272                                     for ( OleProxyPatronDocument proxyPatron : proxyPatronList ) {
273                                         if ( proxyPatron.getOlePatronId().equals(olePatronDocument.getOlePatronId())) {
274                                             proxyPatronDocument.setProxyPatronActivationDate(proxyPatron.getProxyPatronActivationDate());
275                                             proxyPatronDocument.setProxyPatronExpirationDate(proxyPatron.getProxyPatronExpirationDate());
276                                         }
277                                     }
278                                 }
279                             }
280                             proxyPatronDocuments.add(proxyPatronDocument);
281                         }
282                     }
283                     olePatron.setOleProxyPatronDocumentList(proxyPatronDocuments);
284                 }
285             }
286         } catch (ClassNotPersistenceCapableException ex) {
287             if (!document.getOldMaintainableObject().isExternalBusinessObject()) {
288                 throw new RuntimeException("Data Object Class: "
289                         + getDataObjectClass()
290                         + " is not persistable and is not externalizable - configuration error");
291             }
292         }
293 
294         return olePatron;
295     }
296 
297     /**
298      * This method will set the default value for the name object and also set the description when a new patron document is created
299      * @param document
300      * @param requestParameters
301      */
302     @Override
303     public void processAfterNew(MaintenanceDocument document,
304                                 Map<String, String[]> requestParameters) {
305         LOG.debug("Inside processAfterNew()");
306         EntityNameTypeBo nameTypeBo = (EntityNameTypeBo) KRADServiceLocator.getBusinessObjectService().findBySinglePrimaryKey(EntityNameTypeBo.class, "PRFR");
307         OlePatronDocument patronDocument = (OlePatronDocument)document.getNewMaintainableObject().getDataObject();
308         patronDocument.setActiveIndicator(true);
309         patronDocument.getName().setActive(true);
310         patronDocument.getName().setDefaultValue(true);
311         patronDocument.getName().setNameType(nameTypeBo);
312         document.getNewMaintainableObject().setDataObject(patronDocument);
313         super.processAfterNew(document, requestParameters);
314         document.getDocumentHeader().setDocumentDescription("New Patron Document");
315 
316     }
317 
318     /**
319      * This method will set the description when copy is selected
320      * @param document
321      * @param requestParameters
322      */
323     @Override
324     public void processAfterCopy(MaintenanceDocument document, Map<String, String[]> requestParameters) {
325         LOG.debug("Inside processAfterCopy()");
326         super.processAfterCopy(document, requestParameters);
327         document.getDocumentHeader().setDocumentDescription("Copied Patron Document");
328     }
329 
330     /**
331      * This method will set the description for edit operation
332      * @param document
333      * @param requestParameters
334      */
335     @Override
336     public void processAfterEdit(MaintenanceDocument document, Map<String, String[]> requestParameters) {
337         LOG.debug("Inside processAfterEdit()");
338         super.processAfterEdit(document, requestParameters);
339         document.getDocumentHeader().setDocumentDescription("Edited Patron Document");
340     }
341 
342     private List<OlePatronAffiliation> getPatronAffiliationFromEntity(List<EntityAffiliationBo> affiliations,
343                                                                       List<EntityEmploymentBo> employeeDetails) {
344         List<OlePatronAffiliation> patronAffiliations = new ArrayList<OlePatronAffiliation>();
345         for(EntityAffiliationBo entityAffiliationBo : affiliations) {
346             OlePatronAffiliation patronAffiliation = new OlePatronAffiliation(entityAffiliationBo);
347             List<EntityEmploymentBo> employmentBos = new ArrayList<EntityEmploymentBo>();
348             for(EntityEmploymentBo entityEmploymentBo : employeeDetails) {
349                 if(patronAffiliation.getEntityAffiliationId().equalsIgnoreCase(entityEmploymentBo.getEntityAffiliationId())) {
350                     employmentBos.add(entityEmploymentBo);
351                 }
352                 patronAffiliation.setEmployments(employmentBos);
353             }
354             patronAffiliations.add(patronAffiliation);
355         }
356         return patronAffiliations;
357     }
358     public String getTempCircRecords(String olePatronId) {
359         String url = "temporaryCirculationRecord?viewId=OleTemporaryCirculationHistoryRecordView&amp;methodToCall=viewTempCircRecords&amp;patronId="+olePatronId;
360         return url;
361     }
362 
363     public String getLoanedRecords(String olePatronId) {
364         String url = "patronLoanedRecord?viewId=OlePatronLoanedRecordView&amp;methodToCall=viewLoanedRecords&amp;patronId="+olePatronId;
365         return url;
366     }
367 
368     public String getRequestedRecords(String olePatronId) {
369         String url = "patronRequestedRecord?viewId=OlePatronRequestedRecordView&amp;methodToCall=viewRequestedRecords&amp;patronId="+olePatronId;
370         return url;
371     }
372 
373 }