Improved Import process

This commit is contained in:
George Kalampokis 2020-05-29 11:35:32 +03:00
parent 48a17b6d55
commit 544eb92d86
3 changed files with 59 additions and 6 deletions

View File

@ -1,5 +1,6 @@
package eu.eudat.logic.managers;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import eu.eudat.data.entities.DMP;
@ -11,6 +12,7 @@ import org.springframework.stereotype.Component;
import javax.transaction.Transactional;
import java.io.IOException;
import java.io.Serializable;
import java.text.SimpleDateFormat;
@Component
@ -30,18 +32,40 @@ public class RDAManager {
Dmp rdaDmp = dmpRDAMapper.toRDA(dmp);
ObjectMapper mapper = new ObjectMapper();
mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss Z"));
mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"));
result = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(rdaDmp);
DMPWrap wrap = new DMPWrap(rdaDmp);
result = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(wrap);
return result;
}
public DMP convertToEntity(String json) throws IOException {
ObjectMapper mapper = new ObjectMapper();
mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss Z"));
mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"));
Dmp rda = mapper.readValue(json, Dmp.class);
Dmp rda = mapper.readValue(json, DMPWrap.class).getDmp();
return dmpRDAMapper.toEntity(rda);
}
public static class DMPWrap implements Serializable {
@JsonProperty("dmp")
private Dmp dmp;
public DMPWrap() {
}
public DMPWrap(Dmp dmp) {
this.dmp = dmp;
}
public Dmp getDmp() {
return dmp;
}
public void setDmp(Dmp dmp) {
this.dmp = dmp;
}
}
}

View File

@ -70,7 +70,9 @@ public class DmpRDAMapper {
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()));
entity.setAssociatedDmps(((List<String>) rda.getAdditionalProperties().get("templates")).stream().map(this::getProfile).collect(Collectors.toSet()));
if (((List<String>) rda.getAdditionalProperties().get("templates")) != null && !((List<String>) rda.getAdditionalProperties().get("templates")).isEmpty()) {
entity.setAssociatedDmps(((List<String>) rda.getAdditionalProperties().get("templates")).stream().map(this::getProfile).collect(Collectors.toSet()));
}
return entity;
}

View File

@ -1,10 +1,14 @@
package eu.eudat.models.rda.mapper;
import eu.eudat.data.dao.criteria.GrantCriteria;
import eu.eudat.data.entities.Funder;
import eu.eudat.data.entities.Grant;
import eu.eudat.logic.services.ApiContext;
import eu.eudat.models.rda.Funding;
import java.util.Date;
import java.util.UUID;
public class FundingRDAMapper {
public static Funding toRDA(Grant grant) {
@ -27,6 +31,29 @@ public class FundingRDAMapper {
public static Grant toEntity(Funding rda, ApiContext apiContext) {
GrantCriteria criteria = new GrantCriteria();
criteria.setReference(rda.getGrantId().getIdentifier());
return apiContext.getOperationsContext().getDatabaseRepository().getGrantDao().getWithCriteria(criteria).getSingle();
Grant grant;
try {
grant = apiContext.getOperationsContext().getDatabaseRepository().getGrantDao().getWithCriteria(criteria).getSingle();
}catch (Exception e) {
grant = new Grant();
grant.setId(UUID.randomUUID());
grant.setLabel(rda.getGrantId().getIdentifier());
grant.setStatus((short)1);
grant.setCreated(new Date());
grant.setModified(new Date());
grant.setType(0);
grant.setFunder(new Funder());
grant.getFunder().setId(UUID.randomUUID());
grant.getFunder().setLabel(rda.getFunderId().getIdentifier());
grant.getFunder().setStatus((short)1);
grant.getFunder().setCreated(new Date());
grant.getFunder().setModified(new Date());
grant.getFunder().setType(0);
grant.getFunder().setReference(rda.getFunderId().getType().toString()+"::"+rda.getFunderId().getIdentifier());
grant.setReference(rda.getGrantId().getType().toString()+"::"+rda.getGrantId().getIdentifier());
Funder funder = apiContext.getOperationsContext().getDatabaseRepository().getFunderDao().createOrUpdate(grant.getFunder());
grant = apiContext.getOperationsContext().getDatabaseRepository().getGrantDao().createOrUpdate(grant);
}
return grant;
}
}