ChangeCalendar/backend/src/main/java/com/nttdata/calender/changes/ChangeRequest.java

196 lines
6.8 KiB
Java

package com.nttdata.calender.changes;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import com.bmc.arsys.api.ARException;
import com.nttdata.calender.api.Query;
import com.nttdata.calender.api.RemedyJavaAPI;
import com.nttdata.calender.changes.query.Filter;
import com.nttdata.calender.changes.query.Sort;
/**
* Represents a change request object that stores information about slice start,
* slice end,
* filter criteria, and sorting options.
*/
public class ChangeRequest {
private int sliceStart;
private int sliceEnd;
private ArrayList<Filter> filter;
private Sort sort;
/**
* Returns the starting index of the slice.
*
* @return an int representing the slice start index.
*/
public int getSliceStart() {
return this.sliceStart;
}
/**
* Sets the starting index of the slice.
*
* @param sliceStart an int representing the slice start index.
*/
public void setSliceStart(int sliceStart) {
this.sliceStart = sliceStart;
}
/**
* Returns the ending index of the slice.
*
* @return an int representing the slice end index.
*/
public int getSliceEnd() {
return this.sliceEnd;
}
/**
* Sets the ending index of the slice.
*
* @param sliceEnd an int representing the slice end index.
*/
public void setSliceEnd(int sliceEnd) {
this.sliceEnd = sliceEnd;
}
/**
* Returns the array of filter criteria.
*
* @return an array of {@link Filter} objects representing the filter criteria.
*/
public ArrayList<Filter> getFilter() {
return this.filter;
}
/**
* Sets the array of filter criteria.
*
* @param filter an array of {@link Filter} objects representing the filter
* criteria.
*/
public void setFilter(ArrayList<Filter> filter) {
this.filter = filter;
}
/**
* Returns the sorting options for the change request.
*
* @return a {@link Sort} object representing the sorting options.
*/
public Sort getSort() {
return this.sort;
}
/**
* Sets the sorting options for the change request.
*
* @param sort a {@link Sort} object representing the sorting options.
*/
public void setSort(Sort sort) {
this.sort = sort;
}
public void addFilter(Filter filter) {
if (this.filter == null) {
this.filter = new ArrayList<Filter>();
}
this.filter.add(filter);
}
/**
* Constructs a qualifier based on the filters defined in the object and the
* given Query object.
*
* @param query the {@link Query} object containing form and field information.
* @param api the {@link RemedyJavaAPI} object used for accessing the AR
* System API.
* @return a String representing the constructed qualifier.
* @throws ARException if an error occurs while constructing the qualifier or an
* invalid filter is provided.
*/
// TODO: Exception handling (unsuppoprted qualifier)
public String constructQualifier(Query query, RemedyJavaAPI api) throws ARException {
var qualifier = "";
for (int i = 0; i < this.filter.size(); i++) {
var column = this.filter.get(i).getColumn();
if (!column.isEmpty()) {
var inner_qualifier = "";
if (column.equals("D2")) {
var startFrom = filter.get(i).getCriteria()[0];
var startTo = filter.get(i).getCriteria()[1];
System.out.println(startFrom);
System.out.println(startTo);
String inputFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
String outputFormat = "dd/MM/yyyy";
LocalDateTime dateTimeFrom = LocalDateTime.parse(startFrom,
DateTimeFormatter.ofPattern(inputFormat));
LocalDateTime dateTimeTo = LocalDateTime.parse(startTo, DateTimeFormatter.ofPattern(inputFormat));
String startFromFormatted = dateTimeFrom.format(DateTimeFormatter.ofPattern(outputFormat));
String startToFormatted = dateTimeTo.format(DateTimeFormatter.ofPattern(outputFormat));
System.out.println(startFromFormatted);
System.out.println(startToFormatted);
if (!startFromFormatted.isEmpty() && !startToFormatted.isEmpty()) {
var dateColumn = api.getFieldDatabaseName(query.getFormName(), query.getFieldId(column));
// Same day changes need to startFrom=day and startTo=day+24h 60m 60s
if (startFromFormatted.equals(startToFormatted)) {
startToFormatted = "\' < (\"" + startToFormatted + "\"" + " + (24 * (60 * 60)))";
} else
startToFormatted = "\' <= \"" + startToFormatted + "\"";
qualifier += "\'" + dateColumn + "\' >= \"" + startFromFormatted + "\" AND ";
qualifier += "\'" + dateColumn + startToFormatted;
}
qualifier = "(" + qualifier + ")";
} else {
column = api.getFieldDatabaseName(query.getFormName(), query.getFieldId(column));
var inner_filter = "\'" + column + "\' ";
var criterias = filter.get(i).getCriteria();
var inner_concat = " OR ";
var inner_criteria_prefix = "";
switch (filter.get(i).getFilter()) {
case "equals":
inner_filter += "= ";
break;
case "contains":
inner_filter += "LIKE ";
inner_concat = " AND ";
inner_criteria_prefix = "%";
break;
default:
throw new ARException();
}
for (int j = 0; j < criterias.length; j++) {
criterias[j] = inner_criteria_prefix + criterias[j] + inner_criteria_prefix;
inner_qualifier += "(" + inner_filter + "\"" + criterias[j] + "\")";
if (j < criterias.length - 1) {
inner_qualifier += inner_concat;
}
}
qualifier += "(" + inner_qualifier + ")";
}
if (i < filter.size() - 1) {
qualifier += " AND ";
}
}
}
return qualifier;
}
}