Revision 49662
Added by Michele Artini over 6 years ago
DatasourceManagerClients.java | ||
---|---|---|
25 | 25 |
|
26 | 26 |
import eu.dnetlib.enabling.database.rmi.DatabaseException; |
27 | 27 |
import eu.dnetlib.enabling.database.rmi.DatabaseService; |
28 |
import eu.dnetlib.enabling.datasources.common.DatasourceDesc; |
|
29 |
import eu.dnetlib.enabling.datasources.common.DatasourceManagerServiceException; |
|
28 |
import eu.dnetlib.enabling.datasources.common.DatasourceManagerException; |
|
30 | 29 |
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpDocumentNotFoundException; |
31 | 30 |
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpException; |
32 | 31 |
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService; |
... | ... | |
46 | 45 |
private UniqueServiceLocator serviceLocator; |
47 | 46 |
private ResultSetClientFactory resultSetClientFactory; |
48 | 47 |
|
49 |
public String findDatasourceId(final String profileId) throws DatasourceManagerServiceException {
|
|
48 |
public String findDatasourceId(final String profileId) throws DatasourceManagerException { |
|
50 | 49 |
try { |
51 | 50 |
return serviceLocator.getService(ISLookUpService.class).getResourceProfileByQuery( |
52 | 51 |
"/*[.//RESOURCE_IDENTIFIER/@value='" + profileId + "']//EXTRA_FIELDS/FIELD[./key='OpenAireDataSourceId']/value/text()"); |
53 | 52 |
} catch (final Exception e) { |
54 | 53 |
log.error("Error finding dsId of profile " + profileId, e); |
55 |
throw new DatasourceManagerServiceException("Error finding dsId of profile " + profileId, e);
|
|
54 |
throw new DatasourceManagerException(-1, "Error finding dsId of profile " + profileId, e);
|
|
56 | 55 |
} |
57 | 56 |
} |
58 | 57 |
|
59 |
public String getDatasourceProfile(final String dsId) throws DatasourceManagerServiceException {
|
|
58 |
public String getDatasourceProfile(final String dsId) throws DatasourceManagerException { |
|
60 | 59 |
try { |
61 | 60 |
return serviceLocator.getService(ISLookUpService.class) |
62 | 61 |
.getResourceProfileByQuery( |
... | ... | |
67 | 66 |
} |
68 | 67 |
} |
69 | 68 |
|
70 |
public boolean deleteProfile(final String dsId) throws DatasourceManagerServiceException {
|
|
69 |
public boolean deleteProfile(final String dsId) throws DatasourceManagerException { |
|
71 | 70 |
try { |
72 | 71 |
final SAXReader reader = new SAXReader(); |
73 | 72 |
|
... | ... | |
81 | 80 |
return true; |
82 | 81 |
} catch (final Exception e) { |
83 | 82 |
log.error("Error deleting profile", e); |
84 |
throw new DatasourceManagerServiceException("Error deleting profile", e);
|
|
83 |
throw new DatasourceManagerException(-1, "Error deleting profile", e);
|
|
85 | 84 |
} |
86 | 85 |
} |
87 | 86 |
|
88 |
public boolean regenerateProfile(final String dsId) throws DatasourceManagerServiceException {
|
|
87 |
public boolean regenerateProfile(final String dsId) throws DatasourceManagerException { |
|
89 | 88 |
|
90 | 89 |
try { |
91 | 90 |
final SAXReader reader = new SAXReader(); |
92 | 91 |
|
93 | 92 |
final List<String> list = getTransformedDatasourcesByCondition("ds.id= '" + dsId + "'", new ApplyXslt(xslt)); |
94 | 93 |
|
95 |
if (list.size() != 1) { throw new DatasourceManagerServiceException("Illegal number of datasource with id " + dsId + ", size: " + list.size()); }
|
|
94 |
if (list.size() != 1) { throw new DatasourceManagerException(-1, "Illegal number of datasource with id " + dsId + ", size: " + list.size()); }
|
|
96 | 95 |
|
97 | 96 |
final Document doc = reader.read(new StringReader(list.get(0))); |
98 | 97 |
|
... | ... | |
115 | 114 |
return true; |
116 | 115 |
} catch (final Exception e) { |
117 | 116 |
log.error("Error saving profile, id: " + dsId, e); |
118 |
throw new DatasourceManagerServiceException("Error regenerating profile", e);
|
|
117 |
throw new DatasourceManagerException(-1, "Error regenerating profile", e);
|
|
119 | 118 |
} |
120 | 119 |
} |
121 | 120 |
|
... | ... | |
124 | 123 |
return resultSetClientFactory.getClient(epr); |
125 | 124 |
} |
126 | 125 |
|
127 |
public boolean updateSQL(final String dsId, final String sql, final boolean delete, final boolean updateprofile) throws DatasourceManagerServiceException {
|
|
126 |
public boolean updateSQL(final String dsId, final String sql, final boolean delete, final boolean updateprofile) throws DatasourceManagerException { |
|
128 | 127 |
log.debug("Executing query SQL: " + sql); |
129 | 128 |
|
130 | 129 |
try { |
131 | 130 |
serviceLocator.getService(DatabaseService.class).updateSQL(getDb(), sql); |
132 | 131 |
} catch (final DatabaseException e) { |
133 |
throw new DatasourceManagerServiceException(e);
|
|
132 |
throw new DatasourceManagerException(-1, "Error in SQL query", e);
|
|
134 | 133 |
} |
135 | 134 |
|
136 | 135 |
if (updateprofile) { |
... | ... | |
144 | 143 |
} |
145 | 144 |
|
146 | 145 |
public boolean updateSQL(final String dsId, final String sqlTemplate, final Map<String, Object> params, final boolean delete, final boolean updateProfile) |
147 |
throws DatasourceManagerServiceException {
|
|
146 |
throws DatasourceManagerException { |
|
148 | 147 |
|
149 | 148 |
verifyParams(params.values()); |
150 | 149 |
verifyParams(params.keySet().toArray()); |
... | ... | |
156 | 155 |
return updateSQL(dsId, st.toString(), delete, updateProfile); |
157 | 156 |
} catch (final Exception e) { |
158 | 157 |
log.error("Error in updateSQL", e); |
159 |
throw new DatasourceManagerServiceException("Error in updateSQL", e);
|
|
158 |
throw new DatasourceManagerException(-1, "Error in updateSQL", e);
|
|
160 | 159 |
} |
161 | 160 |
} |
162 | 161 |
|
163 |
public List<DatasourceDesc> getDatasourcesByCondition(final String condition) throws DatasourceManagerServiceException { |
|
164 |
final SAXReader reader = new SAXReader(); |
|
165 |
final List<DatasourceDesc> list = Lists.newArrayList(); |
|
166 |
try { |
|
167 |
for (final String s : getXmlDatasourcesByCondition(condition)) { |
|
168 |
final Document doc = reader.read(new StringReader(s)); |
|
169 |
list.add(DatasourceFunctions.xmlToDatasourceDesc(doc)); |
|
170 |
} |
|
171 |
} catch (final Exception e) { |
|
172 |
log.error("Error obtaining datasources from db", e); |
|
173 |
throw new DatasourceManagerServiceException("Error obtaining datasources from db", e); |
|
174 |
} |
|
175 |
return list; |
|
162 |
private void verifyParams(final Object... params) throws DatasourceManagerException { |
|
176 | 163 |
|
177 |
} |
|
178 |
|
|
179 |
private void verifyParams(final Object... params) throws DatasourceManagerServiceException { |
|
180 |
|
|
181 | 164 |
final Pattern pattern = Pattern.compile("\\n"); |
182 | 165 |
|
183 | 166 |
for (final Object p : params) { |
184 | 167 |
log.debug("TESTING SQL PARAM:" + p); |
185 | 168 |
if ((p == null) || p.toString().isEmpty()) { |
186 | 169 |
log.error("Parameter null or empty"); |
187 |
throw new DatasourceManagerServiceException("Parameter null or empty");
|
|
170 |
throw new DatasourceManagerException(-1, "Parameter null or empty");
|
|
188 | 171 |
} else if (pattern.matcher(p.toString()).matches()) { |
189 | 172 |
log.error("Parameter [" + p + "] contains an invalid character"); |
190 |
throw new DatasourceManagerServiceException("Parameter [" + p + "] contains an invalid character");
|
|
173 |
throw new DatasourceManagerException(-1, "Parameter [" + p + "] contains an invalid character");
|
|
191 | 174 |
} else { |
192 | 175 |
log.debug("TEST OK"); |
193 | 176 |
} |
... | ... | |
195 | 178 |
} |
196 | 179 |
|
197 | 180 |
private List<String> getTransformedDatasourcesByCondition(final String condition, final UnaryFunction<String, String> function) |
198 |
throws DatasourceManagerServiceException {
|
|
181 |
throws DatasourceManagerException { |
|
199 | 182 |
final List<String> list = Lists.newArrayList(); |
200 | 183 |
try { |
201 | 184 |
for (final String s : getXmlDatasourcesByCondition(condition)) { |
... | ... | |
203 | 186 |
} |
204 | 187 |
} catch (final Exception e) { |
205 | 188 |
log.error("Error obtaining datasources from db", e); |
206 |
throw new DatasourceManagerServiceException("Error obtaining datasources from db", e);
|
|
189 |
throw new DatasourceManagerException(-1, "Error obtaining datasources from db", e);
|
|
207 | 190 |
} |
208 | 191 |
return list; |
209 | 192 |
} |
210 | 193 |
|
211 |
private Iterable<String> getXmlDatasourcesByCondition(final String condition) throws DatasourceManagerServiceException {
|
|
194 |
private Iterable<String> getXmlDatasourcesByCondition(final String condition) throws DatasourceManagerException { |
|
212 | 195 |
try { |
213 | 196 |
final Map<String, Object> params = Maps.newHashMap(); |
214 | 197 |
|
... | ... | |
218 | 201 |
return searchSQL("getDatasources.sql.st", params); |
219 | 202 |
} catch (final Exception e) { |
220 | 203 |
log.error("Error obtaining datasources from db", e); |
221 |
throw new DatasourceManagerServiceException("Error obtaining datasources from db", e);
|
|
204 |
throw new DatasourceManagerException(-1, "Error obtaining datasources from db", e);
|
|
222 | 205 |
} |
223 | 206 |
} |
224 | 207 |
|
225 |
public Iterable<String> searchSQL(final String sqlTemplate, final Map<String, Object> params) throws DatasourceManagerServiceException {
|
|
208 |
public Iterable<String> searchSQL(final String sqlTemplate, final Map<String, Object> params) throws DatasourceManagerException { |
|
226 | 209 |
try { |
227 | 210 |
final Resource resource = new ClassPathResource(TMPLS_BASEDIR + sqlTemplate); |
228 | 211 |
final StringTemplate st = new StringTemplate(IOUtils.toString(resource.getInputStream())); |
... | ... | |
238 | 221 |
} catch (final Exception e) { |
239 | 222 |
log.error("Error executing sql", e); |
240 | 223 |
|
241 |
throw new DatasourceManagerServiceException("Error obtaining datasources from db", e);
|
|
224 |
throw new DatasourceManagerException(-1, "Error obtaining datasources from db", e);
|
|
242 | 225 |
} |
243 | 226 |
} |
244 | 227 |
|
245 |
public boolean isDefinedParam(final String ifaceId, final String field) throws DatasourceManagerServiceException {
|
|
228 |
public boolean isDefinedParam(final String ifaceId, final String field) throws DatasourceManagerException { |
|
246 | 229 |
final Map<String, Object> params = Maps.newHashMap(); |
247 | 230 |
params.put("ifaceId", DatasourceFunctions.asSqlValue(ifaceId)); |
248 | 231 |
params.put("field", DatasourceFunctions.asSqlValue(field)); |
... | ... | |
252 | 235 |
return !list.isEmpty(); |
253 | 236 |
} |
254 | 237 |
|
255 |
public Date findNextScheduledExecution(final String dsId, final String ifaceId) throws DatasourceManagerServiceException {
|
|
238 |
public Date findNextScheduledExecution(final String dsId, final String ifaceId) throws DatasourceManagerException { |
|
256 | 239 |
final String xquery = "/*[.//DATAPROVIDER/@interface='" + ifaceId + "' and .//SCHEDULING/@enabled='true']//CRON/text()"; |
257 | 240 |
try { |
258 | 241 |
final String cronExpression = serviceLocator.getService(ISLookUpService.class).getResourceProfileByQuery(xquery); |
... | ... | |
263 | 246 |
return null; |
264 | 247 |
} catch (final ISLookUpException e) { |
265 | 248 |
log.error("Error in xquery: " + xquery, e); |
266 |
throw new DatasourceManagerServiceException("Error in xquery: " + xquery, e);
|
|
249 |
throw new DatasourceManagerException(-1, "Error in xquery: " + xquery, e);
|
|
267 | 250 |
} catch (final ParseException e) { |
268 | 251 |
log.error("Error parsing cron expression", e); |
269 |
throw new DatasourceManagerServiceException("Error parsing cron expression", e);
|
|
252 |
throw new DatasourceManagerException(-1, "Error parsing cron expression", e);
|
|
270 | 253 |
} |
271 | 254 |
} |
272 | 255 |
|
Also available in: Unified diff
Work in progress