argos/backend/core/src/main/java/org/opencdmp/model/builder/commonmodels/dmpblueprint/FieldCommonModelBuilder.java

78 lines
3.4 KiB
Java

package org.opencdmp.model.builder.commonmodels.dmpblueprint;
import org.opencdmp.authorization.AuthorizationFlags;
import org.opencdmp.commonmodels.enums.DmpBlueprintFieldCategory;
import org.opencdmp.commonmodels.models.dmpblueprint.FieldModel;
import org.opencdmp.commons.types.dmpblueprint.FieldEntity;
import org.opencdmp.convention.ConventionService;
import org.opencdmp.model.builder.commonmodels.BaseCommonModelBuilder;
import org.opencdmp.model.builder.commonmodels.CommonModelBuilderItemResponse;
import org.opencdmp.model.builder.commonmodels.dmp.DmpBlueprintValueCommonModelBuilder;
import org.opencdmp.model.dmpblueprintdefinition.Field;
import gr.cite.tools.data.builder.BuilderFactory;
import gr.cite.tools.exception.MyApplicationException;
import gr.cite.tools.fieldset.FieldSet;
import gr.cite.tools.logging.LoggerService;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
import java.util.Optional;
@Component("dmpblueprint.FieldCommonModelBuilder")
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public abstract class FieldCommonModelBuilder<Model extends FieldModel, Entity extends FieldEntity> extends BaseCommonModelBuilder<Model, Entity> {
private EnumSet<AuthorizationFlags> authorize = EnumSet.of(AuthorizationFlags.None);
@Autowired
public FieldCommonModelBuilder(
ConventionService conventionService
) {
super(conventionService, new LoggerService(LoggerFactory.getLogger(FieldCommonModelBuilder.class)));
}
public FieldCommonModelBuilder authorize(EnumSet<AuthorizationFlags> values) {
this.authorize = values;
return this;
}
protected abstract Model getInstance();
protected abstract Model buildChild(Entity data, Model model);
@Override
protected List<CommonModelBuilderItemResponse<Model, Entity>> buildInternal(List<Entity> data) throws MyApplicationException {
this.logger.debug("building for {}", Optional.ofNullable(data).map(List::size).orElse(0));
if (data == null || data.isEmpty()) return new ArrayList<>();
List<CommonModelBuilderItemResponse<Model, Entity>> models = new ArrayList<>();
for (Entity d : data) {
Model m = this.getInstance();
m.setId(d.getId());
m.setDescription(d.getDescription());
switch (d.getCategory()){
case System -> m.setCategory(DmpBlueprintFieldCategory.System);
case Extra -> m.setCategory(DmpBlueprintFieldCategory.Extra);
case ReferenceType -> m.setCategory(DmpBlueprintFieldCategory.ReferenceType);
default -> throw new MyApplicationException("unrecognized type " + d.getCategory());
}
m.setPlaceholder(d.getPlaceholder());
m.setDescription(d.getDescription());
m.setSemantics(d.getSemantics());
m.setRequired(d.isRequired());
m.setLabel(d.getLabel());
m.setOrdinal(d.getOrdinal());
this.buildChild(d, m);
models.add(new CommonModelBuilderItemResponse<>(m, d));
}
this.logger.debug("build {} items", Optional.of(models).map(List::size).orElse(0));
return models;
}
}