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.api.OlePatronDefinition;
6   import org.kuali.ole.patron.api.OlePatronNotesDefinition;
7   import org.kuali.ole.patron.api.OlePatronQueryResults;
8   import org.kuali.ole.patron.bo.OleBorrowerType;
9   import org.kuali.ole.patron.bo.OlePatronDocument;
10  import org.kuali.ole.patron.bo.OlePatronNotes;
11  import org.kuali.rice.core.api.criteria.CriteriaLookupService;
12  import org.kuali.rice.core.api.criteria.GenericQueryResults;
13  import org.kuali.rice.core.api.criteria.QueryByCriteria;
14  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
15  import org.kuali.rice.kim.api.KimConstants;
16  import org.kuali.rice.kim.api.identity.IdentityService;
17  import org.kuali.rice.kim.api.identity.address.EntityAddress;
18  import org.kuali.rice.kim.api.identity.email.EntityEmail;
19  import org.kuali.rice.kim.api.identity.entity.Entity;
20  import org.kuali.rice.kim.api.identity.name.EntityName;
21  import org.kuali.rice.kim.api.identity.phone.EntityPhone;
22  import org.kuali.rice.kim.api.identity.type.EntityTypeContactInfo;
23  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
24  import org.kuali.rice.kim.impl.KIMPropertyConstants;
25  import org.kuali.rice.kim.impl.identity.address.EntityAddressBo;
26  import org.kuali.rice.kim.impl.identity.email.EntityEmailBo;
27  import org.kuali.rice.kim.impl.identity.entity.EntityBo;
28  import org.kuali.rice.kim.impl.identity.name.EntityNameBo;
29  import org.kuali.rice.kim.impl.identity.phone.EntityPhoneBo;
30  import org.kuali.rice.kim.impl.identity.type.EntityTypeContactInfoBo;
31  import org.kuali.rice.krad.service.BusinessObjectService;
32  import org.kuali.rice.krad.service.KRADServiceLocator;
33  
34  import java.util.*;
35  
36  /**
37   * Created by IntelliJ IDEA.
38   * User: pvsubrah
39   * Date: 5/9/12
40   * Time: 2:35 PM
41   * To change this template use File | Settings | File Templates.
42   */
43  public class OlePatronServiceImpl implements OlePatronService {
44      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OlePatronServiceImpl.class);
45      private BusinessObjectService businessObjectService;
46      private IdentityService identityService;
47      private CriteriaLookupService criteriaLookupService;
48  
49      protected BusinessObjectService getBusinessObjectService() {
50          if (businessObjectService == null) {
51              businessObjectService = KRADServiceLocator.getBusinessObjectService();
52          }
53          return businessObjectService;
54      }
55  
56      protected IdentityService getIdentityService() {
57          if (identityService == null) {
58              identityService = KimApiServiceLocator.getIdentityService();
59          }
60          return identityService;
61      }
62  
63      protected CriteriaLookupService getCriteriaLookupService() {
64          if(criteriaLookupService == null) {
65              criteriaLookupService = GlobalResourceLoader.getService("criteriaLookupService");
66          }
67          return criteriaLookupService;
68      }
69      @Override
70      public OlePatronDefinition getPatron(String patronId) {
71          LOG.debug("Inside the getPatron method");
72          Map<String, Object> criteria = new HashMap<String, Object>(4);
73          criteria.put(OLEConstants.OlePatron.PATRON_ID, patronId);
74  
75          return OlePatronDocument.to(getBusinessObjectService().findByPrimaryKey(OlePatronDocument.class, criteria));
76      }
77  
78      @Override
79      public OlePatronDefinition createPatron(OlePatronDefinition olePatron) {
80          BusinessObjectService businessObjectService = KRADServiceLocator.getBusinessObjectService();
81          OlePatronDocument olePatronDocument = OlePatronDocument.from(olePatron);
82          OlePatronDocument savedPatronDocument = businessObjectService.save(olePatronDocument);
83          OlePatronDefinition savedOlePatronDefinition = OlePatronDocument.to(savedPatronDocument);
84          return savedOlePatronDefinition;
85      }
86  
87      @Override
88      public OlePatronDefinition updatePatron(OlePatronDefinition olePatron) {
89          LOG.debug("Inside the updatePatron method");
90          boolean doc = false;
91          String documentNumber = "";
92          OlePatronDocument updatedPatronDocument = null;
93  
94          //if (olePatron.getOlePatronId() != null && olePatron.getEntityId() != null) {
95          if (olePatron.getOlePatronId() != null) {
96              OlePatronDocument newPatronBo = OlePatronDocument.from(olePatron);
97              Map<String, Object> criteria = new HashMap<String, Object>(4);
98              criteria.put(OLEConstants.OlePatron.PATRON_ID,olePatron.getOlePatronId());
99  
100             OlePatronDocument olePatronBo = getBusinessObjectService().findByPrimaryKey(OlePatronDocument.class, criteria);
101             if (olePatronBo != null) {
102                 HashMap<String, String> map = new HashMap<String, String>();
103                 map.put("borrowerTypeId", newPatronBo.getBorrowerType());
104                 List<OleBorrowerType> borrowerTypes = (List<OleBorrowerType>) getBusinessObjectService().findMatching(OleBorrowerType.class, map);
105                 if (borrowerTypes.size() > 0) {
106                     newPatronBo.setOleBorrowerType(borrowerTypes.get(0));
107                 }
108                 EntityBo kimEntity = olePatronBo.getEntity();
109                 List<EntityTypeContactInfoBo> entityTypeContactInfoBoList = kimEntity.getEntityTypeContactInfos();
110                 kimEntity.setNames(Arrays.asList(newPatronBo.getName()));
111                 entityTypeContactInfoBoList.get(0).setAddresses(newPatronBo.getAddresses());
112                 entityTypeContactInfoBoList.get(0).setEmailAddresses(newPatronBo.getEmails());
113                 entityTypeContactInfoBoList.get(0).setPhoneNumbers(newPatronBo.getPhones());
114                 kimEntity.setEntityTypeContactInfos(entityTypeContactInfoBoList);
115                 newPatronBo.setEntity(kimEntity);
116                 getBusinessObjectService().delete(olePatronBo);
117                 updatedPatronDocument = getBusinessObjectService().save(newPatronBo);
118             }
119         }
120         LOG.debug("Leaving the updatePatron method");
121         return OlePatronDocument.to(updatedPatronDocument);
122     }
123 
124     private void addName(EntityNameBo name, EntityBo entity) {
125         LOG.debug("Inside the addName method");
126         List<EntityNameBo> entityName = entity.getNames();
127         if (name != null) {
128             //name.setEntityId(entity.getId());
129             entityName.add(name);
130             entity.setNames(entityName);
131         }
132     }
133 
134     @Override
135     public void addNameToEntity(EntityName name, Entity entity) {
136         LOG.debug("Inside the addNameToEntity method");
137         EntityBo entityBo = EntityBo.from(entity);
138         EntityNameBo nameBo = EntityNameBo.from(name);
139         addName(nameBo, entityBo);
140     }
141 
142     @Override
143     public void addEmailToEntity(List<EntityEmail> emails, EntityTypeContactInfo entityTypeContactInfo) {
144         LOG.debug("Inside the addEmailToEntity method");
145         List<EntityEmailBo> emailBos = new ArrayList<EntityEmailBo>();
146         for (EntityEmail email : emails) {
147             emailBos.add(EntityEmailBo.from(email));
148         }
149         addEmail(emailBos, EntityTypeContactInfoBo.from(entityTypeContactInfo));
150     }
151 
152     private void addEmail(List<EntityEmailBo> emails, EntityTypeContactInfoBo entityTypeContactInfoBo) {
153         LOG.debug("Inside the addEmail method");
154         if (emails != null) {
155             entityTypeContactInfoBo.setEmailAddresses(emails);
156         }
157     }
158 
159     private void addAddress(List<EntityAddressBo> entityAddress, EntityTypeContactInfoBo entityTypeContactInfoBo) {
160         LOG.debug("Inside the addAddress method");
161         if (entityAddress != null) {
162             entityTypeContactInfoBo.setAddresses(entityAddress);
163         }
164     }
165 
166     @Override
167     public void addAddressToEntity(List<EntityAddress> entityAddress, EntityTypeContactInfo entityTypeContactInfo) {
168         List<EntityAddressBo> addrBos = new ArrayList<EntityAddressBo>();
169         for (EntityAddress addr : entityAddress) {
170             addrBos.add(EntityAddressBo.from(addr));
171         }
172         addAddress(addrBos, EntityTypeContactInfoBo.from(entityTypeContactInfo));
173     }
174 
175     public void addPhone(List<EntityPhoneBo> entityPhone, EntityTypeContactInfoBo entityTypeContactInfoBo) {
176         LOG.debug("Inside the addPhoneToEntity method");
177         if (entityPhone != null) {
178             entityTypeContactInfoBo.setPhoneNumbers(entityPhone);
179         }
180     }
181 
182     @Override
183     public void addPhoneToEntity(List<EntityPhone> entityPhone, EntityTypeContactInfo entityTypeContactInfo) {
184         LOG.debug("Inside the addPhoneToEntity method");
185         List<EntityPhoneBo> phoneBos = new ArrayList<EntityPhoneBo>();
186         for (EntityPhone phone : entityPhone) {
187             phoneBos.add(EntityPhoneBo.from(phone));
188         }
189         addPhone(phoneBos, EntityTypeContactInfoBo.from(entityTypeContactInfo));
190     }
191 
192     private EntityBo getEntityBo(String entityId) {
193         LOG.debug("Inside the getEntityBo method");
194         EntityBo entityImpl = getBusinessObjectService().findBySinglePrimaryKey(EntityBo.class, entityId);
195         if (entityImpl != null && entityImpl.getEntityTypeContactInfos() != null) {
196             for (EntityTypeContactInfoBo et : entityImpl.getEntityTypeContactInfos()) {
197                 et.refresh();
198             }
199         }
200         return entityImpl;
201     }
202 
203     /*public OlePatronDefinition setEntityFromPatron(OlePatronDefinition patron) {
204         LOG.debug("Inside the setEntityFromPatron method");
205         OlePatronDocument patronBo = OlePatronDocument.from(patron);
206         EntityBo kimEntity;
207         List<EntityTypeContactInfoBo> entityTypes;
208         EntityTypeContactInfoBo entityType;
209         if (patronBo.getEntity() == null) {
210             kimEntity = new EntityBo();
211             entityTypes = new ArrayList<EntityTypeContactInfoBo>();
212             entityType = new EntityTypeContactInfoBo();
213         } else {
214             kimEntity = patronBo.getEntity();
215             entityTypes = kimEntity.getEntityTypeContactInfos();
216             if (!entityTypes.isEmpty()) {
217                 entityType = entityTypes.get(0);
218             } else
219                 entityType = new EntityTypeContactInfoBo();
220         }
221         if (patronBo.getEntityId() == null || patronBo.getEntityId().isEmpty()) {
222             String entityId = "";
223             entityId = KRADServiceLocator.getSequenceAccessorService().getNextAvailableSequenceNumber("KRIM_ENTITY_ID_S").toString();
224             patronBo.setEntityId(entityId);
225             kimEntity.setId(patronBo.getEntityId());
226         } else {
227             kimEntity.setId(patronBo.getEntityId());
228         }
229         kimEntity.setActive(true);
230 
231 
232         entityType.setEntityId(patronBo.getEntityId());
233         entityType.setEntityTypeCode(KimConstants.EntityTypes.PERSON);
234         entityType.setActive(true);
235 
236         kimEntity.setNames(Arrays.asList(patronBo.getName()));
237 
238         entityType.setAddresses(patronBo.getAddresses());
239         entityType.setEmailAddresses(patronBo.getEmails());
240         entityType.setPhoneNumbers(patronBo.getPhones());
241         entityTypes.add(entityType);
242 
243         kimEntity.setEntityTypeContactInfos(entityTypes);
244         getBusinessObjectService().save(kimEntity);
245         patronBo.setEntity(kimEntity);
246 
247         LOG.debug("Leaving the setEntityFromPatron method");
248 
249         return OlePatronDocument.to(patronBo);
250     }
251 */
252     @Override
253     public OlePatronDefinition inactivatePatron(String patronId) {
254         LOG.debug("Inside the inactivatePatron method");
255         OlePatronDocument patronDocument = OlePatronDocument.from(getPatron(patronId));
256         patronDocument.setActiveIndicator(false);
257         return (OlePatronDocument.to(patronDocument));
258     }
259 
260     @Override
261     public EntityName updateName(EntityName name) {
262         LOG.debug("Inside the updateName method");
263         EntityNameBo entityName = EntityNameBo.from(getIdentityService().updateName(name));
264         return EntityNameBo.to(entityName);
265     }
266 
267     @Override
268     public boolean inactivateName(String nameId) {
269         LOG.debug("Inside the inactivateName method");
270         EntityNameBo entityName = EntityNameBo.from(getIdentityService().inactivateName(nameId));
271         if (entityName != null) {
272             return true;
273         }
274         return false;
275     }
276 
277     @Override
278     public boolean updateEmail(EntityEmail entityEmail) {
279         LOG.debug("Inside the updateEmail method");
280         EntityEmailBo email = EntityEmailBo.from(getIdentityService().updateEmail(entityEmail));
281         if (email != null) {
282             return true;
283         }
284         return false;
285     }
286 
287     @Override
288     public boolean inactivateEmail(String emailId) {
289         LOG.debug("Inside the inactivateEmail method");
290         EntityEmailBo entityEmail = EntityEmailBo.from(getIdentityService().inactivateEmail(emailId));
291         if (entityEmail != null) {
292             return true;
293         }
294         return false;
295     }
296 
297     @Override
298     public boolean updateAddress(EntityAddress entityAddress) {
299         LOG.debug("Inside the updateAddress method");
300         EntityAddressBo address = EntityAddressBo.from(getIdentityService().updateAddress(entityAddress));
301         if (address != null) {
302             return true;
303         }
304         return false;
305     }
306 
307     @Override
308     public boolean inactivateAddress(String addressId) {
309         LOG.debug("Inside the inactivateAddress method");
310         EntityAddressBo entityAddress = EntityAddressBo.from(getIdentityService().inactivateAddress(addressId));
311         if (entityAddress != null) {
312             return true;
313         }
314         return false;
315     }
316 
317     @Override
318     public boolean updatePhone(EntityPhone entityPhone) {
319         LOG.debug("Inside the updatePhone method");
320         EntityPhoneBo phone = EntityPhoneBo.from(getIdentityService().updatePhone(entityPhone));
321         if (phone != null) {
322             return true;
323         }
324         return false;
325     }
326 
327     @Override
328     public boolean inactivatePhone(String phoneId) {
329         LOG.debug("Inside the inactivatePhone method");
330         EntityPhoneBo entityPhone = EntityPhoneBo.from(getIdentityService().inactivatePhone(phoneId));
331         if (entityPhone != null) {
332             return true;
333         }
334         return false;
335     }
336 
337     @Override
338     public boolean addNoteToPatron(OlePatronNotesDefinition patronNote) {
339         LOG.debug("Inside the addNoteToPatron method");
340         OlePatronNotes patronNoteBo = OlePatronNotes.from(patronNote);
341         if (patronNoteBo.getOlePatronId() != null && patronNoteBo.getOlePatronNoteType() != null) {
342             getBusinessObjectService().save(patronNoteBo);
343             return true;
344         }
345         return false;
346     }
347 
348     @Override
349     public boolean updateNote(OlePatronNotesDefinition patronNote) {
350         LOG.debug("Inside the updateNote method");
351         OlePatronNotes patronNoteBo = OlePatronNotes.from(patronNote);
352         if (patronNoteBo.getOlePatronId() != null && patronNoteBo.getPatronNoteId() != null) {
353             Map<String, Object> criteria = new HashMap<String, Object>();
354             criteria.put("patronNoteId", patronNote.getPatronNoteId());
355             if (getBusinessObjectService().findByPrimaryKey(OlePatronNotes.class, criteria) != null) {
356                 getBusinessObjectService().save(patronNoteBo);
357                 return true;
358             }
359         }
360         return false;
361     }
362 
363     @Override
364     public boolean inactivateNote(String patronNoteId) {
365         LOG.debug("Inside the inactivateNote method");
366         if (patronNoteId != null) {
367             Map<String, Object> criteria = new HashMap<String, Object>();
368             criteria.put("patronNoteId", patronNoteId);
369             OlePatronNotes patronNotes = getBusinessObjectService().findByPrimaryKey(OlePatronNotes.class, criteria);
370             patronNotes.setActive(false);
371             getBusinessObjectService().save(patronNotes);
372             return true;
373         }
374         return false;
375     }
376 
377     @Override
378     public OlePatronQueryResults getPatrons() {
379         LOG.debug("Inside the findPatron method");
380         GenericQueryResults<OlePatronDocument> results = getCriteriaLookupService().lookup(OlePatronDocument.class, QueryByCriteria.Builder.create().build());
381         OlePatronQueryResults.Builder builder = OlePatronQueryResults.Builder.create();
382         builder.setMoreResultsAvailable(results.isMoreResultsAvailable());
383         builder.setTotalRowCount(results.getTotalRowCount());
384 
385         final List<OlePatronDefinition.Builder> ims = new ArrayList<OlePatronDefinition.Builder>();
386         for (OlePatronDocument bo : results.getResults()) {
387             ims.add(OlePatronDefinition.Builder.create(bo));
388         }
389 
390         builder.setResults(ims);
391         return builder.build();
392 
393     }
394     @Override
395     public OlePatronQueryResults findPatron(QueryByCriteria queryCriteria) {
396         LOG.debug("Inside the findPatron method");
397         GenericQueryResults<OlePatronDocument> results = getCriteriaLookupService().lookup(OlePatronDocument.class, QueryByCriteria.Builder.create().build());
398         OlePatronQueryResults.Builder builder = OlePatronQueryResults.Builder.create();
399         builder.setMoreResultsAvailable(results.isMoreResultsAvailable());
400         builder.setTotalRowCount(results.getTotalRowCount());
401 
402         final List<OlePatronDefinition.Builder> ims = new ArrayList<OlePatronDefinition.Builder>();
403         for (OlePatronDocument bo : results.getResults()) {
404             ims.add(OlePatronDefinition.Builder.create(bo));
405         }
406 
407         builder.setResults(ims);
408         return builder.build();
409     }
410 
411 
412 }