diff --git a/src/main/java/org/gcube/resourcemanagement/support/server/gcube/queries/QueryLoader.java b/src/main/java/org/gcube/resourcemanagement/support/server/gcube/queries/QueryLoader.java
new file mode 100644
index 0000000..2306126
--- /dev/null
+++ b/src/main/java/org/gcube/resourcemanagement/support/server/gcube/queries/QueryLoader.java
@@ -0,0 +1,73 @@
+/****************************************************************************
+ * This software is part of the gCube Project.
+ * Site: http://www.gcube-system.org/
+ ****************************************************************************
+ * The gCube/gCore software is licensed as Free Open Source software
+ * conveying to the EUPL (http://ec.europa.eu/idabc/eupl).
+ * The software and documentation is provided by its authors/distributors
+ * "as is" and no expressed or
+ * implied warranty is given for its use, quality or fitness for a
+ * particular case.
+ ****************************************************************************
+ * Filename: QueryLoader.java
+ ****************************************************************************
+ * @author Daniele Strollo
+ ***************************************************************************/
+
+package org.gcube.resourcemanagement.support.server.gcube.queries;
+
+import java.io.BufferedReader;
+import java.io.InputStreamReader;
+import java.util.HashMap;
+
+import org.gcube.resourcemanagement.support.server.utils.ServerConsole;
+
+/**
+ * Resource_support utility to load at runtime the customized queries to submit to
+ * the IS.
+ * @author Daniele Strollo
+ * @author Massimiliano Assante (ISTI-CNR)
+ */
+public class QueryLoader {
+ private static final HashMap cachedQueries = new HashMap();
+ private static final String LOG_PREFIX = "[QUERY-LOADER]";
+
+ /**
+ * @param query the location of query file to load
+ * @return the string consisting of the xquery to submit to the server
+ * @throws Exception
+ */
+ public static String getQuery(final QueryLocation query) throws Exception {
+ if (query == null) {
+ throw new Exception("Invalid query parameter. Null not allowed.");
+ }
+
+ ServerConsole.trace(LOG_PREFIX, "loading " + query.name());
+
+ if (query != null && cachedQueries.containsKey(query)) {
+ return cachedQueries.get(query);
+ }
+
+ BufferedReader in = new BufferedReader(new InputStreamReader(query.getFileName()));
+ StringBuilder retval = new StringBuilder();
+ String currLine = null;
+
+ while ((currLine = in.readLine()) != null) {
+ // a comment
+ if (currLine.trim().length() > 0 && currLine.trim().startsWith("#")) {
+ continue;
+ }
+ if (currLine.trim().length() == 0) { continue; }
+ retval.append(currLine + System.getProperty("line.separator"));
+ }
+ in.close();
+
+ String tmp = retval.toString();
+ if (cachedQueries != null) {
+ cachedQueries.put(query, tmp);
+ }
+
+ return tmp;
+ }
+}
+
diff --git a/src/main/java/org/gcube/resourcemanagement/support/server/gcube/queries/QueryLocation.java b/src/main/java/org/gcube/resourcemanagement/support/server/gcube/queries/QueryLocation.java
new file mode 100644
index 0000000..b9c95db
--- /dev/null
+++ b/src/main/java/org/gcube/resourcemanagement/support/server/gcube/queries/QueryLocation.java
@@ -0,0 +1,79 @@
+/****************************************************************************
+ * This software is part of the gCube Project.
+ * Site: http://www.gcube-system.org/
+ ****************************************************************************
+ * The gCube/gCore software is licensed as Free Open Source software
+ * conveying to the EUPL (http://ec.europa.eu/idabc/eupl).
+ * The software and documentation is provided by its authors/distributors
+ * "as is" and no expressed or
+ * implied warranty is given for its use, quality or fitness for a
+ * particular case.
+ ****************************************************************************
+ * Filename: QueryLocator.java
+ ****************************************************************************
+ * @author Daniele Strollo
+ ***************************************************************************/
+
+package org.gcube.resourcemanagement.support.server.gcube.queries;
+
+import java.io.InputStream;
+
+/**
+ * Keeps the association between an xquery and the path
+ * on which it can be retrieved.
+ * @author Daniele Strollo
+ * @author Massimiliano Assante (ISTI-CNR)
+ */
+public enum QueryLocation {
+ // These two queries are needed to build the tree of resource types and subtypes
+ // for resources different from wsresources.
+ GET_TREE_TYPES("getTypes.xq"),
+ GET_TREE_SUBTYPES("getSubTypes.xq"),
+
+ // Customized queries to retrieve the relevant data from resources
+ // according to their type
+ LIST_GHN("resources/GHN.xq"),
+ LIST_Collection("resources/Collection.xq"),
+ LIST_VIEW("resources/VIEW.xq"),
+ LIST_Service("resources/Service.xq"),
+ LIST_GenericResource("resources/GenericResource.xq"),
+ LIST_RunningInstance("resources/RunningInstance.xq"),
+ LIST_RuntimeResource("resources/RuntimeResource.xq"),
+
+ // To retrieve the list of generic resources publishing plugins
+ // to deploy activation records
+ GET_GENERIC_RESOURCE_PLUGINS("getPlugins.xq"),
+
+ // to deploy activation records for Tree manager
+ GET_GENERIC_RESOURCE_TREE_MANAGER_PLUGINS("getTreeManagerPlugins.xq"),
+
+ // Related resources
+ LIST_RELATED_GHN("related/GHN.xq"),
+ LIST_RELATED_RunningInstance("related/RunningInstance.xq"),
+ LIST_RELATED_Service("related/Service.xq"),
+
+ // Queries for sweeper
+ SWEEPER_EXPIRED_GHN("sweeper/expiredGhns.xq"),
+ SWEEPER_DEAD_GHN("sweeper/deadGhns.xq"),
+ SWEEPER_ORPHAN_RI("sweeper/orphanRI.xq"),
+
+ // Used to build the gwt model representation inside dialogs.
+ // see getResourceModels inside ISClientRequester.
+ GET_RES_DETAILS_BYTYPE("getResourcesDetails.xq"),
+ GET_RES_DETAILS_BYSUBTYPE("getResourcesDetailsSubtype.xq"),
+
+ GET_RESOURCE_BYID("getResourceByID.xq"),
+ GET_WSRES_TYPES("getWSResourcesTypes.xq"),
+ GET_WSRES_DETAILS_BYTYPE("getWSResourcesDetails.xq"),
+ GET_WSRES_DETAILS_BYSUBTYPE("getWSResourcesDetailsSubType.xq"),
+ GET_WSRESOURCE_BYID("getWSResourceByID.xq");
+
+ private final String path = "org/gcube/resourcemanagement/support/server/gcube/queries/xquery/";
+ private String filename = null;
+ QueryLocation(final String filename) {
+ this.filename = filename;
+ }
+ public InputStream getFileName() {
+ return this.getClass().getClassLoader().getResourceAsStream(this.path + this.filename);
+ }
+}
diff --git a/src/main/java/org/gcube/resourcemanagement/support/server/gcube/queries/xquery/getPlugins.xq b/src/main/java/org/gcube/resourcemanagement/support/server/gcube/queries/xquery/getPlugins.xq
new file mode 100644
index 0000000..061ad85
--- /dev/null
+++ b/src/main/java/org/gcube/resourcemanagement/support/server/gcube/queries/xquery/getPlugins.xq
@@ -0,0 +1,33 @@
+
+for $_outer in collection("/db/Properties")//Document
+where ($_outer//Document/Data/child::*[local-name()='ServiceClass']/text() = 'ContentManagement'
+and exists($_outer/Data/child::*[local-name()='Plugin']/name))
+return
+
+ {
+ for $plugin in $_outer/Data/child::*[local-name()='Plugin']
+ return
+
+ {
+ for $elem in $plugin/parameters/child::*
+ return
+
+ {$plugin/name}
+ {$plugin/description}
+ {namespace-uri($elem)}
+ {local-name($elem)}
+
+ {
+ for $p in $elem/child::*
+ return
+
+ {$p/name()}
+ {$p/text()}
+
+ }
+
+
+ }
+
+ }
+
\ No newline at end of file
diff --git a/src/main/java/org/gcube/resourcemanagement/support/server/gcube/queries/xquery/getResourceByID.xq b/src/main/java/org/gcube/resourcemanagement/support/server/gcube/queries/xquery/getResourceByID.xq
new file mode 100644
index 0000000..ed81f47
--- /dev/null
+++ b/src/main/java/org/gcube/resourcemanagement/support/server/gcube/queries/xquery/getResourceByID.xq
@@ -0,0 +1,8 @@
+#
+# Params: RES_ID the ID of looked up resource
+# RES_TYPE (option) the type of searched resource
+
+
+for $resource in collection('/db/Profiles/')//Resource
+where $resource/ID/string() eq ''
+return $resource
\ No newline at end of file
diff --git a/src/main/java/org/gcube/resourcemanagement/support/server/gcube/queries/xquery/getResourcesDetails.xq b/src/main/java/org/gcube/resourcemanagement/support/server/gcube/queries/xquery/getResourcesDetails.xq
new file mode 100644
index 0000000..f6f9bbf
--- /dev/null
+++ b/src/main/java/org/gcube/resourcemanagement/support/server/gcube/queries/xquery/getResourcesDetails.xq
@@ -0,0 +1,36 @@
+# Given a couple of (type, subtype) retrieves
+# the list of all resources contained in such category.
+# Parameters:
+# RES_TYPE the main type of the resource
+# This query is used by forms
+
+for $profiles in collection('/db/Profiles/')//Resource
+let $ghn-name := $profiles//Resource/Profile/GHN/@UniqueID/string()
+let $gcf-version := $profiles//Resource/Profile/GHNDescription/RunTimeEnv/Variable[Key/text() = 'gCF-version']/Value/text()
+let $ghn-version := $profiles//Resource/Profile/GHNDescription/RunTimeEnv/Variable[Key/text() = 'GHN-distribution-version']/Value/text()
+let $scopes := string-join( $profiles//Resource/Scopes//Scope/text(), ';')
+let $subtype :=
+ if ($profiles//Resource/Type eq "Service")
+ then $profiles//Resource/Profile/Class/text()
+ else if ($profiles//Resource/Type eq "RunningInstance")
+ then $profiles//Resource/Profile/ServiceClass/text()
+ else if ($profiles//Resource/Type eq "GenericResource")
+ then $profiles//Resource/Profile/SecondaryType/text()
+ else if ($profiles//Resource/Type eq "GHN")
+ then $profiles//Resource/Profile/Site/Domain/text()
+ else if ($profiles//Resource/Type eq "MetadataCollection")
+ then $profiles//Resource/Profile/MetadataFormat/Name/text()
+ else if ($profiles//Resource/Type eq "Collection" and ($profiles//Resource/Profile/IsUserCollection/string(@value) eq 'true'))
+ then "User"
+ else if ($profiles//Resource/Type eq "Collection" and ($profiles//Resource/Profile/IsUserCollection/string(@value) eq 'false'))
+ then "System"
+ else ""
+ return
+
+ {$profiles//Resource/child::*}
+ {$subtype}
+ {$gcf-version}
+ {$ghn-version}
+ {$scopes}
+ {$ghn-name}
+
\ No newline at end of file
diff --git a/src/main/java/org/gcube/resourcemanagement/support/server/gcube/queries/xquery/getResourcesDetailsSubtype.xq b/src/main/java/org/gcube/resourcemanagement/support/server/gcube/queries/xquery/getResourcesDetailsSubtype.xq
new file mode 100644
index 0000000..11451f0
--- /dev/null
+++ b/src/main/java/org/gcube/resourcemanagement/support/server/gcube/queries/xquery/getResourcesDetailsSubtype.xq
@@ -0,0 +1,37 @@
+# Given a couple of (type, subtype) retrieves
+# the list of all resources contained in such category.
+# Parameters:
+# RES_TYPE the main type of the resource
+# This query is used by forms
+
+for $profiles in collection('/db/Profiles/')//Resource
+let $ghn-name := $profiles//Resource/Profile/GHN/@UniqueID/string()
+let $gcf-version := $profiles//Resource/Profile/GHNDescription/RunTimeEnv/Variable[Key/text() = 'gCF-version']/Value/text()
+let $ghn-version := $profiles//Resource/Profile/GHNDescription/RunTimeEnv/Variable[Key/text() = 'GHN-distribution-version']/Value/text()
+let $scopes := string-join( $profiles//Resource/Scopes//Scope/text(), ';')
+let $subtype :=
+ if ($profiles//Resource/Type eq "Service")
+ then $profiles//Resource/Profile/Class/text()
+ else if ($profiles//Resource/Type eq "RunningInstance")
+ then $profiles//Resource/Profile/ServiceClass/text()
+ else if ($profiles//Resource/Type eq "GenericResource")
+ then $profiles//Resource/Profile/SecondaryType/text()
+ else if ($profiles//Resource/Type eq "GHN")
+ then $profiles//Resource/Profile/Site/Domain/text()
+ else if ($profiles//Resource/Type eq "MetadataCollection")
+ then $profiles//Resource/Profile/MetadataFormat/Name/text()
+ else if ($profiles//Resource/Type eq "Collection" and ($profiles//Resource/Profile/IsUserCollection/string(@value) eq 'true'))
+ then "User"
+ else if ($profiles//Resource/Type eq "Collection" and ($profiles//Resource/Profile/IsUserCollection/string(@value) eq 'false'))
+ then "System"
+ else ""
+ where $subtype eq ''
+ return
+
+ {$profiles//Resource/child::*}
+ {$subtype}
+ {$gcf-version}
+ {$ghn-version}
+ {$scopes}
+ {$ghn-name}
+
\ No newline at end of file
diff --git a/src/main/java/org/gcube/resourcemanagement/support/server/gcube/queries/xquery/getSubTypes.xq b/src/main/java/org/gcube/resourcemanagement/support/server/gcube/queries/xquery/getSubTypes.xq
new file mode 100644
index 0000000..af546be
--- /dev/null
+++ b/src/main/java/org/gcube/resourcemanagement/support/server/gcube/queries/xquery/getSubTypes.xq
@@ -0,0 +1,27 @@
+let $type := ''
+let $subtypes :=
+ for $_profiles in collection('/db/Profiles/')//Resource
+ let $elem := if ($type eq "Service")
+ then $_profiles//Resource/Profile/Class
+ else if ($type eq "RunningInstance")
+ then $_profiles//Resource/Profile/ServiceClass
+ else if ($type eq "GenericResource")
+ then $_profiles//Resource/Profile/SecondaryType
+ else if ($type eq "GHN")
+ then $_profiles//Resource/Profile/Site/Domain
+ else if ($type eq "MetadataCollection")
+ then $_profiles//Resource/Profile/MetadataFormat/Name
+ else if ($type eq "RuntimeResource")
+ then $_profiles//Resource/Profile/Category
+ else if ($type eq "Collection" and ($_profiles//Resource/Profile/IsUserCollection/string(@value) eq 'true'))
+ then "User"
+ else if ($type eq "Collection" and ($_profiles//Resource/Profile/IsUserCollection/string(@value) eq 'false'))
+ then "System"
+ else ""
+ return $elem
+
+for $subtype in distinct-values($subtypes)
+return
+ {$subtype}
\ No newline at end of file
diff --git a/src/main/java/org/gcube/resourcemanagement/support/server/gcube/queries/xquery/getTreeManagerPlugins.xq b/src/main/java/org/gcube/resourcemanagement/support/server/gcube/queries/xquery/getTreeManagerPlugins.xq
new file mode 100644
index 0000000..c12f8b4
--- /dev/null
+++ b/src/main/java/org/gcube/resourcemanagement/support/server/gcube/queries/xquery/getTreeManagerPlugins.xq
@@ -0,0 +1,29 @@
+
+for $_outer in collection("/db/Properties")//Document
+where ($_outer//Document/Data/child::*[local-name()='ServiceClass']/text() = 'DataAccess'
+and exists($_outer/Data/child::*[local-name()='Plugin']/name))
+return
+
+ {
+ for $plugin in $_outer/Data/child::*[local-name()='Plugin']
+ return
+
+
+ {$plugin/name}
+ {$plugin/description}
+ {namespace-uri($plugin)}
+ treeManagerPlugin
+
+ {
+ for $elem in $plugin/child::*[local-name()='property']
+ return
+
+ {$elem/name/text()}
+ {$elem/value/text()}
+
+ }
+
+
+
+ }
+
\ No newline at end of file
diff --git a/src/main/java/org/gcube/resourcemanagement/support/server/gcube/queries/xquery/getTypes.xq b/src/main/java/org/gcube/resourcemanagement/support/server/gcube/queries/xquery/getTypes.xq
new file mode 100644
index 0000000..031cffa
--- /dev/null
+++ b/src/main/java/org/gcube/resourcemanagement/support/server/gcube/queries/xquery/getTypes.xq
@@ -0,0 +1,4 @@
+
+let $entry0ValueAuth := collection("/db/Profiles")//Resource
+for $types in distinct-values($entry0ValueAuth//Resource/Type/text())
+ return {$types}
\ No newline at end of file
diff --git a/src/main/java/org/gcube/resourcemanagement/support/server/gcube/queries/xquery/getWSResourceByID.xq b/src/main/java/org/gcube/resourcemanagement/support/server/gcube/queries/xquery/getWSResourceByID.xq
new file mode 100644
index 0000000..fe85999
--- /dev/null
+++ b/src/main/java/org/gcube/resourcemanagement/support/server/gcube/queries/xquery/getWSResourceByID.xq
@@ -0,0 +1,4 @@
+
+for $resource in collection("/db/Properties")//Document
+where $resource/ID/string() eq ''
+return $resource
\ No newline at end of file
diff --git a/src/main/java/org/gcube/resourcemanagement/support/server/gcube/queries/xquery/getWSResourcesDetails.xq b/src/main/java/org/gcube/resourcemanagement/support/server/gcube/queries/xquery/getWSResourcesDetails.xq
new file mode 100644
index 0000000..5b4a84b
--- /dev/null
+++ b/src/main/java/org/gcube/resourcemanagement/support/server/gcube/queries/xquery/getWSResourcesDetails.xq
@@ -0,0 +1,16 @@
+for $_outer in collection("/db/Properties")//Document
+let $_scopes := string-join( $_outer//Document/Data/child::*[local-name()='Scope']/text(), ';')
+return
+
+ {$_outer//Document/ID}
+ {$_outer//Document/Source}
+ {$_outer//Document/SourceKey}
+ {$_outer//Document/Data/child::*[local-name()='ServiceClass']/text()}
+ {$_outer//Document/Data/child::*[local-name()='ServiceName']/text()}
+ {$_outer//Document/Data/child::*[local-name()='ServiceClass']/text()}
+ {$_outer//Document/TerminationTimeHuman}
+ {$_outer//Document/LastUpdateHuman}
+ {$_outer//Document/Data/child::*[local-name()='RI']/text()}
+ WSResource
+ {$_scopes}
+
\ No newline at end of file
diff --git a/src/main/java/org/gcube/resourcemanagement/support/server/gcube/queries/xquery/getWSResourcesDetailsSubType.xq b/src/main/java/org/gcube/resourcemanagement/support/server/gcube/queries/xquery/getWSResourcesDetailsSubType.xq
new file mode 100644
index 0000000..c525a8d
--- /dev/null
+++ b/src/main/java/org/gcube/resourcemanagement/support/server/gcube/queries/xquery/getWSResourcesDetailsSubType.xq
@@ -0,0 +1,17 @@
+for $outer in collection("/db/Properties")//Document
+let $scopes := string-join( $outer//Document/Data/child::*[local-name()='Scope']/text(), ';')
+where $outer//Document/Data/child::*[local-name()='ServiceClass']/text() eq ''
+return
+
+ {$outer//Document/ID}
+ {$outer//Document/Source}
+ {$outer//Document/SourceKey}
+ {$outer//Document/Data/child::*[local-name()='ServiceClass']/text()}
+ {$outer//Document/Data/child::*[local-name()='ServiceName']/text()}
+ {$outer//Document/Data/child::*[local-name()='ServiceClass']/text()}
+ {$outer//Document/TerminationTimeHuman}
+ {$outer//Document/LastUpdateHuman}
+ {$outer//Document/Data/child::*[local-name()='RI']/text()}
+ WSResource
+ {$scopes}
+
\ No newline at end of file
diff --git a/src/main/java/org/gcube/resourcemanagement/support/server/gcube/queries/xquery/getWSResourcesTypes.xq b/src/main/java/org/gcube/resourcemanagement/support/server/gcube/queries/xquery/getWSResourcesTypes.xq
new file mode 100644
index 0000000..23c3e35
--- /dev/null
+++ b/src/main/java/org/gcube/resourcemanagement/support/server/gcube/queries/xquery/getWSResourcesTypes.xq
@@ -0,0 +1,5 @@
+
+let $tempcollection := for $outer in collection("/db/Properties")//Document
+ return $outer//Document/Data/child::*[local-name()='ServiceClass']
+for $elem in distinct-values($tempcollection)
+return $elem
\ No newline at end of file
diff --git a/src/main/java/org/gcube/resourcemanagement/support/server/gcube/queries/xquery/related/GHN.xq b/src/main/java/org/gcube/resourcemanagement/support/server/gcube/queries/xquery/related/GHN.xq
new file mode 100644
index 0000000..d8916a5
--- /dev/null
+++ b/src/main/java/org/gcube/resourcemanagement/support/server/gcube/queries/xquery/related/GHN.xq
@@ -0,0 +1,25 @@
+
+for $ris in collection('/db/Profiles/RunningInstance')//Resource
+where $ris//Resource/Profile/GHN/@UniqueID/string() eq ''
+return
+
+ {$ris/ID}
+ {$ris/Profile/ServiceName}
+ {$ris/Profile/ServiceClass}
+ {$ris//Resource/@version/string()}
+ {$ris/Profile/Version/text()}
+ {$ris/Profile/DeploymentData/Status/text()}
+
\ No newline at end of file
diff --git a/src/main/java/org/gcube/resourcemanagement/support/server/gcube/queries/xquery/related/RunningInstance.xq b/src/main/java/org/gcube/resourcemanagement/support/server/gcube/queries/xquery/related/RunningInstance.xq
new file mode 100644
index 0000000..c1daae2
--- /dev/null
+++ b/src/main/java/org/gcube/resourcemanagement/support/server/gcube/queries/xquery/related/RunningInstance.xq
@@ -0,0 +1,55 @@
+
+let $profiles := collection('/db/Profiles/')//Resource[ID/string() eq '']
+let $relatedghn := collection('/db/Profiles/GHN')//Resource[ID/string() eq $profiles/Profile/GHN/@UniqueID/string()]
+let $ghn-name := if (empty($relatedghn/Profile/GHNDescription/Name/string()))
+ then $profiles/Profile/GHN/@UniqueID/string()
+ else $relatedghn/Profile/GHNDescription/Name/string()
+for $ri in $profiles
+return
+
+
+ ID
+ {$ri//Resource/ID/string()}
+
+
+ ServiceStatus
+ {$ri/Profile/DeploymentData/Status/string()}
+
+
+ ActivationTime
+ {$ri/Profile/DeploymentData/ActivationTime/@value/string()}
+
+
+ GHNName
+ {$ghn-name}
+
+
+ GHNSite
+ {$relatedghn/Profile/Site/Domain/string()}
+
+
+ GHNStatus
+ {$relatedghn/Profile/GHNDescription/Status/string()}
+
+
+ GHNActivationTime
+ {$relatedghn/Profile/GHNDescription/ActivationTime/string()}
+
+
+ GHNLastUpdate
+ {$relatedghn/Profile/GHNDescription/LastUpdate/string()}
+
+
+ GHNLoad15Min
+ {$relatedghn/Profile/GHNDescription/Load/@Last15Min/string()}
+
+
+ GHNLoad5Min
+ {$relatedghn/Profile/GHNDescription/Load/@Last5Min/string()}
+
+ GHNLoad1Min
+ {$relatedghn/Profile/GHNDescription/Load/@Last1Min/string()}
+
+
\ No newline at end of file
diff --git a/src/main/java/org/gcube/resourcemanagement/support/server/gcube/queries/xquery/related/Service.xq b/src/main/java/org/gcube/resourcemanagement/support/server/gcube/queries/xquery/related/Service.xq
new file mode 100644
index 0000000..80ca4d6
--- /dev/null
+++ b/src/main/java/org/gcube/resourcemanagement/support/server/gcube/queries/xquery/related/Service.xq
@@ -0,0 +1,36 @@
+
+let $service := collection('/db/Profiles/Service')//Resource//Resource[ID/text() eq '']
+let $ServiceClass := $service/Profile/Class
+let $ServiceName := $service/Profile/Name
+let $ServiceVersion := $service/Profile/Version
+let $riloop := collection('/db/Profiles/RunningInstance')//Resource[Profile/ServiceClass/string() eq $ServiceClass and Profile/ServiceName/string() eq $ServiceName]
+let $relatedris :=
+ for $ri in $riloop
+ let $ghn-id := $ri/Profile/GHN/@UniqueID/string()
+ let $ghn := collection('/db/Profiles/GHN')//Resource[ID/string() eq $ghn-id]
+
+ return
+
+
+ {$ri/ID/string()}
+ {$ri/Profile/DeploymentData/Status/string()}
+ {$ri/Profile/DeploymentData/ActivationTime/@value/string()}
+ {$ri/Profile/Version/string()}
+
+ {$ghn-id}
+ {$ghn/Profile/GHNDescription/Name/string()}
+ {$ghn/Profile/Site/Domain/string()}
+ {$ghn/Profile/GHNDescription/Status/string()}
+ {$ghn/Profile/GHNDescription/Load/@Last15Min/string()}
+ {$ghn/Profile/GHNDescription/Load/@Last15Min/string()}
+ {$ghn/Profile/GHNDescription/Load/@Last15Min/string()}
+ {$ghn/Profile/GHNDescription/ActivationTime/string()}
+ {$ghn/Profile/GHNDescription/LastUpdate/string()}
+
+return
+
+ {$relatedris}
+
\ No newline at end of file
diff --git a/src/main/java/org/gcube/resourcemanagement/support/server/gcube/queries/xquery/resources/Collection.xq b/src/main/java/org/gcube/resourcemanagement/support/server/gcube/queries/xquery/resources/Collection.xq
new file mode 100644
index 0000000..2c9b328
--- /dev/null
+++ b/src/main/java/org/gcube/resourcemanagement/support/server/gcube/queries/xquery/resources/Collection.xq
@@ -0,0 +1,22 @@
+
+
+for $profiles in collection('/db/Profiles/GenericResource')//Resource, $wsresource in collection('/db/Properties')//Document
+let $scopes := string-join( $profiles/Scopes//Scope/text(), ';')
+let $subtype := $profiles//Resource/Profile/SecondaryType/text()
+where $subtype = "GCUBECollection" and $profiles//Resource/ID eq $wsresource/SourceKey and $wsresource/Data//child::*[local-name()='Type']/string() eq 'gDocRead'
+
+ return
+
+ {$profiles//Resource/ID}
+ {$profiles//Resource/Type/text()}
+ {$subtype}
+ {$scopes}
+ {$profiles//Resource/Profile/Name/text()}
+ {$profiles//Resource/Profile/Body/CollectionInfo/creationTime/text()}
+ {$wsresource/LastUpdateHuman/text()}
+ {$wsresource//child::*[local-name()='Cardinality']/text()}
+
\ No newline at end of file
diff --git a/src/main/java/org/gcube/resourcemanagement/support/server/gcube/queries/xquery/resources/GHN.xq b/src/main/java/org/gcube/resourcemanagement/support/server/gcube/queries/xquery/resources/GHN.xq
new file mode 100644
index 0000000..675a2d8
--- /dev/null
+++ b/src/main/java/org/gcube/resourcemanagement/support/server/gcube/queries/xquery/resources/GHN.xq
@@ -0,0 +1,30 @@
+
+
+
+for $profiles in collection('/db/Profiles/GHN')//Resource
+let $gcf-version := $profiles//Resource/Profile/GHNDescription/RunTimeEnv/Variable[Key/text() = 'gCF-version']/Value/text()
+let $ghn-version := $profiles//Resource/Profile/GHNDescription/RunTimeEnv/Variable[Key/text() = 'GHN-distribution-version']/Value/text()
+let $scopes := string-join( $profiles/Scopes//Scope/text(), ';')
+let $subtype := $profiles//Resource/Profile/Site/Domain/text()
+
+ return
+
+ {$profiles//Resource/ID}
+ {$profiles//Resource/Type/text()}
+ {$subtype}
+ {$profiles//Resource/Profile/GHNDescription/Status/text()}
+ {$profiles//Resource/Profile/GHNDescription/Name/text()}
+ {$profiles//Resource/Profile/GHNDescription/Uptime/text()}
+ {$profiles//Resource/Profile/GHNDescription/LastUpdate/text()}
+ {$profiles//Resource/Profile/GHNDescription/LocalAvailableSpace/text()}
+ {$profiles//Resource/Profile/GHNDescription/MainMemory/string(@VirtualSize)}
+ {$profiles//Resource/Profile/GHNDescription/MainMemory/string(@VirtualAvailable)}
+ {$profiles//Resource/Profile/GHNDescription/Load/string(@Last1Min)}
+ {$profiles//Resource/Profile/GHNDescription/Load/string(@Last5Min)}
+ {$profiles//Resource/Profile/GHNDescription/Load/string(@Last15Min)}
+ {$gcf-version}
+ {$ghn-version}
+ {$scopes}
+
\ No newline at end of file
diff --git a/src/main/java/org/gcube/resourcemanagement/support/server/gcube/queries/xquery/resources/GenericResource.xq b/src/main/java/org/gcube/resourcemanagement/support/server/gcube/queries/xquery/resources/GenericResource.xq
new file mode 100644
index 0000000..472a7d6
--- /dev/null
+++ b/src/main/java/org/gcube/resourcemanagement/support/server/gcube/queries/xquery/resources/GenericResource.xq
@@ -0,0 +1,15 @@
+
+
+
+for $profiles in collection('/db/Profiles/GenericResource')//Resource
+let $scopes := string-join( $profiles/Scopes//Scope/text(), ';')
+let $subtype := $profiles//Resource/Profile/SecondaryType/text()
+
+ return
+
+ {$profiles//Resource/ID}
+ {$profiles//Resource/Type/text()}
+ {$subtype}
+ {$scopes}
+ {$profiles//Resource/Profile/Name/text()}
+
\ No newline at end of file
diff --git a/src/main/java/org/gcube/resourcemanagement/support/server/gcube/queries/xquery/resources/RunningInstance.xq b/src/main/java/org/gcube/resourcemanagement/support/server/gcube/queries/xquery/resources/RunningInstance.xq
new file mode 100644
index 0000000..ac8dd3a
--- /dev/null
+++ b/src/main/java/org/gcube/resourcemanagement/support/server/gcube/queries/xquery/resources/RunningInstance.xq
@@ -0,0 +1,26 @@
+
+
+for $profiles in collection('/db/Profiles/RunningInstance')//Resource
+
+let $ghns := collection('/db/Profiles/GHN')//Resource
+let $_ghn-name := for $ghn in $ghns
+ where $ghn/ID/string() eq $profiles/Profile/GHN/@UniqueID/string()
+ return $ghn/Profile/GHNDescription/Name/string()
+let $ghn-name := if (empty($_ghn-name)) then $profiles/Profile/GHN/@UniqueID/string() else $_ghn-name
+let $scopes := string-join( $profiles/Scopes//Scope/text(), ';')
+let $subtype := $profiles/Profile/ServiceClass/text()
+
+ return
+
+ {$profiles/ID}
+ {$profiles/Type/text()}
+ {$subtype}
+ {$scopes}
+ {$profiles/Profile/ServiceClass/text()}
+ {$profiles/Profile/ServiceName/text()}
+ {$profiles/Profile/Version/text()}
+ {$profiles/Profile/DeploymentData/Status/text()}
+ {$ghn-name}
+
\ No newline at end of file
diff --git a/src/main/java/org/gcube/resourcemanagement/support/server/gcube/queries/xquery/resources/RuntimeResource.xq b/src/main/java/org/gcube/resourcemanagement/support/server/gcube/queries/xquery/resources/RuntimeResource.xq
new file mode 100644
index 0000000..7e36fe7
--- /dev/null
+++ b/src/main/java/org/gcube/resourcemanagement/support/server/gcube/queries/xquery/resources/RuntimeResource.xq
@@ -0,0 +1,12 @@
+for $profiles in collection('/db/Profiles/RuntimeResource')//Resource
+let $scopes := string-join( $profiles/Scopes//Scope/text(), ';')
+let $subtype := $profiles//Resource/Profile/Category/text()
+
+ return
+
+ {$profiles//Resource/ID}
+ {$profiles//Resource/Type/text()}
+ {$subtype}
+ {$scopes}
+ {$profiles//Resource/Profile/Name/text()}
+
\ No newline at end of file
diff --git a/src/main/java/org/gcube/resourcemanagement/support/server/gcube/queries/xquery/resources/Service.xq b/src/main/java/org/gcube/resourcemanagement/support/server/gcube/queries/xquery/resources/Service.xq
new file mode 100644
index 0000000..215ea0d
--- /dev/null
+++ b/src/main/java/org/gcube/resourcemanagement/support/server/gcube/queries/xquery/resources/Service.xq
@@ -0,0 +1,18 @@
+
+
+for $profiles in collection('/db/Profiles/Service')//Resource
+let $scopes := string-join( $profiles/Scopes//Scope/text(), ';')
+let $subtype := $profiles//Resource/Profile/Class/text()
+
+ return
+
+ {$profiles//Resource/ID}
+ {$profiles//Resource/Type/text()}
+ {$subtype}
+ {$scopes}
+ {$profiles//Resource/Profile/Class/text()}
+ {$profiles//Resource/Profile/Name/text()}
+
+ {$profiles//Resource/Profile/Packages/Software[1]/Version/text()}
+ {$profiles//Resource/Profile/Packages/Software/Shareable/string(@level)}
+
\ No newline at end of file
diff --git a/src/main/java/org/gcube/resourcemanagement/support/server/gcube/queries/xquery/resources/VIEW.xq b/src/main/java/org/gcube/resourcemanagement/support/server/gcube/queries/xquery/resources/VIEW.xq
new file mode 100644
index 0000000..641e7d8
--- /dev/null
+++ b/src/main/java/org/gcube/resourcemanagement/support/server/gcube/queries/xquery/resources/VIEW.xq
@@ -0,0 +1,21 @@
+declare namespace gc = 'http://gcube-system.org/namespaces/common/core/porttypes/GCUBEProvider';
+declare namespace wmns= 'http://gcube-system.org/namespaces/contentmanagement/viewmanager';
+for $outer in collection("/db/Properties")//Document, $res in $outer/Data where $res/gc:ServiceClass/string() eq 'ContentManagement'
+and count($res//wmns:View)>0 and $res/gc:ServiceName/string() eq 'ViewManager'
+return
+
+ {$outer//Document/ID}
+ {$outer//Document/Source}
+ {$outer//Document/SourceKey}
+ {$outer//Document/Data/child::*[local-name()='View']/child::*[local-name()='property']/child::*[local-name()='name' and text()='name']/../child::*[local-name()='value']/text()}
+ {$outer//Document/Data/child::*[local-name()='View']/child::*[local-name()='cardinality']/text()}
+ {$outer//Document/Data/child::*[local-name()='View']/child::*[local-name()='type']/text()}
+ {$outer//Document/Data/child::*[local-name()='View']/child::*[local-name()='collectionID']/text()}
+ {$outer//Document/Data/child::*[local-name()='ServiceClass']/text()}
+ {$outer//Document/Data/child::*[local-name()='ServiceName']/text()}
+ {$outer//Document/Data/child::*[local-name()='ServiceClass']/text()}
+ {$outer//Document/TerminationTimeHuman} {$outer//Document/LastUpdateHuman}
+ {$outer//Document/Data/child::*[local-name()='RI']/text()}
+ WSResource
+ {$outer//Document/Data/child::*[local-name()='Scope']/text()}
+
diff --git a/src/main/java/org/gcube/resourcemanagement/support/server/gcube/queries/xquery/sweeper/deadGhns.xq b/src/main/java/org/gcube/resourcemanagement/support/server/gcube/queries/xquery/sweeper/deadGhns.xq
new file mode 100644
index 0000000..86d266e
--- /dev/null
+++ b/src/main/java/org/gcube/resourcemanagement/support/server/gcube/queries/xquery/sweeper/deadGhns.xq
@@ -0,0 +1,29 @@
+
+let $RIs := collection("/db/Profiles/RunningInstance")//Document/Data/child::*[local-name()='Profile']/Resource
+for $GHNs in collection("/db/Profiles/GHN")//Document/Data/child::*[local-name()='Profile']/Resource
+let $totalminutes := hours-from-dateTime($GHNs/Profile/GHNDescription/LastUpdate/text())
+let $RIinstalled := $RIs[Profile/GHN/string(@UniqueID)=$GHNs/ID]
+let $scopes := string-join( $GHNs/Scopes//Scope/text(), ';')
+where ($GHNs/Profile/GHNDescription/Status/string() eq 'down' or $GHNs/Profile/GHNDescription/Status/string() eq 'unreachable')
+return
+
+ {$GHNs/ID}
+ {$GHNs/Profile/GHNDescription/Name}
+ {$GHNs/Profile/GHNDescription/Status}
+ {$GHNs/Profile/GHNDescription/Type}
+ {$GHNs/Profile/Site/Location}
+ {$GHNs/Profile/Site/Domain}
+ {$GHNs/Profile/GHNDescription/NetworkAdapter/@IPAddress/string()}
+
+ {$scopes}
+ {count($RIinstalled)}
+ {$GHNs/Profile/GHNDescription/LastUpdate}
+ {$totalminutes}
+
+
\ No newline at end of file
diff --git a/src/main/java/org/gcube/resourcemanagement/support/server/gcube/queries/xquery/sweeper/expiredGhns.xq b/src/main/java/org/gcube/resourcemanagement/support/server/gcube/queries/xquery/sweeper/expiredGhns.xq
new file mode 100644
index 0000000..327987b
--- /dev/null
+++ b/src/main/java/org/gcube/resourcemanagement/support/server/gcube/queries/xquery/sweeper/expiredGhns.xq
@@ -0,0 +1,27 @@
+
+let $RIs := collection("/db/Profiles/RunningInstance")//Document/Data/child::*[local-name()='Profile']/Resource
+for $GHNs in collection("/db/Profiles/GHN")//Document/Data/child::*[local-name()='Profile']/Resource
+let $RIinstalled := $RIs[Profile/GHN/string(@UniqueID)=$GHNs/ID]
+let $scopes := string-join( $GHNs/Scopes//Scope/text(), ';')
+where $GHNs/Profile/GHNDescription/Status/string() != 'down' and $GHNs/Profile/GHNDescription/Status/string() != 'unreachable'
+return
+
+ {$GHNs/ID}
+ {$GHNs/Profile/GHNDescription/Name}
+ {$GHNs/Profile/GHNDescription/Status}
+ {$GHNs/Profile/GHNDescription/Type}
+ {$GHNs/Profile/Site/Location}
+ {$GHNs/Profile/Site/Domain}
+ {$GHNs/Profile/GHNDescription/NetworkAdapter/@IPAddress/string()}
+
+ {$scopes}
+ {count($RIinstalled)}
+ {$GHNs/Profile/GHNDescription/LastUpdate}
+
+
\ No newline at end of file
diff --git a/src/main/java/org/gcube/resourcemanagement/support/server/gcube/queries/xquery/sweeper/orphanRI.xq b/src/main/java/org/gcube/resourcemanagement/support/server/gcube/queries/xquery/sweeper/orphanRI.xq
new file mode 100644
index 0000000..ebac355
--- /dev/null
+++ b/src/main/java/org/gcube/resourcemanagement/support/server/gcube/queries/xquery/sweeper/orphanRI.xq
@@ -0,0 +1,19 @@
+let $ghns := collection('/db/Profiles/GHN')//Resource/ID
+let $ris := collection('/db/Profiles/RunningInstance')//Resource
+for $ri in $ris
+let $counter := index-of(($ghns//ID/string()), $ri/Profile/GHN/@UniqueID/string())
+where empty($counter)
+return
+
+ {$ri//Resource/ID/string()}
+ {$ri/Profile/DeploymentData/Status/string()}
+ {$ri/Profile/DeploymentData/ActivationTime/@value/string()}
+ {$ri/Profile/GHN/@UniqueID/string()}
+ {$ri/Profile/ServiceClass/string()}
+ {$ri/Profile/ServiceName/string()}
+
+
+
+
\ No newline at end of file