1
|
package eu.dnetlib.data.claims.handler;
|
2
|
|
3
|
import eu.dnetlib.data.claims.entity.Result;
|
4
|
import eu.dnetlib.data.claims.utils.ClaimUtils;
|
5
|
import eu.dnetlib.data.claims.utils.QueryGenerator;
|
6
|
import eu.dnetlib.data.claims.sql.SQLStoreException;
|
7
|
import eu.dnetlib.data.claims.sql.SqlDAO;
|
8
|
import org.apache.commons.io.FileUtils;
|
9
|
import org.apache.log4j.Logger;
|
10
|
|
11
|
import java.io.File;
|
12
|
import java.io.IOException;
|
13
|
import java.sql.ResultSet;
|
14
|
import java.sql.SQLException;
|
15
|
|
16
|
/**
|
17
|
* Created by kiatrop on 5/2/2016.
|
18
|
*/
|
19
|
public class ResultHandler {
|
20
|
private Logger log = Logger.getLogger(this.getClass());
|
21
|
SqlDAO sqlDAO = null;
|
22
|
QueryGenerator queryGenerator = null;
|
23
|
String pathToSaveRecord = null;
|
24
|
|
25
|
/* public String generateSaveQueryForResult(Result result){
|
26
|
return queryGenerator.generateInsertResultQuery(result.getOpenaireId(),result.getResultType(),result.getTitle(),result.getCollectedFrom(),result.getExternalUrl(),result.getDOI(), result.getOrcidworkid(), result.getAccessRights(),result.getEmbargoEndDate(),result.getBestLicense(),result.getRecordPath(), result.getRecordFormat());
|
27
|
}
|
28
|
public void saveResult(Result result) throws Exception {
|
29
|
sqlDAO.executePreparedQuery(generateSaveQueryForResult(result));
|
30
|
}*/
|
31
|
|
32
|
public String exportMetadataFileForResult(Result result) {
|
33
|
if(result.getOpenaireId()!=null && result.getMetadataRecord()!=null){
|
34
|
String path=createExportPath(result.getResultType(),result.getCollectedFrom(),result.getOpenaireId());
|
35
|
try {
|
36
|
//TODO check what should be saved in the path field
|
37
|
FileUtils.writeStringToFile(new File(this.pathToSaveRecord+path), result.getMetadataRecord());
|
38
|
return path;
|
39
|
} catch (IOException e) {
|
40
|
log.error("Couldn't write record to file "+ path,e);
|
41
|
|
42
|
}
|
43
|
}
|
44
|
|
45
|
return null;
|
46
|
}
|
47
|
private String createExportPath(String type, String collectedFrom, String openaireId){
|
48
|
String datasource=ClaimUtils.OPENAIRE;
|
49
|
if(collectedFrom.equals(ClaimUtils.COLLECTED_FROM_ORCID)){
|
50
|
datasource=ClaimUtils.ORCID;
|
51
|
}else if(collectedFrom.equals(ClaimUtils.COLLECTED_FROM_CROSSREF)){
|
52
|
datasource=ClaimUtils.CROSSREF;
|
53
|
}else if(collectedFrom.equals(ClaimUtils.COLLECTED_FROM_DATACITE)){
|
54
|
datasource=ClaimUtils.DATACITE;
|
55
|
}
|
56
|
//TODO fix this openaireId.replace("/","*") ?? url encode
|
57
|
//temporary solution for external results with doi which contain "/" (It creates folders in path)
|
58
|
return type+"/"+datasource+"/"+openaireId.replace("/","*");
|
59
|
|
60
|
}
|
61
|
public void deleteRecordFile(String type, String id) throws SQLStoreException, SQLException {
|
62
|
ResultSet rs = sqlDAO.executePreparedQuery(queryGenerator.generateCountClaimsForResultQuery(id));
|
63
|
if(rs.next()){
|
64
|
Integer count = rs.getInt(1);
|
65
|
log.debug("Count is:" + count );
|
66
|
|
67
|
if(count != null && count ==1){
|
68
|
ResultSet rs1 = sqlDAO.executePreparedQuery(queryGenerator.generateSelectResultSource(id));
|
69
|
if(rs1.next()){
|
70
|
String source = rs1.getString(1);
|
71
|
|
72
|
log.debug("source is:" + source );
|
73
|
String path=createExportPath(type, source, id);
|
74
|
File file = new File(this.pathToSaveRecord+path);
|
75
|
log.debug("path is:" + path );
|
76
|
|
77
|
|
78
|
if (file.delete()) {
|
79
|
log.info(file.getName() + " is deleted!");
|
80
|
} else {
|
81
|
log.warn("Delete operation is failed for file "+ this.pathToSaveRecord+path);
|
82
|
}
|
83
|
}else{
|
84
|
log.error("Couldn't get result source for result with id : "+ id );
|
85
|
|
86
|
}
|
87
|
|
88
|
}else{
|
89
|
log.info("This metadata file cannot be removed (count="+count+") for result with id : "+ id );
|
90
|
}
|
91
|
}else{
|
92
|
log.error("Couldn't fetch claims count for result with id : "+ id );
|
93
|
}
|
94
|
}
|
95
|
|
96
|
public SqlDAO getSqlDAO() {
|
97
|
return sqlDAO;
|
98
|
}
|
99
|
|
100
|
public void setSqlDAO(SqlDAO sqlDAO) {
|
101
|
this.sqlDAO = sqlDAO;
|
102
|
}
|
103
|
|
104
|
public QueryGenerator getQueryGenerator() {
|
105
|
return queryGenerator;
|
106
|
}
|
107
|
|
108
|
public void setQueryGenerator(QueryGenerator queryGenerator) {
|
109
|
this.queryGenerator = queryGenerator;
|
110
|
}
|
111
|
|
112
|
public String getPathToSaveRecord() {
|
113
|
return pathToSaveRecord;
|
114
|
}
|
115
|
|
116
|
public void setPathToSaveRecord(String pathToSaveRecord) {
|
117
|
this.pathToSaveRecord = pathToSaveRecord;
|
118
|
}
|
119
|
|
120
|
}
|