Project

General

Profile

« Previous | Next » 

Revision 48670

Copying old connector implementation, just in case.

View differences:

modules/uoa-openaire-connector/trunk/deploy.info
1
{
2
  "type_source": "SVN", 
3
  "goal": "package -U -T 4C source:jar", 
4
  "url": "http://svn-public.driver.research-infrastructures.eu/driver/dnet40/modules/uoa-openaire-connector/trunk", 
5
  "deploy_repository": "dnet4-snapshots", 
6
  "version": "4", 
7
  "mail": "antleb@di.uoa.gr, kiatrop@di.uoa.gr", 
8
  "deploy_repository_url": "http://maven.research-infrastructures.eu/nexus/content/repositories/dnet4-snapshots", 
9
  "name": "uoa-openaire-connector"
10
}
modules/uoa-openaire-connector/trunk/src/test/java/eu/dnetlib/openaire/thrift/ThriftClient.java
1
package eu.dnetlib.openaire.thrift;
2

  
3
import org.apache.thrift.protocol.TBinaryProtocol;
4
import org.apache.thrift.protocol.TProtocol;
5
import org.apache.thrift.transport.TSocket;
6
import org.apache.thrift.transport.TTransport;
7
import org.apache.thrift.transport.TTransportException;
8

  
9
import eu.dnetlib.openaire.thrift.OpenAIREConnector;
10

  
11
public class ThriftClient {
12

  
13
	public OpenAIREConnector.Client getConnector() throws TTransportException {
14
		return getConnector("localhost", 7911);
15
	}
16

  
17
	public OpenAIREConnector.Client getConnector(String host, int port) throws TTransportException {
18
		TTransport transport = new TSocket(host, port);
19
		transport.open();
20

  
21
		TProtocol protocol = new  TBinaryProtocol(transport);
22
		
23
		return new OpenAIREConnector.Client(protocol);
24
	}
25
}
modules/uoa-openaire-connector/trunk/src/test/java/eu/dnetlib/openaire/thrift/TestConnector.java
1
package eu.dnetlib.openaire.thrift;
2

  
3
import org.apache.thrift.TException;
4

  
5
import java.util.ArrayList;
6
import java.util.Arrays;
7

  
8
/**
9
 * Created by antleb on 6/20/14.
10
 */
