99 lines
3.2 KiB
Java
99 lines
3.2 KiB
Java
package eu.dnetlib.apps.oai2ftp.utils;
|
|
|
|
import java.io.UnsupportedEncodingException;
|
|
import java.net.URLEncoder;
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.time.LocalDateTime;
|
|
import java.time.format.DateTimeFormatter;
|
|
import java.util.UUID;
|
|
|
|
import org.apache.commons.codec.digest.DigestUtils;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
import eu.dnetlib.apps.oai2ftp.model.CollectionInfo;
|
|
import eu.dnetlib.apps.oai2ftp.model.CollectionLogEntry;
|
|
import eu.dnetlib.apps.oai2ftp.model.ExecutionStatus;
|
|
|
|
public class SimpleUtils {
|
|
|
|
private static final String UTF_8 = StandardCharsets.UTF_8.toString();
|
|
|
|
private static final DateTimeFormatter oaiDateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
|
|
|
|
public static String generateNewJobId() {
|
|
return "job-" + UUID.randomUUID();
|
|
}
|
|
|
|
public static String oaiFirstUrl(final String baseUrl, final String format, final String setSpec, final LocalDateTime from, final LocalDateTime until) {
|
|
try {
|
|
String url = baseUrl + "?verb=ListRecords&metadataPrefix=" + URLEncoder.encode(format, UTF_8);
|
|
|
|
if (setSpec != null && !setSpec.isEmpty()) {
|
|
url += "&set=" + URLEncoder.encode(setSpec, UTF_8);
|
|
}
|
|
if (from != null) {
|
|
url += "&from=" + URLEncoder.encode(from.format(oaiDateFormatter), UTF_8);
|
|
}
|
|
if (until != null) {
|
|
url += "&until=" + URLEncoder.encode(until.format(oaiDateFormatter), UTF_8);
|
|
}
|
|
return url;
|
|
} catch (final UnsupportedEncodingException e) {
|
|
throw new RuntimeException(e);
|
|
}
|
|
}
|
|
|
|
public static String oaiNextUrl(final String baseUrl, final String rtoken) {
|
|
try {
|
|
if (StringUtils.isNotBlank(rtoken)) {
|
|
return baseUrl + "?verb=ListRecords&resumptionToken=" + URLEncoder.encode(rtoken, UTF_8);
|
|
} else {
|
|
return null;
|
|
}
|
|
} catch (final UnsupportedEncodingException e) {
|
|
throw new RuntimeException(e);
|
|
}
|
|
}
|
|
|
|
public static CollectionInfo logToInfo(final CollectionLogEntry log) {
|
|
final CollectionInfo info = new CollectionInfo();
|
|
info.setId(log.getId());
|
|
info.setOaiBaseUrl(log.getOaiBaseUrl());
|
|
info.setOaiFormat(log.getOaiFormat());
|
|
info.setOaiSet(log.getOaiSet());
|
|
info.setOaiFrom(log.getOaiFrom());
|
|
info.setOaiUntil(log.getOaiUntil());
|
|
info.setFtpServer(log.getFtpServer());
|
|
info.setFtpDir(log.getFtpDir());
|
|
info.setStart(log.getStart());
|
|
info.setEnd(log.getEnd());
|
|
info.setExecutionStatus(log.isSuccess() ? ExecutionStatus.COMPLETED : ExecutionStatus.FAILED);
|
|
info.setTotal(log.getTotal());
|
|
info.setMessage(log.getMessage());
|
|
return info;
|
|
}
|
|
|
|
public static CollectionLogEntry infoToLog(final CollectionInfo info) {
|
|
final CollectionLogEntry log = new CollectionLogEntry();
|
|
log.setId(info.getId());
|
|
log.setOaiBaseUrl(info.getOaiBaseUrl());
|
|
log.setOaiFormat(info.getOaiFormat());
|
|
log.setOaiSet(info.getOaiSet());
|
|
log.setOaiFrom(info.getOaiFrom());
|
|
log.setOaiUntil(info.getOaiUntil());
|
|
log.setFtpServer(info.getFtpServer());
|
|
log.setFtpDir(info.getFtpDir());
|
|
log.setStart(info.getStart());
|
|
log.setEnd(info.getEnd());
|
|
log.setSuccess(info.getExecutionStatus() == ExecutionStatus.COMPLETED);
|
|
log.setTotal(info.getTotal());
|
|
log.setNumberOfCalls(info.getCalls().size());
|
|
log.setMessage(info.getMessage());
|
|
return log;
|
|
}
|
|
|
|
public static String oaiIdToFilename(final String id) {
|
|
return DigestUtils.md5Hex(id) + ".xml";
|
|
}
|
|
}
|