Commit c366c139 authored by Adrien's avatar Adrien Committed by Romain Bioteau

Date widget required property reflect the mandatory bdm value (#2730)

*  Date widget required property reflect the mandatory bdm value
* widget in multiple containers are mandatory

[UID-101](https://bonitasoft.atlassian.net/browse/UID-101)
parent a1bedb8b
......@@ -17,15 +17,10 @@ package org.bonitasoft.web.designer.config;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import javax.validation.Validation;
import javax.validation.Validator;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.joda.JodaModule;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import org.bonitasoft.web.designer.controller.asset.AssetService;
import org.bonitasoft.web.designer.controller.export.Exporter;
import org.bonitasoft.web.designer.controller.export.steps.AssetExportStep;
......@@ -78,6 +73,13 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.client.RestTemplate;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.joda.JodaModule;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
/**
* @author Guillaume EHRET
*/
......
......@@ -35,7 +35,6 @@ import org.bonitasoft.web.designer.experimental.parametrizedWidget.Labeled;
import org.bonitasoft.web.designer.experimental.parametrizedWidget.ParameterConstants;
import org.bonitasoft.web.designer.experimental.parametrizedWidget.ParameterType;
import org.bonitasoft.web.designer.experimental.parametrizedWidget.ParametrizedWidgetFactory;
import org.bonitasoft.web.designer.experimental.parametrizedWidget.Requirable;
import org.bonitasoft.web.designer.experimental.parametrizedWidget.TextWidget;
import org.bonitasoft.web.designer.experimental.parametrizedWidget.TitleWidget;
import org.bonitasoft.web.designer.experimental.parametrizedWidget.Valuable;
......@@ -147,9 +146,6 @@ public class ContractInputToWidgetMapper {
if (component instanceof Valuable) {
((Valuable) component).setValue(ParametrizedWidgetFactory.ITEM_ITERATOR);
}
if (component instanceof Requirable) {
((Requirable) component).setRequired(false);
}
List<Element> row = new ArrayList<>();
row.add(component.toComponent(dimensionFactory));
if (!contractInput.isReadOnly()) {
......
......@@ -20,6 +20,9 @@ import static java.util.Collections.EMPTY_LIST;
import java.io.File;
import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
......@@ -44,11 +47,14 @@ public class FormInputVisitor implements ContractInputVisitor {
defaultValues.put(Float.class.getName(), 0);
defaultValues.put(Long.class.getName(), 0);
defaultValues.put(Date.class.getName(), null);
defaultValues.put(LocalDate.class.getName(), null);
defaultValues.put(LocalDateTime.class.getName(), null);
defaultValues.put(OffsetDateTime.class.getName(), null);
defaultValues.put(File.class.getName(), null);
}
private Map<String, Object> properties = newLinkedHashMap();
private JacksonObjectMapper objectMapperWrapper;
public FormInputVisitor(JacksonObjectMapper objectMapperWrapper) {
......
......@@ -119,7 +119,7 @@ public class ParametrizedWidgetFactory {
fileUploadWidget.setPlaceholder(input.getMode() == EditMode.EDIT
? "Browse to update the file..."
: "Browse to upload a new file...");
fileUploadWidget.setRequired(input.isMandatory());
fileUploadWidget.setRequired(input.isMultiple() || input.isMandatory());
return fileUploadWidget;
}
......@@ -143,6 +143,9 @@ public class ParametrizedWidgetFactory {
datePickerComponent.setPlaceholder(reference.getPlaceholder());
datePickerComponent.setShowToday(reference.getShowToday());
datePickerComponent.setTodayLabel(reference.getTodayLabel());
datePickerComponent.setRequired(input.isMultiple() || input.isMandatory());
datePickerComponent.setLabelHidden(input.isMultiple());
setValuableWidgetValue(input, datePickerComponent);
return datePickerComponent;
......@@ -162,6 +165,9 @@ public class ParametrizedWidgetFactory {
dateTimePickerComponent.setShowNow(reference.getShowNow());
dateTimePickerComponent.setShowToday(reference.getShowToday());
dateTimePickerComponent.setInlineInput(reference.getInlineInput());
dateTimePickerComponent.setRequired(input.isMultiple() || input.isMandatory());
dateTimePickerComponent.setLabelHidden(input.isMultiple());
setValuableWidgetValue(input, dateTimePickerComponent);
return dateTimePickerComponent;
......@@ -183,7 +189,8 @@ public class ParametrizedWidgetFactory {
protected InputWidget createInputWidget(ContractInput input) {
InputWidget inputWidget = inputDefaultWidgetParameters(input, new InputWidget());
inputWidget.setRequired(input.isMandatory());
inputWidget.setRequired(input.isMultiple() || input.isMandatory());
inputWidget.setLabelHidden(input.isMultiple());
inputWidget.setPlaceholder(new PbInput().getPlaceholder());
setValuableWidgetValue(input, inputWidget);
return inputWidget;
......@@ -295,7 +302,7 @@ public class ParametrizedWidgetFactory {
selectWidget.setLabel(label);
selectWidget.setLabelPosition(LabelPosition.TOP);
selectWidget.setPlaceholder(String.format("Select a %s", label));
selectWidget.setRequired(input.isMandatory());
selectWidget.setRequired(input.isMultiple() || input.isMandatory());
selectWidget.setAvailableValues(toBusinessQueryDataName(input.getDataReference()));
if (input.isMultiple()) {
selectWidget.setValue(ITEM_ITERATOR);
......
......@@ -19,12 +19,10 @@ import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.type.MapType;
import com.fasterxml.jackson.databind.type.TypeFactory;
......@@ -77,7 +75,13 @@ public class JacksonObjectMapper {
}
public String prettyPrint(Object object) throws IOException {
return objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(object);
objectMapper.setSerializationInclusion(JsonInclude.Include.ALWAYS);
try {
return objectMapper
.writerWithDefaultPrettyPrinter().writeValueAsString(object);
}finally {
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
}
}
public String prettyPrint(String json) throws IOException {
......
......@@ -17,18 +17,30 @@ package org.bonitasoft.web.designer.experimental.mapping;
import static org.assertj.core.api.Assertions.assertThat;
import static org.bonitasoft.web.designer.model.contract.builders.ContractBuilder.aContract;
import static org.bonitasoft.web.designer.model.contract.builders.ContractInputBuilder.*;
import static org.bonitasoft.web.designer.model.contract.builders.ContractInputBuilder.aBooleanContractInput;
import static org.bonitasoft.web.designer.model.contract.builders.ContractInputBuilder.aFileContractInput;
import static org.bonitasoft.web.designer.model.contract.builders.ContractInputBuilder.aLocalDateContractInput;
import static org.bonitasoft.web.designer.model.contract.builders.ContractInputBuilder.aMultipleStringContractInput;
import static org.bonitasoft.web.designer.model.contract.builders.ContractInputBuilder.aNodeContractInput;
import static org.bonitasoft.web.designer.model.contract.builders.ContractInputBuilder.aStringContractInput;
import static org.bonitasoft.web.designer.model.contract.builders.ContractInputBuilder.anIntegerContractInput;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.util.Date;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.bonitasoft.web.designer.experimental.mapping.data.FormInputVisitor;
import org.bonitasoft.web.designer.model.JacksonObjectMapper;
import org.bonitasoft.web.designer.model.contract.Contract;
import org.bonitasoft.web.designer.model.contract.BusinessDataReference;
import org.bonitasoft.web.designer.model.contract.BusinessDataReference.LoadingType;
import org.bonitasoft.web.designer.model.contract.BusinessDataReference.RelationType;
import org.bonitasoft.web.designer.model.contract.Contract;
import org.bonitasoft.web.designer.model.contract.LeafContractInput;
import org.junit.Before;
import org.junit.Test;
import com.fasterxml.jackson.databind.ObjectMapper;
public class FormInputVisitorTest {
JacksonObjectMapper objectMapper = new JacksonObjectMapper(new ObjectMapper());
......@@ -66,17 +78,21 @@ public class FormInputVisitorTest {
contract.accept(visitor);
assertThat(visitor.toJson())
.isEqualTo(objectMapper.prettyPrint("{\"person\":{\"name\":\"\",\"details\":{\"age\":0}},\"accepted\":false}"));
.isEqualTo(
objectMapper.prettyPrint("{\"person\":{\"name\":\"\",\"details\":{\"age\":0}},\"accepted\":false}"));
}
@Test
public void should_ignore_complex_types_with_dataRef_in_formInput_in_edit_mode() throws Exception {
Contract contract = aContract().inEditMode().withInput(
aNodeContractInput("person").withDataReference(new BusinessDataReference("person","org.test.Person",RelationType.COMPOSITION,LoadingType.EAGER)).withInput(
aStringContractInput("name"),
aNodeContractInput("details")
.withInput(anIntegerContractInput("age"))
.build())
aNodeContractInput("person")
.withDataReference(new BusinessDataReference("person", "org.test.Person", RelationType.COMPOSITION,
LoadingType.EAGER))
.withInput(
aStringContractInput("name"),
aNodeContractInput("details")
.withInput(anIntegerContractInput("age"))
.build())
.build(),
aBooleanContractInput("accepted")).build();
......@@ -106,4 +122,30 @@ public class FormInputVisitorTest {
assertThat(visitor.toJson()).isEqualTo(objectMapper.prettyPrint("{\"myFile\":null}"));
}
@Test
public void should_accept_non_mandatory_local_date() throws Exception {
LeafContractInput aLocalDateContractInput = aLocalDateContractInput("myDate");
LeafContractInput aLocalDateTimeContractInput = new LeafContractInput("myLocalDateTime", LocalDateTime.class);
LeafContractInput aOffsetDateTimeContractInput = new LeafContractInput("myOffsetDateTime", OffsetDateTime.class);
LeafContractInput aLegacyDateContractInput = new LeafContractInput("myLgacyDate", Date.class);
aLocalDateContractInput.setMandatory(false);
aLocalDateTimeContractInput.setMandatory(false);
aOffsetDateTimeContractInput.setMandatory(false);
aLegacyDateContractInput.setMandatory(false);
Contract contract = aContract().withInput(aLocalDateContractInput,
aLocalDateTimeContractInput,
aOffsetDateTimeContractInput,
aLegacyDateContractInput)
.build();
contract.accept(visitor);
assertThat(visitor.toJson()).contains("\"myDate\" : null");
assertThat(visitor.toJson()).contains("\"myLocalDateTime\" : null");
assertThat(visitor.toJson()).contains("\"myOffsetDateTime\" : null");
assertThat(visitor.toJson()).contains("\"myLgacyDate\" : null");
}
}
......@@ -524,6 +524,55 @@ public class ParametrizedWidgetFactoryTest implements ParameterConstants {
assertThat(selectWidget.getValue()).isEqualTo(ParametrizedWidgetFactory.ITEM_ITERATOR);
}
@Test
public void should_set_required_false_for_non_mandatory_contract_input() {
//DatePicker
LeafContractInput aLocalDateContractInput = aLocalDateContractInput("myDate");
aLocalDateContractInput.setMandatory(false);
AbstractParametrizedWidget widget = createFactory().createParametrizedWidget(aLocalDateContractInput);
assertThat(widget).isInstanceOf(DatePickerWidget.class);
assertThat(((DatePickerWidget) widget).isRequired()).isFalse();
aLocalDateContractInput.setMandatory(true);
widget = createFactory().createParametrizedWidget(aLocalDateContractInput);
assertThat(widget).isInstanceOf(DatePickerWidget.class);
assertThat(((DatePickerWidget) widget).isRequired()).isTrue();
//DateTimePIcker
LeafContractInput aLocalDateTimeContractInput = aLocalDateTimeContractInput("myLocalDate");
aLocalDateTimeContractInput.setMandatory(false);
widget = createFactory().createParametrizedWidget(aLocalDateTimeContractInput);
assertThat(widget).isInstanceOf(DateTimePickerWidget.class);
assertThat(((DateTimePickerWidget) widget).isRequired()).isFalse();
aLocalDateTimeContractInput.setMandatory(true);
widget = createFactory().createParametrizedWidget(aLocalDateTimeContractInput);
assertThat(widget).isInstanceOf(DateTimePickerWidget.class);
assertThat(((DateTimePickerWidget) widget).isRequired()).isTrue();
//InputWidget
LeafContractInput aInputWidget = aStringContractInput("myString");
aInputWidget.setMandatory(false);
widget = createFactory().createParametrizedWidget(aInputWidget);
assertThat(widget).isInstanceOf(InputWidget.class);
assertThat(((InputWidget) widget).isRequired()).isFalse();
aInputWidget.setMandatory(true);
widget = createFactory().createParametrizedWidget(aInputWidget);
assertThat(widget).isInstanceOf(InputWidget.class);
assertThat(((InputWidget) widget).isRequired()).isTrue();
//Multiple
aInputWidget.setMultiple(true);
aInputWidget.setMandatory(false);
widget = createFactory().createParametrizedWidget(aInputWidget);
assertThat(widget).isInstanceOf(InputWidget.class);
assertThat(((InputWidget) widget).isRequired()).isTrue();
}
private ParametrizedWidgetFactory createFactory() {
return new ParametrizedWidgetFactory();
}
......
......@@ -91,10 +91,11 @@ public class JacksonObjectMapperTest {
assertThat(objectMapper.prettyPrint(object)).isEqualTo("{" + System.lineSeparator() +
" \"id\" : \"id\"," + System.lineSeparator() +
" \"name\" : \"Vincent\"," + System.lineSeparator() +
" \"number\" : 1" + System.lineSeparator() +
" \"number\" : 1," + System.lineSeparator() +
" \"another\" : null" + System.lineSeparator() +
"}");
}
@Test
public void should_format_json_when_using_pretty_print_on_json() throws Exception {
assertThat(objectMapper.prettyPrint("{\"foo\":\"bar\"}")).isEqualTo("{" + System.lineSeparator() +
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment