2020-04-27 14:52:31 +02:00
2020-03-27 10:42:17 +01:00
package eu.dnetlib.dhp.oa.provision.utils ;
2020-02-13 16:53:27 +01:00
2024-01-22 15:53:17 +01:00
import java.io.* ;
import java.net.HttpURLConnection ;
import java.net.URL ;
2020-04-18 12:42:58 +02:00
import java.util.HashMap ;
2020-04-28 11:23:29 +02:00
2020-02-13 16:53:27 +01:00
import org.dom4j.Document ;
import org.dom4j.DocumentException ;
import org.dom4j.Node ;
import org.dom4j.io.SAXReader ;
2024-01-22 15:53:17 +01:00
import org.jetbrains.annotations.NotNull ;
2021-08-11 12:13:22 +02:00
import org.xml.sax.SAXException ;
2020-02-13 16:53:27 +01:00
2020-04-28 11:23:29 +02:00
import com.google.common.base.Joiner ;
2024-01-22 15:53:17 +01:00
import eu.dnetlib.dhp.common.api.context.* ;
import eu.dnetlib.dhp.common.rest.DNetRestClient ;
2020-04-28 11:23:29 +02:00
import eu.dnetlib.dhp.utils.ISLookupClientFactory ;
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpException ;
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService ;
2020-02-13 16:53:27 +01:00
public class ContextMapper extends HashMap < String , ContextDef > implements Serializable {
2020-04-27 14:52:31 +02:00
private static final long serialVersionUID = 2159682308502487305L ;
private static final String XQUERY = " for $x in //RESOURCE_PROFILE[.//RESOURCE_TYPE/@value='ContextDSResourceType']//*[name()='context' or name()='category' or name()='concept'] return <entry id= \" {$x/@id} \" label= \" {$x/@label|$x/@name} \" name= \" {$x/name()} \" type= \" {$x/@type} \" /> " ;
2024-01-22 15:53:17 +01:00
public static ContextMapper fromAPI ( final String baseURL ) throws Exception {
final ContextMapper contextMapper = new ContextMapper ( ) ;
2024-01-23 15:36:08 +01:00
for ( ContextSummary ctx : DNetRestClient
. doGET ( String . format ( " %s/contexts " , baseURL ) , ContextSummaryList . class ) ) {
2024-01-22 15:53:17 +01:00
contextMapper . put ( ctx . getId ( ) , new ContextDef ( ctx . getId ( ) , ctx . getLabel ( ) , " context " , ctx . getType ( ) ) ) ;
for ( CategorySummary cat : DNetRestClient
2024-01-23 15:36:08 +01:00
. doGET ( String . format ( " %s/context/%s?all=true " , baseURL , ctx . getId ( ) ) , CategorySummaryList . class ) ) {
2024-01-22 15:53:17 +01:00
contextMapper . put ( cat . getId ( ) , new ContextDef ( cat . getId ( ) , cat . getLabel ( ) , " category " , " " ) ) ;
if ( cat . isHasConcept ( ) ) {
for ( ConceptSummary c : DNetRestClient
2024-01-23 15:36:08 +01:00
. doGET (
String . format ( " %s/context/category/%s?all=true " , baseURL , cat . getId ( ) ) ,
ConceptSummaryList . class ) ) {
2024-01-22 15:53:17 +01:00
contextMapper . put ( c . getId ( ) , new ContextDef ( c . getId ( ) , c . getLabel ( ) , " concept " , " " ) ) ;
if ( c . isHasSubConcept ( ) ) {
for ( ConceptSummary cs : c . getConcepts ( ) ) {
contextMapper . put ( cs . getId ( ) , new ContextDef ( cs . getId ( ) , cs . getLabel ( ) , " concept " , " " ) ) ;
if ( cs . isHasSubConcept ( ) ) {
for ( ConceptSummary css : cs . getConcepts ( ) ) {
contextMapper
. put (
css . getId ( ) ,
new ContextDef ( css . getId ( ) , css . getLabel ( ) , " concept " , " " ) ) ;
}
}
}
}
}
}
}
}
return contextMapper ;
}
@Deprecated
2020-04-27 14:52:31 +02:00
public static ContextMapper fromIS ( final String isLookupUrl )
2021-08-11 12:13:22 +02:00
throws DocumentException , ISLookUpException , SAXException {
2020-04-27 14:52:31 +02:00
ISLookUpService isLookUp = ISLookupClientFactory . getLookUpService ( isLookupUrl ) ;
StringBuilder sb = new StringBuilder ( " <ContextDSResources> " ) ;
Joiner . on ( " " ) . appendTo ( sb , isLookUp . quickSearchProfile ( XQUERY ) ) ;
sb . append ( " </ContextDSResources> " ) ;
return fromXml ( sb . toString ( ) ) ;
}
2024-01-22 15:53:17 +01:00
@Deprecated
2021-08-11 12:13:22 +02:00
public static ContextMapper fromXml ( final String xml ) throws DocumentException , SAXException {
2020-04-27 14:52:31 +02:00
final ContextMapper contextMapper = new ContextMapper ( ) ;
2021-08-11 12:13:22 +02:00
final SAXReader reader = new SAXReader ( ) ;
reader . setFeature ( " http://apache.org/xml/features/disallow-doctype-decl " , true ) ;
final Document doc = reader . read ( new StringReader ( xml ) ) ;
2020-04-27 14:52:31 +02:00
for ( Object o : doc . selectNodes ( " //entry " ) ) {
Node node = ( Node ) o ;
String id = node . valueOf ( " ./@id " ) ;
String label = node . valueOf ( " ./@label " ) ;
String name = node . valueOf ( " ./@name " ) ;
String type = node . valueOf ( " ./@type " ) + " " ;
contextMapper . put ( id , new ContextDef ( id , label , name , type ) ) ;
}
return contextMapper ;
}
2020-02-13 16:53:27 +01:00
}