Project

General

Profile

1
	package eu.dnetlib.openaire.thrift;
2

    
3
import eu.dnetlib.actionmanager.common.Agent;
4
import eu.dnetlib.actionmanager.common.Agent.AGENT_TYPE;
5
import eu.dnetlib.actionmanager.common.Operation;
6
import eu.dnetlib.actionmanager.common.Provenance;
7
import eu.dnetlib.actionmanager.set.ActionManagerSet;
8
import eu.dnetlib.api.enabling.ActionManagerService;
9
import eu.dnetlib.api.enabling.ISLookUpService;
10
import eu.dnetlib.api.enabling.ISLookUpServiceException;
11
import eu.dnetlib.openaire.action.ActionUtils;
12
import gr.uoa.di.driver.enabling.ISLookUpException;
13
import gr.uoa.di.driver.util.ServiceLocator;
14

    
15
import java.io.FileWriter;
16
import java.sql.Connection;
17
import java.sql.PreparedStatement;
18
import java.sql.ResultSet;
19
import java.sql.SQLException;
20
import java.util.ArrayList;
21
import java.util.Date;
22
import java.util.List;
23

    
24
import javax.sql.DataSource;
25

    
26
import org.apache.log4j.Logger;
27
import org.apache.velocity.exception.ParseErrorException;
28
import org.apache.velocity.exception.ResourceNotFoundException;
29
import org.springframework.transaction.annotation.Transactional;
30
import org.springframework.jdbc.datasource.DataSourceUtils;
31

    
32
public class ClaimConnector {
33
	
34
	private static Logger logger = Logger.getLogger(ClaimConnector.class);
35
	
36
	private DataSource datasource = null;
37
	private ServiceLocator<ActionManagerService> actionManagerLocator = null;
38

    
39
	//Using an isLookUp Impl just to make an xQuery to IS. Maybe move all the 
40
	//functionality to commons?
41
	private ServiceLocator<ISLookUpService> lookupServiceLocator = null;   
42
	
43
	public List<Claim> getClaimedPublications(final String user) throws SQLException {
44
		Connection con = DataSourceUtils.getConnection(datasource);
45
		PreparedStatement stmt = con.prepareStatement("select \"resultid\", \"date\" , \"type\", \"xml\" from claims where \"agent\" = ?");
46
		stmt.setString(1, user);
47
		ResultSet rs = stmt.executeQuery();
48
		List<Claim> res = new ArrayList<Claim>();
49
		
50
		while (rs.next()) {
51
            res.add(new Claim(rs.getString(1), rs.getDate(2).getTime(), rs.getString(3), rs.getString(4)));
52
		}
53

    
54
        rs.close();
55
        stmt.close();
56
	con.close();
57

    
58
		return res;
59
	}
60

    
61
    public List<Claim> getClaimedPublications(Date from, Date to) throws SQLException {
62
	logger.info("Returning all claimed publications from " + from + " to " + to);
63
        Connection con = DataSourceUtils.getConnection(datasource);
64

    
65
        PreparedStatement stmt = con.prepareStatement("select \"resultid\", \"date\" , \"type\", \"xml\" from claims where \"agent\" != 'inference' and \"date\" >= ?::date and \"date\" <= ?::date ");
66

    
67
        stmt.setDate(1, new java.sql.Date(from.getTime()));
68
        stmt.setDate(2, new java.sql.Date(to.getTime()));
69

    
70
        ResultSet rs = stmt.executeQuery();
71
        List<Claim> res = new ArrayList<Claim>();
72

    
73
        while (rs.next()) {
74
            res.add(new Claim(rs.getString(1), rs.getDate(2).getTime(), rs.getString(3), rs.getString(4)));
75
        }
76

    
77
        rs.close();
78
        stmt.close();
79
	con.close();
80

    
81
	logger.debug("number of claims: " + res.size());
82

    
83
        return res;
84
    }
85

    
86
    public void deleteClaim(String userEmail, String resultId) throws SQLException {
87

    
88
        Connection con = DataSourceUtils.getConnection(datasource);
89

    
90
        PreparedStatement stmt = con.prepareStatement("delete from claims where \"agent\" = ? and resultid =?");
91
        stmt.setString(1, userEmail);
92
        stmt.setString(2, resultId);
93

    
94
        stmt.executeUpdate();
95
        stmt.close();
96
	con.close();
97
    }
98

    
99
    /**
100
     *
101
     * @param id {openaireId, publication doi, ORCIDId (personId-ORCIDPublicationId), data doi}
102
     * @param source {openaire, doi (crossref), orcid, datacite}
103
     * @param title
104
     * @param description
105
     * @param access_mode
106
     * @param embargoEndDate
107
     * @param authors
108
     * @param url
109
     * @param dcSource
110
     * @param userName
111
     * @param userEmail
112
     * @param projects
113
     * @param subjects
114
     * @param publicationDate
115
     * @param publisher
116
     * @param language
117
     * @param category
118
     * @param doi
119
     * @throws ResourceNotFoundException
120
     * @throws ParseErrorException
121
     * @throws Exception
122
     */
123
	@Transactional
124
	public void insertAndClaimDocument(
125
			String id, 
126
			String source, 
127
			String title, 
128
			String description, 
129
			String access_mode, 
130
			String embargoEndDate, 
131
			List<Author> authors, 
132
			String url,
133
			String dcSource,
134
			String userName, 
135
			String userEmail, 
136
			List<Project> projects,
137
			List<String> subjects,
138
            List<String> concepts,
139
			String publicationDate,
140
			String publisher, 
141
			String language, 
142
			String category, 
143
			String doi) throws Exception {
144
		
145
		Connection con = DataSourceUtils.getConnection(datasource);
146
		PreparedStatement stmt = null;
147
		String dmf = null;
148
		String resultId = ActionUtils.getIdentifier(ActionUtils.getPrefix(source), id);
149
		Date now = new Date();
150

    
151
		if (!source.toLowerCase().equals("openaire") && title != null) {
152
			stmt = con.prepareStatement("insert into claims (\"agent\", \"resultid\", \"date\", type, xml, set, provenance, nsprefix) values (?, ?, ?, ?, ?, ?, ?, ?)");
153
			dmf = ActionUtils.createDMF(id, source, title, description, access_mode, embargoEndDate, authors, url, dcSource, doi, new ArrayList<Project>(), subjects, concepts, publicationDate, publisher, language, category);
154
			
155
			stmt.setString(1, userEmail);
156
			stmt.setString(2, resultId);
157
			stmt.setDate(3, new java.sql.Date(now.getTime()));
158
			stmt.setString(4, "dmf2actions");
159
			stmt.setString(5, dmf);
160
            stmt.setString(6, ActionUtils.getSet(source, "dmf"));
161
            stmt.setString(7, ActionUtils.getProvenance(source));
162
            stmt.setString(8, ActionUtils.getPrefix(source));
163
			
164
			stmt.executeUpdate();
165
			stmt.close();
166
		}
167
		
168
		for (Project project:projects) {
169
			String xml = ActionUtils.createResultProjectRelation("openaire".equals(source)?null:source, id, project.getType(), project.getProjectId());
170
			
171
			stmt = con.prepareStatement("insert into claims (\"agent\", \"resultid\", \"date\", type, xml, set, provenance, nsprefix) values (?, ?, ?, ?, ?, ?, ?, ?)");
172
			
173
			stmt.setString(1, userEmail);
174
			stmt.setString(2, resultId);
175
			stmt.setDate(3, new java.sql.Date(now.getTime()));
176
			stmt.setString(4, "rels2actions");
177
			stmt.setString(5, xml);
178
            stmt.setString(6, ActionUtils.getSet(source, "relation"));
179
            stmt.setString(7, ActionUtils.getProvenance(source));
180
            stmt.setString(8, ActionUtils.getPrefix(source));
181

    
182
			stmt.executeUpdate();
183
			stmt.close();
184
		}
185

    
186
		con.close();
187
	}	
188
	@Deprecated
189
	public void insertInferredDocument(
190
			String id, 
191
			String source, 
192
			String title,
193
			String description, 
194
			String access_mode, 
195
			String embargoEndDate,
196
			List<Author> authors, 
197
			String url, 
198
			String dcSource,
199
			List<Project> projects,
200
			List<String> subjects,
201
            List<String> concepts,
202
			String publicationDate, 
203
			String publisher, 
204
			String language, 
205
			String category, 
206
			String doi,
207
            String setName) throws Exception {
208

    
209
        System.out.println("Adding doc " + id);
210

    
211
        try {
212
            actionManagerLocator.getService().createSet(new ActionManagerSet(setName, setName));
213
        } catch (Exception e) {
214
            if (e.getMessage() != null && e.getMessage().contains("already registered")) {
215
                logger.debug("Set already exists");
216
            } else {
217
                throw e;
218
            }
219
        }
220

    
221
        if (title != null) {
222
            String dmf = ActionUtils.createDMF(id, source, title, description, access_mode, embargoEndDate, authors, url, dcSource, doi, projects, subjects, new ArrayList<String>(), publicationDate, publisher, language, category);
223
            FileWriter fw = new FileWriter("/tmp/concepts/" + id + "-insert.xml");
224

    
225
            fw.append(dmf);
226

    
227
            fw.close();
228
           actionManagerLocator.getService().createAction("dmf2actions", setName, new Agent("inference", "inference",  AGENT_TYPE.service), Operation.INSERT, dmf, Provenance.sysimport_mining_repository, "0.9", ActionUtils.getPrefix(source));
229
        }
230

    
231
        String dmf = ActionUtils.createDMF(id, source, null, null, null, null, new ArrayList<Author>(), null, null, null, new ArrayList<Project>(), new ArrayList<String>(), concepts, null, null, null, null);
232

    
233
        FileWriter fw = new FileWriter("/tmp/concepts/" + id + "-update.xml");
234

    
235
        fw.append(dmf);
236

    
237
        fw.close();
238

    
239
        actionManagerLocator.getService().createAction("dmf2updateActions", setName, new Agent("inference", "inference",  AGENT_TYPE.service), Operation.INSERT, dmf, Provenance.sysimport_mining_repository, "0.9", ActionUtils.getPrefix(source));
240

    
241
	}
242
	
243
	@Deprecated
244
	public void insertInferredRelation(String resultSource, String resultId,
245
			String projectType, String projectId, String setName) throws Exception {
246
        String relation = ActionUtils.createResultProjectRelation(resultSource, resultId, projectType, projectId);
247

    
248
		try {
249
			actionManagerLocator.getService().createSet(new ActionManagerSet(setName, setName));
250
		} catch (Exception e) {
251
			logger.warn("Set already exists");
252
		}
253

    
254
		//actionManagerLocator.getService().createAction("rels2actions", setName, new Agent("inference", "inference",  AGENT_TYPE.service), Operation.INSERT, relation, Provenance.sysimport_mining_repository, "0.9", "openaire____");
255
	}
256
	
257
	public ServiceLocator<ActionManagerService> getActionManagerLocator() {
258
		return actionManagerLocator;
259
	}
260

    
261
	public void setActionManagerLocator(
262
			ServiceLocator<ActionManagerService> actionManagerLocator) {
263
		this.actionManagerLocator = actionManagerLocator;
264
	}
265

    
266
	public DataSource getDatasource() {
267
		return datasource;
268
	}
269

    
270
	public void setDatasource(DataSource datasource) {
271
		this.datasource = datasource;
272
	}
273
	
274
	public ServiceLocator<ISLookUpService> getLookupServiceLocator() {
275
		return lookupServiceLocator;
276
	}
277

    
278
	public void setLookupServiceLocator(
279
			ServiceLocator<ISLookUpService> lookupServiceLocator) {
280
		this.lookupServiceLocator = lookupServiceLocator;
281
	}
282

    
283
	public List<String> getContexts() throws ISLookUpException, ISLookUpServiceException {
284
		return lookupServiceLocator.getService().quickSearchProfile("for $x in collection('/db/DRIVER/ContextDSResources/ContextDSResourceType') return $x");
285
	}
286
}
(2-2/4)