|
|
|
@ -5,6 +5,7 @@ import java.io.File;
|
|
|
|
|
import java.io.FileWriter;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.io.PrintWriter;
|
|
|
|
|
import java.io.StringWriter;
|
|
|
|
|
import java.io.UnsupportedEncodingException;
|
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
@ -16,6 +17,7 @@ import java.util.UUID;
|
|
|
|
|
import org.gcube.common.authorization.client.Constants;
|
|
|
|
|
import org.gcube.common.authorization.library.AuthorizationEntry;
|
|
|
|
|
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
|
|
|
|
|
import org.gcube.common.resources.gcore.Resources;
|
|
|
|
|
import org.gcube.common.scope.api.ScopeProvider;
|
|
|
|
|
import org.gcube.informationsystem.exporter.mapper.exception.CreateException;
|
|
|
|
|
import org.gcube.informationsystem.exporter.mapper.exception.UpdateException;
|
|
|
|
@ -70,6 +72,7 @@ public abstract class GCoreResourceMapper<GR extends org.gcube.common.resources.
|
|
|
|
|
protected final Class<GR> grClass;
|
|
|
|
|
protected final Class<R> rClass;
|
|
|
|
|
protected final boolean filteredReport;
|
|
|
|
|
protected final boolean statistics;
|
|
|
|
|
|
|
|
|
|
public static final String UTF8 = "UTF-8";
|
|
|
|
|
|
|
|
|
@ -93,17 +96,33 @@ public abstract class GCoreResourceMapper<GR extends org.gcube.common.resources.
|
|
|
|
|
return format.format(date);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public File getFile(Class<?> grClass, String contextFullName, String dateString) {
|
|
|
|
|
return new File(contextFullName.replace("/", "_") + "-" + grClass.getSimpleName() + "-" + dateString
|
|
|
|
|
+ "-exporter.json");
|
|
|
|
|
private String getFileName(Class<?> rClass, String contextFullName, Calendar calendar, String suffix) {
|
|
|
|
|
String dateString = getDateString(calendar);
|
|
|
|
|
StringBuffer stringBuffer = new StringBuffer();
|
|
|
|
|
stringBuffer.append(contextFullName.replace("/", "_"));
|
|
|
|
|
stringBuffer.append("-");
|
|
|
|
|
stringBuffer.append(rClass.getSimpleName());
|
|
|
|
|
stringBuffer.append("-");
|
|
|
|
|
stringBuffer.append(dateString);
|
|
|
|
|
stringBuffer.append(suffix);
|
|
|
|
|
return stringBuffer.toString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public File getReportFile(Class<?> rClass, String contextFullName, Calendar calendar) {
|
|
|
|
|
return new File(getFileName(rClass, contextFullName, calendar, ".json"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected GCoreResourceMapper(Class<GR> grClass, Class<R> rClass, boolean filteredReport) {
|
|
|
|
|
public File getStatisticFile(Class<?> rClass, String contextFullName, Calendar calendar) {
|
|
|
|
|
return new File(getFileName(rClass, contextFullName, calendar, ".csv"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected GCoreResourceMapper(Class<GR> grClass, Class<R> rClass, boolean filteredReport, Boolean statistics) {
|
|
|
|
|
this.grClass = grClass;
|
|
|
|
|
this.rClass = rClass;
|
|
|
|
|
this.resourceRegistryPublisher = ResourceRegistryPublisherFactory.create();
|
|
|
|
|
this.resourceRegistryClient = ResourceRegistryClientFactory.create();
|
|
|
|
|
this.filteredReport = filteredReport;
|
|
|
|
|
this.statistics = statistics;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected List<GR> getAll() {
|
|
|
|
@ -222,7 +241,7 @@ public abstract class GCoreResourceMapper<GR extends org.gcube.common.resources.
|
|
|
|
|
|
|
|
|
|
protected abstract R map(GR gr) throws Exception;
|
|
|
|
|
|
|
|
|
|
protected void notifyFailures(int allSize, List<GR> failed) {
|
|
|
|
|
protected void notifyFailures(int allSize, List<GR> failed, Calendar start, File reportFile) {
|
|
|
|
|
String contextName = getCurrentContextName();
|
|
|
|
|
|
|
|
|
|
if (failed.size() == 0) {
|
|
|
|
@ -240,8 +259,8 @@ public abstract class GCoreResourceMapper<GR extends org.gcube.common.resources.
|
|
|
|
|
|
|
|
|
|
ObjectNode objectNode = objectMapper.createObjectNode();
|
|
|
|
|
|
|
|
|
|
Calendar calendar = Calendar.getInstance();
|
|
|
|
|
String dateString = getDateString(calendar);
|
|
|
|
|
String dateString = getDateString(start);
|
|
|
|
|
|
|
|
|
|
objectNode.put(DATE, dateString);
|
|
|
|
|
ObjectNode context = objectNode.putObject(contextName);
|
|
|
|
|
ArrayNode mappingArrayNode = context.putArray(MAPPING_ERROR);
|
|
|
|
@ -312,26 +331,13 @@ public abstract class GCoreResourceMapper<GR extends org.gcube.common.resources.
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
String json = objectMapper.writeValueAsString(objectNode);
|
|
|
|
|
|
|
|
|
|
File file = getFile(rClass, contextName, dateString);
|
|
|
|
|
synchronized (file) {
|
|
|
|
|
try (FileWriter fw = new FileWriter(file, true);
|
|
|
|
|
BufferedWriter bw = new BufferedWriter(fw);
|
|
|
|
|
PrintWriter out = new PrintWriter(bw)) {
|
|
|
|
|
out.println(json);
|
|
|
|
|
out.flush();
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
throw e;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
printLineToFile(json, reportFile);
|
|
|
|
|
/*
|
|
|
|
|
* try { publishFileToWorkspace(file); file.delete(); }catch
|
|
|
|
|
* (Exception e) {
|
|
|
|
|
* logger.error("Error while saving file {} on Workspace",
|
|
|
|
|
* file.getName(), e); // TODO Use the Social Notification }
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
logger.error("Error exporting JSON error result", e);
|
|
|
|
|
}
|
|
|
|
@ -351,7 +357,11 @@ public abstract class GCoreResourceMapper<GR extends org.gcube.common.resources.
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void export() {
|
|
|
|
|
|
|
|
|
|
String contextName = getCurrentContextName();
|
|
|
|
|
Calendar start = Calendar.getInstance();
|
|
|
|
|
File statisticsFile = getStatisticFile(rClass, contextName, start);
|
|
|
|
|
File reportFile = getReportFile(rClass, contextName, start);
|
|
|
|
|
|
|
|
|
|
List<GR> all = getAll();
|
|
|
|
|
|
|
|
|
|
logger.debug("-------------------------------------------------------");
|
|
|
|
@ -359,18 +369,70 @@ public abstract class GCoreResourceMapper<GR extends org.gcube.common.resources.
|
|
|
|
|
logger.debug("-------------------------------------------------------");
|
|
|
|
|
|
|
|
|
|
List<GR> failed = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
boolean first = true;
|
|
|
|
|
|
|
|
|
|
for (GR gr : all) {
|
|
|
|
|
R r = null;
|
|
|
|
|
try {
|
|
|
|
|
Thread.sleep(300);
|
|
|
|
|
mapAndPublish(gr);
|
|
|
|
|
r = mapAndPublish(gr);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
failed.add(gr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(statistics && r!=null) {
|
|
|
|
|
try {
|
|
|
|
|
addToCSV(gr, r, statisticsFile, first);
|
|
|
|
|
first = false;
|
|
|
|
|
}catch (Throwable t) {
|
|
|
|
|
logger.trace("Unable to report the statistic in file {} for resource with UUID {}",
|
|
|
|
|
statisticsFile.getAbsolutePath(), r.getHeader().getUUID(), t);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
notifyFailures(all.size(), failed);
|
|
|
|
|
notifyFailures(all.size(), failed, start, reportFile);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void printLineToFile(String line, File file) throws IOException {
|
|
|
|
|
synchronized (file) {
|
|
|
|
|
try (FileWriter fw = new FileWriter(file, true);
|
|
|
|
|
BufferedWriter bw = new BufferedWriter(fw);
|
|
|
|
|
PrintWriter out = new PrintWriter(bw)) {
|
|
|
|
|
out.println(line);
|
|
|
|
|
out.flush();
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
throw e;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected String getUnmarshalledResource(GR gr) {
|
|
|
|
|
StringWriter stringWriter = new StringWriter();
|
|
|
|
|
Resources.marshal(gr, stringWriter);
|
|
|
|
|
return stringWriter.toString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void addToCSV(GR gr, R r, File statisticsFile, boolean addHeader) throws IOException {
|
|
|
|
|
StringBuffer stringBuffer = new StringBuffer();
|
|
|
|
|
if(addHeader) {
|
|
|
|
|
stringBuffer.append(gr.getClass().getSimpleName());
|
|
|
|
|
stringBuffer.append("(byte size)");
|
|
|
|
|
stringBuffer.append(",");
|
|
|
|
|
stringBuffer.append(r.getClass().getSimpleName());
|
|
|
|
|
stringBuffer.append("(byte size)");
|
|
|
|
|
printLineToFile(stringBuffer.toString(), statisticsFile);
|
|
|
|
|
stringBuffer = new StringBuffer();
|
|
|
|
|
}
|
|
|
|
|
String unmarshalledGR = getUnmarshalledResource(gr);
|
|
|
|
|
final byte[] grUTF8Bytes = unmarshalledGR.getBytes("UTF-8");
|
|
|
|
|
stringBuffer.append(grUTF8Bytes.length);
|
|
|
|
|
stringBuffer.append(",");
|
|
|
|
|
String unmarshalledR = ISMapper.marshal(r);
|
|
|
|
|
final byte[] rUTF8Bytes = unmarshalledR.getBytes("UTF-8");
|
|
|
|
|
stringBuffer.append(rUTF8Bytes.length);
|
|
|
|
|
printLineToFile(stringBuffer.toString(), statisticsFile);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|