2020-07-22 17:29:48 +02:00
|
|
|
|
2020-10-06 16:44:51 +02:00
|
|
|
package eu.dnetlib.dhp.oa.dedup.model;
|
2020-07-22 17:29:48 +02:00
|
|
|
|
2021-04-15 10:59:24 +02:00
|
|
|
import java.io.Serializable;
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
import java.util.*;
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
|
2020-10-20 12:19:46 +02:00
|
|
|
import com.google.common.collect.Sets;
|
2021-04-15 10:59:24 +02:00
|
|
|
|
2020-11-04 15:02:02 +01:00
|
|
|
import eu.dnetlib.dhp.oa.dedup.DatePicker;
|
2022-11-09 14:20:59 +01:00
|
|
|
import eu.dnetlib.dhp.oa.dedup.IdentifierComparator;
|
2020-09-29 15:31:46 +02:00
|
|
|
import eu.dnetlib.dhp.schema.common.EntityType;
|
2020-11-27 16:07:24 +01:00
|
|
|
import eu.dnetlib.dhp.schema.common.ModelConstants;
|
2020-11-04 15:02:02 +01:00
|
|
|
import eu.dnetlib.dhp.schema.common.ModelSupport;
|
|
|
|
import eu.dnetlib.dhp.schema.oaf.*;
|
2021-04-27 15:44:01 +02:00
|
|
|
import eu.dnetlib.dhp.schema.oaf.utils.OafMapperUtils;
|
2020-11-04 15:02:02 +01:00
|
|
|
import eu.dnetlib.dhp.schema.oaf.utils.PidComparator;
|
|
|
|
import eu.dnetlib.dhp.schema.oaf.utils.PidType;
|
2020-09-29 15:31:46 +02:00
|
|
|
|
2021-08-11 12:13:22 +02:00
|
|
|
public class Identifier<T extends OafEntity> implements Serializable, Comparable<Identifier<T>> {
|
2020-09-29 15:31:46 +02:00
|
|
|
|
2020-11-26 11:03:12 +01:00
|
|
|
public static final String DATE_FORMAT = "yyyy-MM-dd";
|
2020-11-26 13:08:36 +01:00
|
|
|
public static final String BASE_DATE = "2000-01-01";
|
2020-09-29 15:31:46 +02:00
|
|
|
|
2020-11-04 15:02:02 +01:00
|
|
|
private T entity;
|
2020-09-29 15:31:46 +02:00
|
|
|
|
2020-11-26 11:03:12 +01:00
|
|
|
// cached date value
|
|
|
|
private Date date = null;
|
|
|
|
|
2021-08-11 12:13:22 +02:00
|
|
|
public static <T extends OafEntity> Identifier<T> newInstance(T entity) {
|
|
|
|
return new Identifier<>(entity);
|
2020-09-29 15:31:46 +02:00
|
|
|
}
|
|
|
|
|
2020-11-04 15:02:02 +01:00
|
|
|
public Identifier(T entity) {
|
|
|
|
this.entity = entity;
|
2020-09-29 15:31:46 +02:00
|
|
|
}
|
|
|
|
|
2020-11-04 15:02:02 +01:00
|
|
|
public T getEntity() {
|
|
|
|
return entity;
|
2020-09-29 15:31:46 +02:00
|
|
|
}
|
|
|
|
|
2020-11-04 15:02:02 +01:00
|
|
|
public void setEntity(T entity) {
|
|
|
|
this.entity = entity;
|
2020-09-29 15:31:46 +02:00
|
|
|
}
|
|
|
|
|
2020-11-04 15:02:02 +01:00
|
|
|
public Date getDate() {
|
2020-11-26 11:03:12 +01:00
|
|
|
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()));
|
2020-11-04 15:02:02 +01:00
|
|
|
}
|
|
|
|
}
|
2020-09-29 15:31:46 +02:00
|
|
|
}
|
|
|
|
|
2020-11-04 15:02:02 +01:00
|
|
|
private static boolean isWellformed(Field<String> date) {
|
|
|
|
return date != null && StringUtils.isNotBlank(date.getValue())
|
|
|
|
&& date.getValue().matches(DatePicker.DATE_PATTERN) && DatePicker.inRange(date.getValue());
|
2020-09-29 15:31:46 +02:00
|
|
|
}
|
|
|
|
|
2020-11-04 15:02:02 +01:00
|
|
|
public List<KeyValue> getCollectedFrom() {
|
|
|
|
return entity.getCollectedfrom();
|
2020-09-29 15:31:46 +02:00
|
|
|
}
|
|
|
|
|
2020-11-04 15:02:02 +01:00
|
|
|
public EntityType getEntityType() {
|
|
|
|
return EntityType.fromClass(entity.getClass());
|
2020-09-29 15:31:46 +02:00
|
|
|
}
|
|
|
|
|
2020-11-04 15:02:02 +01:00
|
|
|
public String getOriginalID() {
|
|
|
|
return entity.getId();
|
2020-09-29 15:31:46 +02:00
|
|
|
}
|
|
|
|
|
2022-11-09 14:20:59 +01:00
|
|
|
public PidType getPidType() {
|
2020-11-04 15:02:02 +01:00
|
|
|
return PidType.tryValueOf(StringUtils.substringBefore(StringUtils.substringAfter(entity.getId(), "|"), "_"));
|
2020-09-29 15:31:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-08-11 12:13:22 +02:00
|
|
|
public int compareTo(Identifier<T> i) {
|
2022-11-09 14:20:59 +01:00
|
|
|
return IdentifierComparator.compareIdentifiers(this, i);
|
2020-09-29 15:31:46 +02:00
|
|
|
}
|
2020-07-22 17:29:48 +02:00
|
|
|
}
|