qualifier?
parent
045a5828ae
commit
ce8790490e
|
|
@ -5,17 +5,14 @@ import java.time.DayOfWeek;
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
import java.time.format.DateTimeFormatter;
|
import java.time.format.DateTimeFormatter;
|
||||||
import java.time.temporal.IsoFields;
|
import java.time.temporal.IsoFields;
|
||||||
import java.time.temporal.TemporalAdjuster;
|
|
||||||
import java.time.temporal.TemporalAdjusters;
|
import java.time.temporal.TemporalAdjusters;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.Objects;
|
|
||||||
|
|
||||||
import com.bmc.arsys.api.ARException;
|
import com.bmc.arsys.api.ARException;
|
||||||
import com.bmc.thirdparty.org.springframework.cglib.core.Local;
|
|
||||||
import com.nttdata.calender.api.Query;
|
import com.nttdata.calender.api.Query;
|
||||||
import com.nttdata.calender.api.RemedyJavaAPI;
|
import com.nttdata.calender.api.RemedyJavaAPI;
|
||||||
import com.nttdata.calender.changes.ChangeItem;
|
import com.nttdata.calender.errorhandling.ErrorTypes.ValidationError;
|
||||||
|
|
||||||
public class PlanTimes {
|
public class PlanTimes {
|
||||||
private RemedyJavaAPI remedyJavaAPI;
|
private RemedyJavaAPI remedyJavaAPI;
|
||||||
|
|
@ -27,13 +24,14 @@ public class PlanTimes {
|
||||||
this.calenderWeeks = new ArrayList<>();
|
this.calenderWeeks = new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setPlanTimes(PlanTimesRequest req) throws ARException {
|
public void setPlanTimes(PlanTimesRequest req) throws ARException, ValidationError {
|
||||||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy");
|
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy");
|
||||||
LocalDate startDate = LocalDate.parse(req.getRenderStartDate(), formatter);
|
LocalDate startDate = LocalDate.parse(req.getRenderStartDate(), formatter);
|
||||||
LocalDate endDate = LocalDate.parse(req.getRenderEndDate(), formatter);
|
LocalDate endDate = LocalDate.parse(req.getRenderEndDate(), formatter);
|
||||||
|
|
||||||
var queryChanges = new Query.QueryBuilder(formName).addFieldId("D2", 1000000350)
|
var queryChanges = new Query.QueryBuilder(formName).addFieldId("D2", 1000000350)
|
||||||
.addFieldId("ChangeNr", 1000000182).addFieldId("PlanTimeHours", 666000009).addFieldId("PlanTimeMinutes", 666000010).build();
|
.addFieldId("ChangeNr", 1000000182).addFieldId("PlanTimeHours", 666000009)
|
||||||
|
.addFieldId("PlanTimeMinutes", 666000010).build();
|
||||||
|
|
||||||
while (!startDate.isAfter(endDate)) {
|
while (!startDate.isAfter(endDate)) {
|
||||||
CalendarWeek calendarWeek = new CalendarWeek();
|
CalendarWeek calendarWeek = new CalendarWeek();
|
||||||
|
|
@ -55,9 +53,12 @@ public class PlanTimes {
|
||||||
startDate = startDate.with(TemporalAdjusters.next(DayOfWeek.MONDAY));
|
startDate = startDate.with(TemporalAdjusters.next(DayOfWeek.MONDAY));
|
||||||
}
|
}
|
||||||
|
|
||||||
var changesInDateRange = remedyJavaAPI.queryFieldsById(
|
var qualifier = req.constructQualifier(queryChanges,
|
||||||
"\'Scheduled Start Date\' > \"" + req.getRenderStartDate() + "\" AND \'Scheduled Start Date\' <= \""
|
"\'Scheduled Start Date\' > \"" + req.getRenderStartDate() + "\" AND \'Scheduled Start Date\' <= \""
|
||||||
+ req.getRenderEndDate() + "\"",
|
+ req.getRenderEndDate() + "\"",
|
||||||
|
remedyJavaAPI);
|
||||||
|
|
||||||
|
var changesInDateRange = remedyJavaAPI.queryFieldsById(qualifier,
|
||||||
queryChanges.getFieldIds(), formName, null, 0, 0);
|
queryChanges.getFieldIds(), formName, null, 0, 0);
|
||||||
|
|
||||||
for (var week : calenderWeeks) {
|
for (var week : calenderWeeks) {
|
||||||
|
|
@ -68,10 +69,10 @@ public class PlanTimes {
|
||||||
LocalDate dateEnd = LocalDate.parse(week.getEndDate(), formatter);
|
LocalDate dateEnd = LocalDate.parse(week.getEndDate(), formatter);
|
||||||
|
|
||||||
if (d2.isAfter(dateStart) && (d2.isEqual(dateEnd) || d2.isBefore(dateEnd))) {
|
if (d2.isAfter(dateStart) && (d2.isEqual(dateEnd) || d2.isBefore(dateEnd))) {
|
||||||
var minutes = change.get(queryChanges.getFieldId("PlanTimeMinutes")).toString();
|
var minutes = change.get(queryChanges.getFieldId("PlanTimeMinutes")).toString();
|
||||||
var hours = change.get(queryChanges.getFieldId("PlanTimeHours")).toString();
|
var hours = change.get(queryChanges.getFieldId("PlanTimeHours")).toString();
|
||||||
double plantime = convertPlanTime(hours, minutes);
|
double plantime = convertPlanTime(hours, minutes);
|
||||||
planTimePerWeek += plantime;
|
planTimePerWeek += plantime;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
week.setPlanTime((int) Math.ceil(planTimePerWeek));
|
week.setPlanTime((int) Math.ceil(planTimePerWeek));
|
||||||
|
|
@ -81,12 +82,13 @@ public class PlanTimes {
|
||||||
public double convertPlanTime(String hours, String minutes) throws ARException {
|
public double convertPlanTime(String hours, String minutes) throws ARException {
|
||||||
double mins = 0.0;
|
double mins = 0.0;
|
||||||
double hrs = 0.0;
|
double hrs = 0.0;
|
||||||
if (minutes != null) mins = (Double.valueOf(minutes) / 60.0);
|
if (minutes != null)
|
||||||
if (hours != null) Double.valueOf(hours);
|
mins = (Double.valueOf(minutes) / 60.0);
|
||||||
|
if (hours != null)
|
||||||
|
Double.valueOf(hours);
|
||||||
double totalHours = hrs + mins;
|
double totalHours = hrs + mins;
|
||||||
return totalHours;
|
return totalHours;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public LocalDate timestampToDate(String timestamp) {
|
public LocalDate timestampToDate(String timestamp) {
|
||||||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy");
|
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy");
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,15 @@
|
||||||
package com.nttdata.calender.planTimes;
|
package com.nttdata.calender.planTimes;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.time.format.DateTimeFormatter;
|
||||||
|
import java.time.format.DateTimeParseException;
|
||||||
import java.util.ArrayList;
|
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.Filter;
|
||||||
|
import com.nttdata.calender.errorhandling.ErrorTypes.ValidationError;
|
||||||
|
|
||||||
public class PlanTimesRequest {
|
public class PlanTimesRequest {
|
||||||
private ArrayList<Filter> filter;
|
private ArrayList<Filter> filter;
|
||||||
|
|
@ -32,4 +39,138 @@ public class PlanTimesRequest {
|
||||||
public void setRenderEndDate(String renderEndDate) {
|
public void setRenderEndDate(String renderEndDate) {
|
||||||
this.renderEndDate = renderEndDate;
|
this.renderEndDate = renderEndDate;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs and returns a qualifier based on the filters defined in the object
|
||||||
|
* and the given Query object.
|
||||||
|
*
|
||||||
|
* @param query the Query object containing form and field information
|
||||||
|
* @param api the 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
|
||||||
|
* @throws ValidationError if the fields inside the filter are empty or an
|
||||||
|
* invalid inner filter argument is provided
|
||||||
|
*/
|
||||||
|
public String constructQualifier(Query query, String qualstr, RemedyJavaAPI api) throws ARException, ValidationError {
|
||||||
|
var qualifier = "";
|
||||||
|
|
||||||
|
if (this.filter == null) {
|
||||||
|
return qualifier;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < this.filter.size(); i++) {
|
||||||
|
var current_filter = this.filter.get(i);
|
||||||
|
var column = current_filter.getColumn();
|
||||||
|
var criterias = current_filter.getCriteria();
|
||||||
|
|
||||||
|
if (column.isEmpty() || criterias.length <= 0) {
|
||||||
|
throw new ValidationError("Fields inside filter empty");
|
||||||
|
}
|
||||||
|
var inner_qualifier = "";
|
||||||
|
|
||||||
|
if (column.equals("D2")) {
|
||||||
|
var dateQualifier = constructDateQualifier(current_filter, column, query, api);
|
||||||
|
qualifier = "(" + dateQualifier + ")";
|
||||||
|
} else {
|
||||||
|
column = api.getFieldDatabaseName(query.getFormName(), query.getFieldId(column));
|
||||||
|
|
||||||
|
var inner_filter = "\'" + column + "\' ";
|
||||||
|
var inner_concat = " OR ";
|
||||||
|
var inner_criteria_prefix = "";
|
||||||
|
|
||||||
|
switch (current_filter.getFilter()) {
|
||||||
|
case "equals":
|
||||||
|
inner_filter += "= ";
|
||||||
|
break;
|
||||||
|
case "contains":
|
||||||
|
inner_filter += "LIKE ";
|
||||||
|
inner_concat = " AND ";
|
||||||
|
inner_criteria_prefix = "%";
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw new ValidationError("Invalid inner filter argument");
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs and returns a date qualifier for the given filter, column, query,
|
||||||
|
* and API.
|
||||||
|
* Throws a ValidationError if the date filter does not contain two date
|
||||||
|
* elements.
|
||||||
|
*
|
||||||
|
* @param current_filter the current filter to construct the date qualifier for
|
||||||
|
* @param column the column to apply the date qualifier on
|
||||||
|
* @param query the query object
|
||||||
|
* @param api the RemedyJavaAPI object
|
||||||
|
* @return the constructed date qualifier as a string
|
||||||
|
* @throws ValidationError if the date filter does not contain two date elements
|
||||||
|
* @throws ARException if an AR exception occurs
|
||||||
|
*/
|
||||||
|
private String constructDateQualifier(Filter current_filter, String column, Query query, RemedyJavaAPI api)
|
||||||
|
throws ValidationError, ARException {
|
||||||
|
if (current_filter.getCriteria().length != 2) {
|
||||||
|
throw new ValidationError("Date Filter does not contain 2 date elements");
|
||||||
|
}
|
||||||
|
var startFrom = current_filter.getCriteria()[0];
|
||||||
|
var startTo = current_filter.getCriteria()[1];
|
||||||
|
var startFromFormatted = convertDate(startFrom);
|
||||||
|
var startToFormatted = convertDate(startTo);
|
||||||
|
var qualifier = "";
|
||||||
|
|
||||||
|
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;
|
||||||
|
return qualifier;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts the provided date string into a Remedy-specific date format and
|
||||||
|
* returns it.
|
||||||
|
* Throws a ValidationError if the provided date format cannot be parsed into
|
||||||
|
* the Remedy-specific date format.
|
||||||
|
*
|
||||||
|
* @param date the date string to convert
|
||||||
|
* @return the converted date string in Remedy-specific format
|
||||||
|
* @throws ValidationError if the provided date format cannot be parsed into the
|
||||||
|
* Remedy-specific date format
|
||||||
|
*/
|
||||||
|
private String convertDate(String date) throws ValidationError {
|
||||||
|
String inputFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
|
||||||
|
String outputFormat = "dd/MM/yyyy";
|
||||||
|
|
||||||
|
var parsed = "";
|
||||||
|
try {
|
||||||
|
LocalDateTime parser = LocalDateTime.parse(date, DateTimeFormatter.ofPattern(inputFormat));
|
||||||
|
parsed = parser.format(DateTimeFormatter.ofPattern(outputFormat));
|
||||||
|
} catch (DateTimeParseException e) {
|
||||||
|
throw new ValidationError("Provided date format cannot be parsed into Remedy specific date format");
|
||||||
|
}
|
||||||
|
return parsed;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue