View Javadoc
1   package org.kuali.ole.batch.rule;
2   
3   import org.apache.commons.lang.StringUtils;
4   import org.apache.log4j.Logger;
5   import org.kuali.ole.OLEConstants;
6   import org.kuali.ole.batch.bo.OLEBatchProcessProfileBo;
7   import org.kuali.ole.batch.bo.OLEBatchProcessScheduleBo;
8   import org.kuali.ole.batch.document.OLEBatchProcessDefinitionDocument;
9   import org.kuali.ole.batch.form.OLEBatchProcessDefinitionForm;
10  import org.kuali.ole.batch.helper.OLESchedulerHelper;
11  import org.kuali.ole.select.bo.OLESerialReceivingDocument;
12  import org.kuali.rice.coreservice.framework.parameter.ParameterService;
13  import org.kuali.rice.coreservice.impl.parameter.ParameterBo;
14  import org.kuali.rice.kim.api.permission.PermissionService;
15  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
16  import org.kuali.rice.kns.datadictionary.validation.fieldlevel.EmailAddressValidationPattern;
17  import org.kuali.rice.krad.service.KRADServiceLocator;
18  import org.kuali.rice.krad.util.GlobalVariables;
19  import org.kuali.rice.krad.util.KRADConstants;
20  import org.quartz.CronExpression;
21  
22  import java.text.ParseException;
23  import java.util.Date;
24  import java.util.HashMap;
25  import java.util.List;
26  import java.util.Map;
27  import java.util.regex.Matcher;
28  import java.util.regex.Pattern;
29  
30  /**
31   * Created with IntelliJ IDEA.
32   * User: rajeshbabuk
33   * Date: 7/30/13
34   * Time: 3:48 PM
35   * Class to perform validations on the Batch process screen
36   */
37  public class OLEBatchProcessRule {
38  
39      Logger LOG = Logger.getLogger(OLEBatchProcessRule.class);
40  
41      public boolean batchValidations(OLEBatchProcessDefinitionForm oleBatchProcessDefinitionForm) {
42          OLEBatchProcessDefinitionDocument oleBatchProcessDefinitionDocument = (OLEBatchProcessDefinitionDocument) oleBatchProcessDefinitionForm.getDocument();
43          if (validateUser(GlobalVariables.getUserSession().getPrincipalId(), oleBatchProcessDefinitionDocument)) {
44              if (StringUtils.isBlank(oleBatchProcessDefinitionDocument.getBatchProcessProfileName())) {
45                  GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, OLEConstants.OLEBatchProcess.OLE_BATCH_SCHEDULE, "Profile Name");
46              }
47              else {
48                  if (oleBatchProcessDefinitionDocument.getBatchProcessType().equalsIgnoreCase(OLEConstants.OLEBatchProcess.ORDER_RECORD_IMPORT)) {
49                      validateOrderRecordImport(oleBatchProcessDefinitionDocument);
50                  }
51                  else if (!oleBatchProcessDefinitionDocument.getBatchProcessType().equalsIgnoreCase(OLEConstants.OLEBatchProcess.SERIAL_RECORD_IMPORT) && !oleBatchProcessDefinitionDocument.getBatchProcessType().equalsIgnoreCase(OLEConstants.OLEBatchProcess.BATCH_EXPORT) && !oleBatchProcessDefinitionDocument.getBatchProcessType().equalsIgnoreCase(OLEConstants.OLEBatchProcess.CLAIM_REPORT) && (oleBatchProcessDefinitionDocument.getIngestedFile() == null || oleBatchProcessDefinitionDocument.getIngestedFile().isEmpty())) {
52                      GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, OLEConstants.OLEBatchProcess.UPLOAD_FILE);
53                  }
54                  else if (oleBatchProcessDefinitionDocument.getBatchProcessType().equalsIgnoreCase(OLEConstants.OLEBatchProcess.BATCH_BIB_IMPORT)) {
55                      if (oleBatchProcessDefinitionDocument.getIngestedFile() != null && !oleBatchProcessDefinitionDocument.getIngestedFile().getOriginalFilename().contains(".mrc")) {
56                          GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, OLEConstants.OLEBatchProcess.BATCH_BIB_IMPORT_INGEST_FILE_FORMAT);
57                      }
58                  } else if (oleBatchProcessDefinitionDocument.getBatchProcessType().equalsIgnoreCase(OLEConstants.OLEBatchProcess.BATCH_DELETE)) {
59                      if (oleBatchProcessDefinitionDocument.getIngestedFile() != null && !(oleBatchProcessDefinitionDocument.getIngestedFile().getOriginalFilename().contains(".txt")
60                              || oleBatchProcessDefinitionDocument.getIngestedFile().getOriginalFilename().contains(".mrc"))) {
61                          GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, OLEConstants.OLEBatchProcess.BATCH_DELETE_INGEST_FILE_FORMAT);
62                      }
63                      Map<String,String> map = new HashMap<String,String>();
64                      map.put("batchProcessProfileId", oleBatchProcessDefinitionDocument.getBatchProcessProfileId());
65                      List<OLEBatchProcessProfileBo> oleBatchProcessProfileBos = (List<OLEBatchProcessProfileBo>) KRADServiceLocator.getBusinessObjectService().findMatching(OLEBatchProcessProfileBo.class, map);
66                      if (oleBatchProcessProfileBos != null && oleBatchProcessProfileBos.size() > 0) {
67                          OLEBatchProcessProfileBo oleBatchProcessProfileBo = oleBatchProcessProfileBos.get(0);
68                          if (oleBatchProcessProfileBo.getOleBatchProcessProfileBibMatchPointList() == null) {
69                              GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, OLEConstants.OLEBatchProcess.BATCH_DELETE_MATCH_POINT);
70                          } else if (oleBatchProcessProfileBo.getOleBatchProcessProfileBibMatchPointList().size() == 0) {
71                              GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, OLEConstants.OLEBatchProcess.BATCH_DELETE_MATCH_POINT);
72                          } else if (StringUtils.isEmpty(oleBatchProcessProfileBo.getOleBatchProcessProfileBibMatchPointList().get(0).getOleBibMatchPoint())) {
73                              GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, OLEConstants.OLEBatchProcess.BATCH_DELETE_MATCH_POINT);
74                          }
75  
76                      }
77                  } else if (oleBatchProcessDefinitionDocument.getBatchProcessType().equalsIgnoreCase(OLEConstants.OLEBatchProcess.LOCATION_IMPORT)) {
78                      if (oleBatchProcessDefinitionDocument.getIngestedFile() != null && !oleBatchProcessDefinitionDocument.getIngestedFile().getOriginalFilename().contains(".xml")){
79                          GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, OLEConstants.OLEBatchProcess.LOCATION_IMPORT_INGEST_FILE_FORMAT);
80                      }
81                  } else if (oleBatchProcessDefinitionDocument.getBatchProcessType().equalsIgnoreCase(OLEConstants.OLEBatchProcess.PATRON_IMPORT)) {
82                      if (oleBatchProcessDefinitionDocument.getIngestedFile() != null && !oleBatchProcessDefinitionDocument.getIngestedFile().getOriginalFilename().contains(".xml")){
83                          GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, OLEConstants.OLEBatchProcess.PATRON_IMPORT_INGEST_FILE_FORMAT);
84                      }
85                  } else if (oleBatchProcessDefinitionDocument.getBatchProcessType().equalsIgnoreCase(OLEConstants.OLEBatchProcess.CLAIM_REPORT)){
86                      //TODO validation for Claim Report
87                  } else if (oleBatchProcessDefinitionDocument.getBatchProcessType().equalsIgnoreCase(OLEConstants.OLEBatchProcess.SERIAL_RECORD_IMPORT)){
88                      if(oleBatchProcessDefinitionDocument.getInputFormat().equalsIgnoreCase("XMl")){
89                          if (oleBatchProcessDefinitionDocument.getIngestedFile() != null){
90                              if(oleBatchProcessDefinitionDocument.getSerialRecordDocumentFile() == null && oleBatchProcessDefinitionDocument.getSerialRecordHistoryFile() == null && oleBatchProcessDefinitionDocument.getSerialRecordTypeFile() == null){
91                                  if(!oleBatchProcessDefinitionDocument.getIngestedFile().getOriginalFilename().contains(".xml")){
92                                       clearFile(oleBatchProcessDefinitionDocument);
93                                      GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, OLEConstants.OLEBatchProcess.SERIAL_RECEIVING_IMPORT_INGEST_FILE_FORMAT);
94                                  }
95                              } else{
96                                  clearFile(oleBatchProcessDefinitionDocument);
97                                  GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, OLEConstants.OLEBatchProcess.SERIAL_XML_CSV_FORMAT);
98                              }
99                          } else {
100                             clearFile(oleBatchProcessDefinitionDocument);
101                             GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, OLEConstants.OLEBatchProcess.UPLOAD_FILE);
102                         }
103 
104                     } else if(oleBatchProcessDefinitionDocument.getInputFormat().equalsIgnoreCase("CSV")){
105                          if(oleBatchProcessDefinitionDocument.getIngestedFile() != null){
106                              clearFile(oleBatchProcessDefinitionDocument);
107                              GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, OLEConstants.OLEBatchProcess.SERIAL_XML_CSV_FORMAT);
108                          } else{
109                              if (oleBatchProcessDefinitionDocument.getSerialRecordDocumentFile() == null) {
110                                  clearFile(oleBatchProcessDefinitionDocument);
111                                  GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, OLEConstants.OLEBatchProcess.RECORD_UPLOAD_CSV);
112                              }
113                              if(oleBatchProcessDefinitionDocument.getSerialRecordDocumentFile()!=null){
114                                  if(!oleBatchProcessDefinitionDocument.getSerialRecordDocumentFile().getOriginalFilename().contains(".csv")){
115                                      clearFile(oleBatchProcessDefinitionDocument);
116                                      GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, OLEConstants.OLEBatchProcess.SERIAL_REC_CSV);
117                                  }else if(!oleBatchProcessDefinitionDocument.getSerialRecordDocumentFile().getOriginalFilename().endsWith(getParameter(OLEConstants.OLEBatchProcess.SERIAL_RECORD_NAME)+".csv")){
118                                      clearFile(oleBatchProcessDefinitionDocument);
119                                      GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, OLEConstants.OLEBatchProcess.RECORD_NAME_MISMATCH);
120                                  }
121                              }
122                              if(oleBatchProcessDefinitionDocument.getSerialRecordTypeFile()!=null){
123                                  if(!oleBatchProcessDefinitionDocument.getSerialRecordTypeFile().getOriginalFilename().contains(".csv")){
124                                      clearFile(oleBatchProcessDefinitionDocument);
125                                      GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS,OLEConstants.OLEBatchProcess.SERIAL_TYP_CSV);
126                                  }
127                                  else if(!oleBatchProcessDefinitionDocument.getSerialRecordTypeFile().getOriginalFilename().endsWith(getParameter(OLEConstants.OLEBatchProcess.SERIAL_TYPE_NAME)+".csv")){
128                                      clearFile(oleBatchProcessDefinitionDocument);
129                                      GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, OLEConstants.OLEBatchProcess.RECORD_TYPE_NAME_MISMATCH);
130                                  }
131                              }
132                              if(oleBatchProcessDefinitionDocument.getSerialRecordHistoryFile()!=null){
133                                  if(!oleBatchProcessDefinitionDocument.getSerialRecordHistoryFile().getOriginalFilename().contains(".csv")){
134                                      clearFile(oleBatchProcessDefinitionDocument);
135                                      GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, OLEConstants.OLEBatchProcess.SERIAL_HIS_CSV);
136                                  }
137                                  else if(!oleBatchProcessDefinitionDocument.getSerialRecordHistoryFile().getOriginalFilename().endsWith(getParameter(OLEConstants.OLEBatchProcess.SERIAL_HISTORY_NAME)+".csv")){
138                                      clearFile(oleBatchProcessDefinitionDocument);
139                                      GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS,OLEConstants.OLEBatchProcess.RECORD_HISTORY_NAME_MISMATCH);
140                                  }
141                              }
142 
143                          }
144                     }
145 /*                    if (oleBatchProcessDefinitionDocument.getIngestedFile() != null){
146                         if(oleBatchProcessDefinitionDocument.getSerialRecordDocumentFile() == null && oleBatchProcessDefinitionDocument.getSerialRecordHistoryFile() == null && oleBatchProcessDefinitionDocument.getSerialRecordTypeFile() == null){
147                         if(!oleBatchProcessDefinitionDocument.getIngestedFile().getOriginalFilename().contains(".xml")){
148                         GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, OLEConstants.OLEBatchProcess.SERIAL_RECEIVING_IMPORT_INGEST_FILE_FORMAT);
149                         }
150                         } else{
151                             GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, "Input file format should be either xml or csv");
152                         }
153                     }else if(oleBatchProcessDefinitionDocument.getIngestedFile()==null){
154                         if(oleBatchProcessDefinitionDocument.getSerialRecordDocumentFile()!=null){
155                             if(!oleBatchProcessDefinitionDocument.getSerialRecordDocumentFile().getOriginalFilename().contains(".csv")){
156                                 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, "it should be an csv file");
157                             }
158                         }
159                         if(oleBatchProcessDefinitionDocument.getSerialRecordTypeFile()!=null){
160                             if(!oleBatchProcessDefinitionDocument.getSerialRecordTypeFile().getOriginalFilename().contains(".csv")){
161                                 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, "it should be an csv file");
162                             }
163                         }
164                         if(oleBatchProcessDefinitionDocument.getSerialRecordHistoryFile()!=null){
165                             if(!oleBatchProcessDefinitionDocument.getSerialRecordHistoryFile().getOriginalFilename().contains(".csv")){
166                                 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, "it should be an csv file");
167                             }
168                         }
169 
170                     }*/
171 
172                 }
173             }
174             if(StringUtils.isNotBlank(oleBatchProcessDefinitionDocument.getEmailIds()) && !validateEmailIds(oleBatchProcessDefinitionDocument.getEmailIds())){
175                 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, OLEConstants.OLEBatchProcess.ERROR_EMAIL_ID);
176             }
177             if (oleBatchProcessDefinitionDocument.isScheduleFlag()) {
178                 String cronOrSchedule = oleBatchProcessDefinitionDocument.getCronOrSchedule();
179                 if (StringUtils.isNotBlank(cronOrSchedule)) {
180                     if (oleBatchProcessDefinitionDocument.getCronOrSchedule().equals(OLEConstants.OLEBatchProcess.PROVIDED_CRON)) {
181                         String cronExpression = oleBatchProcessDefinitionDocument.getEnteredCronExp();
182                         if (StringUtils.isBlank(cronExpression)) {
183                             GlobalVariables.getMessageMap().putError(OLEConstants.OLEBatchProcess.BATCH_PROCESS_SCHEDULE_SECTION_ID, OLEConstants.OLEBatchProcess.OLE_BATCH_SCHEDULE, "Cron Expression");
184                         }
185                         boolean validCronExpression = org.quartz.CronExpression.isValidExpression(cronExpression);
186                         if (StringUtils.isNotBlank(cronExpression) && !validCronExpression) {
187                             GlobalVariables.getMessageMap().putError(OLEConstants.OLEBatchProcess.BATCH_PROCESS_SCHEDULE_SECTION_ID, OLEConstants.OLEBatchProcess.ERROR_CRON_EXPRESSION);
188                         }
189                     } else {
190                         OLEBatchProcessScheduleBo oleBatchProcessScheduleBo = oleBatchProcessDefinitionDocument.getOleBatchProcessScheduleBo();
191                         String oneTimeOrRecur = oleBatchProcessDefinitionDocument.getOneTimeOrRecurring();
192                         if (StringUtils.isNotBlank(oneTimeOrRecur)) {
193                             oleBatchProcessScheduleBo.setOneTimeOrRecurring(oneTimeOrRecur);
194                             if (oneTimeOrRecur.equalsIgnoreCase(OLEConstants.OLEBatchProcess.SCHEDULE_ONETIME)) {
195                                 Date oneDate = oleBatchProcessScheduleBo.getOneTimeDate();
196                                 String oneTime = oleBatchProcessScheduleBo.getOneTimeStartTime();
197                                 if (oneDate == null) {
198                                     GlobalVariables.getMessageMap().putError(OLEConstants.OLEBatchProcess.BATCH_PROCESS_SCHEDULE_SECTION_ID, OLEConstants.OLEBatchProcess.OLE_BATCH_SCHEDULE, "Date");
199                                 }
200                                 if (StringUtils.isBlank(oneTime)) {
201                                     GlobalVariables.getMessageMap().putError(OLEConstants.OLEBatchProcess.BATCH_PROCESS_SCHEDULE_SECTION_ID, OLEConstants.OLEBatchProcess.OLE_BATCH_SCHEDULE, "Time");
202                                 }
203                                 if (StringUtils.isNotBlank(oneTime) && !validateTime(oneTime)) {
204                                     GlobalVariables.getMessageMap().putError(OLEConstants.OLEBatchProcess.BATCH_PROCESS_SCHEDULE_SECTION_ID, OLEConstants.OLEBatchProcess.OLE_BATCH_SCHEDULE_TIME);
205                                 }
206                                 if(oneDate!=null && StringUtils.isNotEmpty(oneTime) && validateTime(oneTime) && isScheduleInValid(oleBatchProcessScheduleBo)){
207                                     GlobalVariables.getMessageMap().putError(OLEConstants.OLEBatchProcess.BATCH_PROCESS_SCHEDULE_SECTION_ID, OLEConstants.OLEBatchProcess.OLE_BATCH_SCHEDULE_ERR);
208                                 }
209                             }
210 
211                             if (oneTimeOrRecur.equalsIgnoreCase(OLEConstants.OLEBatchProcess.SCHEDULE_RECURRING)) {
212                                 String schduleType = oleBatchProcessDefinitionDocument.getScheduleType();
213                                 if (StringUtils.isNotBlank(schduleType)) {
214                                     if (schduleType.equalsIgnoreCase(OLEConstants.OLEBatchProcess.SCHEDULE_TYPE_DAILY)) {
215                                         String startTimeDaily = oleBatchProcessScheduleBo.getStartTime();
216                                         if (StringUtils.isBlank(startTimeDaily)) {
217                                             GlobalVariables.getMessageMap().putError(OLEConstants.OLEBatchProcess.BATCH_PROCESS_SCHEDULE_SECTION_ID, OLEConstants.OLEBatchProcess.OLE_BATCH_SCHEDULE, "Start Time");
218                                         }
219                                         if (StringUtils.isNotBlank(startTimeDaily) && !validateTime(startTimeDaily)) {
220                                             GlobalVariables.getMessageMap().putError(OLEConstants.OLEBatchProcess.BATCH_PROCESS_SCHEDULE_SECTION_ID, OLEConstants.OLEBatchProcess.OLE_BATCH_SCHEDULE_TIME);
221                                         }
222                                     }
223                                     if (schduleType.equalsIgnoreCase(OLEConstants.OLEBatchProcess.SCHEDULE_TYPE_WEEKLY)) {
224                                         String startTimeWeekly = oleBatchProcessScheduleBo.getStartTime();
225                                         String weekDays = oleBatchProcessScheduleBo.getWeekDays();
226                                         if (StringUtils.isBlank(startTimeWeekly)) {
227                                             GlobalVariables.getMessageMap().putError(OLEConstants.OLEBatchProcess.BATCH_PROCESS_SCHEDULE_SECTION_ID, OLEConstants.OLEBatchProcess.OLE_BATCH_SCHEDULE, "Start Time");
228                                         }
229                                         if (StringUtils.isNotBlank(startTimeWeekly) && !validateTime(startTimeWeekly)) {
230                                             GlobalVariables.getMessageMap().putError(OLEConstants.OLEBatchProcess.BATCH_PROCESS_SCHEDULE_SECTION_ID, OLEConstants.OLEBatchProcess.OLE_BATCH_SCHEDULE_TIME);
231                                         }
232                                         if (StringUtils.isBlank(weekDays)) {
233                                             GlobalVariables.getMessageMap().putError(OLEConstants.OLEBatchProcess.BATCH_PROCESS_SCHEDULE_SECTION_ID, OLEConstants.OLEBatchProcess.OLE_BATCH_SCHEDULE, "Week Day");
234                                         }
235                                     }
236                                     if (schduleType.equalsIgnoreCase(OLEConstants.OLEBatchProcess.SCHEDULE_TYPE_MONTHLY)) {
237                                         String startTimeMonthly = oleBatchProcessScheduleBo.getStartTime();
238                                         String dayNumberMonthly = oleBatchProcessScheduleBo.getDayNumber();
239                                         String monthNumberMonthly = oleBatchProcessScheduleBo.getMonthNumber();
240                                         if (StringUtils.isBlank(startTimeMonthly)) {
241                                             GlobalVariables.getMessageMap().putError(OLEConstants.OLEBatchProcess.BATCH_PROCESS_SCHEDULE_SECTION_ID, OLEConstants.OLEBatchProcess.OLE_BATCH_SCHEDULE, "Start Time");
242                                         }
243                                         if (StringUtils.isNotBlank(startTimeMonthly) && !validateTime(startTimeMonthly)) {
244                                             GlobalVariables.getMessageMap().putError(OLEConstants.OLEBatchProcess.BATCH_PROCESS_SCHEDULE_SECTION_ID, OLEConstants.OLEBatchProcess.OLE_BATCH_SCHEDULE_TIME);
245                                         }
246                                         if (StringUtils.isBlank(dayNumberMonthly)) {
247                                             GlobalVariables.getMessageMap().putError(OLEConstants.OLEBatchProcess.BATCH_PROCESS_SCHEDULE_SECTION_ID, OLEConstants.OLEBatchProcess.OLE_BATCH_SCHEDULE, "Day of Month");
248                                         }
249                                         if (StringUtils.isBlank(monthNumberMonthly)) {
250                                             GlobalVariables.getMessageMap().putError(OLEConstants.OLEBatchProcess.BATCH_PROCESS_SCHEDULE_SECTION_ID, OLEConstants.OLEBatchProcess.OLE_BATCH_SCHEDULE, "Month Number");
251                                         }
252                                     }
253                                 } else {
254                                     GlobalVariables.getMessageMap().putError(OLEConstants.OLEBatchProcess.BATCH_PROCESS_SCHEDULE_SECTION_ID, OLEConstants.OLEBatchProcess.DAILY_WEEKLY_MONTHLY);
255                                 }
256                             }
257                         } else {
258                             GlobalVariables.getMessageMap().putError(OLEConstants.OLEBatchProcess.BATCH_PROCESS_SCHEDULE_SECTION_ID, OLEConstants.OLEBatchProcess.ONE_TIME_OR_RECUR_ERROR);
259                         }
260                     }
261                 } else {
262                     GlobalVariables.getMessageMap().putError(OLEConstants.OLEBatchProcess.BATCH_PROCESS_SCHEDULE_SECTION_ID, OLEConstants.OLEBatchProcess.CRON_OR_SCHEDULE);
263                 }
264             }
265         } else {
266             GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, OLEConstants.OLEBatchProcess.ERROR_AUTHORIZATION);
267         }
268         int errorCount = GlobalVariables.getMessageMap().getErrorCount();
269         if (errorCount > 0) {
270             return false;
271         } else {
272             return true;
273         }
274     }
275 
276 
277 
278     private boolean validateTime(String time) {
279         String TIME_24HR_PATTERN = "([01]?[0-9]|2[0-3]):[0-5][0-9]";
280         Matcher match = Pattern.compile(TIME_24HR_PATTERN).matcher(time);
281         return match.matches();
282     }
283 
284     public boolean validateUser(String principalId, OLEBatchProcessDefinitionDocument oleBatchProcessDefinitionDocument) {
285         boolean hasPermission = true;
286         if (StringUtils.isNotEmpty(oleBatchProcessDefinitionDocument.getBatchProcessType())) {
287             if (oleBatchProcessDefinitionDocument.getBatchProcessType().equalsIgnoreCase(OLEConstants.OLEBatchProcess.BATCH_BIB_IMPORT)) {
288                 hasPermission = canPerformBatchImport(principalId);
289             } else if(oleBatchProcessDefinitionDocument.getBatchProcessType().equalsIgnoreCase(OLEConstants.OLEBatchProcess.BATCH_EXPORT)) {
290                 hasPermission = canPerformBatchExport(principalId);
291             } else if (oleBatchProcessDefinitionDocument.getBatchProcessType().equalsIgnoreCase(OLEConstants.OLEBatchProcess.BATCH_DELETE)) {
292                 hasPermission = canPerformBatchDelete(principalId);
293             }
294         }
295         return hasPermission;
296     }
297 
298     private boolean validateEmailIds(String emailsIds){
299         boolean validEmail=true;
300         String[] emailsIdsArray = emailsIds.split(",");
301         for(String emailId:emailsIdsArray){
302             validEmail = validateEmailAddress(emailId.trim());
303             if(!validEmail){
304                 return false;
305             }
306         }
307         return validEmail;
308     }
309 
310     /**
311      * validate the email Address against the email address pattern
312      *
313      * @param emailAddress
314      * @return true if email Address follows the pattern else return false.
315      */
316     private boolean validateEmailAddress(String emailAddress) {
317         boolean valid = true;
318 
319         //perform the validation against email address
320         if (StringUtils.isNotBlank(emailAddress)) {
321             EmailAddressValidationPattern emailAddressPattern = new EmailAddressValidationPattern();
322             if (!emailAddressPattern.matches(emailAddress)) {
323                 return false;
324             }
325         }
326         return valid;
327     }
328 
329 
330     public boolean  canPerformBatchImport(String principalId){
331         PermissionService service= KimApiServiceLocator.getPermissionService();
332         return  service.hasPermission(principalId,OLEConstants.CAT_NAMESPACE,OLEConstants.BATCH_PROCESS_IMPORT);
333     }
334 
335     public boolean  canPerformBatchExport(String principalId){
336         PermissionService service= KimApiServiceLocator.getPermissionService();
337         return  service.hasPermission(principalId,OLEConstants.CAT_NAMESPACE,OLEConstants.BATCH_PROCESS_EXPORT);
338     }
339 
340     public boolean  canPerformBatchDelete(String principalId){
341         PermissionService service= KimApiServiceLocator.getPermissionService();
342         return  service.hasPermission(principalId,OLEConstants.CAT_NAMESPACE,OLEConstants.BATCH_PROCESS_DELETE);
343     }
344 
345     private boolean isScheduleInValid(OLEBatchProcessScheduleBo scheduleBo) {
346         String cronExp = OLESchedulerHelper.getInstance().getCronExpression(scheduleBo);
347         CronExpression exp;
348         try {
349             exp = new CronExpression(cronExp);
350         } catch (ParseException e) {
351             LOG.error("Error while parsing the cron expression:: "+cronExp,e);
352             return false;
353         }
354         Date date = exp.getNextValidTimeAfter(new Date());
355         return date == null;
356     }
357 
358     private void validateOrderRecordImport(OLEBatchProcessDefinitionDocument oleBatchProcessDefinitionDocument){
359         if(oleBatchProcessDefinitionDocument.getMarcOnly()){
360             if(oleBatchProcessDefinitionDocument.getMarcFile()== null){
361                 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, OLEConstants.OLEBatchProcess.UPLOAD_MARC_FILE);
362             }
363         }
364         else {
365             if(oleBatchProcessDefinitionDocument.getMarcFile() == null && oleBatchProcessDefinitionDocument.getEdiFile() == null){
366                 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, OLEConstants.OLEBatchProcess.UPLOAD_FILE);
367             }
368             else if(oleBatchProcessDefinitionDocument.getMarcFile() == null){
369                 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, OLEConstants.OLEBatchProcess.UPLOAD_MARC_FILE);
370             }
371             else if(oleBatchProcessDefinitionDocument.getEdiFile() == null){
372                 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, OLEConstants.OLEBatchProcess.UPLOAD_EDI_FILE);
373             }
374         }
375         if(oleBatchProcessDefinitionDocument.getEdiFile() != null){
376             if (oleBatchProcessDefinitionDocument.getMarcFile()!=null && !(oleBatchProcessDefinitionDocument.getMarcFile().getOriginalFilename().contains(".mrc") ||
377                     oleBatchProcessDefinitionDocument.getMarcFile().getOriginalFilename().contains(".xml"))) {
378                 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, OLEConstants.OLEBatchProcess.UPLOAD_MARC_FILE);
379             }
380             if (oleBatchProcessDefinitionDocument.getEdiFile()!=null && !(oleBatchProcessDefinitionDocument.getEdiFile().getOriginalFilename().contains(".edi") ||
381                     oleBatchProcessDefinitionDocument.getEdiFile().getOriginalFilename().contains(".xml"))) {
382                 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, OLEConstants.OLEBatchProcess.UPLOAD_EDI_FILE);
383             }
384         }
385         else {
386             if (oleBatchProcessDefinitionDocument.getIngestedFile()!=null && !(oleBatchProcessDefinitionDocument.getIngestedFile().getOriginalFilename().contains(".mrc") ||
387                     oleBatchProcessDefinitionDocument.getIngestedFile().getOriginalFilename().contains(".xml"))) {
388                 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, OLEConstants.OLEBatchProcess.UPLOAD_MARC_FILE);
389             }
390         }
391     }
392 
393     private String getParameter(String name) {
394         String parameter = "";
395         try {
396             Map<String, String> criteriaMap = new HashMap<String, String>();
397             criteriaMap.put("namespaceCode", OLEConstants.SYS_NMSPC);
398             criteriaMap.put("componentCode", OLEConstants.BATCH_CMPNT);
399             criteriaMap.put("name", name);
400             List<ParameterBo> parametersList = (List<ParameterBo>)KRADServiceLocator.getBusinessObjectService().findMatching(ParameterBo.class, criteriaMap);
401             for (ParameterBo parameterBo : parametersList) {
402                 parameter = parameterBo.getValue();
403             }
404         } catch (Exception e) {
405             LOG.error(e, e);
406         }
407         return parameter;
408     }
409 
410     private void clearFile(OLEBatchProcessDefinitionDocument oleBatchProcessDefinitionDocument ){
411         oleBatchProcessDefinitionDocument.setIngestedFile(null);
412         oleBatchProcessDefinitionDocument.setSerialRecordTypeFile(null);
413         oleBatchProcessDefinitionDocument.setSerialRecordDocumentFile(null);
414         oleBatchProcessDefinitionDocument.setSerialRecordHistoryFile(null);
415 
416     }
417 }
418