11
public class TestConnector {
12

  
13
	public static void main(String[] args) throws TException {
14

  
15
		OpenAIREConnector.Client client = new ThriftClient().getConnector("194.177.192.223", 7911);
16

  
17
		String id = client.insertDocument("123123/3434.23", "doi", "a title", " a description", "OPEN ACCESS", null,
18
                Arrays.asList(new Author("id" ,"lastname", "firstname")), "url", "dcsource", "antleb@di.uoa.gr",
19
                Arrays.asList("subject"), "2012-01-02", "publisher", "language", "category", "doioioioi" ,
20
                new ArrayList<Project4Index>(), new ArrayList<String>());
21
		System.out.println(id);
22

  
23
		client.insertConcept(id, "doi", "egi", "antleb@di.uoa.gr", "123");
24
		client.insertProjectRelation(id, "doi", new Project("ec", "1233312"), "antleb@di.uoa.gr");
25
		client.insertProjectRelation(id, "doi", new Project(null, "1233312"), "antleb@di.uoa.gr");
26
		client.insertResultRelation(id, "result", "doi", id, "result", "antleb@di.uoa.gr");
27

  
28
		id = "od______1406::fbda7b28cda277b04c8dc27a5e0deb68";
29

  
30
		client.insertConcept(id, "openaire", "ec", "antleb@di.uoa.gr", "123");
31
		client.insertProjectRelation(id, "openaire", new Project("ec", "099809987"), "antleb@di.uoa.gr");
32
		client.insertProjectRelation(id, "openaire", new Project(null, "098908908"), "antleb@di.uoa.gr");
33
		client.insertResultRelation(id, "result", "openaire", id, "dataset", "antleb@di.uoa.gr");
34
	}
35
}
modules/uoa-openaire-connector/trunk/src/main/thrift/OpenAIREConnector.thrift
1
namespace java eu.dnetlib.openaire.thrift
2

  
3
struct Author {
4
	1: string id,
5
	2: string lastName,
6
	3: string firstName
7
}
8

  
9
// alerts
10
struct AlertSubscription {
11
	1: string templateId,
12
	2: string notificationService,
13
	3: string queryId,
14
	4: string resultId,
15
	5: string alertMode,
16
	6: string subscriber
17
}
18

  
19
struct ResultPage {
20
	1: i32 limit,
21
	2: i32 offset,
22
	3: list<string> columns,
23
	4: list<list<string>> rows
24
}
25

  
26
struct Claim {
27
    1: string userEmail,
28
	2: string resultid,
29
	3: i64 date,
30
	4: string type,
31
	5: string xml,
32
	6: string id
33
}
34

  
35
struct Project {
36
    1: string type,
37
    2: string projectId
38
}
39

  
40
struct Project4Index {
41
    1: string projectid,
42
    2: string funder,
43
    3: string fundingStreamLevel0,
44
    4: string fundingStreamLevel1,
45
    5: string fundingStreamLevel2,
46
    6: string name,
47
    7: string code,
48
    8: string acronym,
49
    9: string type
50
}
51

  
52
// connector
53
service OpenAIREConnector {
54
	// claim
55
	list<Claim> getClaimedPublications(1: string userEmail),
56

  
57
	// from: date in milisecs
58
	// to: ditto
59
	list<Claim> getAllClaimedPublications(1: i64 from, 2: i64 to),
60
	
61
	string insertDocument (
62
			1: string originalId,
63
			2: string source, 
64
			3: string title, 
65
			4: string description, 
66
			5: string access_mode, 
67
			6: string embargoEndDate, 
68
			7: list<Author> authors, 
69
			8: string url,
70
			9: string dcSource,
71
			10: string userEmail,
72
			11: list<string> subjects,
73
			12: string publicationDate,
74
			13: string publisher,
75
			14: string language,
76
			15: string category,
77
			16: string doi,
78
			17: list<Project4Index> projects,
79
			18: list<string> concepts),
80

  
81
    void insertConcept (
82
        1: string docId,
83
        2: string documentSource,
84
        3: string concept,
85
        4: string userEmail,
86
        5: string doi),
87

  
88
    void insertProjectRelation (
89
        1: string docId,
90
        2: string documentSource,
91
        3: Project project,
92
        4: string userEmail),
93

  
94
    void insertResultRelation (
95
        1: string sourceResultId,
96
        2: string sourceResultType,
97
        3: string sourceResultSource,
98
        4: string targetResultId,
99
        5: string targetResultType,
100
        6: string userEmail
101
    ),
102

  
103
	void deleteClaim(1: string userEmail, 2:string resultId),
104

  
105
	// alerts
106
	list<AlertSubscription> getSubscriptions(
107
			1: string alertMode, 
108
			2: string subscriber, 
109
			3: i32 limit, 
110
			4: i32 offset),
111
			
112
	void addSubscription(1: AlertSubscription subscription),
113
	
114
	void removeSubscription(1: AlertSubscription subscription),
115
	
116
	i32 countAlertResults(
117
			1: string notificationService, 
118
			2: string queryId, 
119
			3: i64 date, 
120
			5: string resultId),
121
			
122
	ResultPage getAlertResults(
123
			1: string notificationService, 
124
			2: string queryId, 
125
			3: string resultId, 
126
			4: i64 fromDate, 
127
			5: i64 toDate, 
128
			6: i32 limit, 
129
			7: i32 offset),
130
	
131
	list<string> getContexts(),
132
}
modules/uoa-openaire-connector/trunk/src/main/java/eu/dnetlib/openaire/thrift/OpenAIREConnectorImpl.java
1
package eu.dnetlib.openaire.thrift;
2

  
3
import eu.dnetlib.api.functionality.AlertServiceException;
4
import org.apache.log4j.Logger;
5
import org.apache.thrift.TException;
6

  
7
import java.net.MalformedURLException;
8
import java.net.URISyntaxException;
9
import java.util.Date;
10
import java.util.List;
11

  
12
public class OpenAIREConnectorImpl implements OpenAIREConnector.Iface {
13
	private static final Logger logger = Logger.getLogger(OpenAIREConnectorImpl.class);
14

  
15
	private ClaimConnector claimConnector;
16
	private AlertConnector alertConnector;
17

  
18
	public void setClaimConnector(final ClaimConnector claimConnector) {
19
		this.claimConnector = claimConnector;
20
	}
21

  
22
	public void setAlertConnector(final AlertConnector alertConnector) {
23
		this.alertConnector = alertConnector;
24
	}
25

  
26
	@Override
27
	public List<Claim> getClaimedPublications(final String user) throws TException {
28
		try {
29
			return claimConnector.getClaimedPublications(user);
30
		} catch (final Exception e) {
31
			logger.error("Error retrieving claimed publications (user: " + user + ")", e);
32
			throw new TException("Error retrieving claimed publications (user: " + user + ")", e);
33
		}
34
	}
35

  
36
    @Override
37
    public List<Claim> getAllClaimedPublications(long from, long to) throws TException {
38
        try {
39
            return claimConnector.getClaimedPublications(new Date(from), new Date(to));
40
        } catch (Exception e) {
41
            logger.error("Error retrieving claimed publications", e);
42

  
43
            throw new TException("Error retrieving claimed publications", e);
44
        }
45
    }
46

  
47
    @Override
48
    public String insertDocument(String originalId, String source, String title, String description, String access_mode,
49
                                 String embargoEndDate, List<Author> authors, String url, String dcSource,
50
                                 String userEmail, List<String> subjects, String publicationDate, String publisher,
51
                                 String language, String category, String doi, List<Project4Index> project4Indexes,
52
                                 List<String> concepts) throws org.apache.thrift.TException {
53
        try {
54
            logger.info("concepts " + concepts);
55
            return claimConnector.insertDocument(originalId, source, title, description, access_mode, embargoEndDate,
56
                    authors, url, dcSource, userEmail, subjects, publicationDate, publisher, language, category, doi,
57
                    project4Indexes, concepts);
58
        } catch (Exception e) {
59
            logger.error("Error adding document", e);
60

  
61
            throw new TException(e);
62
        }
63
    }
64

  
65
    @Override
66
    public void insertConcept(String docId, String documentSource, String concept, String userEmail, String doi) throws org.apache.thrift.TException {
67
        try {
68
            claimConnector.insertConceptRelation(docId, documentSource, concept, userEmail, doi);
69
        } catch (Exception e) {
70
            logger.error("Error adding concept relation", e);
71

  
72
            throw new TException(e);
73
        }
74
    }
75

  
76
    @Override
77
    public void insertProjectRelation(String docId, String documentSource, Project project, String userEmail) throws org.apache.thrift.TException {
78
        try {
79
            claimConnector.insertProjectRelation(docId, documentSource, project, userEmail);
80
        } catch (Exception e) {
81
            logger.error("Error adding project relation", e);
82

  
83
            throw new TException(e);
84
        }
85
    }
86

  
87
    @Override
88
    public void insertResultRelation(String sourceResultId, String sourceResultType, String sourceResultSource, String targetResultId, String targetResultType, String userEmail) throws TException {
89
        try {
90
            claimConnector.insertResultRelation(sourceResultId, sourceResultType, sourceResultSource, targetResultId, targetResultType,userEmail);
91
        } catch (Exception e) {
92
            logger.error("Error adding result relation", e);
93

  
94
            throw new TException(e);
95
        }
96
    }
97

  
98
    @Override
99
	public List<AlertSubscription> getSubscriptions(final String alertMode, final String subscriber, final int limit, final int offset) throws TException {
100
		try {
101
			return alertConnector.getSubscriptions(alertMode, subscriber, limit, offset);
102
		} catch (final AlertServiceException e) {
103
			logger.error("Error retrieving subscriptions (alert mode: " + alertMode + ", subscriber: " + subscriber + ", limit: " + limit + ", offset: " + offset + ")", e);
104
			throw new TException("Error retrieving subscriptions (alert mode: " + alertMode + ", subscriber: " + subscriber + ", limit: " + limit + ", offset: " + offset + ")", e);
105
		}
106
	}
107

  
108
	@Override
109
	public void addSubscription(final AlertSubscription subscription) throws TException {
110
		try {
111
			alertConnector.addSubscription(subscription);
112
		} catch (final AlertServiceException e) {
113
			logger.error("Error adding subscription", e);
114
			throw new TException("Error adding subscription", e);
115
		} catch (final MalformedURLException e) {
116
			logger.error("Error adding subscription", e);
117
			throw new TException("Error adding subscription", e);
118
		} catch (final URISyntaxException e) {
119
			logger.error("Error adding subscription", e);
120
			throw new TException("Error adding subscription", e);
121
		}
122
	}
123

  
124
	@Override
125
	public void removeSubscription(final AlertSubscription subscription) throws TException {
126
		try {
127
			alertConnector.removeSubscription(subscription);
128
		} catch (final AlertServiceException e) {
129
			logger.error("Error removing subscription", e);
130
			throw new TException("Error removing subscription", e);
131
		} catch (final MalformedURLException e) {
132
			logger.error("Error removing subscription", e);
133
			throw new TException("Error removing subscription", e);
134
		} catch (final URISyntaxException e) {
135
			logger.error("Error removing subscription", e);
136
			throw new TException("Error removing subscription", e);
137
		}
138
	}
139

  
140
	@Override
141
	public int countAlertResults(final String notificationService, final String queryId, final long date, final String resultId) throws TException {
142
		try {
143
			return alertConnector.countAlertResults(notificationService, queryId, date, resultId);
144
		} catch (final AlertServiceException e) {
145
			logger.error("Error counting alert results (notification service: " + notificationService + ", query: " + queryId + ", date: " + date + ", result: " + resultId + ")", e);
146
			throw new TException("Error counting alert results (notification service: " + notificationService + ", query: " + queryId + ", date: " + date + ", result: " + resultId + ")", e);
147
		} catch (final MalformedURLException e) {
148
			logger.error("Error counting alert results (notification service: " + notificationService + ", query: " + queryId + ", date: " + date + ", result: " + resultId + ")", e);
149
			throw new TException("Error counting alert results (notification service: " + notificationService + ", query: " + queryId + ", date: " + date + ", result: " + resultId + ")", e);
150
		}
151
	}
152

  
153
	@Override
154
	public ResultPage getAlertResults(final String notificationService, final String queryId, final String resultId, final long fromDate, final long toDate, final int limit, final int offset) throws TException {
155
		try {
156
			return alertConnector.getAlertResults(notificationService, queryId, resultId, fromDate, toDate, limit, offset);
157
		} catch (final AlertServiceException e) {
158
			logger.error("Error retrieving alert results (notification service: " + notificationService + ", query: " + queryId + ", result: " + resultId + ", from date: " + fromDate + ", to date: " + toDate + ", limit: " +
159
					limit + ", offset: " + offset + ")", e);
160
			throw new TException("Error retrieving alert results (notification service: " + notificationService + ", query: " + queryId + ", result: " + resultId + ", from date: " + fromDate + ", to date: " + toDate +
161
					", limit: " + limit + ", offset: " + offset + ")", e);
162
		} catch (final MalformedURLException e) {
163
			logger.error("Error retrieving alert results (notification service: " + notificationService + ", query: " + queryId + ", result: " + resultId + ", from date: " + fromDate + ", to date: " + toDate + ", limit: " +
164
					limit + ", offset: " + offset + ")", e);
165
			throw new TException("Error retrieving alert results (notification service: " + notificationService + ", query: " + queryId + ", result: " + resultId + ", from date: " + fromDate + ", to date: " + toDate +
166
					", limit: " + limit + ", offset: " + offset + ")", e);
167
		}
168
	}
169

  
170
    @Override
171
    public void deleteClaim(String userEmail, String resultId) throws TException {
172
        try {
173
            claimConnector.deleteClaim(userEmail, resultId);
174
        } catch (Exception e) {
175
            logger.error("Error deleting claim", e);
176

  
177
            throw new TException(e);
178
        }
179
    }
180

  
181
	@Override
182
	public List<String> getContexts() throws TException {
183
		try {
184
			return claimConnector.getContexts();
185
		} catch (Exception e) {
186
			logger.error("Error getting contexts from IS.", e);
187
			
188
			throw new TException(e);
189
		}
190
		
191
	}
192
}
modules/uoa-openaire-connector/trunk/src/main/java/eu/dnetlib/openaire/thrift/ClaimConnector.java
1
package eu.dnetlib.openaire.thrift;
2

  
3

  
4
import eu.dnetlib.api.enabling.ISLookUpService;
5
import eu.dnetlib.api.enabling.ISLookUpServiceException;
6
import eu.dnetlib.openaire.action.ActionUtils;
7
import eu.dnetlib.openaire.action.IndexUtils;
8
import gr.uoa.di.driver.enabling.ISLookUpException;
9
import gr.uoa.di.driver.util.ServiceLocator;
10
import org.apache.log4j.Logger;
11
import org.apache.velocity.exception.ParseErrorException;
12
import org.apache.velocity.exception.ResourceNotFoundException;
13
import org.springframework.jdbc.datasource.DataSourceUtils;
14
import org.springframework.transaction.annotation.Transactional;
15

  
16
import javax.sql.DataSource;
17
import java.sql.Connection;
18
import java.sql.PreparedStatement;
19
import java.sql.ResultSet;
20
import java.sql.SQLException;
21
import java.sql.Timestamp;
22
import java.util.ArrayList;
23
import java.util.Arrays;
24
import java.util.Collections;
25
import java.util.Date;
26
import java.util.List;
27

  
28
@Transactional
29
public class ClaimConnector {
30

  
31
    private static Logger logger = Logger.getLogger(ClaimConnector.class);
32

  
33
    private String directClaimAPIUrl = null;
34
    private DataSource datasource = null;
35

  
36
    //Using an isLookUp Impl just to make an xQuery to IS. Maybe move all the
37
    //functionality to commons?
38
    private ServiceLocator<ISLookUpService> lookupServiceLocator = null;
39

  
40
    public List<Claim> getClaimedPublications(final String user) throws SQLException {
41
        Connection con = DataSourceUtils.getConnection(datasource);
42
        PreparedStatement stmt = con.prepareStatement("select \"resultid\", \"date\" , \"type\", \"xml\", \"id\" from claims where \"agent\" = ?");
43
        stmt.setString(1, user);
44
        ResultSet rs = stmt.executeQuery();
45
        List<Claim> res = new ArrayList<Claim>();
46

  
47
        while (rs.next()) {
48
            res.add(new Claim(user, rs.getString(1), rs.getTimestamp(2).getTime(), rs.getString(3), rs.getString(4), rs.getString(5)));
49
        }
50

  
51
        rs.close();
52
        stmt.close();
53

  
54
        logger.info("Returning " + res.size() + " claimed publications for user "  + user);
55

  
56
        return res;
57
    }
58

  
59
    public List<Claim> getClaimedPublications(Date from, Date to) throws SQLException {
60
        Connection con = DataSourceUtils.getConnection(datasource);
61

  
62
        PreparedStatement stmt = con.prepareStatement("select \"agent\", \"resultid\", \"date\" , \"type\", \"xml\",  \"id\"  from claims where \"agent\" != 'inference' and \"date\" >= ?::timestamp and \"date\" <= ?::timestamp ");
63
        stmt.setTimestamp(1, new Timestamp(from.getTime()));
64
        stmt.setTimestamp(2, new Timestamp(to.getTime()));
65

  
66
        ResultSet rs = stmt.executeQuery();
67
        List<Claim> res = new ArrayList<Claim>();
68

  
69
        while (rs.next()) {
70
            res.add(new Claim(rs.getString(1), rs.getString(2), rs.getTimestamp(3).getTime(), rs.getString(4), rs.getString(5), rs.getString(6)));
71
        }
72

  
73
        rs.close();
74
        stmt.close();
75

  
76
        logger.debug("number of claims: " + res.size());
77
        logger.info("Returning " + res.size() + " claimed publications for date "  + from + "-" + to);
78

  
79
        return res;
80
    }
81

  
82
    public void deleteClaim(String userEmail, String id) throws SQLException {
83
        logger.info("Deleting claim with id " + id + " for agent " +  userEmail);
84

  
85
        Connection con = DataSourceUtils.getConnection(datasource);
86
        int claim_id = Integer.parseInt(id);
87
        String type = null;
88

  
89
        PreparedStatement stmt = con.prepareStatement("select \"resultid\", \"type\" from claims where \"id\" =?");
90
        stmt.setInt(1, claim_id);
91

  
92
        ResultSet rs = stmt.executeQuery();
93

  
94
        String resultid = null;
95
        String xml = null;
96

  
97
        while (rs.next()) {
98
            resultid = rs.getString("resultid");
99
            type = rs.getString("type");
100
        }
101

  
102
        if (resultid != null) {
103
            stmt = con.prepareStatement("delete from claims where \"agent\" =? and \"id\" =?");
104
            stmt.setString(1, userEmail);
105
            stmt.setInt(2, claim_id);
106

  
107
            stmt.executeUpdate();
108
            logger.info("Deleted claim with claim id: " + claim_id + ", result id: " + resultid + " and type: " + type +".") ;
109

  
110
            //prepare to get 2nd claim to be deleted
111
            int claim2_id = claim_id - 1;
112

  
113
            if (type != null && type.equals("rels2actions")) {
114
                con = DataSourceUtils.getConnection(datasource);
115
                stmt = con.prepareStatement("select \"type\", \"xml\" from claims where \"id\" =? and \"resultid\"=?");
116
                stmt.setInt(1, claim2_id);
117
                stmt.setString(2, resultid);
118
                rs = stmt.executeQuery();
119

  
120
                while (rs.next()) {
121
                    type = rs.getString("type");
122
                    xml = rs.getString("xml");
123
                }
124

  
125
                if(type!= null && type.equals("dmf2actions") && xml != null && !xml.contains("oaf:concept")) { //check if the relation has dmf
126
                    stmt =  con.prepareStatement("delete from claims where \"agent\" =? and \"id\" =? and \"resultid\" =? and \"type\" = ?");
127
                    stmt.setString(1, userEmail);
128
                    stmt.setInt(2, claim2_id);
129
                    stmt.setString(3, resultid);
130
                    stmt.setString(4, "dmf2actions");
131

  
132
                    stmt.executeUpdate();
133
                    logger.info("Deleted claim with id " + claim2_id + " and type: dmf2actions.");
134
                }
135

  
136
            } /*else if (type != null && type.equals("dmf2actions")) {
137
                logger.info("dmf2actions");
138
                con = DataSourceUtils.getConnection(datasource);
139
                stmt = con.prepareStatement("select \"type\", \"xml\" from claims where \"id\" =? and \"resultid\"=?");
140
                stmt.setInt(1, claim2_id);
141
                stmt.setString(2, resultid);
142

  
143
                rs = stmt.executeQuery();
144
                while (rs.next()) {
145
                    type = rs.getString("type");
146
                    xml = rs.getString("xml");
147
                }
148

  
149
                logger.info("xml: " + xml);
150
                logger.info("xml? " + xml.contains("oaf:concept"));
151

  
152
                if(type!= null && type.equals("dmf2actions") && xml != null && !xml.contains("oaf:concept")) { //delete concept with extra dmf (doi,orchid,... etc)
153
                    logger.debug("dmf2actions and not concept");
154

  
155
                    stmt =  con.prepareStatement("delete from claims where \"agent\" =? and \"id\" =? and \"resultid\" =? and \"type\" = ?");
156
                    stmt.setString(1, userEmail);
157
                    stmt.setInt(2, claim2_id);
158
                    stmt.setString(3, resultid);
159
                    stmt.setString(4, "dmf2actions");
160

  
161
                    stmt.executeUpdate();
162
                    logger.info("Deleted claim with id " + claim2_id + " and type: dmf2actions.");
163
                }
164

  
165
            }*/
166
        }
167

  
168
        stmt.close();
169

  
170
        logger.info("Deleted claim with id " + id + " for agent " +  userEmail);
171
    }
172

  
173
    /**
174
     * @param id     {openaireId, publication doi, ORCIDId (personId-ORCIDPublicationId), data doi}
175
     * @param source {openaire, doi (crossref), orcid, datacite}
176
     * @throws ResourceNotFoundException
177
     * @throws ParseErrorException
178
     * @throws Exception
179
     */
180
    public String insertDocument(
181
            String id,
182
            String source,
183
            String title,
184
            String description,
185
            String access_mode,
186
            String embargoEndDate,
187
            List<Author> authors,
188
            String url,
189
            String dcSource,
190
            String userEmail,
191
            List<String> subjects,
192
            String publicationDate,
193
            String publisher,
194
            String language,
195
            String category,
196
            String doi,
197
            List<Project4Index> project4Indexes,
198
            List<String> concepts) throws Exception {
199

  
200
        logger.info("Request to insert document with id " + id + " and source " + source);
201
        logger.info("The title is: " + title);
202

  
203
        String resultId;
204

  
205
        try {
206
            Connection con = DataSourceUtils.getConnection(datasource);
207
            PreparedStatement stmt;
208
            String dmf;
209

  
210
            resultId = ActionUtils.getIdentifier(ActionUtils.getPrefix(source), id);
211
            Date now = new Date();
212

  
213
            if (!source.toLowerCase().equals("openaire") && title != null) {
214
                stmt = con.prepareStatement("insert into claims (\"agent\", \"resultid\", \"date\", type, xml, set, provenance, nsprefix) values (?, ?, ?, ?, ?, ?, ?, ?)");
215
                dmf = ActionUtils.createDMF(id, source, title, description, access_mode, embargoEndDate, authors, url, dcSource, doi, Collections.EMPTY_LIST, subjects, Collections.EMPTY_LIST, publicationDate, publisher, language, category, false);
216

  
217
                stmt.setString(1, userEmail);
218
                stmt.setString(2, resultId);
219
                stmt.setTimestamp(3, new Timestamp(new Date().getTime()));
220
                stmt.setString(4, "dmf2actions");
221
                stmt.setString(5, dmf);
222
                stmt.setString(6, "userclaim_dmf");
223
                stmt.setString(7, ActionUtils.getProvenance(source));
224
                stmt.setString(8, ActionUtils.getPrefix(source));
225

  
226
                stmt.executeUpdate();
227
                stmt.close();
228

  
229
            }
230
        } catch (Exception e) {
231
            logger.error("Error inserting document", e);
232
            throw e;
233
        }
234

  
235
        if (!datasource.equals("openaire"))
236
            IndexUtils.feedIndex(id, source, title, description, access_mode, embargoEndDate, authors, url, dcSource,
237
                    doi, Collections.EMPTY_LIST, subjects, Collections.EMPTY_LIST, publicationDate, publisher,
238
                    language, category, false, project4Indexes, concepts, directClaimAPIUrl);
239

  
240
        logger.info("Inserted document with id " + id + ", source " + source + " and given resultId " + resultId);
241
        return resultId;
242
    }
243

  
244
    public void insertConceptRelation(String documentId, String documentSource, String concept, String userEmail, String doi) throws Exception {
245
        logger.info("Request for concept relation between document with id " + documentId + " with source " + documentSource + " and concept " + concept + "with doi " + doi);
246

  
247
        try {
248
            Connection con = DataSourceUtils.getConnection(datasource);
249
            PreparedStatement stmt;
250
            String dmf;
251

  
252
            stmt = con.prepareStatement("insert into claims (\"agent\", \"resultid\", \"date\", type, xml, set, provenance, nsprefix) values (?, ?, ?, ?, ?, ?, ?, ?)");
253
            dmf = ActionUtils.createDMF(documentId, documentSource, null, null, null, null, Collections.EMPTY_LIST, null, null, doi, Collections.EMPTY_LIST, Collections.EMPTY_LIST, Arrays.asList(concept), null, null, null, null, true);
254

  
255
            stmt.setString(1, userEmail);
256
            stmt.setString(2, documentId);
257
            stmt.setTimestamp(3, new Timestamp(new Date().getTime()));
258
            stmt.setString(4, "updates2actions");
259
            stmt.setString(5, dmf);
260
            stmt.setString(6, "userclaim_dmf");
261
            stmt.setString(7, ActionUtils.getProvenance(documentSource));
262
            stmt.setString(8, ActionUtils.getPrefix(documentSource));
263

  
264
            stmt.executeUpdate();
265
            stmt.close();
266
        } catch (Exception e) {
267
            logger.error("Error adding concept", e);
268
            throw e;
269
        }
270

  
271
        logger.info("Inserted concept relation between document with id " + documentId + " with source " + documentSource + " and concept " + concept);
272
    }
273

  
274
    public void insertProjectRelation(String docId, String documentSource, Project project, String userEmail) throws Exception {
275
        logger.info("Request for project relation between document with id " + docId + " with source " + documentSource + " and project " + project);
276

  
277
        try {
278
            Connection con = DataSourceUtils.getConnection(datasource);
279
            PreparedStatement stmt;
280
            String xml = ActionUtils.createResultProjectRelation("openaire".equals(documentSource) ? null : documentSource, docId, project.getType(), project.getProjectId());
281

  
282
            stmt = con.prepareStatement("insert into claims (\"agent\", \"resultid\", \"date\", type, xml, set, provenance, nsprefix) values (?, ?, ?, ?, ?, ?, ?, ?)");
283

  
284
            stmt.setString(1, userEmail);
285
            stmt.setString(2, docId);
286
            stmt.setTimestamp(3, new Timestamp(new Date().getTime()));
287
            stmt.setString(4, "rels2actions");
288
            stmt.setString(5, xml);
289
            stmt.setString(6, "userclaim_result_project");
290
            stmt.setString(7, ActionUtils.getProvenance(documentSource));
291
            stmt.setString(8, ActionUtils.getPrefix(documentSource));
292

  
293
            stmt.executeUpdate();
294
            stmt.close();
295
        } catch (Exception e) {
296
            logger.error("Error adding project", e);
297
            throw e;
298
        }
299

  
300
        logger.info("Inserted project relation between document with id " + docId + " with source " + documentSource + " and project " + project);
301
    }
302

  
303
    public void insertResultRelation(String sourceResultId, String sourceResultType, String sourceResultSource, String targetResultId, String targetResultType, String userEmail) throws Exception {
304
        logger.info("Request for result relation between source document with id " + sourceResultId + ", type " + sourceResultType + "and source " + sourceResultSource + " and " +
305
                "target document with id " + targetResultId + ", type " + targetResultType);
306
        try {
307
            Connection con = DataSourceUtils.getConnection(datasource);
308
            PreparedStatement stmt;
309
            String xml = ActionUtils.createResultResultRelation(sourceResultId, sourceResultType, targetResultId, targetResultType);
310

  
311
            stmt = con.prepareStatement("insert into claims (\"agent\", \"resultid\", \"date\", type, xml, set, provenance, nsprefix) values (?, ?, ?, ?, ?, ?, ?, ?)");
312

  
313
            stmt.setString(1, userEmail);
314
            stmt.setString(2, sourceResultId);
315
            stmt.setTimestamp(3, new Timestamp(new Date().getTime()));
316
            stmt.setString(4, "rels2actions");
317
            stmt.setString(5, xml);
318
            stmt.setString(6, "userclaim_result_result");
319
            stmt.setString(7, ActionUtils.getProvenance(sourceResultSource));
320
            stmt.setString(8, ActionUtils.getPrefix(sourceResultSource));
321

  
322
            stmt.executeUpdate();
323
            stmt.close();
324
        } catch (Exception e) {
325
            logger.error("Error adding result relation", e);
326
            throw e;
327
        }
328

  
329
        logger.info("Inserted result relation between source document with id " + sourceResultId + ", type " + sourceResultType + "and source " + sourceResultSource + " and " +
330
                "target document with id " + targetResultId + ", type " + targetResultType);
331
    }
332

  
333
    public DataSource getDatasource() {
334
        return datasource;
335
    }
336

  
337
    public void setDatasource(DataSource datasource) {
338
        this.datasource = datasource;
339
    }
340

  
341
    public ServiceLocator<ISLookUpService> getLookupServiceLocator() {
342
        return lookupServiceLocator;
343
    }
344

  
345
    public void setLookupServiceLocator(
346
            ServiceLocator<ISLookUpService> lookupServiceLocator) {
347
        this.lookupServiceLocator = lookupServiceLocator;
348
    }
349

  
350
    public List<String> getContexts() throws ISLookUpException, ISLookUpServiceException {
351
        return lookupServiceLocator.getService().quickSearchProfile("for $x in collection('/db/DRIVER/ContextDSResources/ContextDSResourceType') return $x");
352
    }
353

  
354
    public String getDirectClaimAPIUrl() {
355
        return directClaimAPIUrl;
356
    }
357

  
358
    public void setDirectClaimAPIUrl(String directClaimAPIUrl) {
359
        this.directClaimAPIUrl = directClaimAPIUrl;
360
    }
361
}
modules/uoa-openaire-connector/trunk/src/main/java/eu/dnetlib/openaire/thrift/AlertConnector.java
1
package eu.dnetlib.openaire.thrift;
2

  
3
import eu.dnetlib.api.functionality.AlertService;
4
import eu.dnetlib.api.functionality.AlertServiceException;
5
import gr.uoa.di.driver.util.ServiceLocator;
6

  
7
import java.net.MalformedURLException;
8
import java.net.URI;
9
import java.net.URISyntaxException;
10
import java.net.URL;
11
import java.util.ArrayList;
12
import java.util.Arrays;
13
import java.util.Date;
14
import java.util.List;
15

  
16
public class AlertConnector {
17
	private ServiceLocator<AlertService> alertServiceLocator;
18

  
19
	public void setAlertServiceLocator(
20
			final ServiceLocator<AlertService> alertServiceLocator) {
21
		this.alertServiceLocator = alertServiceLocator;
22
	}
23

  
24
	public List<AlertSubscription> getSubscriptions(final String alertMode,
25
			final String subscriber, final int limit, final int offset)
26
			throws AlertServiceException {
27
		final List<AlertSubscription> subscriptions = new ArrayList<AlertSubscription>();
28
		for (eu.dnetlib.domain.functionality.AlertSubscription subscription : alertServiceLocator
29
				.getService().getSubscriptions(alertMode, subscriber, limit,
30
						offset))
31
			subscriptions.add(new AlertSubscription(subscription
32
					.getTemplateId(), subscription.getNotificationService()
33
					.toString(), subscription.getQueryId(), subscription
34
					.getResultId(), subscription.getAlertMode(), subscription
35
					.getSubscriber().toString()));
36
		return subscriptions;
37
	}
38

  
39
	public void addSubscription(final AlertSubscription subscription)
40
			throws AlertServiceException, MalformedURLException,
41
			URISyntaxException {
42
		alertServiceLocator.getService().addSubscription(
43
				new eu.dnetlib.domain.functionality.AlertSubscription(
44
						subscription.getTemplateId(), new URL(subscription
45
								.getNotificationService()), subscription
46
								.getQueryId(), subscription.getResultId(),
47
						subscription.getAlertMode(), new URI(subscription
48
								.getSubscriber()), true));
49
	}
50

  
51
	public void removeSubscription(final AlertSubscription subscription)
52
			throws AlertServiceException, MalformedURLException,
53
			URISyntaxException {
54
		alertServiceLocator.getService().removeSubscription(
55
				subscription.getTemplateId(),
56
				new URL(subscription.getNotificationService()),
57
				subscription.getQueryId(), subscription.getResultId(),
58
				subscription.getAlertMode(),
59
				new URI(subscription.getSubscriber()));
60
	}
61

  
62
	public int countAlertResults(final String notificationService,
63
			final String queryId, final long date, final String resultId)
64
			throws AlertServiceException, MalformedURLException {
65
		return alertServiceLocator.getService()
66
				.countAlertResults(new URL(notificationService), queryId,
67
						new Date(date), resultId);
68
	}
69

  
70
	public ResultPage getAlertResults(final String notificationService,
71
			final String queryId, final String resultId, final long fromDate,
72
			final long toDate, final int limit, final int offset)
73
			throws AlertServiceException, MalformedURLException {
74
		final eu.dnetlib.domain.functionality.ResultPage resultPage = alertServiceLocator
75
				.getService().getAlertResults(new URL(notificationService),
76
						queryId, resultId, new Date(fromDate),
77
						new Date(toDate), limit, offset);
78
		final List<List<String>> rows = new ArrayList<List<String>>();
79
		for (String[] row : resultPage.getRows())
80
			rows.add(Arrays.asList(row));
81
		return new ResultPage(resultPage.getLimit(), resultPage.getOffset(),
82
				Arrays.asList(resultPage.getColumns()), rows);
83
	}
84
}
modules/uoa-openaire-connector/trunk/src/main/java/eu/dnetlib/openaire/thrift/ThriftServer.java
1
package eu.dnetlib.openaire.thrift;
2

  
3
import org.apache.log4j.Logger;
4
import org.apache.thrift.server.TThreadPoolServer;
5
import org.apache.thrift.transport.TServerSocket;
6
import org.apache.thrift.transport.TTransportException;
7

  
8
public class ThriftServer {
9
	private OpenAIREConnector.Iface openAireConnector;
10
	private int port;
11
	private int timeout;
12
	private Thread thread;
13

  
14
	public void setOpenAireConnector(final OpenAIREConnector.Iface openAireConnector) {
15
		this.openAireConnector = openAireConnector;
16
	}
17
	
18
	public void setPort(final int port) {
19
		this.port = port;
20
	}
21
	
22
	public void setTimeout(final int timeout) {
23
		this.timeout = timeout;
24
	}
25
	
26
	public void start() {
27
		thread = new Thread(new Runnable() {
28
			@Override
29
			public void run() {
30
				try {
31
					new TThreadPoolServer(new TThreadPoolServer.Args(new TServerSocket(port, timeout)).processor(new OpenAIREConnector.Processor<OpenAIREConnector.Iface>(openAireConnector))).serve();
32
				} catch (final TTransportException e) {
33
					Logger.getLogger(ThriftServer.class).error("Error starting thrift server", e);
34
				}
35
			}
36
		});
37
		thread.start();
38
	}
39
	
40
	public void stop() {
41
		thread.interrupt();
42
	}
43
}
modules/uoa-openaire-connector/trunk/src/main/java/eu/dnetlib/openaire/action/IndexUtils.java
1
package eu.dnetlib.openaire.action;
2

  
3
import eu.dnetlib.openaire.thrift.Author;
4
import eu.dnetlib.openaire.thrift.Project;
5
import eu.dnetlib.openaire.thrift.Project4Index;
6
import org.apache.log4j.Logger;
7
import org.json.simple.JSONArray;
8
import org.json.simple.JSONObject;
9
import sun.net.www.protocol.http.HttpURLConnection;
10

  
11
import java.io.*;
12
import java.net.URL;
13
import java.util.List;
14

  
15
/**
16
 * Created by kiatrop on 26/1/2016.
17
 */
