
fr.ird.observe.dto.DtoPropertyModifications Maven / Gradle / Ivy
package fr.ird.observe.dto;
/*-
* #%L
* ObServe Toolkit :: Dto
* %%
* Copyright (C) 2017 - 2021 Ultreia.io
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* .
* #L%
*/
import io.ultreia.java4all.bean.JavaBean;
import io.ultreia.java4all.bean.monitor.BeanMonitor;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.io.Serializable;
import java.util.LinkedHashSet;
import java.util.Optional;
import java.util.Set;
/**
* Created on 22/09/2021.
*
* @author Tony Chemit - [email protected]
* @since 9.0.0
*/
public class DtoPropertyModifications implements ObserveDto {
private static final Logger log = LogManager.getLogger(DtoPropertyModifications.class);
private final String id;
private final String label;
private final Set properties;
private final String lengthWeightParameterNotFound;
public static abstract class Builder {
private final BeanMonitor monitor;
private final Set modifications = new LinkedHashSet<>();
private String id;
private String label;
private String lengthWeightParameterNotFound;
public Builder(String... propertyNames) {
monitor = new BeanMonitor(propertyNames);
}
protected void watch(IdDto dto) {
modifications.clear();
id = label = lengthWeightParameterNotFound = null;
monitor.setBean(dto);
}
protected void flush() {
if (monitor.wasModified()) {
IdDto dto = (IdDto) monitor.getBean();
flushToResult(dto, monitor.getModifiedProperties());
}
monitor.setBean(null);
}
protected Optional build() {
boolean noModification = modifications.isEmpty();
if (noModification) {
return Optional.empty();
}
R result = createResult(id, label, modifications, lengthWeightParameterNotFound);
return Optional.of(result);
}
protected abstract R createResult(String id, String label, Set modifications, String lengthWeightParameterNotFound);
public void flushToResult(IdDto dto, String... propertyNamesModified) {
this.id = dto.getId();
this.label = dto.toString();
for (String modifiedProperty : propertyNamesModified) {
DtoPropertyModification element = new DtoPropertyModification(dto, modifiedProperty);
modifications.add(element);
}
}
public void registerLengthWeightParameterNotFound(String lengthWeightParameterNotFound) {
this.lengthWeightParameterNotFound = lengthWeightParameterNotFound;
if (log.isWarnEnabled()) {
log.warn(lengthWeightParameterNotFound);
}
}
}
public DtoPropertyModifications(String id, String label, Set properties, String lengthWeightParameterNotFound) {
this.id = id;
this.label = label;
this.properties = properties;
this.lengthWeightParameterNotFound = lengthWeightParameterNotFound;
}
public String getId() {
return id;
}
public String getLabel() {
return label;
}
public Set getProperties() {
return properties;
}
public String getLengthWeightParameterNotFound() {
return lengthWeightParameterNotFound;
}
public void flushToDto(JavaBean dto) {
properties.forEach(modification -> {
Serializable newValue = modification.getNewValue();
dto.set(modification.getPropertyName(), newValue);
});
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy