Project

General

Profile

1
package eu.dnetlib.openaire.blacklist;
2

    
3
import java.io.StringReader;
4
import java.util.Collection;
5
import java.util.List;
6
import java.util.Set;
7

    
8
import javax.xml.ws.wsaddressing.W3CEndpointReference;
9

    
10
import com.google.common.collect.Sets;
11
import org.apache.commons.lang.StringUtils;
12
import org.apache.commons.logging.Log;
13
import org.apache.commons.logging.LogFactory;
14
import org.dom4j.Document;
15
import org.dom4j.DocumentException;
16
import org.dom4j.Element;
17
import org.dom4j.io.SAXReader;
18
import org.springframework.beans.factory.annotation.Autowired;
19
import org.springframework.beans.factory.annotation.Value;
20

    
21
import com.google.common.collect.Lists;
22

    
23
import eu.dnetlib.enabling.database.rmi.DatabaseException;
24
import eu.dnetlib.enabling.database.rmi.DatabaseService;
25
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
26
import eu.dnetlib.enabling.resultset.client.IterableResultSetClient;
27
import eu.dnetlib.enabling.resultset.client.ResultSetClientFactory;
28
import eu.dnetlib.miscutils.collections.MappedCollection;
29
import eu.dnetlib.miscutils.functional.UnaryFunction;
30
import eu.dnetlib.openaire.hadoop.utils.HBaseTableUtils;
31

    
32
public class BlacklistManager {
33

    
34
	private static final Log log = LogFactory.getLog(BlacklistManager.class); // NOPMD by marko on 11/24/08 5:02 PM
35
	@Value("${dnet.openaire.blacklist.db.name}")
36
	private String blacklistDatabaseName;
37
	@Autowired
38
	private UniqueServiceLocator serviceLocator;
39
	@Autowired
40
	private ResultSetClientFactory resultSetClientFactory;
41
	@Autowired
42
	private OpenaireIdResolver openaireIdResolver;
43

    
44
	private Set<String> getOriginalIds(final String id, final String entityType) {
45
		Set<String> originalIds = Sets.newHashSet();
46
		// We need to find original ids only for entities that are deduplicated: result, organization, person.
47
		if (entityType.equals("result") || entityType.equals("organization") || entityType.equals("person")) {
48
			originalIds = openaireIdResolver.resolveIdentifier(id);
49
		}
50
		return originalIds;
51
	}
52

    
53
	public void addToBlacklist(final BlacklistEntry entry) throws DatabaseException {
54
		final Set<String> sourceIds = getOriginalIds(entry.getSourceObject(), entry.getSourceType());
55
		final Set<String> targetIds = getOriginalIds(entry.getTargetObject(), entry.getTargetType());
56
		sourceIds.add(entry.getSourceObject());
57
		targetIds.add(entry.getTargetObject());
58

    
59
		final DatabaseService dbService = serviceLocator.getService(DatabaseService.class);
60
		final String addQuery = String.format(
61
				"INSERT INTO blacklist(userid, relationship, provenance, iis_module, iis_status, status, source_object, source_type, target_object, target_type, "
62
						+ "ticket_id, original_source_objects, original_target_objects, note) "
63
						+ " VALUES(%1$s, %2$s, %3$s, %4$s, %5$s, %6$s, %7$s, %8$s, %9$s, %10$s, %11$s, %12$s, %13$s, %14$s)",
64
				asSqlParam(entry.getUser()),
65
				asSqlParam(entry.getRelationship()),
66
				asSqlParam(entry.getProvenance()),
67
				asSqlParam(entry.getIisModule()),
68
				asSqlParam(entry.getIisStatus()),
69
				asSqlParam(entry.getStatus()),
70
				asSqlParam(entry.getSourceObject()),
71
				asSqlParam(entry.getSourceType()),
72
				asSqlParam(entry.getTargetObject()),
73
				asSqlParam(entry.getTargetType()),
74
				asSqlParam(entry.getTicketId()),
75
				asSqlParam(joinCollectionForSQL(sourceIds)),
76
				asSqlParam(joinCollectionForSQL(targetIds)),
77
				asSqlParam(entry.getNote()));
78
		log.debug("Adding new blacklist entry");
79
		this.safeUpdateSql(dbService, blacklistDatabaseName, addQuery);
80
	}
81

    
82
	private String joinCollectionForSQL(final Collection<String> coll) {
83
		return "{\"" + StringUtils.join(coll, "\",\"") + "\"}";
84
	}
85

    
86
	public void editBlacklistEntry(final BlacklistEntry entry) throws DatabaseException {
87

    
88
		final DatabaseService dbService = serviceLocator.getService(DatabaseService.class);
89
		final String editQuery = String.format(
90
				"UPDATE blacklist SET userid=%s, relationship=%s, provenance=%s, iis_module=%s, iis_status=%s, status=%s, source_type=%s, "
91
						+ "target_type=%s, ticket_id=%s, note=%s WHERE id=%s",
92
				asSqlParam(entry.getUser()),
93
				asSqlParam(entry.getRelationship()),
94
				asSqlParam(entry.getProvenance()),
95
				asSqlParam(entry.getIisModule()),
96
				asSqlParam(entry.getIisStatus()),
97
				asSqlParam(entry.getStatus()),
98
				asSqlParam(entry.getSourceType()),
99
				asSqlParam(entry.getTargetType()),
100
				asSqlParam(entry.getTicketId()),
101
				asSqlParam(entry.getNote()),
102
				asSqlParam(entry.getId()));
103
		log.debug("Editing blacklist entry: " + entry.getId());
104
		this.safeUpdateSql(dbService, blacklistDatabaseName, editQuery);
105
	}
106

    
107
	private String asSqlParam(final Object o) {
108
		if (o == null) {
109
			return "NULL";
110
		} else if ((o instanceof Number) || (o instanceof Boolean)) {
111
			return o.toString();
112
		} else {
113
			return "'" + o.toString() + "'";
114
		}
115
	}
116

    
117
	public void deleteFromBlacklist(final int entryId) throws DatabaseException {
118
		final DatabaseService dbService = serviceLocator.getService(DatabaseService.class);
119
		log.debug("Deleting entry " + entryId + " from blacklist");
120
		this.safeUpdateSql(dbService, blacklistDatabaseName, String.format("DELETE FROM blacklist WHERE id='%s' ", entryId));
121
	}
122

    
123
	public Iterable<BlacklistEntry> getBlacklist() throws DatabaseException {
124
		final String sqlQuery = "SELECT * from blacklist order by ticket_id";
125
		final W3CEndpointReference epr = serviceLocator.getService(DatabaseService.class).searchSQL(blacklistDatabaseName, sqlQuery);
126
		final IterableResultSetClient iter = resultSetClientFactory.getClient(epr);
127
		return this.getBlacklistIterable(iter);
128
	}
129

    
130
	public W3CEndpointReference getAcceptedBlacklistEntries() throws DatabaseException {
131
		final String sqlQuery =
132
				"SELECT source_type, unnest(original_source_objects) as source, target_type, unnest(original_target_objects) as target, relationship FROM blacklist WHERE status = 'ACCEPTED'";
133
		return serviceLocator.getService(DatabaseService.class).searchSQL(blacklistDatabaseName, sqlQuery);
134
	}
135

    
136
	private void safeUpdateSql(final DatabaseService dbService, final String dbName, final String sql) throws DatabaseException {
137
		log.info(sql);
138
		dbService.updateSQL(dbName, sql);
139
	}
140

    
141
	public Iterable<BlacklistEntry> getBlacklistIterable(final Iterable<String> xmlEntries) {
142
		return new MappedCollection<BlacklistEntry, String>(xmlEntries, new UnaryFunction<BlacklistEntry, String>() {
143

    
144
			@Override
145
			public BlacklistEntry evaluate(final String dbEntry) {
146
				final SAXReader saxReader = new SAXReader();
147
				final BlacklistEntry be = new BlacklistEntry();
148
				Document doc;
149
				try {
150
					doc = saxReader.read(new StringReader(dbEntry));
151
					be.setId(Integer.parseInt(doc.selectSingleNode("//FIELD[./@name='id']").getText()));
152
					be.setCreationDate(doc.selectSingleNode("//FIELD[./@name='creation_time']").getText());
153
					be.setLastUpdateDate(doc.selectSingleNode("//FIELD[./@name='last_update_time']").getText());
154
					be.setNote(doc.selectSingleNode("//FIELD[./@name='note']").getText());
155
					be.setRelationship(doc.selectSingleNode(".//FIELD[./@name='relationship']").getText());
156
					be.setStatus(STATUS.valueOf(StringUtils.upperCase(doc.selectSingleNode("//FIELD[./@name='status']").getText())));
157
					be.setTicketId(doc.selectSingleNode("//FIELD[./@name='ticket_id']").getText());
158
					be.setUser(doc.selectSingleNode("//FIELD[./@name='userid']").getText());
159
					be.setSourceObject(doc.selectSingleNode("//FIELD[./@name='source_object']").getText());
160
					be.setSourceType(doc.selectSingleNode("//FIELD[./@name='source_type']").getText());
161
					be.setTargetObject(doc.selectSingleNode("//FIELD[./@name='target_object']").getText());
162
					be.setTargetType(doc.selectSingleNode("//FIELD[./@name='target_type']").getText());
163
					final String provenance = doc.selectSingleNode("//FIELD[./@name='provenance']").getText();
164
					be.setProvenance(provenance);
165
					if (provenance.equalsIgnoreCase("iis")) {
166
						be.setIisModule(doc.selectSingleNode("//FIELD[./@name='iis_module']").getText());
167
						be.setIisStatus(IIS_STATUS.valueOf(StringUtils.upperCase(doc.selectSingleNode("//FIELD[./@name='iis_status']").getText())));
168
					}
169
					// get the array of original identifiers
170
					final List<Element> sources = doc.selectNodes("//FIELD[./@name='original_source_objects']/ITEM");
171
					if ((sources != null) && !sources.isEmpty()) {
172
						for (final Element e : sources) {
173
							be.getOriginalSourceObjects().add(e.getText());
174
						}
175
					}
176

    
177
					final List<Element> targets = doc.selectNodes("//FIELD[./@name='original_target_objects']/ITEM");
178
					if ((targets != null) && !targets.isEmpty()) {
179
						for (final Element e : targets) {
180
							be.getOriginalTargetObjects().add(e.getText());
181
						}
182
					}
183
				} catch (final DocumentException e) {
184
					log.error(e);
185
					throw new RuntimeException(e);
186
				}
187
				return be;
188
			}
189
		});
190
	}
191

    
192
	public Set<String> getListOfRelationships() {
193
		return HBaseTableUtils.listRelationships();
194
	}
195

    
196
	public enum IIS_STATUS {
197
		UNSOLVED, SOLVED
198
	}
199

    
200
	public enum STATUS {
201
		PENDING, ACCEPTED, REFUSED, DELETED
202
	}
203

    
204
}
(2-2/3)