18
public class IndexUtils {
19

  
20
    private static final Logger logger = Logger.getLogger(IndexUtils.class);
21

  
22
    public static JSONObject createJSON(String id, String source, String title, String description, String access_mode,
23
                                        String embargoEndDate, List<Author> authors, String url, String dcSource,
24
                                        String doi, List<Project> projects, List<String> subjects, List<String> concepts,
25
                                        String publicationDate, String publisher, String language, String category,
26
                                        boolean isConcept, List<Project4Index> project4Indexes, List<String> concepts4index) throws Exception {
27

  
28
        if (source.equals("openaire"))
29
            throw new IllegalArgumentException("cannot create JSON for openaire");
30

  
31
        //logger.info("create JSON for doi " + doi);
32
        //logger.info("projects " + project4Indexes);
33

  
34
        //if the orcid id exists
35
        String orcid = null;
36
        String hostedby = null;
37
        String hostedbyName = null;
38
        String collectedFrom = null;
39
        String collectedFromName = null;
40
        String recordIdentifier = id;
41

  
42
        if (source.equals("doi") && !isConcept) {
43
            hostedby = "openaire____::1256f046-bf1f-4afc-8b47-d0b147148b18";
44
            hostedbyName = "Unknown Repository";
45
            collectedFrom = "openaire____::crossref";
46
            collectedFromName = "Crossref";
47
            doi = id;
48

  
49
        } else if (source.equals("datacite")) {
50
            hostedby = "openaire____::1256f046-bf1f-4afc-8b47-d0b147148b18";
51
            hostedbyName = "Unknown Repository";
52
            collectedFrom = "openaire____::datacite";
53
            collectedFromName = "Datacite";
54

  
55
        } else if (source.equals("orcid")) {
56
            orcid = id;
57
            hostedby = "openaire____::1256f046-bf1f-4afc-8b47-d0b147148b18";
58
            hostedbyName = "Unknown Repository";
59
            collectedFrom = "openaire____::orcid";
60
            collectedFromName = "ORCID";
61

  
62
        } else if (source.equals("driver")) {
63
            hostedby = "openaire____::1256f046-bf1f-4afc-8b47-d0b147148b18";
64
            hostedbyName = "Unknown Repository";
65
            collectedFrom = "openaire____::driver";
66
            collectedFromName = "Digital Repository Infrastructure Vision for European Research";
67
        }
68

  
69
        JSONObject claimJSON = new JSONObject();
70
        String prefix = ActionUtils.getPrefix(source);
71
        claimJSON.put("originalId", id);
72
        claimJSON.put("title", title);
73

  
74
        JSONArray authorsJSONArray = new JSONArray();
75
        for (Author author: authors) {
76
            authorsJSONArray.add(author.getFirstName() + " " + author.getLastName());
77
        }
78
        claimJSON.put("authors", authorsJSONArray);
79

  
80
        claimJSON.put("publisher", publisher);
81
        claimJSON.put("description", description);
82
        claimJSON.put("language", language);
83

  
84
        JSONArray pidsJSONArray = new JSONArray();
85
        JSONObject pidJSON = null;
86
        if (doi != null) {
87
            pidJSON = new JSONObject();
88
            pidJSON.put("type", "doi");
89
            pidJSON.put("value", doi);
90
            pidsJSONArray.add(pidJSON);
91
        }
92
        if (orcid != null) {
93
            pidJSON = new JSONObject();
94
            pidJSON.put("type", "orcidworkid");
95
            pidJSON.put("value", orcid);
96
            pidsJSONArray.add(pidJSON);
97
        }
98
        claimJSON.put("pids", pidsJSONArray);
99

  
100
        claimJSON.put("licenseCode", access_mode);
101
        if(source.equals("crossref")) {
102
            claimJSON.put("resourceType", "0001");
103
        } else if (source.equals("datacite")) {
104
            claimJSON.put("resourceType", "0021");
105
        } else {
106
            claimJSON.put("resourceType", "0001");
107
        }
108

  
109
        claimJSON.put("url", url);
110
        claimJSON.put("collectedFromId", collectedFrom);
111
        claimJSON.put("hostedById", hostedby);
112

  
113
        JSONArray linksToProjectsJSONArray = new JSONArray();
114
        for (Project4Index project:project4Indexes) {
115
            linksToProjectsJSONArray.add(createProjectPath(project)) ;
116
        }
117
        claimJSON.put("linksToProjects",linksToProjectsJSONArray);
118

  
119
        JSONArray contextsJSONArray = new JSONArray();
120
        if(!concepts4index.isEmpty()) {
121
            for (String context: concepts4index) {
122
                contextsJSONArray.add(context); //TODO check
123
            }
124
        }
125
        claimJSON.put("contexts", contextsJSONArray);
126

  
127
        return claimJSON;
128
    }
129

  
130
    public static void feedIndex(String id, String source, String title,
131
                                 String description, String access_mode, String embargoEndDate,
132
                                 List<Author> authors, String url, String dcSource, String doi,
133
                                 List<Project> projects, List<String> subjects, List<String> concepts,
134
                                 String publicationDate, String publisher, String language, String category,
135
                                 boolean isConcept, List<Project4Index> project4Indexes, List<String> concepts4index,
136
                                 String directClaimAPIUrl) throws Exception {
137

  
138
        logger.info("number of projects " + project4Indexes);
139
        if (project4Indexes!= null) {
140
            logger.info("number of projects " + project4Indexes.size());
141
        }
142

  
143
        URL obj = new URL(directClaimAPIUrl);
144
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();
145

  
146
        JSONObject claimJson = createJSON(id, source, title, description, access_mode, embargoEndDate,
147
                                    authors, url, dcSource, doi, projects, subjects, concepts, publicationDate,
148
                                    publisher, language, category, isConcept, project4Indexes, concepts4index);
149
        //add request header
150
        con.setRequestMethod("POST");
151
        //con.setRequestProperty("User-Agent", USER_AGENT);
152
        con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
153
        con.setRequestProperty("Content-Type", "application/json");
154

  
155
        logger.info("Feeding index with " + claimJson.toJSONString());
156
        String urlParameters = claimJson.toJSONString();
157

  
158
        // Send post request
159
        con.setDoOutput(true);
160
        DataOutputStream wr = new DataOutputStream(con.getOutputStream());
161
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(wr, "UTF-8"));
162
        writer.write(urlParameters);
163
        writer.close();
164
        wr.close();
165

  
166
        int responseCode = con.getResponseCode();
167
        System.out.println("\nSending 'POST' request to URL : " + url);
168
        System.out.println("Post parameters : " + urlParameters);
169
        System.out.println("Response Code : " + responseCode);
170

  
171
        BufferedReader in = new BufferedReader(
172
                new InputStreamReader(con.getInputStream()));
173
        String inputLine;
174
        StringBuffer response = new StringBuffer();
175

  
176
        while ((inputLine = in.readLine()) != null) {
177
            response.append(inputLine);
178
        }
179
        in.close();
180

  
181
        //print result
182
        System.out.println(response.toString());
183
    }
184

  
185
    private static String createProjectPath(Project4Index project) {
186
        if (project.getFunder().equals("EC")) {
187
            if(project.getAcronym() != null && !project.getAcronym().trim().isEmpty())
188
                return "info:eu-repo/grantAgreement/EC/"+project.getFundingStreamLevel0()+"/"
189
                    +project.getCode()+"/EU//"+project.getAcronym();
190

  
191
            return "info:eu-repo/grantAgreement/EC/"+project.getFundingStreamLevel0()+"/"
192
                    +project.getCode()+"/EU//"+project.getName();
193
        }
194

  
195
        if (project.getFunder().equals("WT")) {
196
            if(project.getAcronym() != null && !project.getAcronym().trim().isEmpty())
197
                return "info:eu-repo/grantAgreement/WT//"+project.getCode()+"//" + project.getAcronym();
198

  
199
            return "info:eu-repo/grantAgreement/WT//"+project.getCode()+project.getName();
200
        }
201

  
202
        if (project.getFunder().equals("ARC")) {
203
            logger.debug("name: '" + project.getName()+"'");
204
            logger.debug("name: '" + project.getAcronym()+"'");
205
            if(project.getAcronym() != null && !project.getAcronym().trim().isEmpty())
206
                return "info:eu-repo/grantAgreement/ARC/"+project.getFundingStreamLevel0()+"/"
207
                    +project.getCode()+"/AU//"+project.getAcronym();
208

  
209
            return "info:eu-repo/grantAgreement/ARC/"+project.getFundingStreamLevel0()+"/"
210
                    +project.getCode()+"/AU//"+project.getName();
211
        }
212

  
213
        if (project.getFunder().equals("NHMRC")) {
214
            if(project.getAcronym() != null && !project.getAcronym().trim().isEmpty())
215
                return "info:eu-repo/grantAgreement/NHMRC/"+project.getFundingStreamLevel0()+"/"
216
                        +project.getCode()+"/AU//"+project.getAcronym();
217

  
218
            return "info:eu-repo/grantAgreement/NHMRC/"+project.getFundingStreamLevel0()+"/"
219
                    +project.getCode()+"/AU//"+project.getName();
220
        }
221

  
222
        if (project.getFunder().equals("FCT")) {
223
            if(project.getAcronym() != null && !project.getAcronym().trim().isEmpty())
224
                return "info:eu-repo/grantAgreement/FCT/"+project.getFundingStreamLevel0()+"/"
225
                        +project.getCode()+"/PT//"+project.getAcronym();
226

  
227
            return "info:eu-repo/grantAgreement/FCT/"+project.getFundingStreamLevel0()+"/"
228
                    +project.getCode()+"/PT//"+project.getName();
229
        }
230

  
231
        return "";
232
    }
