001 package org.kuali.ole.service;
002
003 import org.apache.commons.lang.StringUtils;
004 import org.kuali.ole.OLEConstants;
005 import org.kuali.ole.patron.bill.PatronBillPayment;
006 import org.kuali.ole.patron.bo.*;
007 import org.kuali.rice.kim.impl.KIMPropertyConstants;
008 import org.kuali.rice.kim.impl.identity.address.EntityAddressBo;
009 import org.kuali.rice.kim.impl.identity.email.EntityEmailBo;
010 import org.kuali.rice.kim.impl.identity.employment.EntityEmploymentBo;
011 import org.kuali.rice.kim.impl.identity.phone.EntityPhoneBo;
012 import org.kuali.rice.krad.lookup.LookupableImpl;
013 import org.kuali.rice.krad.service.BusinessObjectService;
014 import org.kuali.rice.krad.service.KRADServiceLocator;
015
016 import java.text.SimpleDateFormat;
017 import java.util.*;
018
019 /**
020 * OlePatronHelperServiceImpl converts PatronProperties to EntityProperties and generate new search criteria.
021 */
022 public class OlePatronHelperServiceImpl extends LookupableImpl implements OlePatronHelperService {
023 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OlePatronHelperServiceImpl.class);
024
025 protected static final String ENTITY_EMAIL_PROPERTY_PREFIX = "entity.entityTypeContactInfos.emailAddresses.";
026 protected static final String ENTITY_NAME_PROPERTY_PREFIX = "entity.names.";
027 private BusinessObjectService businessObjectService;
028 protected Map<String, String> criteriaConversion = new HashMap<String, String>();
029
030 {
031 criteriaConversion.put(KIMPropertyConstants.Person.FIRST_NAME, "entity.names.firstName");
032 criteriaConversion.put(KIMPropertyConstants.Person.LAST_NAME, "entity.names.lastName");
033 criteriaConversion.put(KIMPropertyConstants.Person.EMAIL_ADDRESS, "entity.entityTypeContactInfos.emailAddresses.emailAddress");
034 }
035
036 /**
037 * Populate search criteria from the patron document
038 * @param criteria
039 * @return newCriteria
040 */
041 @Override
042 public Map<String, String> convertPatronPropertiesToEntityProperties(Map<String, String> criteria) {
043 LOG.debug("Inside the convertPatronPropertiesToEntityProperties method");
044 boolean nameCriteria = false;
045 boolean emailCriteria = false;
046
047 Map<String, String> newCriteria = new HashMap<String, String>();
048 newCriteria.put("entity.entityTypeContactInfos.entityTypeCode","PERSON|SYSTEM");
049 if (criteria != null) {
050 for (String key : criteria.keySet()) {
051 if (key.equals(OLEConstants.PATRON_ENTITY_ACTIVE)) {
052 newCriteria.put(OLEConstants.PATRON_ENTITY_ACTIVE, criteria.get(OLEConstants.PATRON_ENTITY_ACTIVE));
053 } else {
054 if (!(criteria.containsKey(OLEConstants.PATRON_ENTITY_ACTIVE))) {
055 newCriteria.remove(OLEConstants.PATRON_ENTITY_ACTIVE);
056 }
057 }
058
059 if (StringUtils.isEmpty(criteria.get(key))) {
060 continue;
061 }
062
063 String entityProperty = criteriaConversion.get(key);
064 if (entityProperty != null) {
065 newCriteria.put(entityProperty, criteria.get(key));
066 } else {
067 entityProperty = key;
068 newCriteria.put(key, criteria.get(key));
069 }
070 if (isNameEntityCriteria(entityProperty)) {
071 nameCriteria = true;
072 }
073 if (isEmailEntityCriteria(entityProperty)) {
074 emailCriteria = true;
075 }
076
077 if (nameCriteria) {
078 newCriteria.put(ENTITY_NAME_PROPERTY_PREFIX + "active", "Y");
079 }
080 if (emailCriteria) {
081 Map<String, String> email = new HashMap<String, String>();
082 email.put("emailAddress",criteria.get("emailAddress"));
083 List<String> entityIds = new ArrayList<String>();
084 List<EntityEmailBo> entityEmails = (List<EntityEmailBo>) getLookupService()
085 .findCollectionBySearchHelper(EntityEmailBo.class, email,true);
086 StringBuilder builder = new StringBuilder("-");
087 if(entityEmails.size()>0) {
088 builder.setLength(0);
089 for(EntityEmailBo entityEmail : entityEmails) {
090 builder.append(entityEmail.getEntityId()+"|");
091 entityIds.add(entityEmail.getEntityId());
092 }
093 int index = builder.lastIndexOf("|");
094 if(index>0)
095 builder.replace(index, index + 1, "");
096 newCriteria.remove("emailAddress");
097 }
098 if(criteria.containsKey("olePatronId")) {
099 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 }