argos/dmp-backend/web/src/main/java/eu/eudat/models/rda/mapper/DmpRDAMapper.java

191 lines
8.3 KiB
Java

package eu.eudat.models.rda.mapper;
import eu.eudat.data.DmpDescriptionTemplateEntity;
import eu.eudat.data.DmpEntity;
import eu.eudat.logic.services.ApiContext;
import eu.eudat.models.rda.Dmp;
import eu.eudat.query.DmpDescriptionTemplateQuery;
import gr.cite.tools.data.query.QueryFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import jakarta.transaction.Transactional;
import javax.management.InvalidApplicationException;
import java.io.IOException;
import java.util.*;
@Component
public class DmpRDAMapper {
private static final Logger logger = LoggerFactory.getLogger(DmpRDAMapper.class);
private DatasetRDAMapper datasetRDAMapper;
private ApiContext apiContext;
private final QueryFactory queryFactory;
@Autowired
public DmpRDAMapper(DatasetRDAMapper datasetRDAMapper, ApiContext apiContext, QueryFactory queryFactory) throws IOException {
this.datasetRDAMapper = datasetRDAMapper;
this.apiContext = apiContext;
this.queryFactory = queryFactory;
}
@Transactional
public Dmp toRDA(DmpEntity dmp) throws InvalidApplicationException {
// if (dmp.getDataset() == null || dmp.getDataset().isEmpty()) { //TODO
// throw new IllegalArgumentException("DMP has no Datasets");
// }
// Map<String, Object> extraProperties;
// if (dmp.getExtraProperties() == null) {
// throw new IllegalArgumentException("DMP is missing language and contact properties");
// } else {
// extraProperties = new org.json.JSONObject(dmp.getExtraProperties()).toMap();
// /*if (extraProperties.get("language") == null) {
// throw new IllegalArgumentException("DMP must have it's language property defined");
// }*/
// if (extraProperties.get("contact") == null) {
// throw new IllegalArgumentException("DMP must have it's contact property defined");
// }
// }
Dmp rda = new Dmp();
// if (dmp.getDois() != null && !dmp.getDois().isEmpty()) {
// for(EntityDoiEntity doi: dmp.getDois()){
// if(doi.getRepositoryId().equals("Zenodo")){
// rda.setDmpId(DmpIdRDAMapper.toRDA(doi.getDoi()));
// }
// }
// } else {
// rda.setDmpId(DmpIdRDAMapper.toRDA(dmp.getId()));
// }
if (dmp.getCreatedAt() == null) {
throw new IllegalArgumentException("DMP Created is missing");
}
if (dmp.getUpdatedAt() == null) {
throw new IllegalArgumentException("DMP Modified is missing");
}
if (dmp.getLabel() == null) {
throw new IllegalArgumentException("DMP Label is missing");
}
// rda.setCreated(dmp.getCreated()); //TODO
// rda.setDescription(dmp.getDescription());
// rda.setModified(dmp.getModified());
// rda.setTitle(dmp.getLabel());
//
// if (!extraProperties.isEmpty()) {
// if (extraProperties.get("ethicalIssues") != null) {
// rda.setEthicalIssuesExist(Dmp.EthicalIssuesExist.fromValue(extraProperties.get("ethicalIssues").toString()));
// } else {
// rda.setEthicalIssuesExist(Dmp.EthicalIssuesExist.UNKNOWN);
// }
// rda.setLanguage(LanguageRDAMapper.mapLanguageIsoToRDAIso(extraProperties.get("language") != null ? extraProperties.get("language").toString() : "en"));
// if (extraProperties.get("costs") != null) {
// rda.setCost(new ArrayList<>());
// ((List) extraProperties.get("costs")).forEach(costl -> {
// rda.getCost().add(CostRDAMapper.toRDA((Map)costl));
// });
// }
// try {
// UserInfo contactDb = apiContext.getOperationsContext().getDatabaseRepository().getUserInfoDao().find(UUID.fromString((String) extraProperties.get("contact")));
// UserInfo contact = new UserInfo();
// contact.setId(contactDb.getId());
// contact.setName(contactDb.getName());
// contact.setEmail(contactDb.getEmail());
// if (contact.getEmail() == null) {
// for (UserDMP userDMP : dmp.getUsers()) {
// if (userDMP.getDmp().getId() == dmp.getId() && userDMP.getUser().getEmail() != null) {
// contact.setEmail(userDMP.getUser().getEmail());
// break;
// }
// }
// }
// rda.setContact(ContactRDAMapper.toRDA(contact));
// } catch (NoResultException e) {
// logger.error(e.getMessage(), e);
// }
// }
//
// /*UserInfo creator;
// if (dmp.getCreator() != null) {
// creator = dmp.getCreator();
// } else {
// creator = dmp.getUsers().stream().filter(userDMP -> userDMP.getRole().equals(UserDMP.UserDMPRoles.OWNER.getValue())).map(UserDMP::getUser).findFirst().orElse(new UserInfo());
// }
// rda.setContact(ContactRDAMapper.toRDA(creator));*/
// rda.setContributor(new ArrayList<>());
// if (dmp.getResearchers() != null && !dmp.getResearchers().isEmpty()) {
// rda.getContributor().addAll(dmp.getResearchers().stream().map(ContributorRDAMapper::toRDA).collect(Collectors.toList()));
// }
//// rda.getContributor().addAll(dmp.getUsers().stream().map(ContributorRDAMapper::toRDA).collect(Collectors.toList()));
// rda.setDataset(dmp.getDataset().stream().filter(dataset -> dataset.getIsActive() != IsActive.Inactive).map(dataset -> datasetRDAMapper.toRDA(dataset, rda)).collect(Collectors.toList()));
// if (dmp.getProject() != null) {
// rda.setProject(Collections.singletonList(ProjectRDAMapper.toRDA(dmp.getProject(), dmp.getGrant())));
// }
// rda.setAdditionalProperty("templates", dmp.getAssociatedDmps().stream().map(datasetProfile -> datasetProfile.getId().toString()).toArray());
return rda;
}
public DmpEntity toEntity(Dmp rda, String[] profiles) throws InvalidApplicationException {
DmpEntity entity = new DmpEntity();
entity.setLabel(rda.getTitle());
// if (rda.getDmpId().getType() == DmpId.Type.DOI) { //TODO
// try {
// //TODO: Find from doi = rda.getDmpId().getIdentifier()
// EntityDoiEntity doi = new EntityDoiEntity();
// Set<EntityDoiEntity> dois = new HashSet<>();
// dois.add(doi);
// entity.setDois(dois);
// }
// catch (NoResultException e) {
// logger.warn("No entity doi: " + rda.getDmpId().getIdentifier() + " found in database. No dois are added to dmp.");
// entity.setDois(new HashSet<>());
// }
// }
// if (((List<String>) rda.getAdditionalProperties().get("templates")) != null && !((List<String>) rda.getAdditionalProperties().get("templates")).isEmpty() && entity.getId() != null) {
// entity.setAssociatedDmps(((List<String>) rda.getAdditionalProperties().get("templates")).stream().map(x -> {
// try {
// return this.getProfile(x, entity.getId());
// } catch (InvalidApplicationException e) {
// throw new RuntimeException(e);
// }
// }).filter(Objects::nonNull).collect(Collectors.toSet()));
// }
// if (entity.getAssociatedDmps() == null) {
// entity.setAssociatedDmps(new HashSet<>());
// }
// if (profiles != null && entity.getId() != null) {
// for (String profile : profiles) {
// entity.getAssociatedDmps().add(this.getProfile(profile, entity.getId()));
// }
// }
// if (rda.getContributor() != null && !rda.getContributor().isEmpty() && rda.getContributor().get(0).getContributorId() != null) {
// entity.setResearchers(rda.getContributor().stream().filter(r -> r.getContributorId() != null).map(ContributorRDAMapper::toEntity).filter(StreamDistinctBy.distinctByKey(Researcher::getReference)).collect(Collectors.toSet()));
// }
// entity.setCreated(rda.getCreated());
// entity.setModified(rda.getModified());
// entity.setDescription(rda.getDescription());
// DescriptionTemplateEntity defaultProfile = ((DescriptionTemplateEntity)entity.getAssociatedDmps().toArray()[0]);
// entity.setDataset(rda.getDataset().stream().map(rda1 -> datasetRDAMapper.toEntity(rda1, defaultProfile)).collect(Collectors.toSet()));
// if (rda.getProject().size() > 0) {
// Map<String, Object> result = ProjectRDAMapper.toEntity(rda.getProject().get(0), apiContext);
// entity.setProject((Project) result.get("project"));
// result.entrySet().stream().filter(entry -> entry.getKey().startsWith("grant")).forEach(entry -> entity.setGrant((Grant) entry.getValue()));
// }
//
// Map<String, Object> extraProperties = new HashMap<>();
// extraProperties.put("language", LanguageRDAMapper.mapRDAIsoToLanguageIso(rda.getLanguage()));
//
// entity.setExtraProperties(JSONObject.toJSONString(extraProperties));
return entity;
}
// private DmpDescriptionTemplateEntity getProfile(String descriptionTemplateId, UUID dmpId) throws InvalidApplicationException {
// return this.queryFactory.query(DmpDescriptionTemplateQuery.class).dmpIds(dmpId).descriptionTemplateIds(UUID.fromString(descriptionTemplateId)).first();
// }
}