View Javadoc
1   package org.kuali.ole.service.impl;
2   
3   import org.apache.commons.lang3.StringUtils;
4   import org.joda.time.DateTime;
5   import org.kuali.ole.OLEConstants;
6   import org.kuali.ole.select.bo.OLEPlatformAdminUrl;
7   import org.kuali.ole.select.bo.OLEPlatformEventLog;
8   import org.kuali.ole.select.bo.OLESearchCondition;
9   import org.kuali.ole.select.document.OLEPlatformRecordDocument;
10  import org.kuali.ole.service.OLEPlatformService;
11  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
12  import org.kuali.rice.kew.api.document.search.DocumentSearchCriteria;
13  import org.kuali.rice.kew.api.document.search.DocumentSearchResult;
14  import org.kuali.rice.kew.api.document.search.DocumentSearchResults;
15  import org.kuali.rice.kew.service.KEWServiceLocator;
16  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
17  import org.kuali.rice.krad.util.GlobalVariables;
18  import org.kuali.rice.krad.util.KRADConstants;
19  
20  import java.text.SimpleDateFormat;
21  import java.util.*;
22  
23  /**
24   * Created by chenchulakshmig on 9/19/14.
25   */
26  public class OLEPlatformServiceImpl implements OLEPlatformService {
27  
28      private static final SimpleDateFormat dateFormat = new SimpleDateFormat(OLEConstants.OLEEResourceRecord.CREATED_DATE_FORMAT);
29  
30      @Override
31      public List<OLEPlatformRecordDocument> performSearch(List<OLESearchCondition> oleSearchConditionsList) {
32          boolean flag = true;
33          Map<String, List<String>> searchCriteriaMap = new HashMap<>();
34          List<OLEPlatformRecordDocument> platformRecordDocumentList = new ArrayList<>();
35          List<OLEPlatformRecordDocument> platformList = new ArrayList<>();
36          DocumentSearchCriteria.Builder docSearchCriteria = DocumentSearchCriteria.Builder.create();
37          docSearchCriteria.setDocumentTypeName(OLEConstants.OLE_PLATFORM_DOC);
38          for (int searchCriteriaCnt = 0; searchCriteriaCnt < oleSearchConditionsList.size(); searchCriteriaCnt++) {
39              if (StringUtils.isNotBlank(oleSearchConditionsList.get(searchCriteriaCnt).getSearchBy()) && StringUtils.isBlank(oleSearchConditionsList.get(searchCriteriaCnt).getSearchCriteria())) {
40                  GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, OLEConstants.SERACH_CRITERIA_REQUIRED);
41              } else if (StringUtils.isBlank(oleSearchConditionsList.get(searchCriteriaCnt).getSearchBy()) && StringUtils.isNotBlank(oleSearchConditionsList.get(searchCriteriaCnt).getSearchCriteria())) {
42                  GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, OLEConstants.SERACH_BY_REQUIRED);
43              } else if (!GlobalVariables.getMessageMap().hasMessages() && StringUtils.isNotBlank(oleSearchConditionsList.get(searchCriteriaCnt).getSearchBy()) && StringUtils.isNotBlank(oleSearchConditionsList.get(searchCriteriaCnt).getSearchCriteria())
44                      && (OLEConstants.OLEEResourceRecord.AND.equals(oleSearchConditionsList.get(searchCriteriaCnt).getOperator()) || OLEConstants.OLEEResourceRecord.OR.equals(oleSearchConditionsList.get(searchCriteriaCnt).getOperator()))) {
45                  flag = false;
46                  if (searchCriteriaCnt != 0 && OLEConstants.OLEEResourceRecord.AND.equals(oleSearchConditionsList.get(searchCriteriaCnt).getOperator())) {
47                      if (!searchCriteriaMap.containsKey(oleSearchConditionsList.get(searchCriteriaCnt).getSearchBy())) {
48                          searchCriteriaMap = getSearchCriteriaMap(oleSearchConditionsList.get(searchCriteriaCnt).getSearchBy(), oleSearchConditionsList.get(searchCriteriaCnt).getSearchCriteria(), searchCriteriaMap);
49                      } else {
50                          searchCriteriaMap.clear();
51                          break;
52                      }
53                  } else {
54                      searchCriteriaMap = getSearchCriteriaMap(oleSearchConditionsList.get(searchCriteriaCnt).getSearchBy(), oleSearchConditionsList.get(searchCriteriaCnt).getSearchCriteria(), searchCriteriaMap);
55                  }
56                  if (searchCriteriaCnt < oleSearchConditionsList.size() - 1) {
57                      if (StringUtils.isNotBlank(oleSearchConditionsList.get(searchCriteriaCnt + 1).getSearchBy()) && !oleSearchConditionsList.get(searchCriteriaCnt + 1).getSearchCriteria().isEmpty()) {
58                          if (OLEConstants.OLEEResourceRecord.AND.equals(oleSearchConditionsList.get(searchCriteriaCnt).getOperator())) {
59                              if (!searchCriteriaMap.containsKey(oleSearchConditionsList.get(searchCriteriaCnt + 1).getSearchBy())) {
60                                  searchCriteriaMap = getSearchCriteriaMap(oleSearchConditionsList.get(searchCriteriaCnt + 1).getSearchBy(), oleSearchConditionsList.get(searchCriteriaCnt + 1).getSearchCriteria(), searchCriteriaMap);
61                                  if (searchCriteriaCnt < oleSearchConditionsList.size() - 2 && oleSearchConditionsList.get(searchCriteriaCnt + 2).getSearchBy() != null && !oleSearchConditionsList.get(searchCriteriaCnt + 2).getSearchCriteria().isEmpty()) {
62                                      if (OLEConstants.OLEEResourceRecord.AND.equals(oleSearchConditionsList.get(searchCriteriaCnt + 1).getOperator())) {
63                                          searchCriteriaCnt++;
64                                      }
65                                  } else {
66                                      if (searchCriteriaMap.size() > 0) {
67                                          platformList = findMatching(searchCriteriaMap, docSearchCriteria);
68                                      }
69                                      break;
70                                  }
71                              } else {
72                                  searchCriteriaMap.clear();
73                                  break;
74                              }
75                          } else if (OLEConstants.OLEEResourceRecord.OR.equals(oleSearchConditionsList.get(searchCriteriaCnt).getOperator())) {
76                              if (searchCriteriaMap.size() > 0) {
77                                  platformRecordDocumentList = findMatching(searchCriteriaMap, docSearchCriteria);
78                                  platformList.addAll(platformRecordDocumentList);
79                                  searchCriteriaMap.clear();
80                              }
81                              if (searchCriteriaCnt == oleSearchConditionsList.size() - 2) {
82                                  searchCriteriaMap = getSearchCriteriaMap(oleSearchConditionsList.get(searchCriteriaCnt + 1).getSearchBy(), oleSearchConditionsList.get(searchCriteriaCnt + 1).getSearchCriteria(), searchCriteriaMap);
83                                  if (searchCriteriaMap.size() > 0) {
84                                      platformRecordDocumentList = findMatching(searchCriteriaMap, docSearchCriteria);
85                                      platformList.addAll(platformRecordDocumentList);
86                                      searchCriteriaMap.clear();
87                                  }
88                                  break;
89                              }
90                              if (OLEConstants.OLEEResourceRecord.AND.equals(oleSearchConditionsList.get(searchCriteriaCnt + 1).getOperator())) {
91                                  searchCriteriaMap = getSearchCriteriaMap(oleSearchConditionsList.get(searchCriteriaCnt + 1).getSearchBy(), oleSearchConditionsList.get(searchCriteriaCnt + 1).getSearchCriteria(), searchCriteriaMap);
92                                  if (searchCriteriaMap.size() > 0) {
93                                      platformRecordDocumentList = findMatching(searchCriteriaMap, docSearchCriteria);
94                                      platformList.addAll(platformRecordDocumentList);
95                                  }
96                              }
97                          }
98                      } else {
99                          if (!searchCriteriaMap.isEmpty()) {
100                             if (searchCriteriaMap.size() > 0) {
101                                 platformRecordDocumentList = findMatching(searchCriteriaMap, docSearchCriteria);
102                                 platformList.addAll(platformRecordDocumentList);
103                                 searchCriteriaMap.clear();
104                             }
105                         }
106                     }
107                 }
108                 if (searchCriteriaCnt == oleSearchConditionsList.size() - 1) {
109                     if (searchCriteriaMap.size() > 0) {
110                         platformRecordDocumentList = findMatching(searchCriteriaMap, docSearchCriteria);
111                         platformList.addAll(platformRecordDocumentList);
112                         searchCriteriaMap.clear();
113                     }
114                 }
115             }
116         }
117         if (flag && !GlobalVariables.getMessageMap().hasMessages()) {
118             platformList = findMatching(searchCriteriaMap, docSearchCriteria);
119         }
120         if (platformList.size() > 0) {
121             for (int searchCriteriaCnt = 0; searchCriteriaCnt < oleSearchConditionsList.size(); searchCriteriaCnt++) {
122                 if (oleSearchConditionsList.get(searchCriteriaCnt).getSearchBy() != null && StringUtils.isNotEmpty(oleSearchConditionsList.get(searchCriteriaCnt).getSearchBy()) && !oleSearchConditionsList.get(searchCriteriaCnt).getSearchCriteria().isEmpty() &&
123                         (OLEConstants.OLEEResourceRecord.NOT.equals(oleSearchConditionsList.get(searchCriteriaCnt).getOperator()))) {
124                     searchCriteriaMap.clear();
125                     searchCriteriaMap = getSearchCriteriaMap(oleSearchConditionsList.get(searchCriteriaCnt).getSearchBy(), oleSearchConditionsList.get(searchCriteriaCnt).getSearchCriteria(), searchCriteriaMap);
126                     if (searchCriteriaMap.size() > 0) {
127                         platformRecordDocumentList = findMatching(searchCriteriaMap, docSearchCriteria);
128                     }
129                     List<String> list = new ArrayList<String>();
130                     for (OLEPlatformRecordDocument olePlatformRecordDocument : platformRecordDocumentList) {
131                         int count = 0;
132                         for (OLEPlatformRecordDocument platformRecordDocument : platformList) {
133                             if (olePlatformRecordDocument.getDocumentNumber().toString().equalsIgnoreCase(platformRecordDocument.getDocumentNumber().toString())) {
134                                 list.add(count + "");
135                             }
136                             count++;
137                         }
138                     }
139                     for (String str : list) {
140                         platformList.remove(Integer.parseInt(str));
141                     }
142                 }
143             }
144         }
145         return platformList;
146     }
147 
148     @Override
149     public void getNewPlatformDoc(String olePlatformId) {
150         Map<String, String> tempId = new HashMap<>();
151         tempId.put(OLEConstants.OLE_PLATFORM_ID, olePlatformId);
152         OLEPlatformRecordDocument tempDocument = KRADServiceLocatorWeb.getLegacyDataAdapter().findByPrimaryKey(OLEPlatformRecordDocument.class, tempId);
153         if (tempDocument != null) {
154             if (tempDocument.getGeneralNotes().size() > 0) {
155                 KRADServiceLocatorWeb.getLegacyDataAdapter().delete(tempDocument.getGeneralNotes());
156             }
157             if (tempDocument.getVariantTitles().size()>0){
158                 KRADServiceLocatorWeb.getLegacyDataAdapter().delete(tempDocument.getVariantTitles());
159             }
160             if (tempDocument.getAlerts().size() > 0) {
161                 KRADServiceLocatorWeb.getLegacyDataAdapter().delete(tempDocument.getAlerts());
162             }
163             if (tempDocument.getAdminUrls().size() > 0) {
164                 KRADServiceLocatorWeb.getLegacyDataAdapter().delete(tempDocument.getAdminUrls());
165             }
166             if (tempDocument.getEventLogs().size() > 0) {
167                 KRADServiceLocatorWeb.getLegacyDataAdapter().delete(tempDocument.getEventLogs());
168             }
169         }
170     }
171 
172     @Override
173     public List<OLEPlatformAdminUrl> saveUrls(List<OLEPlatformAdminUrl> olePlatformAdminUrls) {
174         List<OLEPlatformAdminUrl> olePlatformAdminUrlList = new ArrayList<>();
175         for (OLEPlatformAdminUrl olePlatformAdminUrl : olePlatformAdminUrls) {
176             if (olePlatformAdminUrl.isSaveFlag()) {
177                 olePlatformAdminUrlList.add(olePlatformAdminUrl);
178             }
179         }
180         return olePlatformAdminUrlList;
181     }
182 
183     private Map<String, List<String>> getSearchCriteriaMap(String searchBy, String searchCriteria, Map<String, List<String>> searchCriteriaMap) {
184         List<String> valueList = new ArrayList<>();
185         valueList.add(searchCriteria);
186         searchCriteriaMap.put(searchBy, valueList);
187         return searchCriteriaMap;
188     }
189 
190     private List<OLEPlatformRecordDocument> findMatching(Map<String, List<String>> map, DocumentSearchCriteria.Builder docSearchCriteria) {
191         Map<String, List<String>> attributes = new HashMap<String, List<String>>();
192         if (docSearchCriteria != null) {
193             if (!map.isEmpty()) {
194                 for (String propertyField : map.keySet()) {
195                     if (map.get(propertyField) != null) {
196                         attributes.put(propertyField, map.get(propertyField));
197                     }
198                 }
199             }
200         }
201         docSearchCriteria.setDocumentAttributeValues(attributes);
202         Date currentDate = new Date();
203         docSearchCriteria.setDateCreatedTo(new DateTime(currentDate));
204         DocumentSearchCriteria docSearchCriteriaDTO = docSearchCriteria.build();
205         DocumentSearchResults components = null;
206         components = KEWServiceLocator.getDocumentSearchService().lookupDocuments(GlobalVariables.getUserSession().getPrincipalId(), docSearchCriteriaDTO);
207         List<DocumentSearchResult> docSearchResults = components.getSearchResults();
208         OLEPlatformRecordDocument olePlatformRecordDocument;
209         List<OLEPlatformRecordDocument> olePlatformRecordDocumentList = new ArrayList<>();
210         if (!docSearchResults.isEmpty()) {
211             for (DocumentSearchResult searchResult : docSearchResults) {
212                 olePlatformRecordDocument = new OLEPlatformRecordDocument();
213                 olePlatformRecordDocument.setResultDetails(searchResult, new ArrayList());
214                 if (olePlatformRecordDocument != null) {
215                     olePlatformRecordDocumentList.add(olePlatformRecordDocument);
216                 }
217             }
218         }
219         return olePlatformRecordDocumentList;
220     }
221 
222     @Override
223     public List<OLEPlatformEventLog> filterByReportedDate(Date beginDate, Date endDate, List<OLEPlatformEventLog> eventLogs) {
224         List<OLEPlatformEventLog> olePlatformEventLogList = new ArrayList<>();
225         try {
226             String begin = null;
227             if (beginDate != null) {
228                 begin = dateFormat.format(beginDate);
229             }
230             String end = null;
231             if (endDate != null) {
232                 end = dateFormat.format(endDate);
233             }
234             boolean isValid = false;
235             for (OLEPlatformEventLog eventLog : eventLogs) {
236                 Date eventReportedDate = eventLog.getEventReportedDate();
237                 OleLicenseRequestServiceImpl oleLicenseRequestService = GlobalResourceLoader.getService(OLEConstants.OleLicenseRequest.LICENSE_REQUEST_SERVICE);
238                 isValid = oleLicenseRequestService.validateDate(eventReportedDate, begin, end);
239                 if (isValid) {
240                     olePlatformEventLogList.add(eventLog);
241                 }
242             }
243         } catch (Exception e) {
244             throw new RuntimeException(e);
245         }
246         return olePlatformEventLogList;
247     }
248 
249     @Override
250     public List<OLEPlatformEventLog> filterByResolvedDate(Date beginDate, Date endDate, List<OLEPlatformEventLog> eventLogs) {
251         List<OLEPlatformEventLog> olePlatformEventLogList = new ArrayList<>();
252         try {
253             String begin = null;
254             if (beginDate != null) {
255                 begin = dateFormat.format(beginDate);
256             }
257             String end = null;
258             if (endDate != null) {
259                 end = dateFormat.format(endDate);
260             }
261             boolean isValid = false;
262             for (OLEPlatformEventLog eventLog : eventLogs) {
263                 Date eventResolvedDate = eventLog.getEventResolvedDate();
264                 OleLicenseRequestServiceImpl oleLicenseRequestService = GlobalResourceLoader.getService(OLEConstants.OleLicenseRequest.LICENSE_REQUEST_SERVICE);
265                 isValid = oleLicenseRequestService.validateDate(eventResolvedDate, begin, end);
266                 if (isValid) {
267                     olePlatformEventLogList.add(eventLog);
268                 }
269             }
270         } catch (Exception e) {
271             throw new RuntimeException(e);
272         }
273         return olePlatformEventLogList;
274     }
275 
276     @Override
277     public List<OLEPlatformEventLog> filterByStatus(List<OLEPlatformEventLog> eventLogs, String status) {
278         List<OLEPlatformEventLog> olePlatformEventLogList = new ArrayList<>();
279         for (OLEPlatformEventLog olePlatformEventLog : eventLogs) {
280             if (olePlatformEventLog.getEventStatus()!=null && olePlatformEventLog.getEventStatus().equals(status)) {
281                 olePlatformEventLogList.add(olePlatformEventLog);
282             }
283         }
284         return olePlatformEventLogList;
285     }
286 
287     @Override
288     public List<OLEPlatformEventLog> filterByLogType(List<OLEPlatformEventLog> eventLogs, String logType) {
289         List<OLEPlatformEventLog> olePlatformEventLogList = new ArrayList<>();
290         for (OLEPlatformEventLog olePlatformEventLog : eventLogs) {
291             if (olePlatformEventLog.getLogTypeId()!=null && olePlatformEventLog.getLogTypeId().equals(logType)) {
292                 olePlatformEventLogList.add(olePlatformEventLog);
293             }
294         }
295         return olePlatformEventLogList;
296     }
297 
298     @Override
299     public List<OLEPlatformEventLog> filterByEventType(List<OLEPlatformEventLog> eventLogs, String eventType) {
300         List<OLEPlatformEventLog> olePlatformEventLogList = new ArrayList<>();
301         for (OLEPlatformEventLog olePlatformEventLog : eventLogs) {
302             if (olePlatformEventLog.getEventTypeId()!=null && olePlatformEventLog.getEventTypeId().equals(eventType)) {
303                 olePlatformEventLogList.add(olePlatformEventLog);
304             }
305         }
306         return olePlatformEventLogList;
307     }
308 
309     @Override
310     public List<OLEPlatformEventLog> filterByProblemType(List<OLEPlatformEventLog> eventLogs, String problemType) {
311         List<OLEPlatformEventLog> olePlatformEventLogList = new ArrayList<>();
312         for (OLEPlatformEventLog olePlatformEventLog : eventLogs) {
313             if (olePlatformEventLog.getProblemTypeId()!=null && olePlatformEventLog.getProblemTypeId().equals(problemType)) {
314                 olePlatformEventLogList.add(olePlatformEventLog);
315             }
316         }
317         return olePlatformEventLogList;
318     }
319 }