From 4a3ba043841ccb422725befe413c83d9f00977a9 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Thu, 12 Apr 2018 13:04:25 +0000 Subject: [PATCH] Added class to create missing context git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/private/luca.frosini/resource-registry-database-creator@166941 82a268e6-3cf1-43bd-a215-b396298e98cf --- pom.xml | 6 ++ .../context/ContextCreator.java | 77 +++++++++++++++++++ .../context/ContextManagementTest.java | 16 ++++ src/test/resources/logback-test.xml | 4 +- src/test/resources/scopedata.xml | 53 +++++++++++++ 5 files changed, 154 insertions(+), 2 deletions(-) create mode 100644 src/main/java/org/gcube/informationsystem/resourceregistry/context/ContextCreator.java create mode 100644 src/test/resources/scopedata.xml diff --git a/pom.xml b/pom.xml index 8a1484e..8e1d159 100644 --- a/pom.xml +++ b/pom.xml @@ -60,6 +60,12 @@ 0.0.1-SNAPSHOT + + org.gcube.common + context-enumeration + [1.0.0-SNAPSHOT, 2.0.0-SNAPSHOT) + + org.slf4j slf4j-api diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/context/ContextCreator.java b/src/main/java/org/gcube/informationsystem/resourceregistry/context/ContextCreator.java new file mode 100644 index 0000000..5bd6e14 --- /dev/null +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/context/ContextCreator.java @@ -0,0 +1,77 @@ +package org.gcube.informationsystem.resourceregistry.context; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; +import java.util.UUID; + +import org.gcube.common.scope.impl.ScopeBean; +import org.gcube.context.ContextElaborator; +import org.gcube.informationsystem.impl.entity.ContextImpl; +import org.gcube.informationsystem.impl.utils.ISMapper; +import org.gcube.informationsystem.model.entity.Context; +import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; +import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ContextCreator extends ContextElaborator { + + private static Logger logger = LoggerFactory.getLogger(ContextCreator.class); + + protected Map contexts; + + protected ContextManagement contextManagement; + + public ContextCreator() { + super(); + contexts = new HashMap<>(); + contextManagement = new ContextManagement(); + } + + protected Context create(Context context) throws ResourceRegistryException, IOException { + ContextManagement contextManagement = new ContextManagement(); + contextManagement.setJSON(ISMapper.marshal(context)); + String contextString = contextManagement.create(); + logger.info("Created {}", contextString); + Context c = ISMapper.unmarshal(Context.class, contextString); + return c; + } + + protected UUID getContextUUID(ScopeBean scopeBean) throws ResourceRegistryException, IOException { + if(scopeBean!=null) { + try { + UUID uuid = contexts.get(scopeBean.toString()); + if(uuid==null) { + uuid = ContextUtility.getInstance().getContextUUIDFromFullName(scopeBean.toString()); + if(uuid!=null) { + contexts.put(scopeBean.toString(), uuid); + } + } + return uuid; + }catch (ContextException e) { + logger.info("{} does not exists", scopeBean); + } + } + return null; + } + + + @Override + protected void elaborateContext(ScopeBean scopeBean) throws Exception { + UUID uuid = getContextUUID(scopeBean); + if(uuid==null) { + logger.info("Parent Context of {} is {}", scopeBean, scopeBean.enclosingScope()); + UUID parentUUID = getContextUUID(scopeBean.enclosingScope()); + Context context = new ContextImpl(scopeBean.name()); + if(parentUUID!=null) { + context.setParent(parentUUID); + } + context = create(context); + contexts.put(scopeBean.toString(), context.getHeader().getUUID()); + }else { + logger.info("Context {} is already present with UUID {}", scopeBean, uuid.toString()); + } + } + +} diff --git a/src/test/java/org/gcube/informationsystem/resourceregistry/context/ContextManagementTest.java b/src/test/java/org/gcube/informationsystem/resourceregistry/context/ContextManagementTest.java index 6e04167..3882337 100644 --- a/src/test/java/org/gcube/informationsystem/resourceregistry/context/ContextManagementTest.java +++ b/src/test/java/org/gcube/informationsystem/resourceregistry/context/ContextManagementTest.java @@ -1,5 +1,6 @@ package org.gcube.informationsystem.resourceregistry.context; +import java.io.File; import java.io.IOException; import java.util.UUID; @@ -7,6 +8,7 @@ import org.gcube.informationsystem.impl.entity.ContextImpl; import org.gcube.informationsystem.impl.utils.ISMapper; import org.gcube.informationsystem.model.entity.Context; import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; +import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -46,4 +48,18 @@ public class ContextManagementTest { } + @Test + public void createProductionContext() throws Exception { + File src = new File("src"); + File test = new File(src, "test"); + File resources = new File(test, "resources"); + + File voFile = new File(resources, "scopedata.xml"); + + ContextCreator contextCreator = new ContextCreator(); + contextCreator.all(voFile); + } + + + } diff --git a/src/test/resources/logback-test.xml b/src/test/resources/logback-test.xml index da584de..fa86414 100644 --- a/src/test/resources/logback-test.xml +++ b/src/test/resources/logback-test.xml @@ -9,8 +9,8 @@ - - + + diff --git a/src/test/resources/scopedata.xml b/src/test/resources/scopedata.xml new file mode 100644 index 0000000..a84d476 --- /dev/null +++ b/src/test/resources/scopedata.xml @@ -0,0 +1,53 @@ + + + + d4science + + + d4science + ServiceMap_d4science.research-infrastructures.eu.xml + /d4science.research-infrastructures.eu + + + d4science/D4Research + ServiceMap_D4Research.xml + /d4science.research-infrastructures.eu/D4Research + + + d4science/Edison + ServiceMap_Edison.xml + /d4science.research-infrastructures.eu/Edison + + + d4science/FARM + ServiceMap_FARM.xml + /d4science.research-infrastructures.eu/FARM + + + d4science/gCubeApps + ServiceMap_gCubeApps.xml + /d4science.research-infrastructures.eu/gCubeApps + + + d4science/OpenAIRE + ServiceMap_OpenAIRE.xml + /d4science.research-infrastructures.eu/OpenAIRE + + + d4science/ParthenosVO + ServiceMap_ParthenosVO.xml + /d4science.research-infrastructures.eu/ParthenosVO + + + d4science/SmartArea + ServiceMap_SmartArea.xml + /d4science.research-infrastructures.eu/SmartArea + + + d4science/SoBigData + ServiceMap_SoBigData.xml + /d4science.research-infrastructures.eu/SoBigData + + + +