package eu.dnetlib.dhp.oa.dedup.model; import java.io.Serializable; import java.text.SimpleDateFormat; import java.util.*; import java.util.stream.Collectors; import org.apache.commons.lang3.StringUtils; import com.google.common.collect.Sets; import eu.dnetlib.dhp.oa.dedup.DatePicker; import eu.dnetlib.dhp.oa.dedup.IdentifierComparator; import eu.dnetlib.dhp.schema.common.EntityType; import eu.dnetlib.dhp.schema.common.ModelConstants; import eu.dnetlib.dhp.schema.common.ModelSupport; import eu.dnetlib.dhp.schema.oaf.*; import eu.dnetlib.dhp.schema.oaf.utils.OafMapperUtils; import eu.dnetlib.dhp.schema.oaf.utils.PidComparator; import eu.dnetlib.dhp.schema.oaf.utils.PidType; public class Identifier implements Serializable, Comparable> { public static final String DATE_FORMAT = "yyyy-MM-dd"; public static final String BASE_DATE = "2000-01-01"; private T entity; // cached date value private Date date = null; public static Identifier newInstance(T entity) { return new Identifier<>(entity); } public Identifier(T entity) { this.entity = entity; } public T getEntity() { return entity; } public void setEntity(T entity) { this.entity = entity; } public Date getDate() { if (Objects.nonNull(date)) { return date; } else { String sDate = BASE_DATE; if (ModelSupport.isSubClass(getEntity(), Result.class)) { Result result = (Result) getEntity(); if (isWellformed(result.getDateofacceptance())) { sDate = result.getDateofacceptance().getValue(); } } try { this.date = new SimpleDateFormat(DATE_FORMAT).parse(sDate); return date; } catch (Throwable e) { throw new RuntimeException( String.format("cannot parse date: '%s' from record: '%s'", sDate, entity.getId())); } } } private static boolean isWellformed(Field date) { return date != null && StringUtils.isNotBlank(date.getValue()) && date.getValue().matches(DatePicker.DATE_PATTERN) && DatePicker.inRange(date.getValue()); } public List getCollectedFrom() { return entity.getCollectedfrom(); } public EntityType getEntityType() { return EntityType.fromClass(entity.getClass()); } public String getOriginalID() { return entity.getId(); } public PidType getPidType() { return PidType.tryValueOf(StringUtils.substringBefore(StringUtils.substringAfter(entity.getId(), "|"), "_")); } @Override public int compareTo(Identifier i) { return IdentifierComparator.compareIdentifiers(this, i); } }