Skip to content

Commit

Permalink
Change logic for custom fields
Browse files Browse the repository at this point in the history
  • Loading branch information
nntthuy-axonivy committed Dec 11, 2024
1 parent 2cdb75d commit d21ea94
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.axonivy.solutions.process.analyser.converter;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
import javax.faces.convert.FacesConverter;

import ch.ivyteam.ivy.environment.Ivy;

@FacesConverter("dateCustomFieldConverter")
public class DateCustomFieldConverter implements Converter {
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) throws ConverterException {
Ivy.log().warn("stringValue " + value);
if (value == null || value.trim().isEmpty()) {
return null;
}

try {
String[] dateRange = value.split(",");
Ivy.log().warn("dateRange " + dateRange.length);
if (dateRange.length == 1) {
Ivy.log().warn("here ");
throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Invalid Selection",
"Please select either no dates or two dates."));
}

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
List<LocalDate> dateList =
Arrays.stream(dateRange).map(date -> LocalDate.parse(date.trim(), formatter)).collect(Collectors.toList());

return dateList;

} catch (Exception e) {
throw new ConverterException(
new FacesMessage(FacesMessage.SEVERITY_ERROR, "Conversion Error", "Invalid date format"));
}
}

@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
Ivy.log().warn("value " + value);
if (value == null) {
return "";
}

if (value instanceof List) {
List<?> dateList = (List<?>) value;
return dateList.stream().map(Object::toString).collect(Collectors.joining(", "));
}
throw new ConverterException(
new FacesMessage(FacesMessage.SEVERITY_ERROR, "Conversion Error", "Invalid date object."));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ public void onCustomFieldSelect() {
selectedCustomFilters.add(customField);
} else if (!isSelectedCustomField) {
customField.setCustomFieldValues(new ArrayList<>());
customField.setTimestampCustomFieldValues(new ArrayList<>());
selectedCustomFilters.removeIf(selectedFilter -> selectedFilter.getCustomFieldMeta().name()
.equals(customField.getCustomFieldMeta().name()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,7 @@ public static List<ICase> getAllCasesFromTaskStartIdWithTimeInterval(Long taskSt

for (CustomFieldFilter customFieldFilter : validCustomFilters) {
CaseQuery customFieldQuery = CaseQuery.create();
handleQueryForEachFieldType(customFieldFilter, customFieldQuery);

allCustomFieldsQuery.where().or(customFieldQuery);
handleQueryForEachFieldType(customFieldFilter, customFieldQuery, allCustomFieldsQuery);
}
query.where().andOverall(allCustomFieldsQuery);
}
Expand All @@ -221,27 +219,49 @@ private static List<CustomFieldFilter> getValidCustomFilters(List<CustomFieldFil
|| ObjectUtils.isNotEmpty(filter.getTimestampCustomFieldValues())).collect(Collectors.toList());
}

private static void handleQueryForEachFieldType(CustomFieldFilter customFieldFilter, CaseQuery customFieldQuery) {
private static void handleQueryForEachFieldType(CustomFieldFilter customFieldFilter, CaseQuery customFieldQuery,
CaseQuery allCustomFieldsQuery) {
CustomFieldType customFieldType = customFieldFilter.getCustomFieldMeta().type();

switch (customFieldType) {
case TIMESTAMP:
addCustomFieldSubQuery(customFieldQuery, customFieldFilter, customFieldFilter.getTimestampCustomFieldValues());
addCustomFieldSubQueryForTimestamp(customFieldQuery, customFieldFilter, customFieldFilter.getTimestampCustomFieldValues());
allCustomFieldsQuery.where().and(customFieldQuery);
break;
case NUMBER:
addCustomFieldSubQuery(customFieldQuery, customFieldFilter, customFieldFilter.getCustomFieldValues());
allCustomFieldsQuery.where().and(customFieldQuery);
break;
case STRING:
case TEXT:
for (Object customFieldValue : customFieldFilter.getCustomFieldValues()) {
addCustomFieldSubQuery(customFieldQuery, customFieldFilter, customFieldValue);
}
allCustomFieldsQuery.where().or(customFieldQuery);
break;
default:
break;
}
}

private static void addCustomFieldSubQueryForTimestamp(CaseQuery customFieldQuery,
CustomFieldFilter customFieldFilter, List<LocalDate> timestampCustomFieldValues) {
boolean isCustomFieldFromCase = customFieldFilter.isCustomFieldFromCase();
String customFieldName = customFieldFilter.getCustomFieldMeta().name();

Date startDate = DateUtils.getDateFromLocalDate(timestampCustomFieldValues.get(0), null);
Date endDate = DateUtils.getDateFromLocalDate(timestampCustomFieldValues.get(1), LocalTime.MAX);

if (isCustomFieldFromCase) {
customFieldQuery.where().or().customField().timestampField(customFieldName).isGreaterOrEqualThan(startDate).and()
.customField().timestampField(customFieldName).isLowerOrEqualThan(endDate);
} else {
customFieldQuery.where().or().tasks(
TaskQuery.create().where().customField().timestampField(customFieldName).isGreaterOrEqualThan(startDate).and()
.customField().timestampField(customFieldName).isLowerOrEqualThan(endDate));
}
}

/**
* Cast values to right type and Create sub-query for each custom field type from case or task
**/
Expand Down Expand Up @@ -294,21 +314,6 @@ private static void addCustomFieldSubQuery(CaseQuery customFieldQuery, CustomFie
.isLowerOrEqualThan(endNumber));
}
break;
case TIMESTAMP:
List<LocalDate> dateRange = (List<LocalDate>) customFieldValue;
Date startDate = DateUtils.getDateFromLocalDate(dateRange.get(0), null);
Date endDate = DateUtils.getDateFromLocalDate(dateRange.get(1), LocalTime.MAX);

if (isCustomFieldFromCase) {
customFieldQuery.where().or().customField().timestampField(customFieldName).isGreaterOrEqualThan(startDate)
.and().customField().timestampField(customFieldName).isLowerOrEqualThan(endDate);
} else {
customFieldQuery.where().or()
.tasks(TaskQuery.create().where().customField().timestampField(customFieldName)
.isGreaterOrEqualThan(startDate).and().customField().timestampField(customFieldName)
.isLowerOrEqualThan(endDate));
}
break;
default:
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,8 @@
<h:panelGroup layout="block" rendered="#{'TIMESTAMP' == customField.customFieldMeta.type()}">
<p:datePicker id="date-range" selectionMode="range" showIcon="true"
maxdate="#{managedBean.timeIntervalFilter.to}" styleClass="col-12 m-0 p-0 grid formgrid"
value="#{customField.timestampCustomFieldValues}" readonlyInput="true" required="true"
pattern="dd/MM/yyyy"
requiredMessage="#{ivy.cms.co('/Dialogs/com/axonivy/solutions/process/analyser/ProcessesMonitor/DateErrorMessage')}">
value="#{customField.timestampCustomFieldValues}"
pattern="dd/MM/yyyy">
<p:ajax event="dateSelect" process="@this" partialSubmit="true" listener="#{managedBean.updateDiagramAndStatistic}"/>
</p:datePicker>
<p:message styleClass="col-12 m-0 p-2 grid formgrid" for="date-range" />
Expand Down

0 comments on commit d21ea94

Please sign in to comment.