View Javadoc
1   package org.kuali.ole.deliver.service;
2   
3   import org.apache.commons.lang3.StringUtils;
4   import org.apache.log4j.Logger;
5   import org.kuali.asr.service.ASRHelperServiceImpl;
6   import org.kuali.ole.OLEConstants;
7   import org.kuali.ole.OLEParameterConstants;
8   import org.kuali.ole.deliver.batch.OleMailer;
9   import org.kuali.ole.deliver.bo.*;
10  import org.kuali.ole.deliver.notice.util.NoticeUtil;
11  import org.kuali.ole.deliver.processor.LoanProcessor;
12  import org.kuali.ole.describe.bo.OleInstanceItemType;
13  import org.kuali.ole.describe.keyvalue.LocationValuesBuilder;
14  import org.kuali.ole.docstore.common.client.DocstoreClientLocator;
15  import org.kuali.ole.docstore.common.document.ItemOleml;
16  import org.kuali.ole.docstore.common.document.content.instance.Item;
17  import org.kuali.ole.docstore.common.search.SearchResponse;
18  import org.kuali.ole.docstore.common.search.SearchResult;
19  import org.kuali.ole.docstore.common.search.SearchResultField;
20  import org.kuali.ole.sys.context.SpringContext;
21  import org.kuali.ole.util.DocstoreUtil;
22  import org.kuali.rice.core.api.config.property.ConfigContext;
23  import org.kuali.rice.core.api.mail.EmailBody;
24  import org.kuali.rice.core.api.mail.EmailFrom;
25  import org.kuali.rice.core.api.mail.EmailSubject;
26  import org.kuali.rice.core.api.mail.EmailTo;
27  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
28  import org.kuali.rice.kim.impl.identity.type.EntityTypeContactInfoBo;
29  import org.kuali.rice.krad.service.BusinessObjectService;
30  import org.kuali.rice.krad.service.KRADServiceLocator;
31  import org.kuali.rice.krad.util.GlobalVariables;
32  import org.kuali.rice.krad.util.KRADConstants;
33  
34  import javax.sql.rowset.serial.SerialBlob;
35  import java.sql.Blob;
36  import java.sql.Timestamp;
37  import java.util.*;
38  
39  /**
40   * Created by pvsubrah on 4/8/15.
41   */
42  public abstract class NoticesExecutor implements Runnable {
43  
44      private static final Logger LOG = Logger.getLogger(NoticesExecutor.class);
45      private BusinessObjectService businessObjectService;
46      private ParameterValueResolver parameterResolverInstance;
47      private OleMailer oleMailer;
48      private CircDeskLocationResolver circDeskLocationResolver;
49      private DocstoreClientLocator docstoreClientLocator;
50      private DocstoreUtil docstoreUtil;
51      private NoticeUtil noticeUtil;
52  
53      public DocstoreClientLocator getDocstoreClientLocator() {
54  
55          if (docstoreClientLocator == null) {
56              docstoreClientLocator = (DocstoreClientLocator) SpringContext.getService("docstoreClientLocator");
57  
58          }
59          return docstoreClientLocator;
60      }
61  
62  
63      public DocstoreUtil getDocstoreUtil() {
64          if (docstoreUtil == null) {
65              docstoreUtil = (DocstoreUtil)SpringContext.getService("docstoreUtil");
66          }
67          return docstoreUtil;
68      }
69      public ParameterValueResolver getParameterResolverInstance() {
70          if (null == parameterResolverInstance) {
71              parameterResolverInstance = ParameterValueResolver.getInstance();
72          }
73          return parameterResolverInstance;
74      }
75  
76  
77  
78      public CircDeskLocationResolver getCircDeskLocationResolver() {
79          if (null == circDeskLocationResolver) {
80              circDeskLocationResolver = new CircDeskLocationResolver();
81          }
82          return circDeskLocationResolver;
83      }
84  
85      public OleMailer getOleMailer() {
86          if (null == oleMailer) {
87              oleMailer = GlobalResourceLoader.getService("oleMailer");
88          }
89          return oleMailer;
90      }
91  
92      public void setBusinessObjectService(BusinessObjectService businessObjectService) {
93          this.businessObjectService = businessObjectService;
94      }
95  
96      public BusinessObjectService getBusinessObjectService(){
97          return KRADServiceLocator.getBusinessObjectService();
98      }
99  
100     public void setOleMailer(OleMailer oleMailer) {
101         this.oleMailer = oleMailer;
102     }
103 
104     public NoticeUtil getNoticeUtil() {
105         if(noticeUtil == null){
106             noticeUtil = new NoticeUtil();
107         }
108         return noticeUtil;
109     }
110 
111     public void setNoticeUtil(NoticeUtil noticeUtil) {
112         this.noticeUtil = noticeUtil;
113     }
114 
115 
116 
117     @Override
118     public void run() {
119 
120     }
121 
122     public abstract  void sendMail(String mailContent);
123 
124     public void deleteNotices(List<OLEDeliverNotice> oleDeliverNotices) {
125         getBusinessObjectService().delete(oleDeliverNotices);
126     }
127 
128     public void saveOLEDeliverNoticeHistory(List<OLEDeliverNotice> oleDeliverNotices, String mailContent) {
129         List<OLEDeliverNoticeHistory> oleDeliverNoticeHistoryList = new ArrayList<OLEDeliverNoticeHistory>();
130         for (OLEDeliverNotice oleDeliverNotice : oleDeliverNotices) {
131             OLEDeliverNoticeHistory oleDeliverNoticeHistory = new OLEDeliverNoticeHistory();
132             oleDeliverNoticeHistory.setLoanId(oleDeliverNotice.getLoanId());
133             oleDeliverNoticeHistory.setNoticeType(oleDeliverNotice.getNoticeType());
134             oleDeliverNoticeHistory.setNoticeSentDate(new Timestamp(new Date().getTime()));
135             oleDeliverNoticeHistory.setPatronId(oleDeliverNotice.getPatronId());
136             oleDeliverNoticeHistory.setNoticeSendType(oleDeliverNotice.getNoticeSendType());
137             oleDeliverNoticeHistory.setNoticeContent(mailContent.getBytes());
138             oleDeliverNoticeHistory.setRequestId(oleDeliverNotice.getRequestId());
139             oleDeliverNoticeHistoryList.add(oleDeliverNoticeHistory);
140         }
141         getBusinessObjectService().save(oleDeliverNoticeHistoryList);
142     }
143 
144     public String getPatronHomeEmailId(EntityTypeContactInfoBo entityTypeContactInfoBo) throws Exception {
145         String emailId = "";
146         if (entityTypeContactInfoBo.getEmailAddresses() != null) {
147             for (int j = 0; j < entityTypeContactInfoBo.getEmailAddresses().size(); j++) {
148                 if (entityTypeContactInfoBo.getEmailAddresses().get(j).getDefaultValue()) {
149                     emailId = (entityTypeContactInfoBo.getEmailAddresses().get(j).getEmailAddress());
150                     break;
151                 }
152             }
153         }
154         return emailId;
155     }
156 
157     public String sendMailsToPatron(String emailAddress, String noticeContent, String itemLocation) {
158         String fromAddress = getCircDeskLocationResolver().getReplyToEmail(itemLocation);
159 
160         if (fromAddress == null) {
161             fromAddress = getParameterResolverInstance().getParameter(OLEConstants.APPL_ID, OLEConstants
162                     .DLVR_NMSPC, OLEConstants.DLVR_CMPNT, OLEParameterConstants
163                     .NOTICE_FROM_MAIL);
164         }
165         try {
166             if (fromAddress != null && (fromAddress.equals("") || fromAddress.trim().isEmpty())) {
167                 fromAddress = OLEConstants.KUALI_MAIL;
168             }
169             if (emailAddress != null && !emailAddress.isEmpty()) {
170                 noticeContent = noticeContent.replace('[', ' ');
171                 noticeContent = noticeContent.replace(']', ' ');
172                 if (!noticeContent.trim().equals("")) {
173                     OleMailer oleMailer = getOleMailer();
174                     oleMailer.sendEmail(new EmailFrom(fromAddress), new EmailTo(emailAddress), new EmailSubject(OLEConstants.NOTICE_MAIL), new EmailBody(noticeContent), true);
175                 }
176             } else {
177             }
178         } catch (Exception e) {
179         }
180 
181         return noticeContent;
182     }
183 
184 
185 
186     public String getItemTypeCodeByName(String itemTypeName) {
187         String itemTypeCode = "";
188         List<OleInstanceItemType> instanceItemTypeList = null;
189         Map<String, String> instanceItemTypeMap = new HashMap<String, String>();
190         instanceItemTypeMap.put("instanceItemTypeName", itemTypeName);
191         instanceItemTypeList = (List<OleInstanceItemType>) getBusinessObjectService().findMatching(OleInstanceItemType.class, instanceItemTypeMap);
192         if (instanceItemTypeList != null && instanceItemTypeList.size() > 0) {
193             itemTypeCode = instanceItemTypeList.get(0).getInstanceItemTypeCode();
194         }
195         return itemTypeCode;
196     }
197 
198     public Timestamp getSendToDate(String noticeToDate) {
199         String lostNoticeToDate;
200         lostNoticeToDate = getParameterResolverInstance().getParameter(OLEConstants
201                 .APPL_ID_OLE, OLEConstants.DLVR_NMSPC, OLEConstants.DLVR_CMPNT, noticeToDate);
202         Timestamp lostNoticetoSendDate = new Timestamp(System.currentTimeMillis());
203         if (!StringUtils.isEmpty(lostNoticeToDate)) {
204             lostNoticetoSendDate = new Timestamp(new Date(lostNoticeToDate).getTime());
205         }
206         return lostNoticetoSendDate;
207     }
208 
209 
210     public boolean setItemInformations(OleDeliverRequestBo oleDeliverRequestBo) {
211         ASRHelperServiceImpl asrHelperService = new ASRHelperServiceImpl();
212         LOG.info("Inside isItemAvailableInDocStore");
213         boolean available = false;
214         Map<String, String> itemMap = new HashMap<String, String>();
215         LocationValuesBuilder locationValuesBuilder = new LocationValuesBuilder();
216         String holdingsId = "";
217         String bibTitle="";
218         String bibAuthor="";
219         try {
220             try {
221                 org.kuali.ole.docstore.common.document.Item item = new ItemOleml();
222                 org.kuali.ole.docstore.common.search.SearchParams search_Params = new org.kuali.ole.docstore.common.search.SearchParams();
223                 SearchResponse searchResponse = null;
224                 search_Params.getSearchConditions().add(search_Params.buildSearchCondition("phrase", search_Params.buildSearchField(org.kuali.ole.docstore.common.document.content.enums.DocType.ITEM.getCode(), item.ITEM_BARCODE, oleDeliverRequestBo.getItemId()), ""));
225                 search_Params.getSearchResultFields().add(search_Params.buildSearchResultField(org.kuali.ole.docstore.common.document.content.enums.DocType.ITEM.getCode(), "id"));
226                 search_Params.getSearchResultFields().add(search_Params.buildSearchResultField(org.kuali.ole.docstore.common.document.content.enums.DocType.ITEM.getCode(), "holdingsIdentifier"));
227                 search_Params.getSearchResultFields().add(search_Params.buildSearchResultField(org.kuali.ole.docstore.common.document.content.enums.DocType.ITEM.getCode(), "Title_display"));
228                 search_Params.getSearchResultFields().add(search_Params.buildSearchResultField(org.kuali.ole.docstore.common.document.content.enums.DocType.ITEM.getCode(), "Author_display"));
229                 searchResponse = getDocstoreClientLocator().getDocstoreClient().search(search_Params);
230                 for (SearchResult searchResult : searchResponse.getSearchResults()) {
231                     for (SearchResultField searchResultField : searchResult.getSearchResultFields()) {
232                         String fieldName = searchResultField.getFieldName();
233                         String fieldValue = searchResultField.getFieldValue() != null ? searchResultField.getFieldValue() : "";
234                         if (fieldName.equalsIgnoreCase("holdingsIdentifier") && !fieldValue.isEmpty() && searchResultField.getDocType().equalsIgnoreCase(org.kuali.ole.docstore.common.document.content.enums.DocType.ITEM.getCode())) {
235                             holdingsId = fieldValue;
236                         } else if (searchResultField.getFieldName().equalsIgnoreCase("Title_display") &&!fieldValue.isEmpty()) {
237                             bibTitle = searchResultField.getFieldValue();
238                         } else if (searchResultField.getFieldName().equalsIgnoreCase("Author_display") &&!fieldValue.isEmpty()) {
239                             bibAuthor = searchResultField.getFieldValue();
240                         } else  if (searchResultField.getFieldName().equalsIgnoreCase("id") &&!fieldValue.isEmpty()){
241                             oleDeliverRequestBo.setItemUuid(fieldValue);
242                         }
243                     }
244                 }
245             } catch (Exception ex) {
246                 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, "Item Exists");
247                 LOG.error(OLEConstants.ITEM_EXIST + ex);
248             }
249             OleItemSearch itemSearchList = getDocstoreUtil().getOleItemSearchList(oleDeliverRequestBo.getItemUuid());
250             if (asrHelperService.isAnASRItem(itemSearchList.getShelvingLocation())) {
251                 oleDeliverRequestBo.setAsrFlag(true);
252             } else {
253                 oleDeliverRequestBo.setAsrFlag(false);
254             }
255             if (itemSearchList != null) {
256                 oleDeliverRequestBo.setTitle(itemSearchList.getTitle());
257                 oleDeliverRequestBo.setAuthor(itemSearchList.getAuthor());
258                 oleDeliverRequestBo.setCallNumber(itemSearchList.getCallNumber());
259                 oleDeliverRequestBo.setItemType(itemSearchList.getItemType());
260                 oleDeliverRequestBo.setItemLocation(itemSearchList.getShelvingLocation());
261             }
262             if(org.apache.commons.lang.StringUtils.isNotEmpty(bibTitle)){
263                 oleDeliverRequestBo.setTitle(bibTitle);
264             }
265             if(org.apache.commons.lang.StringUtils.isNotEmpty(bibAuthor)){
266                 oleDeliverRequestBo.setAuthor(bibAuthor);
267             }
268             LoanProcessor loanProcessor = new LoanProcessor();
269             String itemXml = loanProcessor.getItemXML(oleDeliverRequestBo.getItemUuid());
270             Item oleItem = loanProcessor.getItemPojo(itemXml);
271             oleDeliverRequestBo.setOleItem(oleItem);
272             oleDeliverRequestBo.setCopyNumber(oleItem.getCopyNumber());
273             oleDeliverRequestBo.setEnumeration(oleItem.getEnumeration());
274             oleDeliverRequestBo.setChronology(oleItem.getChronology());
275             oleDeliverRequestBo.setItemStatus(oleItem.getItemStatus().getCodeValue());
276             oleDeliverRequestBo.setClaimsReturnedFlag(oleItem.isClaimsReturnedFlag());
277             locationValuesBuilder.getLocation(oleItem, oleDeliverRequestBo, holdingsId);
278             available = true;
279         } catch (Exception e) {
280             LOG.error(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.INVAL_LOC) + e);
281         }
282         return available;
283     }
284 
285 
286 }