151 lines
5.9 KiB
Java
151 lines
5.9 KiB
Java
package com.nttdata.calender.planTimes;
|
|
|
|
import java.text.SimpleDateFormat;
|
|
import java.time.DayOfWeek;
|
|
import java.time.LocalDate;
|
|
import java.time.format.DateTimeFormatter;
|
|
import java.time.temporal.IsoFields;
|
|
import java.time.temporal.TemporalAdjusters;
|
|
import java.util.ArrayList;
|
|
import java.util.Date;
|
|
import com.bmc.arsys.api.ARException;
|
|
import com.nttdata.calender.DateConverter;
|
|
import com.nttdata.calender.api.Query;
|
|
import com.nttdata.calender.api.RemedyJavaAPI;
|
|
import com.nttdata.calender.errorhandling.ErrorTypes.ValidationError;
|
|
|
|
/**
|
|
* The PlanTimes class manages the calculation of planned times for calendar weeks.
|
|
* It interacts with RemedyJavaAPI and performs queries to retrieve relevant data.
|
|
*/
|
|
public class PlanTimes {
|
|
private RemedyJavaAPI remedyJavaAPI;
|
|
private ArrayList<CalendarWeek> calenderWeeks;
|
|
private final String formName = "ASF:WI_TAS_Paket";
|
|
|
|
/**
|
|
* Constructs a PlanTimes object with the provided RemedyJavaAPI instance.
|
|
*
|
|
* @param remedyJavaAPI the RemedyJavaAPI instance to use for data retrieval
|
|
*/
|
|
public PlanTimes(RemedyJavaAPI remedyJavaAPI) {
|
|
this.remedyJavaAPI = remedyJavaAPI;
|
|
this.calenderWeeks = new ArrayList<>();
|
|
}
|
|
|
|
/**
|
|
* Sets the plan times for calendar weeks based on the provided PlanTimesRequest.
|
|
*
|
|
* @param req the PlanTimesRequest object containing the required information
|
|
* @throws ARException if an error occurs during the Remedy API operation
|
|
* @throws ValidationError if there is a validation error
|
|
*/
|
|
public void setPlanTimes(PlanTimesRequest req) throws ARException, ValidationError {
|
|
try {
|
|
var dateConverter = new DateConverter();
|
|
var startDate = dateConverter.convertDateTime(req.getRenderStartDate());
|
|
var endDate = dateConverter.convertDateTime(req.getRenderEndDate());
|
|
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy");
|
|
|
|
var queryChanges = new Query.QueryBuilder(formName).addFieldId("D2", 1000000350)
|
|
.addFieldId("ChangeNr", 1000000182).addFieldId("PlanTimeHours", 666000009)
|
|
.addFieldId("PlanTimeMinutes", 666000010).build();
|
|
|
|
while (!startDate.isAfter(endDate)) {
|
|
CalendarWeek calendarWeek = new CalendarWeek();
|
|
|
|
int startWeek = startDate.get(IsoFields.WEEK_OF_WEEK_BASED_YEAR);
|
|
int year = startDate.get(IsoFields.WEEK_BASED_YEAR);
|
|
String formattedStartWeek = String.format("%02d", startWeek);
|
|
|
|
LocalDate startOfWeek = startDate.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
|
|
LocalDate endOfWeek = startDate.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));
|
|
|
|
|
|
|
|
calendarWeek.setWeek(year + "w" + formattedStartWeek);
|
|
calendarWeek.setPlanTime(0);
|
|
calendarWeek.setStartDate(startOfWeek.format(formatter));
|
|
calendarWeek.setEndDate(endOfWeek.format(formatter));
|
|
|
|
calenderWeeks.add(calendarWeek);
|
|
startDate = startDate.with(TemporalAdjusters.next(DayOfWeek.MONDAY));
|
|
}
|
|
|
|
var filter = req.getFilter();
|
|
var qual = filter.constructQualifier(null, remedyJavaAPI);
|
|
var changesInDateRange = remedyJavaAPI.queryFieldsById(qual, queryChanges.getFieldIds(), formName, null, 0, 0);
|
|
|
|
for (var week : calenderWeeks) {
|
|
double planTimePerWeek = 0.0;
|
|
for (var change : changesInDateRange) {
|
|
LocalDate d2 = timestampToDate(change.get(queryChanges.getFieldId("D2")).toString());
|
|
LocalDate dateStart = LocalDate.parse(week.getStartDate(), formatter);
|
|
LocalDate dateEnd = LocalDate.parse(week.getEndDate(), formatter);
|
|
|
|
if (d2.isAfter(dateStart) && (d2.isEqual(dateEnd) || d2.isBefore(dateEnd))) {
|
|
var minutes = change.get(queryChanges.getFieldId("PlanTimeMinutes")).toString();
|
|
var hours = change.get(queryChanges.getFieldId("PlanTimeHours")).toString();
|
|
double plantime = convertPlanTime(hours, minutes);
|
|
planTimePerWeek += plantime;
|
|
}
|
|
}
|
|
week.setPlanTime((int) Math.ceil(planTimePerWeek));
|
|
}
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
* Converts plan time hours and minutes to a decimal value.
|
|
*
|
|
* @param hours the plan time hours as a String
|
|
* @param minutes the plan time minutes as a String
|
|
* @return the total plan time as a decimal value
|
|
* @throws ARException if an error occurs during the conversion
|
|
*/
|
|
public double convertPlanTime(String hours, String minutes) throws ARException {
|
|
double mins = 0.0;
|
|
double hrs = 0.0;
|
|
if (minutes != null) {
|
|
mins = (Double.valueOf(minutes) / 60.0); // Convert minutes to hours
|
|
}
|
|
if (hours != null) {
|
|
hrs = Double.valueOf(hours); // Correctly assign the value to hrs
|
|
}
|
|
double totalHours = hrs + mins;
|
|
return totalHours;
|
|
}
|
|
|
|
|
|
/**
|
|
* Converts a timestamp string to a LocalDate object.
|
|
*
|
|
* @param timestamp the timestamp string to convert
|
|
* @return the corresponding LocalDate object
|
|
*/
|
|
public LocalDate timestampToDate(String timestamp) {
|
|
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy");
|
|
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
|
|
String numericPart = timestamp.replaceAll("[^\\d]", "");
|
|
long ts = Long.parseLong(numericPart);
|
|
|
|
if (numericPart.length() <= 10)
|
|
ts *= 1000;
|
|
|
|
String date = sdf.format(new Date(ts));
|
|
return LocalDate.parse(date, formatter);
|
|
}
|
|
|
|
/**
|
|
* Retrieves the calculated plan times for calendar weeks.
|
|
*
|
|
* @return the list of CalendarWeek objects representing the plan times
|
|
*/
|
|
public ArrayList<CalendarWeek> getPlanTimes() {
|
|
return this.calenderWeeks;
|
|
}
|
|
}
|