Tony CHEMIT pushed to branch develop-9.0.x at ultreiaio / ird-observe
Commits:
-
984da686
by Tony Chemit at 2023-02-14T10:08:49+01:00
15 changed files:
- client/datasource/editor/api/src/main/java/fr/ird/observe/client/datasource/editor/api/content/data/table/ContentTableUIModelStates.java
- client/datasource/editor/ll/src/main/java/fr/ird/observe/client/datasource/editor/ll/data/observation/SetDetailCompositionUIModelStates.java
- client/datasource/editor/ps/src/main/java/fr/ird/observe/client/datasource/editor/ps/data/localmarket/SampleUIModelStates.java
- client/datasource/editor/ps/src/main/java/fr/ird/observe/client/datasource/editor/ps/data/localmarket/SurveyUIModelStates.java
- client/datasource/editor/ps/src/main/java/fr/ird/observe/client/datasource/editor/ps/data/logbook/FloatingObjectUIModelStates.java
- client/datasource/editor/ps/src/main/java/fr/ird/observe/client/datasource/editor/ps/data/logbook/SampleUIModelStates.java
- client/datasource/editor/ps/src/main/java/fr/ird/observe/client/datasource/editor/ps/data/observation/FloatingObjectUIModelStates.java
- toolkit/api/src/main/java/fr/ird/observe/dto/BusinessDto.java
- toolkit/api/src/main/java/fr/ird/observe/dto/IdDto.java
- toolkit/api/src/main/java/fr/ird/observe/dto/data/ContainerDto.java
- toolkit/api/src/main/java/fr/ird/observe/dto/data/EditableDto.java
- toolkit/api/src/main/java/fr/ird/observe/dto/data/SimpleDto.java
- toolkit/api/src/main/java/fr/ird/observe/dto/data/WithSimpleComment.java
- + toolkit/templates/src/main/java/fr/ird/observe/toolkit/templates/dto/AutoTrimGenerator.java
- toolkit/templates/src/main/java/fr/ird/observe/toolkit/templates/dto/DtoTransformer.java
Changes:
| ... | ... | @@ -180,10 +180,11 @@ public class ContentTableUIModelStates<D extends DataDto, C extends ContainerChi |
| 180 | 180 | |
| 181 | 181 | @Override
|
| 182 | 182 | public D getBeanToSave() {
|
| 183 | - D bean = super.getBeanToSave();
|
|
| 183 | + D bean = getBean();
|
|
| 184 | 184 | if (sortable) {
|
| 185 | 185 | bean.set(getTableModel().getChildrenPropertyName(), getTableModel().getData());
|
| 186 | 186 | }
|
| 187 | + bean.autoTrim();
|
|
| 187 | 188 | return bean;
|
| 188 | 189 | }
|
| 189 | 190 |
| ... | ... | @@ -175,13 +175,14 @@ public class SetDetailCompositionUIModelStates extends GeneratedSetDetailComposi |
| 175 | 175 | |
| 176 | 176 | @Override
|
| 177 | 177 | public SetDetailCompositionDto getBeanToSave() {
|
| 178 | - SetDetailCompositionDto bean = super.getBeanToSave();
|
|
| 178 | + SetDetailCompositionDto bean = getBean();
|
|
| 179 | 179 | SectionTableModel sectionsTableModel = getSectionsTableModel();
|
| 180 | 180 | SectionDto selectedSection = sectionsTableModel.getSelectedRow();
|
| 181 | 181 | if (selectedSection != null) {
|
| 182 | 182 | // flush selected section before save
|
| 183 | 183 | getBasketsTableModel().flushSection(selectedSection, getBranchlinesTableModel());
|
| 184 | 184 | }
|
| 185 | + bean.autoTrim();
|
|
| 185 | 186 | return bean;
|
| 186 | 187 | }
|
| 187 | 188 |
| ... | ... | @@ -62,9 +62,10 @@ public class SampleUIModelStates extends GeneratedSampleUIModelStates { |
| 62 | 62 | |
| 63 | 63 | @Override
|
| 64 | 64 | public SampleDto getBeanToSave() {
|
| 65 | - SampleDto bean = super.getBeanToSave();
|
|
| 65 | + SampleDto bean = getBean();
|
|
| 66 | 66 | // on keep valid well ids
|
| 67 | 67 | bean.setWell(SampleDto.toIds(getWellTableModel().getValidData()));
|
| 68 | + bean.autoTrim();
|
|
| 68 | 69 | return bean;
|
| 69 | 70 | }
|
| 70 | 71 |
| ... | ... | @@ -65,9 +65,10 @@ public class SurveyUIModelStates extends GeneratedSurveyUIModelStates { |
| 65 | 65 | |
| 66 | 66 | @Override
|
| 67 | 67 | public SurveyDto getBeanToSave() {
|
| 68 | - SurveyDto beanToSave = super.getBeanToSave();
|
|
| 69 | - beanToSave.setSurveyPart(getSurveyPartTableModel().getValidData());
|
|
| 70 | - return beanToSave;
|
|
| 68 | + SurveyDto bean = getBean();
|
|
| 69 | + bean.setSurveyPart(getSurveyPartTableModel().getValidData());
|
|
| 70 | + bean.autoTrim();
|
|
| 71 | + return bean;
|
|
| 71 | 72 | }
|
| 72 | 73 | |
| 73 | 74 | public SurveyPartTableModel getSurveyPartTableModel() {
|
| ... | ... | @@ -107,11 +107,12 @@ public class FloatingObjectUIModelStates extends GeneratedFloatingObjectUIModelS |
| 107 | 107 | |
| 108 | 108 | @Override
|
| 109 | 109 | public FloatingObjectDto getBeanToSave() {
|
| 110 | - FloatingObjectDto bean = super.getBeanToSave();
|
|
| 110 | + FloatingObjectDto bean = getBean();
|
|
| 111 | 111 | recomputeComputedValues();
|
| 112 | 112 | Set<FloatingObjectPartDto> parts = toParts();
|
| 113 | 113 | log.info(String.format("will persist %d part(s).", parts.size()));
|
| 114 | 114 | bean.setFloatingObjectPart(new LinkedHashSet<>(parts));
|
| 115 | + bean.autoTrim();
|
|
| 115 | 116 | return bean;
|
| 116 | 117 | }
|
| 117 | 118 |
| ... | ... | @@ -84,8 +84,9 @@ public class SampleUIModelStates extends GeneratedSampleUIModelStates { |
| 84 | 84 | |
| 85 | 85 | @Override
|
| 86 | 86 | public SampleDto getBeanToSave() {
|
| 87 | - SampleDto bean = super.getBeanToSave();
|
|
| 87 | + SampleDto bean = getBean();
|
|
| 88 | 88 | bean.setSampleActivity(getSampleActivityTableModel().getValidData());
|
| 89 | + bean.autoTrim();
|
|
| 89 | 90 | return bean;
|
| 90 | 91 | }
|
| 91 | 92 | } |
| ... | ... | @@ -111,11 +111,12 @@ public class FloatingObjectUIModelStates extends GeneratedFloatingObjectUIModelS |
| 111 | 111 | |
| 112 | 112 | @Override
|
| 113 | 113 | public FloatingObjectDto getBeanToSave() {
|
| 114 | - FloatingObjectDto bean = super.getBeanToSave();
|
|
| 114 | + FloatingObjectDto bean = getBean();
|
|
| 115 | 115 | recomputeComputedValues();
|
| 116 | 116 | Set<FloatingObjectPartDto> parts = toParts();
|
| 117 | 117 | log.info(String.format("will persist %d part(s).", parts.size()));
|
| 118 | 118 | bean.setFloatingObjectPart(new LinkedHashSet<>(parts));
|
| 119 | + bean.autoTrim();
|
|
| 119 | 120 | return bean;
|
| 120 | 121 | }
|
| 121 | 122 |
| ... | ... | @@ -34,9 +34,7 @@ import java.util.Collection; |
| 34 | 34 | import java.util.Date;
|
| 35 | 35 | import java.util.Objects;
|
| 36 | 36 | import java.util.Optional;
|
| 37 | -import java.util.function.Consumer;
|
|
| 38 | 37 | import java.util.function.Predicate;
|
| 39 | -import java.util.function.Supplier;
|
|
| 40 | 38 | |
| 41 | 39 | /**
|
| 42 | 40 | * Created on 21/07/2021.
|
| ... | ... | @@ -128,7 +128,7 @@ public abstract class IdDto extends AbstractJavaBean implements ObserveDto, DtoR |
| 128 | 128 | public void autoTrim() {
|
| 129 | 129 | }
|
| 130 | 130 | |
| 131 | - protected void autoTrim(Supplier<String> getter, Consumer<String> setter) {
|
|
| 131 | + public static void autoTrim(Supplier<String> getter, Consumer<String> setter) {
|
|
| 132 | 132 | String stringValue = getter.get();
|
| 133 | 133 | if (stringValue != null) {
|
| 134 | 134 | String stringTrimmedValue = stringValue.trim();
|
| ... | ... | @@ -81,4 +81,9 @@ public abstract class ContainerDto<C extends ContainerChildDto> extends DataDto |
| 81 | 81 | firePropertyChange(PROPERTY_CHILDREN, oldValue, children);
|
| 82 | 82 | }
|
| 83 | 83 | |
| 84 | + @Override
|
|
| 85 | + public void autoTrim() {
|
|
| 86 | + super.autoTrim();
|
|
| 87 | + WithSimpleComment.super.autoTrim();
|
|
| 88 | + }
|
|
| 84 | 89 | } |
| ... | ... | @@ -55,6 +55,6 @@ public abstract class EditableDto extends DataDto implements WithSimpleComment { |
| 55 | 55 | @Override
|
| 56 | 56 | public void autoTrim() {
|
| 57 | 57 | super.autoTrim();
|
| 58 | - autoTrim(this::getComment, this::setComment);
|
|
| 58 | + WithSimpleComment.super.autoTrim();
|
|
| 59 | 59 | }
|
| 60 | 60 | } |
| ... | ... | @@ -46,4 +46,9 @@ public abstract class SimpleDto extends DataDto implements WithSimpleComment { |
| 46 | 46 | firePropertyChange(PROPERTY_COMMENT, oldValue, comment);
|
| 47 | 47 | }
|
| 48 | 48 | |
| 49 | + @Override
|
|
| 50 | + public void autoTrim() {
|
|
| 51 | + super.autoTrim();
|
|
| 52 | + WithSimpleComment.super.autoTrim();
|
|
| 53 | + }
|
|
| 49 | 54 | } |
| ... | ... | @@ -22,6 +22,7 @@ package fr.ird.observe.dto.data; |
| 22 | 22 | * #L%
|
| 23 | 23 | */
|
| 24 | 24 | |
| 25 | +import fr.ird.observe.dto.IdDto;
|
|
| 25 | 26 | import fr.ird.observe.dto.ObserveDto;
|
| 26 | 27 | import io.ultreia.java4all.bean.JavaBean;
|
| 27 | 28 | |
| ... | ... | @@ -37,4 +38,8 @@ public interface WithSimpleComment extends JavaBean, ObserveDto { |
| 37 | 38 | |
| 38 | 39 | void setComment(String comment);
|
| 39 | 40 | |
| 41 | + default void autoTrim() {
|
|
| 42 | + IdDto.autoTrim(this::getComment, this::setComment);
|
|
| 43 | + }
|
|
| 44 | + |
|
| 40 | 45 | } |
| 1 | +package fr.ird.observe.toolkit.templates.dto;
|
|
| 2 | + |
|
| 3 | +/*-
|
|
| 4 | + * #%L
|
|
| 5 | + * ObServe Toolkit :: Templates
|
|
| 6 | + * %%
|
|
| 7 | + * Copyright (C) 2008 - 2023 IRD, Ultreia.io
|
|
| 8 | + * %%
|
|
| 9 | + * This program is free software: you can redistribute it and/or modify
|
|
| 10 | + * it under the terms of the GNU General Public License as
|
|
| 11 | + * published by the Free Software Foundation, either version 3 of the
|
|
| 12 | + * License, or (at your option) any later version.
|
|
| 13 | + *
|
|
| 14 | + * This program is distributed in the hope that it will be useful,
|
|
| 15 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
| 16 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
| 17 | + * GNU General Public License for more details.
|
|
| 18 | + *
|
|
| 19 | + * You should have received a copy of the GNU General Public
|
|
| 20 | + * License along with this program. If not, see
|
|
| 21 | + * <http://www.gnu.org/licenses/gpl-3.0.html>.
|
|
| 22 | + * #L%
|
|
| 23 | + */
|
|
| 24 | + |
|
| 25 | + |
|
| 26 | +/*{generator option: parentheses = false}*/
|
|
| 27 | +/*{generator option: writeString = +}*/
|
|
| 28 | + |
|
| 29 | +import fr.ird.observe.toolkit.templates.TemplateContract;
|
|
| 30 | +import fr.ird.observe.toolkit.templates.validation.ValidationTagValues;
|
|
| 31 | +import io.ultreia.java4all.lang.Strings;
|
|
| 32 | +import org.nuiton.eugene.GeneratorUtil;
|
|
| 33 | +import org.nuiton.eugene.models.object.ObjectModel;
|
|
| 34 | +import org.nuiton.eugene.models.object.ObjectModelAttribute;
|
|
| 35 | +import org.nuiton.eugene.models.object.ObjectModelClass;
|
|
| 36 | +import org.nuiton.eugene.models.object.ObjectModelJavaModifier;
|
|
| 37 | +import org.nuiton.eugene.models.object.ObjectModelOperation;
|
|
| 38 | + |
|
| 39 | +import java.util.Collection;
|
|
| 40 | +import java.util.LinkedList;
|
|
| 41 | +import java.util.List;
|
|
| 42 | +import java.util.function.BiPredicate;
|
|
| 43 | + |
|
| 44 | +/**
|
|
| 45 | + * To generate autoTrim method on a class.
|
|
| 46 | + * <p>
|
|
| 47 | + * Created on 14/02/2023.
|
|
| 48 | + *
|
|
| 49 | + * @author Tony Chemit - dev@tchemit.fr
|
|
| 50 | + * @since 9.0.25
|
|
| 51 | + */
|
|
| 52 | +public class AutoTrimGenerator {
|
|
| 53 | + |
|
| 54 | + private final ValidationTagValues validationTagValues;
|
|
| 55 | + private final TemplateContract template;
|
|
| 56 | + private final ObjectModel model;
|
|
| 57 | + |
|
| 58 | + private final BiPredicate<ObjectModelClass, ObjectModelAttribute> compositeAttributePredicate;
|
|
| 59 | + |
|
| 60 | + public AutoTrimGenerator(ValidationTagValues validationTagValues,
|
|
| 61 | + TemplateContract template,
|
|
| 62 | + ObjectModel model,
|
|
| 63 | + BiPredicate<ObjectModelClass, ObjectModelAttribute> compositeAttributePredicate) {
|
|
| 64 | + this.validationTagValues = validationTagValues;
|
|
| 65 | + this.template = template;
|
|
| 66 | + this.model = model;
|
|
| 67 | + this.compositeAttributePredicate = compositeAttributePredicate;
|
|
| 68 | + }
|
|
| 69 | + |
|
| 70 | + public void generate(ObjectModelClass input, ObjectModelClass output) {
|
|
| 71 | + Collection<ObjectModelAttribute> attributes = new LinkedList<>(input.getAttributes());
|
|
| 72 | + |
|
| 73 | + List<String> stringPropertyNames = TemplateContract.getProperties(input, attributes, (c, a) -> "String".equals(GeneratorUtil.getSimpleName(a.getType())) && validationTagValues.isNotBlank(model.getTagValuesStore(), c, a));
|
|
| 74 | + List<String> compositionPropertyNames = TemplateContract.getProperties(input, attributes, (c, a) -> !GeneratorUtil.isNMultiplicity(a) && compositeAttributePredicate.test(c, a));
|
|
| 75 | + List<String> multipleCompositionPropertyNames = TemplateContract.getProperties(input, attributes, (c, a) -> GeneratorUtil.isNMultiplicity(a) && compositeAttributePredicate.test(c, a));
|
|
| 76 | + if (stringPropertyNames.isEmpty()
|
|
| 77 | + && compositionPropertyNames.isEmpty()
|
|
| 78 | + && multipleCompositionPropertyNames.isEmpty()) {
|
|
| 79 | + return;
|
|
| 80 | + }
|
|
| 81 | + |
|
| 82 | + ObjectModelOperation autoTrimMethod = template.addOperation(output, "autoTrim", void.class.getName(), ObjectModelJavaModifier.PUBLIC);
|
|
| 83 | + template.addAnnotation(output, autoTrimMethod, Override.class);
|
|
| 84 | + StringBuilder content = new StringBuilder(""/*{
|
|
| 85 | + super.autoTrim();}*/);
|
|
| 86 | + for (String property : stringPropertyNames) {
|
|
| 87 | + String capitalize = Strings.capitalize(property);
|
|
| 88 | + content.append(""/*{
|
|
| 89 | + autoTrim(this::get<%=capitalize%>, this::set<%=capitalize%>);}*/);
|
|
| 90 | + }
|
|
| 91 | + for (String property : compositionPropertyNames) {
|
|
| 92 | + String capitalize = Strings.capitalize(property);
|
|
| 93 | + content.append(""/*{
|
|
| 94 | + if (get<%=capitalize%>() != null) {
|
|
| 95 | + get<%=capitalize%>().autoTrim();
|
|
| 96 | + }}*/);
|
|
| 97 | + }
|
|
| 98 | + for (String property : multipleCompositionPropertyNames) {
|
|
| 99 | + String capitalize = Strings.capitalize(property);
|
|
| 100 | + content.append(""/*{
|
|
| 101 | + if (isNot<%=capitalize%>Empty()) {
|
|
| 102 | + get<%=capitalize%>().forEach(c -> c.autoTrim());
|
|
| 103 | + }}*/);
|
|
| 104 | + }
|
|
| 105 | + template.setOperationBody(autoTrimMethod, ""/*{<%=content.toString()%>
|
|
| 106 | + }*/);
|
|
| 107 | + }
|
|
| 108 | +} |
| ... | ... | @@ -38,6 +38,7 @@ import fr.ird.observe.dto.data.DataGroupByType; |
| 38 | 38 | import fr.ird.observe.dto.data.EditableDto;
|
| 39 | 39 | import fr.ird.observe.dto.data.InlineDataDto;
|
| 40 | 40 | import fr.ird.observe.dto.data.LayoutAware;
|
| 41 | +import fr.ird.observe.dto.data.NoValidationDto;
|
|
| 41 | 42 | import fr.ird.observe.dto.data.OpenableDto;
|
| 42 | 43 | import fr.ird.observe.dto.data.RootOpenableDto;
|
| 43 | 44 | import fr.ird.observe.dto.data.UsingLayout;
|
| ... | ... | @@ -132,6 +133,7 @@ public class DtoTransformer extends BeanTransformer implements TemplateContract |
| 132 | 133 | private Map<String, String> decomposeTimes;
|
| 133 | 134 | private Map<String, String> decomposeDates;
|
| 134 | 135 | private String currentTimestampPropertyName;
|
| 136 | + private AutoTrimGenerator autoTrimGenerator;
|
|
| 135 | 137 | |
| 136 | 138 | public static String getCoordinateField(boolean before, String coordinatePrefix, String type) {
|
| 137 | 139 | return Introspector.decapitalize(before ? (type + coordinatePrefix) : (coordinatePrefix + type));
|
| ... | ... | @@ -530,28 +532,16 @@ public class DtoTransformer extends BeanTransformer implements TemplateContract |
| 530 | 532 | return <%=content%>;
|
| 531 | 533 | }*/);
|
| 532 | 534 | }
|
| 533 | - Collection<ObjectModelAttribute> attributes = new LinkedList<>(input.getAttributes());
|
|
| 534 | - List<String> notBlankString = TemplateContract.getProperties(input, attributes, (c, a) -> validationTagValues.isNotBlank(tagValuesStore, c, a) && "String".equals(GeneratorUtil.getSimpleName(a.getType())));
|
|
| 535 | - addAutoTrim(output, notBlankString);
|
|
| 536 | - return output;
|
|
| 537 | - }
|
|
| 538 | - |
|
| 539 | - private void addAutoTrim(ObjectModelClass output, List<String> notBlankString) {
|
|
| 540 | - if (notBlankString.isEmpty()) {
|
|
| 541 | - return;
|
|
| 535 | + if (autoTrimGenerator == null) {
|
|
| 536 | + autoTrimGenerator = new AutoTrimGenerator(validationTagValues, this, model, (c, a) -> {
|
|
| 537 | + ObjectModelClass aClass = model.getClass(a.getType());
|
|
| 538 | + return aClass != null
|
|
| 539 | + && aClass.getSuperclasses().stream().anyMatch(i -> model.getClass(i.getQualifiedName()) != null)
|
|
| 540 | + && aClass.getInterfaces().stream().noneMatch(i -> i.getQualifiedName().equals(NoValidationDto.class.getName()));
|
|
| 541 | + });
|
|
| 542 | 542 | }
|
| 543 | - |
|
| 544 | - ObjectModelOperation autoTrimMethod = addOperation(output, "autoTrim", void.class, ObjectModelJavaModifier.PUBLIC);
|
|
| 545 | - addAnnotation(output, autoTrimMethod, Override.class);
|
|
| 546 | - StringBuilder content = new StringBuilder("");
|
|
| 547 | - for (String property : notBlankString) {
|
|
| 548 | - String capitalize = Strings.capitalize(property);
|
|
| 549 | - content.append(""/*{
|
|
| 550 | - super.autoTrim();
|
|
| 551 | - autoTrim(this::get<%=capitalize%>, this::set<%=capitalize%>);}*/);
|
|
| 552 | - }
|
|
| 553 | - setOperationBody(autoTrimMethod, ""/*{<%=content.toString()%>
|
|
| 554 | - }*/);
|
|
| 543 | + autoTrimGenerator.generate(input, output);
|
|
| 544 | + return output;
|
|
| 555 | 545 | }
|
| 556 | 546 | |
| 557 | 547 | void addGroupBy(ObjectModelClass input, String... groupByList) {
|