233

  
234
    public static void main(String[] args) throws IOException {
235

  
236
        String url = "http://beta.services.openaire.eu:8280/is/mvc/api/publications/feedObject";
237
        URL obj = new URL(url);
238
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();
239

  
240
        //add reuqest header
241
        con.setRequestMethod("POST");
242
        //con.setRequestProperty("User-Agent", USER_AGENT);
243
        con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
244
        con.setRequestProperty("Content-Type", "application/json");
245

  
246

  
247
        /*String urlParameters = "{\n" +
248
                "\"collectedFromId\": \"openaire____::crossref\",\n" +
249
                "\"authors\": [\"George Papastefanatos\", \"Panos Vassiliadis\", \"Alkis Simitsis\", \"Yannis Vassiliou\"],\n" +
250
                "\"title\": \"HECATAEUS: Regulating schema evolution\",\n" +
251
                "\"hostedById\": \"openaire____::1256f046-bf1f-4afc-8b47-d0b147148b18\",\n" +
252
                "\"licenseCode\": \"OPEN\",\n" +
253
                "\"pids\": [{\n" +
254
                "\"value\": \"10.1109\\/icde.2010.5447778\",\n" +
255
                "\"type\": \"doi\"\n" +
256
                "}],\n" +
257
                "\"description\": null,\n" +
258
                "\"originalId\": \"crossref____::817ee94bc4a0515adadb1fdaa78927f6\",\n" +
259
                "\"contexts\": [],\n" +
260
                "\"language\": null,\n" +
261
                "\"linksToProjects\": [\"info:eu-repo/grantAgreement/EC/FP7/283595/EU//OpenAIREplus\"],\n" +
262
                "\"url\": \"http:\\/\\/dx.doi.org\\/10.1109\\/icde.2010.5447778\",\n" +
263
                "\"resourceType\": \"0001\",\n" +
264
                "\"publisher\": \"Institute of Electrical & Electronics Engineers (IEEE)\"\n" +
265
                "}";
266

  
267
        String urlParameters = "{\n" +
268
                "\"collectedFromId\": \"openaire____::crossref\",\n" +
269
                "\"authors\": [\"George Papastefanatos\", \"Panos Vassiliadis\", \"Alkis Simitsis\", \"Yannis Vassiliou\"],\n" +
270
                " \"title\": \"â\\\\ dark bottomless Abyss, that lies under our feet, had yawned openâ\\\\\",\n" +
271
                "\"hostedById\": \"openaire____::1256f046-bf1f-4afc-8b47-d0b147148b18\",\n" +
272
                "\"licenseCode\": \"CLOSED\",\n" +
273
                "\"pids\": [{\n" +
274
                "\"value\": \"10.1007\\/s13740_012_0006_9\",\n" +
275
                "\"type\": \"doi\"\n" +
276
                "}],\n" +
277
                "\"description\": null,\n" +
278
                "\"originalId\": \"crossref____::test\",\n" +
279
                "\"contexts\": [\"egi::classification::natsc::infos::dmin\"], " +
280
                "\"language\": null, " +
281
                "\"linksToProjects\": [\"info:eu-repo/grantAgreement/EC/FP7/283595/EU//OpenAIREplus\"], " +
282
                "\"url\": \"http:\\/\\/dx.doi.org\\/10.1007\\/s13740_012_0006_9\", " +
283
                "\"resourceType\": \"0001\", " +
284
                "\"publisher\": \"Springer Science + Business Media\" " +
285
                "}";
286
*/
287

  
288
        String urlParameters = "{ " +
289
                "\"collectedFromId\": \"openaire____::crossref\", " +
290
                "\"authors\": [\"Agapi Dalkou\"], " +
291
                "\"title\": \"Η ανάπτυξη ερμηνευτικών στρατηγικών στο μάθημα της νεοελληνικής λογοτεχνίας στη δευτεροβάθμια εκπαίδευση σε περιβάλλον συνεργατικής μάθησης\", " +
292
                "\"hostedById\": \"openaire____::1256f046-bf1f-4afc-8b47-d0b147148b18\", " +
293
                "\"licenseCode\": \"CLOSED\", " +
294
                "\"pids\": [{ " +
295
                "\"value\": \"10.12681\\/eadd\\/29202\", " +
296
                "\"type\": \"doi\" " +
297
                "}], " +
298
                "\"description\": null, " +
299
                "\"originalId\": \"crossref____::cf7d85a13ac43f92431b102723d13d5a\", " +
300
                "\"contexts\": [\"egi\", \"egi::country::gr\"], " +
301
                "\"language\": null, " +
302
                "\"linksToProjects\": [\"info:eu-repo\\/grantAgreement\\/EC\\/FP7\\/246686\\/EU\\/\\/OPENAIRE\", \"info:eu-repo\\/grantAgreement\\/EC\\/FP7\\/288611\\/EU\\/\\/ECODRIVER\"], " +
303
                "\"url\": \"http:\\/\\/dx.doi.org\\/10.12681\\/eadd\\/29202\", " +
304
                "\"resourceType\": \"0001\", " +
305
                "\"publisher\": \"National Documentation Centre\"" +
306
                "}";
307

  
308
        // Send post request
309
        con.setDoOutput(true);
310
        /*DataOutputStream wr = new DataOutputStream(con.getOutputStream());
311
        wr.writeBytes(urlParameters);
312
        wr.flush();
313
        wr.close();
314
        */
315

  
316
        DataOutputStream wr = new DataOutputStream(con.getOutputStream());
317
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(wr, "UTF-8"));
318
        writer.write(urlParameters);
