Skip to content
Snippets Groups Projects
Commit 83ec09a7 authored by Piotr Gawron's avatar Piotr Gawron
Browse files

new implementation of miriam connector (using identifiers.org API)

parent 10810a8a
No related branches found
No related tags found
1 merge request!773Resolve "move from log4j to log4j2"
package lcsb.mapviewer.annotation.services; package lcsb.mapviewer.annotation.services;
import java.io.IOException; import java.io.IOException;
import java.util.Map;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
...@@ -9,13 +10,14 @@ import org.springframework.stereotype.Service; ...@@ -9,13 +10,14 @@ import org.springframework.stereotype.Service;
import org.w3c.dom.Document; import org.w3c.dom.Document;
import org.w3c.dom.Node; import org.w3c.dom.Node;
import com.google.gson.Gson;
import lcsb.mapviewer.annotation.cache.CachableInterface; import lcsb.mapviewer.annotation.cache.CachableInterface;
import lcsb.mapviewer.annotation.cache.GeneralCacheInterface; import lcsb.mapviewer.annotation.cache.GeneralCacheInterface;
import lcsb.mapviewer.annotation.cache.SourceNotAvailable; import lcsb.mapviewer.annotation.cache.SourceNotAvailable;
import lcsb.mapviewer.annotation.cache.WebPageDownloader; import lcsb.mapviewer.annotation.cache.WebPageDownloader;
import lcsb.mapviewer.common.XmlParser; import lcsb.mapviewer.common.XmlParser;
import lcsb.mapviewer.common.exception.InvalidArgumentException; import lcsb.mapviewer.common.exception.InvalidArgumentException;
import lcsb.mapviewer.common.exception.NotImplementedException;
import lcsb.mapviewer.model.map.MiriamData; import lcsb.mapviewer.model.map.MiriamData;
import lcsb.mapviewer.model.map.MiriamRelationType; import lcsb.mapviewer.model.map.MiriamRelationType;
import lcsb.mapviewer.model.map.MiriamType; import lcsb.mapviewer.model.map.MiriamType;
...@@ -35,11 +37,6 @@ public final class MiriamConnector extends CachableInterface implements IExterna ...@@ -35,11 +37,6 @@ public final class MiriamConnector extends CachableInterface implements IExterna
*/ */
static final String LINK_DB_PREFIX = "Link: "; static final String LINK_DB_PREFIX = "Link: ";
/**
* String used to distinguish cached data for checking if uri is valid.
*/
protected static final String VALID_URI_PREFIX = "Validity: ";
/** /**
* String describing invalid miriam entries that will be put into db (instead of * String describing invalid miriam entries that will be put into db (instead of
* null). * null).
...@@ -79,23 +76,43 @@ public final class MiriamConnector extends CachableInterface implements IExterna ...@@ -79,23 +76,43 @@ public final class MiriamConnector extends CachableInterface implements IExterna
} }
return result; return result;
} }
String uri = miriamData.getDataType().getUris().get(0) + ":" + miriamData.getResource(); String id;
throw new NotImplementedException(); if (miriamData.getDataType().getNamespace().isEmpty()) {
// String[] urls = getLink().getLocations(uri); id = miriamData.getResource();
// if (urls == null) { } else {
// result = null; id = miriamData.getDataType().getNamespace() + ":" + miriamData.getResource();
// } else if (urls.length > 0) { }
// result = urls[0];
// } query = "https://identifiers.org/rest/identifiers/validate/" + id;
// if (result != null) { String page;
// setCacheValue(query, result); try {
// return result; page = getWebPageContent(query);
// } else { Gson gson = new Gson();
// logger.warn("Cannot find url for miriam: " + miriamData); Map<?, ?> map = gson.fromJson(page, Map.class);
// // if url cannot be found then mark miriam data as invalid for one day return (String) map.get("url");
// setCacheValue(query, INVALID_LINK, 1);
// return null; } catch (Exception e) {
// } throw new AnnotationException("Problem with accessing identifiers.org", e);
}
// Document document = XmlParser.getXmlDocumentFromString(page);
// Node uri = XmlParser.getNode("uri", document.getChildNodes());
// return XmlParser.getNodeValue(uri);
// String[] urls = getLink().getLocations(uri);
// if (urls == null) {
// result = null;
// } else if (urls.length > 0) {
// result = urls[0];
// }
// if (result != null) {
// setCacheValue(query, result);
// return result;
// } else {
// logger.warn("Cannot find url for miriam: " + miriamData);
// // if url cannot be found then mark miriam data as invalid for one day
// setCacheValue(query, INVALID_LINK, 1);
// return null;
// }
} }
@Override @Override
...@@ -151,9 +168,6 @@ public final class MiriamConnector extends CachableInterface implements IExterna ...@@ -151,9 +168,6 @@ public final class MiriamConnector extends CachableInterface implements IExterna
MiriamType dataType = MiriamType.getTypeByUri(rows[0]); MiriamType dataType = MiriamType.getTypeByUri(rows[0]);
String resource = rows[1]; String resource = rows[1];
result = getUrlString(new MiriamData(MiriamRelationType.BQ_BIOL_IS_DESCRIBED_BY, dataType, resource)); result = getUrlString(new MiriamData(MiriamRelationType.BQ_BIOL_IS_DESCRIBED_BY, dataType, resource));
} else if (name.startsWith(VALID_URI_PREFIX)) {
String tmp = name.substring(VALID_URI_PREFIX.length());
result = "" + isValid(tmp);
} else if (name.startsWith("http")) { } else if (name.startsWith("http")) {
try { try {
result = getWebPageContent(name); result = getWebPageContent(name);
...@@ -171,23 +185,18 @@ public final class MiriamConnector extends CachableInterface implements IExterna ...@@ -171,23 +185,18 @@ public final class MiriamConnector extends CachableInterface implements IExterna
} }
/** /**
* Checks if uri (like the one defined in {@link MiriamType#getUris()}) is valid. * Checks if {@link MiriamType} is valid.
* *
* @param uri * @param type
* uri to check * type to be checked
* @return <code>true</code> if uri in parameter is valid, <code>false</code> * @return <code>true</code> if {@link MiriamType} is valid, <code>false</code>
* otherwise * otherwise
*/ */
public boolean isValid(String uri) { public boolean isValidMiriamType(MiriamType type) {
throw new NotImplementedException(); if (type.getNamespace() == null || type.getExampleIdentifier() == null) {
// boolean result = false; return false;
// String name = getLink().getName(uri); }
// if (name == null) { return getUrlString(new MiriamData(type, type.getExampleIdentifier())) != null;
// result = false;
// } else {
// result = !name.isEmpty();
// }
// return result;
} }
/** /**
......
...@@ -136,8 +136,7 @@ public class MiriamConnectorTest extends AnnotationTestFunctions { ...@@ -136,8 +136,7 @@ public class MiriamConnectorTest extends AnnotationTestFunctions {
try { try {
for (MiriamType mt : MiriamType.values()) { for (MiriamType mt : MiriamType.values()) {
if (!MiriamType.UNKNOWN.equals(mt)) { if (!MiriamType.UNKNOWN.equals(mt)) {
String uri = mt.getUris().get(0); assertTrue("Invalid MiriamType (" + mt + ") for MiriamType: " + mt, miriamConnector.isValidMiriamType(mt));
assertTrue("Invalid URI (" + uri + ") for MiriamType: " + mt, miriamConnector.isValid(mt.getUris().get(0)));
} }
} }
...@@ -147,18 +146,6 @@ public class MiriamConnectorTest extends AnnotationTestFunctions { ...@@ -147,18 +146,6 @@ public class MiriamConnectorTest extends AnnotationTestFunctions {
} }
} }
@Test
public void testRefresh() throws Exception {
String query = MiriamConnector.VALID_URI_PREFIX + MiriamType.HGNC.getUris().get(0);
try {
String res = miriamConnector.refreshCacheQuery(query);
assertNotNull(res);
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
@Test @Test
public void testRefresh2() throws Exception { public void testRefresh2() throws Exception {
MiriamData md = TaxonomyBackend.HUMAN_TAXONOMY; MiriamData md = TaxonomyBackend.HUMAN_TAXONOMY;
...@@ -306,10 +293,10 @@ public class MiriamConnectorTest extends AnnotationTestFunctions { ...@@ -306,10 +293,10 @@ public class MiriamConnectorTest extends AnnotationTestFunctions {
} }
@Test @Test
public void testCheckValidyOfEmptyUri() throws Exception { public void testCheckValidyOfUnknownMiriamType() throws Exception {
try { try {
// user connector without cache // user connector without cache
boolean value = new MiriamConnector().isValid(""); boolean value = miriamConnector.isValidMiriamType(MiriamType.UNKNOWN);
assertFalse(value); assertFalse(value);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
......
delete from cache_query_table where query like 'Validity:%';
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment