View Javadoc

1   package org.kuali.ole.service;
2   
3   import org.apache.commons.lang.StringUtils;
4   import org.kuali.ole.OLEConstants;
5   import org.kuali.ole.patron.bill.PatronBillPayment;
6   import org.kuali.ole.patron.bo.*;
7   import org.kuali.rice.kim.impl.KIMPropertyConstants;
8   import org.kuali.rice.kim.impl.identity.address.EntityAddressBo;
9   import org.kuali.rice.kim.impl.identity.email.EntityEmailBo;
10  import org.kuali.rice.kim.impl.identity.employment.EntityEmploymentBo;
11  import org.kuali.rice.kim.impl.identity.phone.EntityPhoneBo;
12  import org.kuali.rice.krad.lookup.LookupableImpl;
13  import org.kuali.rice.krad.service.BusinessObjectService;
14  import org.kuali.rice.krad.service.KRADServiceLocator;
15  
16  import java.text.SimpleDateFormat;
17  import java.util.*;
18  
19  /**
20   * OlePatronHelperServiceImpl converts PatronProperties to EntityProperties and generate new search criteria.
21   */
22  public class OlePatronHelperServiceImpl extends LookupableImpl implements OlePatronHelperService {
23      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OlePatronHelperServiceImpl.class);
24  
25      protected static final String ENTITY_EMAIL_PROPERTY_PREFIX = "entity.entityTypeContactInfos.emailAddresses.";
26      protected static final String ENTITY_NAME_PROPERTY_PREFIX = "entity.names.";
27      private BusinessObjectService businessObjectService;
28      protected Map<String, String> criteriaConversion = new HashMap<String, String>();
29  
30      {
31          criteriaConversion.put(KIMPropertyConstants.Person.FIRST_NAME, "entity.names.firstName");
32          criteriaConversion.put(KIMPropertyConstants.Person.LAST_NAME, "entity.names.lastName");
33          criteriaConversion.put(KIMPropertyConstants.Person.EMAIL_ADDRESS, "entity.entityTypeContactInfos.emailAddresses.emailAddress");
34      }
35  
36      /**
37       * Populate search criteria from the patron document
38       * @param criteria
39       * @return newCriteria
40       */
41      @Override
42      public Map<String, String> convertPatronPropertiesToEntityProperties(Map<String, String> criteria) {
43          LOG.debug("Inside the convertPatronPropertiesToEntityProperties method");
44          boolean nameCriteria = false;
45          boolean emailCriteria = false;
46  
47          Map<String, String> newCriteria = new HashMap<String, String>();
48          newCriteria.put("entity.entityTypeContactInfos.entityTypeCode","PERSON|SYSTEM");
49          if (criteria != null) {
50              for (String key : criteria.keySet()) {
51                  if (key.equals(OLEConstants.PATRON_ENTITY_ACTIVE)) {
52                      newCriteria.put(OLEConstants.PATRON_ENTITY_ACTIVE, criteria.get(OLEConstants.PATRON_ENTITY_ACTIVE));
53                  } else {
54                      if (!(criteria.containsKey(OLEConstants.PATRON_ENTITY_ACTIVE))) {
55                          newCriteria.remove(OLEConstants.PATRON_ENTITY_ACTIVE);
56                      }
57                  }
58  
59                  if (StringUtils.isEmpty(criteria.get(key))) {
60                      continue;
61                  }
62  
63                  String entityProperty = criteriaConversion.get(key);
64                  if (entityProperty != null) {
65                      newCriteria.put(entityProperty, criteria.get(key));
66                  } else {
67                      entityProperty = key;
68                      newCriteria.put(key, criteria.get(key));
69                  }
70                  if (isNameEntityCriteria(entityProperty)) {
71                      nameCriteria = true;
72                  }
73                  if (isEmailEntityCriteria(entityProperty)) {
74                      emailCriteria = true;
75                  }
76  
77                  if (nameCriteria) {
78                      newCriteria.put(ENTITY_NAME_PROPERTY_PREFIX + "active", "Y");
79                  }
80                  if (emailCriteria) {
81                      Map<String, String> email = new HashMap<String, String>();
82                      email.put("emailAddress",criteria.get("emailAddress"));
83                      List<String> entityIds = new ArrayList<String>();
84                      List<EntityEmailBo> entityEmails = (List<EntityEmailBo>) getLookupService()
85                              .findCollectionBySearchHelper(EntityEmailBo.class, email,true);
86                      StringBuilder builder = new StringBuilder("-");
87                      if(entityEmails.size()>0) {
88                          builder.setLength(0);
89                          for(EntityEmailBo entityEmail : entityEmails) {
90                              builder.append(entityEmail.getEntityId()+"|");
91                              entityIds.add(entityEmail.getEntityId());
92                          }
93                          int index = builder.lastIndexOf("|");
94                          if(index>0)
95                              builder.replace(index, index + 1, "");
96                          newCriteria.remove("emailAddress");
97                      }
98                      if(criteria.containsKey("olePatronId")) {
99                          if(!entityIds.contains(criteria.get("olePatronId"))){
100                             newCriteria.put("olePatronId", "-");
101                         }
102                     }
103                     else {
104                         newCriteria.put("olePatronId",builder.toString());
105                     }
106                 }
107             }
108         }
109         if (LOG.isDebugEnabled()) {
110             LOG.debug("Converted: " + newCriteria);
111         }
112         return newCriteria;
113     }
114 
115     /**
116      * Check whether name entity is present in search criteria
117      * @param propertyName
118      * @return boolean
119      */
120     protected boolean isNameEntityCriteria(String propertyName) {
121         LOG.debug("Inside the isNameEntityCriteria method");
122         return propertyName.startsWith(ENTITY_NAME_PROPERTY_PREFIX);
123     }
124 
125     /**
126      * Check whether email entity is present in search criteria
127      * @param propertyName
128      * @return boolean
129      */
130     protected boolean isEmailEntityCriteria(String propertyName) {
131         LOG.debug("Inside the isEmailEntityCriteria method");
132         return propertyName.startsWith(ENTITY_EMAIL_PROPERTY_PREFIX);
133     }
134 
135 
136     public boolean deletePatron(OlePatronDocument olePatronDocument) {
137         LOG.debug("Inside the deletePatron method");
138         boolean deleteFlag = false;
139         SimpleDateFormat fmt = new SimpleDateFormat(OLEConstants.OlePatron.PATRON_MAINTENANCE_DATE_FORMAT);
140         Map billMap = new HashMap();
141         billMap.put(OLEConstants.OlePatron.PAY_BILL_PATRON_ID, olePatronDocument.getOlePatronId());
142         List<PatronBillPayment> patronBillPayments = (List<PatronBillPayment>) KRADServiceLocator.getBusinessObjectService().findMatching(PatronBillPayment.class,billMap);
143 
144         if(olePatronDocument != null && olePatronDocument.getOlePatronId() != null) {
145            if (olePatronDocument.getOleLoanDocuments() == null || olePatronDocument.getOleLoanDocuments().size() == 0) {
146                 if( (olePatronDocument.getOleTemporaryCirculationHistoryRecords() == null || olePatronDocument.getOleTemporaryCirculationHistoryRecords().size() == 0)) {
147                     if( (olePatronDocument.getOleDeliverRequestBos() == null || olePatronDocument.getOleDeliverRequestBos().size() == 0)) {
148                         if( (patronBillPayments == null || patronBillPayments.size() == 0)) {
149                             olePatronDocument.setActiveIndicator(false);
150                             List<OleAddressBo> addressBos = olePatronDocument.getOleAddresses();
151                             if(addressBos.size() > 0) {
152                                 for( OleAddressBo addressBo : addressBos) {
153                                      if(addressBo.getAddressSource().isEmpty() || "".equals(addressBo.getAddressSource())) {
154                                          addressBo.setAddressSource(null);
155                                      }
156                                 }
157                             }
158                             KRADServiceLocator.getBusinessObjectService().save(olePatronDocument);
159                             deleteFlag = true;
160                         }
161                     }
162                 }
163             }
164         }
165         return deleteFlag;
166     }
167 
168     /**
169      * This method is for checking the added address source or not
170      * @param oleAddresses
171      * @return true , if the address source has value in a single patron record, else false
172      */
173     public boolean checkAddressSource (List<OleAddressBo> oleAddresses) {
174 
175         boolean valid = true;
176         for(OleAddressBo oleAddress : oleAddresses){
177             if((oleAddress.getAddressSource() != null) && (oleAddress.getAddressSource().equals(""))) {
178                 valid = false;
179             }
180         }
181         return valid;
182     }
183 
184     /**
185      * This method is for checking the entity phone has multiple defaults
186      * @param phoneBoList
187      * @return true , if the entity phone has only one default in a single patron record, else false
188      */
189     public boolean checkPhoneMultipleDefault (List<EntityPhoneBo> phoneBoList) {
190         boolean valid = true;
191         boolean isDefaultSet = false;
192         int i = 0;
193         for (EntityPhoneBo phone : phoneBoList) {
194             if (phone.isDefaultValue()) {
195                 if (isDefaultSet) {
196                     valid = false;
197                 } else {
198                     isDefaultSet = true;
199                 }
200             }
201             i++;
202         }
203         if (!phoneBoList.isEmpty() && !isDefaultSet) {
204             valid = false;
205         }
206         return valid;
207     }
208     /**
209      * This method is for checking the entity address has multiple defaults
210      * @param addrBoList
211      * @return true , if the entity address has only one default in a single patron record, else false
212      */
213     public boolean checkAddressMultipleDefault (List<OleEntityAddressBo> addrBoList) {
214         boolean valid = true;
215         boolean isDefaultSet = false;
216         int i = 0;
217         for (OleEntityAddressBo addr : addrBoList) {
218             EntityAddressBo entityAddressBo = addr.getEntityAddressBo();
219             if (entityAddressBo.isDefaultValue()) {
220                 if (isDefaultSet) {
221                     valid = false;
222                 } else {
223                     isDefaultSet = true;
224                 }
225             }
226             i++;
227         }
228         if (!addrBoList.isEmpty() && !isDefaultSet) {
229             valid = false;
230         }
231         return valid;
232     }
233     /**
234      * This method is for checking the entity email address has multiple defaults
235      * @param emailBoList
236      * @return true , if the entity email address has only one default in a single patron record, else false
237      */
238     public boolean checkEmailMultipleDefault (List<EntityEmailBo> emailBoList) {
239         boolean valid = true;
240         boolean isDefaultSet = false;
241         int i = 0;
242         for (EntityEmailBo email : emailBoList) {
243             if (email.isDefaultValue()) {
244                 if (isDefaultSet) {
245                     valid = false;
246                 } else {
247                     isDefaultSet = true;
248                 }
249             }
250             i++;
251         }
252         if (!emailBoList.isEmpty() && !isDefaultSet) {
253             valid = false;
254         }
255         return valid;
256     }
257 
258     public boolean CheckBarcodeAndLostBarcode(OlePatronDocument olePatronDocument) {
259         boolean barcodeFlag = false;
260         List<OlePatronLostBarcode> lostBarcodeList = olePatronDocument.getLostBarcodes();
261         if(lostBarcodeList.size() > 0) {
262             for (OlePatronLostBarcode lostBarcode : lostBarcodeList) {
263                 if( !lostBarcode.getInvalidOrLostBarcodeNumber().equals(olePatronDocument.getBarcode())) {
264                      barcodeFlag = true;
265                 }
266             }
267         }
268         return barcodeFlag;
269     }
270 
271     public boolean isBorrowerTypeActive(OlePatronDocument olePatronDocument) {
272         boolean isPatronTypeActive = false;
273         HashMap<String, String> map = new HashMap<String, String>();
274         map.put(OLEConstants.BORROWER_TYPE_ID, olePatronDocument.getBorrowerType());
275         List<OleBorrowerType> borrowerTypes = (List<OleBorrowerType>) getBusinessObjectService().findMatching(OleBorrowerType.class, map);
276         if (borrowerTypes.size() > 0) {
277             if(borrowerTypes.get(0).isActive()) {
278                 isPatronTypeActive = true;
279             }
280         }
281         return isPatronTypeActive;
282     }
283 
284         /**
285          * Gets the instance of BusinessObjectService
286          * @return businessObjectService(BusinessObjectService)
287          */
288     protected BusinessObjectService getBusinessObjectService() {
289         if (businessObjectService == null) {
290             businessObjectService = KRADServiceLocator.getBusinessObjectService();
291         }
292         return businessObjectService;
293     }
294 
295 
296 }