319
        writer.close();
320
        wr.close();
321

  
322

  
323
        int responseCode = con.getResponseCode();
324
        System.out.println(" Sending 'POST' request to URL : " + url);
325
        System.out.println("Post parameters : " + urlParameters);
326
        System.out.println("Response Code : " + responseCode);
327

  
328
        BufferedReader in = new BufferedReader(
329
                new InputStreamReader(con.getInputStream()));
330
        String inputLine;
331
        StringBuffer response = new StringBuffer();
332

  
333
        while ((inputLine = in.readLine()) != null) {
334
            response.append(inputLine);
335
        }
336
        in.close();
337

  
338
        //print result
339
        System.out.println(response.toString());
340
    }
341
}
modules/uoa-openaire-connector/trunk/src/main/java/eu/dnetlib/openaire/action/Relation.java
1
package eu.dnetlib.openaire.action;
2

  
3
public class Relation {
4
	private String type;
5
	private String sourceId;
6
	private String targetId;
7

  
8
	public Relation(String type, String sourceId, String targetId) {
9
		super();
10
		this.type = type;
11
		this.sourceId = sourceId;
12
		this.targetId = targetId;
13
	}
14

  
15
	public String getType() {
16
		return type;
17
	}
18

  
19
	public void setType(String type) {
20
		this.type = type;
21
	}
22

  
23
	public String getSourceId() {
24
		return sourceId;
25
	}
26

  
27
	public void setSourceId(String sourceId) {
28
		this.sourceId = sourceId;
29
	}
30

  
31
	public String getTargetId() {
32
		return targetId;
33
	}
34

  
35
	public void setTargetId(String targetId) {
36
		this.targetId = targetId;
37
	}
38

  
39
}
modules/uoa-openaire-connector/trunk/src/main/java/eu/dnetlib/openaire/action/ActionUtils.java
1
package eu.dnetlib.openaire.action;
2

  
3
import eu.dnetlib.miscutils.datetime.DateUtils;
4
import eu.dnetlib.openaire.thrift.Author;
5
import eu.dnetlib.openaire.thrift.Project;
6
import org.apache.commons.codec.binary.Hex;
7
import org.apache.commons.lang.StringEscapeUtils;
8
import org.apache.log4j.Logger;
9
import org.apache.velocity.Template;
10
import org.apache.velocity.VelocityContext;
11
import org.apache.velocity.app.VelocityEngine;
12
import org.apache.velocity.runtime.RuntimeConstants;
13
import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;
14

  
15
import java.io.StringWriter;
16
import java.security.MessageDigest;
17
import java.util.ArrayList;
18
import java.util.List;
19
import java.util.regex.Pattern;
20

  
21
public class ActionUtils {
22

  
23
	private static Logger logger = Logger.getLogger(ActionUtils.class);
24

  
25
	public static String createResultProjectRelation(String source,
26
			String resultId, String projectType, String projectId)
27
			throws Exception {
28
		VelocityEngine ve = new VelocityEngine();
29
		ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
30
		ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
31
		ve.setProperty("runtime.log.logsystem.class", "org.apache.velocity.runtime.log.NullLogSystem");
32

  
33
		ve.init();
34

  
35
		Template t = ve.getTemplate("/eu/dnetlib/openaire/action/relation.vm");
36
		VelocityContext vc = new VelocityContext();
37

  
38
		List<Relation> rels = new ArrayList<Relation>();
39

  
40
		rels.add(createRelation(source, resultId, projectType, projectId));
41

  
42
		vc.put("relations", rels);
43

  
44
		StringWriter sw = new StringWriter();
45
		t.merge(vc, sw);
46

  
47
		return sw.toString();
48
	}
49

  
50
    public static String createResultResultRelation(String sourceResultId, String sourceResultType, String targetResultId, String targetResultType) {
51
        VelocityEngine ve = new VelocityEngine();
52
        ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
53
        ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
54
        ve.setProperty("runtime.log.logsystem.class", "org.apache.velocity.runtime.log.NullLogSystem");
55

  
56
        ve.init();
57

  
58
        Template t = ve.getTemplate("/eu/dnetlib/openaire/action/relation.vm");
59
        VelocityContext vc = new VelocityContext();
60

  
61
        List<Relation> rels = new ArrayList<Relation>();
62

  
63
        rels.add(new Relation("resultResult_" + sourceResultType + targetResultType + "_isRelatedTo", "50|" + sourceResultId, "50|" + targetResultId));
64

  
65
        vc.put("relations", rels);
66

  
67
        StringWriter sw = new StringWriter();
68
        t.merge(vc, sw);
69

  
70
        return sw.toString();
71
    }
72

  
73
	// input id: 1306.3402
74
	// oai:arXiv.org:1306.3402
75
	// MD5("od________18::oai:arXiv.org:1306.3402")
76

  
77
    /**
78
     * Creates a new relation action.
79
     * @param source the result source. If not null, the resultId is transformed to openaire id. If null, the result is
80
     *               considered to be already an openaire result and is used verbatim
81
     * @param resultId the result id
82
     * @param projectType the project type. If not null, the projectId is transformed to openaire id. If null, the projectId is
83
     *               considered to be already in openaire format and is used verbatim
84
     * @param projectId the project id
85
     *
86
     * @return a relation
87
     */
88
	private static Relation createRelation(String source, String resultId,
89
			String projectType, String projectId) {
90
		String resultPrefix;
91
		String projectPrefix = null;
92

  
93
        if (!isValidOpenaireId(resultId) && source != null) {
94
            if (source.equals("arxiv")) {
95
                resultId = "oai:arXiv.org:" + resultId;
96
            } else if (source.equals("pubmed")) {
97
                resultId = "oai:europepmc.org:" + resultId;
98
            }
99

  
100
            resultPrefix = getPrefix(source);
101
            resultId = getIdentifier(resultPrefix, resultId);
102
        }
103

  
104
	resultId = "50|" + resultId;
105

  
106
        if (projectType != null) {
107
            if (projectType.equals("ec")) {
108
                projectPrefix = "corda_______";
109
            } else if (projectType.equals("wt")) {
110
                projectPrefix = "wt__________::WT";
111
            }
112

  
113
            projectId = projectPrefix + "::" + projectId;
114
        }
115

  
116
	projectId = "40|" + projectId;
117

  
118
		return new Relation("resultProject", resultId, projectId);
119
	}
120

  
121
	private static boolean isValidOpenaireId(String resultId) {
122

  
123
		return Pattern.matches("\\w{12}::[a-f0-9]{32}", resultId);
124
	}
125

  
126
	public static String getPrefix(String source) {
127
        logger.debug("SOURCE " + source);
128
		if (source == null || source.equals("openaire")) {
129
			return "openaire____";
130
		} else if (source.equals("doi")) {
131
			return "crossref____";
132
		} if (source.equals("orcid")) {
133
            		return "orcid_______";
134
		} else if (source.equals("datacite")) {
135
			return "datacite____";
136
		} else if (source.equals("driver")) {
137
			return "driver______";
138
		} else
139
			return null;
140
	}
141

  
142
    public static String getProvenance(String source) {
143
        if (source == null || source.equals("openaire"))
144
            return "user:claim:search";
145
        else if (source.equals("doi") || source.equals("orcid"))
146
            return "user:claim:pid";
147
		else if (source.equals("datacite"))
148
			return "user:claim:datacite";
149
        else
150
            return null;
151
    }
152

  
153
	public static String createDMF(String id, String source, String title,
154
			String description, String access_mode, String embargoEndDate,
155
			List<Author> authors, String url, String dcSource, String doi,
156
			List<Project> projects, List<String> subjects, List<String> concepts, String publicationDate,
157
            String publisher, String language, String category, boolean isConcept) throws Exception {
158

  
159
        logger.info("create with doi " + doi);
160

  
161
		VelocityEngine ve = new VelocityEngine();
162
		ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
163
		ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
164
		ve.setProperty("runtime.log.logsystem.class", "org.apache.velocity.runtime.log.NullLogSystem");
165
		ve.init();
166
		
167

  
168
		Template t = ve.getTemplate("/eu/dnetlib/openaire/action/dmf.vm");
169
		VelocityContext vc = new VelocityContext();
170

  
171
		String hostedby = null;
172
		String hostedbyName = null;
173
		String collectedFrom = null;
174
		String collectedFromName = null;
175
        String recordIdentifier = id;
176
		String prefix = getPrefix(source);
177

  
178

  
179
        //if the orcid id exists
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff