diff --git a/pom.xml b/pom.xml index 907c324..d41aaba 100644 --- a/pom.xml +++ b/pom.xml @@ -35,6 +35,12 @@ provided + + com.sencha.gxt + gxt + 2.2.5 + provided + junit junit diff --git a/src/main/java/org/gcube/resourcemanagement/support/client/views/ResourceTypeDecorator.java b/src/main/java/org/gcube/resourcemanagement/support/client/views/ResourceTypeDecorator.java new file mode 100644 index 0000000..c3601b4 --- /dev/null +++ b/src/main/java/org/gcube/resourcemanagement/support/client/views/ResourceTypeDecorator.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: ResourceTypeDecorator.java + **************************************************************************** + * @author Daniele Strollo + ***************************************************************************/ + +package org.gcube.resourcemanagement.support.client.views; + +/** + * @author Daniele Strollo (ISTI-CNR) + * + */ +public enum ResourceTypeDecorator { + /**************************************** + * RESOURCES IN THE TREE + ****************************************/ + GHN("gCube Hosting Node", "ghn-icon"), + RunningInstance("Running Instances", "runninginstance-icon"), + Service("Software", "service-icon"), + VIEW("View", "metadatacollection-icon"), + GenericResource("Generic Resources", "genericresource-icon"), + Collection("Collection", "collection-icon"), + WSResource("WSResource", "wsresources-icon"), + Empty("Empty Tree", "empty-icon"), + RuntimeResource("Runtime Resources", "runtimeresource-icon"), + + /**************************************** + * Other components + ****************************************/ + // For deploying services - similar to the software but with an + // extension to handle checkbox for install + InstallableSoftware("InstallableSoftware", "empty-icon"), + // In the taskbar for handlig the refresh of deployment reports + DeployReport("Deploy Report", "report-big-icon"), + AddScopeReport("Add Scope Report", "report-big-icon"), + + + /**************************************** + * Related resources + ****************************************/ + GHNRelated("Running Instances", "runninginstance-icon"), + ServiceRelated("Running Instances", "runninginstance-icon"), + RunningInstanceRelated("Running Instances", "runninginstance-icon"), + + /**************************************** + * Models for SWEEPER + ***************************************/ + Sweeper_GHN("gCube Hosting Node", "ghn-icon"), + Sweeper_RI("Running Instance", "runninginstance-icon"); + + + private String label = null; + private String icon = null; + + ResourceTypeDecorator( + final String label, + final String icon) { + this.label = label; + this.icon = icon; + } + + public String getLabel() { + return this.label; + } + + public String getIcon() { + return this.icon; + } +} diff --git a/src/main/java/org/gcube/resourcemanagement/support/server/gcube/CacheManager.java b/src/main/java/org/gcube/resourcemanagement/support/server/gcube/CacheManager.java new file mode 100644 index 0000000..f4311d5 --- /dev/null +++ b/src/main/java/org/gcube/resourcemanagement/support/server/gcube/CacheManager.java @@ -0,0 +1,18 @@ +package org.gcube.resourcemanagement.support.server.gcube; + +public class CacheManager { + private boolean useCache = false; + + + public CacheManager() { + // for serialization only + } + + public boolean isUsingCache() { + return this.useCache; + } + + public void setUseCache(final boolean useCache) { + this.useCache = useCache; + } +} diff --git a/src/main/java/org/gcube/resourcemanagement/support/server/gcube/ISClientRequester.java b/src/main/java/org/gcube/resourcemanagement/support/server/gcube/ISClientRequester.java new file mode 100644 index 0000000..30562ca --- /dev/null +++ b/src/main/java/org/gcube/resourcemanagement/support/server/gcube/ISClientRequester.java @@ -0,0 +1,828 @@ +/**************************************************************************** + * 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: ISClientRequester.java + **************************************************************************** + * @author Daniele Strollo + ***************************************************************************/ + +package org.gcube.resourcemanagement.support.server.gcube; + +import java.io.BufferedReader; +import java.io.ByteArrayInputStream; +import java.io.FileInputStream; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.StringReader; +import java.io.StringWriter; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.StringTokenizer; +import java.util.Vector; + +import javax.xml.transform.OutputKeys; +import javax.xml.transform.Templates; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.stream.StreamResult; +import javax.xml.transform.stream.StreamSource; + +import org.gcube.common.core.contexts.GHNContext; +import org.gcube.common.core.informationsystem.client.ISClient; +import org.gcube.common.core.informationsystem.client.QueryParameter; +import org.gcube.common.core.informationsystem.client.XMLResult; +import org.gcube.common.core.informationsystem.client.XMLResult.ISResultEvaluationException; +import org.gcube.common.core.informationsystem.client.queries.GCUBEGenericQuery; +import org.gcube.common.core.scope.GCUBEScope; +import org.gcube.common.core.utils.logging.GCUBEClientLog; +import org.gcube.resourcemanagement.support.client.views.ResourceTypeDecorator; +import org.gcube.resourcemanagement.support.server.gcube.queries.QueryLoader; +import org.gcube.resourcemanagement.support.server.gcube.queries.QueryLocation; + + +import org.gcube.resourcemanagement.support.server.managers.scope.ScopeManager; +import org.gcube.resourcemanagement.support.server.utils.ServerConsole; +import org.gcube.resourcemanagement.support.shared.plugins.GenericResourcePlugin; +import org.gcube.resourcemanagement.support.shared.plugins.TMPluginFormField; +import org.gcube.resourcemanagement.support.shared.types.Tuple; +import org.gcube.resourcemanagement.support.shared.types.datamodel.CompleteResourceProfile; +import org.gcube.resourcemanagement.support.shared.types.datamodel.ResourceDescriptor; +import org.w3c.dom.Document; +import org.w3c.dom.NamedNodeMap; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + + +/** + * All the requests to the IS are implemented here. + * @author Massimiliano Assante (ISTI-CNR) + * @author Daniele Strollo + */ +public class ISClientRequester { + static GCUBEClientLog _log = new GCUBEClientLog(ISClientRequester.class); + + private static final ISQueryCache CACHE = new ISQueryCache(); + private static final String LOG_PREFIX = "[ISCLIENT-REQS]"; + + public static void emptyCache() { + CACHE.empty(); + } + + private static final List getResourceTypes( + final CacheManager status, + final GCUBEScope queryScope) + throws Exception { + List results = null; + ISClient client = GHNContext.getImplementation(ISClient.class); + GCUBEGenericQuery isQuery = null; + isQuery = client.getQuery(GCUBEGenericQuery.class); + isQuery.setExpression(QueryLoader.getQuery(QueryLocation.GET_TREE_TYPES)); + + // Handles the cache + ISQueryCacheKeyT cacheKey = new ISQueryCacheKeyT(queryScope.toString(), + isQuery.getExpression(), + "getResourcesTypes"); + + if (status.isUsingCache() && CACHE.contains(cacheKey) && CACHE.get(cacheKey) != null) { + results = CACHE.get(cacheKey); + } else { + try { + results = client.execute(isQuery, queryScope); + if (status.isUsingCache()) { + CACHE.insert(cacheKey, results); + } + } catch (Exception e) { + throw new Exception(e.getMessage()); + } + } + if (results == null || results.size() == 0) { + return null; + } + + String type = null; + List types = new Vector(); + try { + for (XMLResult elem : results) { + + type = elem.evaluate("/type/text()").get(0); + if (type != null && type.trim().length() > 0) { + types.add(type.trim()); + } + } + //**** CHANGES TO KNOW IF VIEWS AND GCUBECollection are present + types.remove("MetadataCollection"); + + isQuery.setExpression("declare namespace vm = 'http://gcube-system.org/namespaces/contentmanagement/viewmanager';"+ + "declare namespace gc = 'http://gcube-system.org/namespaces/common/core/porttypes/GCUBEProvider';"+ + "declare namespace is = 'http://gcube-system.org/namespaces/informationsystem/registry';" + + "for $data in collection(\"/db/Properties\")//Document, $view in $data/Data where $view/gc:ServiceName/string() eq \"ViewManager\" " + + " and $view/gc:ServiceClass/string() eq \"ContentManagement\" and count($view//vm:View)>0 return true"); + + if (client.execute(isQuery, queryScope).size() > 0) + types.add("VIEW"); + + isQuery.setExpression("declare namespace gc = 'http://gcube-system.org/namespaces/common/core/porttypes/GCUBEProvider';"+ + "declare namespace is = 'http://gcube-system.org/namespaces/informationsystem/registry';" + + "for $data in collection(\"/db/Profiles/GenericResource\")//Document/Data/is:Profile/Resource where $data/Profile/SecondaryType eq \"GCUBECollection\" " + + " return true"); + + if (client.execute(isQuery, queryScope).size() > 0) + types.add("Collection"); + + } catch (ISResultEvaluationException e) { + _log.error("error during getResourcesTypes"); + } catch (IndexOutOfBoundsException e) { + // ignore exception + } + + return types; + } + + private static final List getResourceSubTypes( + final CacheManager status, + final GCUBEScope queryScope, + final String resourceType) + throws Exception { + + List subtypes = new Vector(); + if (resourceType.equals("Collection")) { + subtypes.add("System"); + subtypes.add("User"); + return subtypes; + } + if (resourceType.equals("VIEW")) { + subtypes.add("Not supported"); + return subtypes; + } + + List results = null; + ISClient client = GHNContext.getImplementation(ISClient.class); + GCUBEGenericQuery isQuery = null; + isQuery = client.getQuery(GCUBEGenericQuery.class); + isQuery.setExpression(QueryLoader.getQuery(QueryLocation.GET_TREE_SUBTYPES)); + + isQuery.addParameters(new QueryParameter("RES_TYPE", resourceType)); + + // Handles the cache + ISQueryCacheKeyT cacheKey = new ISQueryCacheKeyT(queryScope.toString(), + isQuery.getExpression(), + "getResourcesSubTypes"); + + if (status.isUsingCache() && CACHE.contains(cacheKey) && CACHE.get(cacheKey) != null) { + results = CACHE.get(cacheKey); + } else { + try { + results = client.execute(isQuery, queryScope); + if (status.isUsingCache()) { + CACHE.insert(cacheKey, results); + } + } catch (Exception e) { + throw new Exception(e.getMessage()); + } + } + if (results == null || results.size() == 0) { + return null; + } + + String subtype = null; + + for (XMLResult elem : results) { + try { + subtype = elem.evaluate("/subtype/text()").get(0); + if (subtype != null && subtype.trim().length() > 0) { + subtypes.add(subtype.trim()); + } + } catch (ISResultEvaluationException e) { + _log.error("error during getResourcesTypes"); + } catch (IndexOutOfBoundsException e) { + // ignore exception + } + } + return subtypes; + } + + /** + * For all the resource in the scope retrieves their + * (type, subtype) values. + * The result is a list of couples of that form. + * @return a list of string tuples (type, subtype) + * @throws Exception + */ + public static final Map> getResourcesTree( //qui si perde + final CacheManager status, + final GCUBEScope queryScope) + throws Exception { + Map> retval = new HashMap>(); + + // Loads the Resources + List types = getResourceTypes(status, queryScope); + List subtypes = null; + + for (String type : types) { + try { + subtypes = getResourceSubTypes(status, queryScope, type); + if (subtypes != null && subtypes.size() > 0) { + retval.put(type, subtypes); + } + } catch (Exception e) { + ServerConsole.error(LOG_PREFIX, e); + } + } + + // Loads the WSResources + // This types is statically handled since it is a particular + // case of resource. + List results = null; + ISClient client = GHNContext.getImplementation(ISClient.class); + GCUBEGenericQuery isQuery = null; + isQuery = client.getQuery(GCUBEGenericQuery.class); + isQuery.setExpression(QueryLoader.getQuery(QueryLocation.GET_WSRES_TYPES)); + results = client.execute(isQuery, queryScope); + if (results == null || results.size() == 0) { + return retval; + } + subtypes = new Vector(); + for (XMLResult elem : results) { + subtypes.add(elem.toString()); + } + retval.put("WSResource", subtypes); + return retval; + } + + public static final List getRelatedResources( + final CacheManager status, + final String type, + final String id, + final GCUBEScope queryScope) + throws Exception { + ISClient client = GHNContext.getImplementation(ISClient.class); + GCUBEGenericQuery isQuery = null; + isQuery = client.getQuery(GCUBEGenericQuery.class); + + QueryLocation queryPath = null; + try { + queryPath = QueryLocation.valueOf("LIST_RELATED_" + type); + } catch (Exception e) { + ServerConsole.error(LOG_PREFIX, "Getting the resource query.", e); + throw new Exception(e.getMessage()); + } + isQuery.setExpression(QueryLoader.getQuery(queryPath)); + isQuery.addParameters(new QueryParameter("RES_ID", id.trim())); + + ISQueryCacheKeyT cacheKey = new ISQueryCacheKeyT(queryScope.toString(), + isQuery.getExpression(), + "getResourceRelated"); + + List results = null; + // Handle cache + if (status.isUsingCache() && CACHE.contains(cacheKey) && CACHE.get(cacheKey) != null) { + results = CACHE.get(cacheKey); + } else { + results = client.execute(isQuery, queryScope); + if (status.isUsingCache()) { + CACHE.insert(cacheKey, results); + } + } + if (results == null || results.size() == 0) { + return null; + } + // ENDOF Handle cache + + List retval = new Vector(); + + for (XMLResult elem : results) { + // Removes the resources with no ID or empty + try { + String toAdd = elem.toString(); + if (toAdd != null) { + toAdd = toAdd.replace("", ""); + toAdd = toAdd.replace("", ""); + } + retval.add(toAdd); + } catch (Exception e) { + ServerConsole.debug(LOG_PREFIX, "[getResourcesRelated] found and invalid resource"); + } + } + ServerConsole.trace(LOG_PREFIX, "Retrieved (" + retval.size() + ") ServiceDetails for type: " + type); + return retval; + } + + public static final List getResourcesByType( + final CacheManager status, + final GCUBEScope queryScope, + final String type, + final String subType) + throws Exception { + ISClient client = GHNContext.getImplementation(ISClient.class); + GCUBEGenericQuery isQuery = null; + isQuery = client.getQuery(GCUBEGenericQuery.class); + + QueryLocation queryPath = null; + try { + if (type.equals(ResourceTypeDecorator.WSResource.name())) { + queryPath = QueryLocation.GET_WSRES_DETAILS_BYSUBTYPE; + } else { + queryPath = QueryLocation.valueOf("LIST_" + type); + } + } catch (Exception e) { + ServerConsole.error(LOG_PREFIX, "Getting the resource query.", e); + throw new Exception(e.getMessage()); + } + + isQuery.setExpression(QueryLoader.getQuery(queryPath)); + if (type.equals(ResourceTypeDecorator.WSResource.name())) { + if (subType != null && subType.length() > 0) { + isQuery.addParameters(new QueryParameter("RES_SUBTYPE", + subType.trim())); + } + } else { + if (subType != null && subType.length() > 0) { + if (subType.equalsIgnoreCase("User") || subType.equalsIgnoreCase("System")) { + isQuery.addParameters(new QueryParameter("RES_SUBTYPE", " and $profiles/Profile/Body/CollectionInfo/user/text() = \"" + ((subType.trim().equals("User")) ? "true" : "false") + "\"")); + } + else + isQuery.addParameters(new QueryParameter("RES_SUBTYPE", "where $subtype = \"" + subType.trim() + "\"")); + } + } + + ISQueryCacheKeyT cacheKey = new ISQueryCacheKeyT(queryScope.toString(), + isQuery.getExpression(), + "getResourcesTypes"); + + List results = null; + + ServerConsole.debug(LOG_PREFIX, "XQUERYY:\n"+isQuery.getExpression()); + if (status.isUsingCache() && CACHE.contains(cacheKey) && CACHE.get(cacheKey) != null) { + results = CACHE.get(cacheKey); + } else { + results = client.execute(isQuery, queryScope); + if (status.isUsingCache()) { + CACHE.insert(cacheKey, results); + } + } + if (results == null || results.size() == 0) { + return null; + } + List retval = new Vector(); + + for (XMLResult elem : results) { + // Removes the resources with no ID or empty + try { + if (elem.evaluate("//ID").get(0) != null && elem.evaluate("//ID").get(0).trim().length() > 0) { + retval.add(elem.toString()); + //ServerConsole.debug("", elem.toString());// Print the result + } else { + ServerConsole.debug(LOG_PREFIX, "*** Found an invalid element with no ID"); + } + + } catch (Exception e) { + ServerConsole.debug(LOG_PREFIX, "[getResourcesByType] found a resource with empty ID"); + } + } + ServerConsole.trace(LOG_PREFIX, "Retrieved (" + retval.size() + ") ServiceDetails for type: " + type); + return retval; + } + + public static final List getResourceModels( + final GCUBEScope queryScope, + final String type, + final String subType, + final List> additionalMaps) + throws Exception { + ISClient client = GHNContext.getImplementation(ISClient.class); + GCUBEGenericQuery isQuery = null; + isQuery = client.getQuery(GCUBEGenericQuery.class); + if (subType != null && subType.trim().length() > 0) { + isQuery.setExpression(QueryLoader.getQuery(QueryLocation.GET_RES_DETAILS_BYSUBTYPE)); + isQuery.addParameters( + new QueryParameter("RES_TYPE", type) + ); + isQuery.addParameters( + new QueryParameter("RES_SUBTYPE", subType) + ); + } else { + isQuery.setExpression(QueryLoader.getQuery(QueryLocation.GET_RES_DETAILS_BYTYPE)); + isQuery.addParameters( + new QueryParameter("RES_TYPE", type) + ); + } + List results = client.execute(isQuery, queryScope); + + List retval = new Vector(); + ResourceDescriptor toAdd = null; + for (XMLResult elem : results) { + // Removes the resources with no ID or empty + try { + if (elem.evaluate("//ID").get(0) != null) { + toAdd = new ResourceDescriptor( + elem.evaluate("//Type/text()").get(0), + elem.evaluate("//SubType/text()").get(0), + elem.evaluate("//ID/text()").get(0), + elem.evaluate("//Name/text()").get(0)); + + // Additional mappings can be defined by the requester. + // e.g. new Tuple("description", "//Profile/Description/text()"); + if (additionalMaps != null && additionalMaps.size() > 0) { + for (Tuple map : additionalMaps) { + try { + toAdd.addProperty(map.get(0), + elem.evaluate(map.get(1)).get(0)); + } catch (final Exception e) { + toAdd.addProperty(map.get(0), + ""); + } + } + } + + retval.add(toAdd); + } + } catch (Exception e) { + ServerConsole.debug(LOG_PREFIX, "[getResourcesByType] found a resource with empty ID"); + } + } + ServerConsole.trace(LOG_PREFIX, "Retrieved (" + retval.size() + ") ServiceDetails for type: " + type); + return retval; + } + + + public static final List getWSResources( + final GCUBEScope queryScope) + throws Exception { + ISClient client = GHNContext.getImplementation(ISClient.class); + GCUBEGenericQuery isQuery = null; + isQuery = client.getQuery(GCUBEGenericQuery.class); + isQuery.setExpression(QueryLoader.getQuery(QueryLocation.GET_WSRES_DETAILS_BYTYPE)); + List results = client.execute(isQuery, queryScope); + + List retval = new Vector(); + + for (XMLResult elem : results) { + retval.add(elem.toString()); + } + ServerConsole.trace(LOG_PREFIX, "Retrieved (" + retval.size() + ") ServiceDetails for type: " + ResourceTypeDecorator.WSResource.name()); + return retval; + } + + public static final CompleteResourceProfile getResourceByID( + final String xmlFilePath, + final GCUBEScope queryScope, + final String resType, + final String resID) + throws Exception { + ISClient client = GHNContext.getImplementation(ISClient.class); + GCUBEGenericQuery isQuery = null; + isQuery = client.getQuery(GCUBEGenericQuery.class); + if (resType == null) { + isQuery.setExpression(QueryLoader.getQuery(QueryLocation.GET_RESOURCE_BYID)); + isQuery.addParameters( + new QueryParameter("RES_ID", resID) + ); + } else if (resType.equalsIgnoreCase(ResourceTypeDecorator.WSResource.name())) { + isQuery.setExpression(QueryLoader.getQuery(QueryLocation.GET_WSRESOURCE_BYID)); + isQuery.addParameters( + new QueryParameter("RES_ID", resID) + ); + } else { + isQuery.setExpression(QueryLoader.getQuery(QueryLocation.GET_RESOURCE_BYID)); + isQuery.addParameters( + new QueryParameter("RES_ID", resID), + new QueryParameter("RES_TYPE", resType) + ); + } + List results = client.execute(isQuery, queryScope); + + List retval = new Vector(); + + ServerConsole.trace(LOG_PREFIX, "Retrieved (" + retval.size() + ") Resource for ID: " + resID); + + if (results != null && results.size() > 0) { + String type = null; + if (resType == null) { + try { + type = results.get(0).evaluate("/Resource/Type/text()").get(0); + } catch (Exception e) { + ServerConsole.error(LOG_PREFIX, e); + } + } else { + type = resType; + } + String xmlRepresentation = results.get(0).toString(); + String htmlRepresentation = XML2HTML(xmlRepresentation, xmlFilePath); + return new CompleteResourceProfile(resID, ResourceTypeDecorator.valueOf(type), getResourceName(type, resID, results.get(0)), xmlRepresentation, htmlRepresentation); + } + return null; + } + + public static Map getGenericResourcePlugins(final GCUBEScope scope) throws Exception { + ISClient client = GHNContext.getImplementation(ISClient.class); + GCUBEGenericQuery isQuery = null; + isQuery = client.getQuery(GCUBEGenericQuery.class); + isQuery.setExpression(QueryLoader.getQuery(QueryLocation.GET_GENERIC_RESOURCE_PLUGINS)); + List results = client.execute(isQuery, scope); + + Map retval = new HashMap(); + gonext: for (XMLResult plugin : results) { + System.out.println(plugin.toString()); + try { + for (String entry : plugin.evaluate("/CMPlugins/Plugin/Entry")) { + Document doc = ScopeManager.getDocumentGivenXML(entry); + String name = doc.getElementsByTagName("name").item(0).getFirstChild().getNodeValue(); + ServerConsole.info(LOG_PREFIX, "[LOAD-PLUGIN] found: *** name " + name); + String pluginType = doc.getElementsByTagName("Type").item(0).getFirstChild().getNodeValue(); + ServerConsole.info(LOG_PREFIX, "[LOAD-PLUGIN] found: *** type " + pluginType); + String description = doc.getElementsByTagName("description").item(0).getFirstChild().getNodeValue(); + ServerConsole.info(LOG_PREFIX, "[LOAD-PLUGIN] found: *** description " + description); + String namespace = null; + try { + namespace = doc.getElementsByTagName("namespace").item(0).getFirstChild().getNodeValue(); + ServerConsole.info(LOG_PREFIX, "[LOAD-PLUGIN] found: *** namespace " + namespace); + } catch (Exception e) { + ServerConsole.warn(LOG_PREFIX, "[LOAD-PLUGIN] namespace not found"); + } + + GenericResourcePlugin toAdd = new GenericResourcePlugin(name, namespace, description, pluginType); + + NodeList params = doc.getElementsByTagName("param"); + for (int i = 0; i < params.getLength(); i++) { + + NodeList paramTree = params.item(i).getChildNodes(); + String paramName = null; + String paramDefinition = null; + for (int j = 0; j < paramTree.getLength(); j++) { + if (paramTree.item(j).getNodeName().equals("param-name")) { + paramName = paramTree.item(j).getFirstChild().getNodeValue(); + } + + if (paramTree.item(j).getNodeName().equals("param-definition")) { + paramDefinition = paramTree.item(j).getFirstChild().getNodeValue(); + } + } + + ServerConsole.info(LOG_PREFIX, "[LOAD-PLUGIN] found: param " + paramName); + + GenericResourcePlugin.Field paramField = new GenericResourcePlugin.Field(paramName, GenericResourcePlugin.FieldType.string); + if (paramDefinition != null) { + StringTokenizer parser = new StringTokenizer(paramDefinition, ";"); + while (parser.hasMoreTokens()) { + try { + String currElem = parser.nextToken(); + String key = currElem.substring(0, currElem.indexOf("=")); + String value = currElem.substring(currElem.indexOf("=") + 1, currElem.length()).trim(); + if (key.equals("type")) { + paramField.setType(GenericResourcePlugin.FieldType.valueOf(value)); + } + if (key.equals("opt")) { + paramField.setIsRequired(!Boolean.parseBoolean(value)); + } + if (key.equals("label")) { + if (value.startsWith("'")) { + value = value.substring(1, value.length()); + } + if (value.endsWith("'")) { + value = value.substring(0, value.length() - 1); + } + paramField.setLabel(value); + } + if (key.equals("default")) { + if (value.startsWith("'")) { + value = value.substring(1, value.length()); + } + if (value.endsWith("'")) { + value = value.substring(0, value.length() - 1); + } + paramField.setDefaultValue(value); + } + } catch (Exception e) { + // parsing error - not well formed string + } + } + } + toAdd.addParam(paramField); + } + + retval.put(name + "::" + pluginType, toAdd); + } + } catch (RuntimeException e) { + continue gonext; + } + + + } + + return retval; + } + /** + * get the plugins for tree manager + * @param scope + * @return a map containing the plugin name as key and a List of formfield + * @throws Exception + */ + public static HashMap> getGenericResourceTreeManagerPlugins(final GCUBEScope scope) throws Exception { + ISClient client = GHNContext.getImplementation(ISClient.class); + GCUBEGenericQuery isQuery = null; + isQuery = client.getQuery(GCUBEGenericQuery.class); + isQuery.setExpression(QueryLoader.getQuery(QueryLocation.GET_GENERIC_RESOURCE_TREE_MANAGER_PLUGINS)); + List results = client.execute(isQuery, scope); + + HashMap> retval = new HashMap>(); + gonext: for (XMLResult plugin : results) { + System.out.println(plugin.toString()); + try { + for (String entry : plugin.evaluate("/TMPlugins/Plugin/Entry")) { + Document doc = ScopeManager.getDocumentGivenXML(entry); + String name = doc.getElementsByTagName("name").item(0).getFirstChild().getNodeValue(); + ServerConsole.info(LOG_PREFIX, "[LOAD-PLUGIN] found: *** name " + name); + String pluginType = doc.getElementsByTagName("Type").item(0).getFirstChild().getNodeValue(); + ServerConsole.info(LOG_PREFIX, "[LOAD-PLUGIN] found: *** type " + pluginType); + String description = doc.getElementsByTagName("description").item(0).getFirstChild().getNodeValue(); + ServerConsole.info(LOG_PREFIX, "[LOAD-PLUGIN] found: *** description " + description); + String namespace = null; + try { + namespace = doc.getElementsByTagName("namespace").item(0).getFirstChild().getNodeValue(); + ServerConsole.info(LOG_PREFIX, "[LOAD-PLUGIN] found: *** namespace " + namespace); + } catch (Exception e) { + ServerConsole.warn(LOG_PREFIX, "[LOAD-PLUGIN] namespace not found"); + } + + NodeList params = doc.getElementsByTagName("param"); + ArrayList formFields = new ArrayList(); + for (int i = 0; i < params.getLength(); i++) { + + NodeList paramTree = params.item(i).getChildNodes(); + String paramName = null; + String xmlToParse = null; + boolean foundSample = false; + for (int j = 0; j < paramTree.getLength(); j++) { + if (paramTree.item(j).getNodeName().equals("param-name")) { + paramName = paramTree.item(j).getFirstChild().getNodeValue(); + if (paramName.compareTo("requestSample") == 0) { + foundSample = true; + } + } + if (paramTree.item(j).getNodeName().equals("param-definition") && foundSample) { + xmlToParse = paramTree.item(j).getFirstChild().getNodeValue(); + xmlToParse = xmlToParse.replaceAll("<", "<"); + xmlToParse = xmlToParse.replaceAll(">", "<"); + System.out.println("toParse:" + xmlToParse); + foundSample = false; + formFields = getPluginFormFromXml(xmlToParse); + } + + } + } + retval.put(name, formFields); + } + } catch (RuntimeException e) { + continue gonext; + } + } + return retval; + } + + /** + * parses the following and return the list to generate the form automatically + * + * sample + * + * Parachela collection + * Parachela collection from Itis + * Parachela + * ITIS + * true + * 5 + * MINUTES + * + * + * @param xmlToParse + * @return the list + */ + private static ArrayList getPluginFormFromXml(String xmlToParse) { + ArrayList toReturn = new ArrayList(); + Document doc = ScopeManager.getDocumentGivenXML(xmlToParse); + Node root = doc.getElementsByTagName("speciesRequest").item(0); + NodeList children = root.getChildNodes(); + for (int i = 0; i < children.getLength(); i++) { + String label = children.item(i).getNodeName(); + String defaultValue = children.item(i).getFirstChild().getNodeValue(); + boolean repeatable = false; + boolean required = false; + if (children.item(i).hasAttributes()) { + NamedNodeMap attributes = children.item(i).getAttributes(); + if (children.item(i).getAttributes().getNamedItem("repeatable") != null) + repeatable = attributes.getNamedItem("repeatable").getNodeValue().equalsIgnoreCase("true"); + if (children.item(i).getAttributes().getNamedItem("required") != null) + required = attributes.getNamedItem("required").getNodeValue().equalsIgnoreCase("true"); + } + toReturn.add(new TMPluginFormField(label, defaultValue, required, repeatable)); + } + return toReturn; + } + + /** + * From the ID of a resource retrieves its name. Notice that resource name + * is retrieved according to their type. + * @param type the type of the resource + * @param ID the identifier of the resource + * @param node the XML node from which retrieve the information + * @return + */ + private static String getResourceName(String type, String ID, XMLResult node) { + if (type.equalsIgnoreCase(ResourceTypeDecorator.GHN.name())) { + try { + return node.evaluate("/Resource/Profile/GHNDescription/Name/text()").get(0); + } catch (Exception e) { + return ID; + } + } + if (type.equalsIgnoreCase(ResourceTypeDecorator.Collection.name())) { + try { + return node.evaluate("/Resource/Profile/Name/text()").get(0); + } catch (Exception e) { + return ID; + } + } + if (type.equalsIgnoreCase(ResourceTypeDecorator.Service.name())) { + try { + return node.evaluate("/Resource/Profile/Name/text()").get(0); + } catch (Exception e) { + return ID; + } + } + if (type.equalsIgnoreCase(ResourceTypeDecorator.RunningInstance.name())) { + try { + return node.evaluate("/Resource/Profile/ServiceName/text()").get(0); + } catch (Exception e) { + return ID; + } + } + if (type.equalsIgnoreCase(ResourceTypeDecorator.VIEW.name())) { + try { + return node.evaluate("/Resource/Profile/Name/text()").get(0); + } catch (Exception e) { + return ID; + } + } + if (type.equalsIgnoreCase(ResourceTypeDecorator.RuntimeResource.name())) { + try { + return node.evaluate("/Resource/Profile/Name/text()").get(0); + } catch (Exception e) { + return ID; + } + } + if (type.equalsIgnoreCase(ResourceTypeDecorator.GenericResource.name())) { + try { + return node.evaluate("/Resource/Profile/Name/text()").get(0); + } catch (Exception e) { + return ID; + } + } + if (type.equalsIgnoreCase(ResourceTypeDecorator.WSResource.name())) { + try { + return node.evaluate("/Document/Data/child::*[local-name()='ServiceName']/text()").get(0); + } catch (Exception e) { + return ID; + } + } + return null; + } + + public static String XML2HTML(final String xml, final String xslt) + throws Exception { + TransformerFactory tf = TransformerFactory.newInstance(); + + InputStream stream = new FileInputStream(xslt); + BufferedReader in = new BufferedReader(new InputStreamReader(stream)); + 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(); + + + StreamSource source = new StreamSource(new ByteArrayInputStream(retval.toString().getBytes())); + Templates compiledXSLT = tf.newTemplates(source); + Transformer t = compiledXSLT.newTransformer(); + t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "true"); + StringWriter w = new StringWriter(); + t.transform(new StreamSource(new StringReader(xml)), new StreamResult(w)); + return w.toString(); + } + +} diff --git a/src/main/java/org/gcube/resourcemanagement/support/server/gcube/ISQueryCache.java b/src/main/java/org/gcube/resourcemanagement/support/server/gcube/ISQueryCache.java new file mode 100644 index 0000000..2227abc --- /dev/null +++ b/src/main/java/org/gcube/resourcemanagement/support/server/gcube/ISQueryCache.java @@ -0,0 +1,93 @@ +/**************************************************************************** + * 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: ISQueryCache.java + **************************************************************************** + * @author Daniele Strollo + ***************************************************************************/ + +package org.gcube.resourcemanagement.support.server.gcube; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.gcube.common.core.informationsystem.client.XMLResult; + +class ISQueryCacheKeyT { + String keyValue = null; + String queryExpression = null; + + public ISQueryCacheKeyT(String scope, String queryExpression, String... params) { + if (scope != null && queryExpression != null && params != null && params.length > 0) { + this.queryExpression = queryExpression.trim(); + this.keyValue = scope + "*" + queryExpression + "*" + "["; + for (String entry : params) { + keyValue += "(" + entry + ")"; + } + this.keyValue += "]"; + } + } + + public String getQueryExpression() { + return this.queryExpression; + } + + @Override + public String toString() { + return this.keyValue; + } + + @Override + public boolean equals(Object obj) { + if (this.keyValue == null) { + return false; + } + if (obj instanceof ISQueryCacheKeyT) { + return this.keyValue.equals(((ISQueryCacheKeyT) obj).keyValue); + } + return super.equals(obj); + } + + public int hashCode() { + return this.keyValue.hashCode(); + } +} + +/** + * @author Daniele Strollo (ISTI-CNR) + * + */ +public class ISQueryCache { + Map> results = new HashMap>(); + + public void insert(ISQueryCacheKeyT key, List elem) { + this.results.put(key, elem); + } + + public boolean contains(ISQueryCacheKeyT key) { + if (key == null) { + return false; + } + return this.results.containsKey(key); + } + + public List get(ISQueryCacheKeyT key) { + if (key != null && this.results.containsKey(key)) { + return this.results.get(key); + } + return null; + } + + public void empty() { + this.results.clear(); + } +} diff --git a/src/main/java/org/gcube/resourcemanagement/support/shared/exceptions/InvalidParameterException.java b/src/main/java/org/gcube/resourcemanagement/support/shared/exceptions/InvalidParameterException.java new file mode 100644 index 0000000..1755c7e --- /dev/null +++ b/src/main/java/org/gcube/resourcemanagement/support/shared/exceptions/InvalidParameterException.java @@ -0,0 +1,44 @@ +/**************************************************************************** + * 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: InvalidParameterException.java + **************************************************************************** + * @author Daniele Strollo + ***************************************************************************/ + +package org.gcube.resourcemanagement.support.shared.exceptions; + +import com.google.gwt.user.client.rpc.IsSerializable; + +/** + * @author Daniele Strollo (ISTI-CNR) + * + */ +public class InvalidParameterException extends Exception implements IsSerializable { + private static final long serialVersionUID = 1L; + + public InvalidParameterException() { + super(); + } + + public InvalidParameterException(String message, Throwable cause) { + super(message, cause); + } + + public InvalidParameterException(String message) { + super(message); + } + + public InvalidParameterException(Throwable cause) { + super(cause); + } + +} diff --git a/src/main/java/org/gcube/resourcemanagement/support/shared/plugins/GenericResourcePlugin.java b/src/main/java/org/gcube/resourcemanagement/support/shared/plugins/GenericResourcePlugin.java new file mode 100644 index 0000000..3ac3b32 --- /dev/null +++ b/src/main/java/org/gcube/resourcemanagement/support/shared/plugins/GenericResourcePlugin.java @@ -0,0 +1,171 @@ +/**************************************************************************** + * 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: GenericResourcePlugin.java + **************************************************************************** + * @author Daniele Strollo + ***************************************************************************/ + +package org.gcube.resourcemanagement.support.shared.plugins; + +import java.io.Serializable; +import java.util.List; +import java.util.Vector; +import com.google.gwt.user.client.rpc.IsSerializable; + +/** + * @author Daniele Strollo (ISTI-CNR) + * + */ +public class GenericResourcePlugin implements Serializable, IsSerializable { + public enum FieldType implements Serializable, IsSerializable { + string(), + number(), + XML(); + } + + public static class Field implements Serializable, IsSerializable { + private static final long serialVersionUID = 5921865866801474305L; + private FieldType type = null; + private String name = null; + private boolean required = true; + private String label = null; + private String defaultValue = ""; + + /** + * @deprecated for serialization only + */ + public Field() { + } + + public Field(final String name, final FieldType type) { + this(name, type, true); + } + + public Field(final String name, final FieldType type, final boolean required) { + this(name, null, type, required); + } + + public Field(final String name, final String label, final FieldType type, final boolean required) { + this.setName(name); + this.setLabel(label); + this.type = type; + this.setIsRequired(required); + } + + public final String getDefaultValue() { + return this.defaultValue; + } + + public final void setDefaultValue(final String defaultValue) { + if (defaultValue != null && defaultValue.trim().length() > 0) { + this.defaultValue = defaultValue; + } + } + + public final void setLabel(final String label) { + if (label != null && label.trim().length() > 0) { + this.label = label.trim(); + } else { + this.label = name; + } + } + + public final String getLabel() { + if (this.label == null || this.label.trim().length() == 0) { + return this.name; + } else { + return this.label; + } + } + + private void setName(final String name) { + if (name != null) { + this.name = name.trim(); + } + } + + /** + * Corresponds to the tag name in the body + * @return + */ + public final String getName() { + return this.name; + } + + public final FieldType getType() { + return this.type; + } + + public final void setType(final FieldType type) { + this.type = type; + } + + public final boolean isRequired() { + return this.required; + } + + public final void setIsRequired(final boolean required) { + this.required = required; + } + } + + private static final long serialVersionUID = 6070331744211410508L; + private String name = null; + private String description = null; + private String type = null; + private List params = new Vector(); + private String namespace = "xmlns:ns4=\"http://gcube-system.org/namespaces/contentmanagement/contentmanager/oaiplugin\""; + + + /** + * @deprecated for serialization only + */ + public GenericResourcePlugin() { + } + + + public GenericResourcePlugin(final String name, final String namespace, final String description, final String type) { + super(); + this.name = name; + this.description = description; + this.type = type; + if (namespace != null && namespace.trim().length() > 0) { + this.namespace = "xmlns:ns4=\"" + namespace.trim() + "\""; + } + } + + public final void addParam(final Field param) { + this.params.add(param); + } + + + public final String getName() { + return name; + } + + public final String getDescription() { + return description; + } + + + public final String getType() { + return type; + } + + public final String getNamespace() { + return namespace; + } + + public final List getParams() { + return params; + } +} diff --git a/src/main/java/org/gcube/resourcemanagement/support/shared/plugins/TMPluginFormField.java b/src/main/java/org/gcube/resourcemanagement/support/shared/plugins/TMPluginFormField.java new file mode 100644 index 0000000..2eea1ac --- /dev/null +++ b/src/main/java/org/gcube/resourcemanagement/support/shared/plugins/TMPluginFormField.java @@ -0,0 +1,64 @@ +package org.gcube.resourcemanagement.support.shared.plugins; + +import java.io.Serializable; +/** + * + * @author Massimiliano Assante, ISTI-CNR + * @version 1.0 Oct 2012 + * + */ +@SuppressWarnings("serial") +public class TMPluginFormField implements Serializable { + + private String label; + private String defaultValue; + private boolean required; + private boolean repeatable; + + public TMPluginFormField() { + super(); + } + + public TMPluginFormField(String label, String defaultValue, + boolean required, boolean repeatable) { + super(); + this.label = label; + this.defaultValue = defaultValue; + this.required = required; + this.repeatable = repeatable; + } + + public String getLabel() { + return label; + } + + public void setLabel(String label) { + this.label = label; + } + + public String getDefaultValue() { + return defaultValue; + } + + public void setDefaultValue(String defaultValue) { + this.defaultValue = defaultValue; + } + + public boolean isRequired() { + return required; + } + + public void setRequired(boolean required) { + this.required = required; + } + + public boolean isRepeatable() { + return repeatable; + } + + public void setRepeatable(boolean repeatable) { + this.repeatable = repeatable; + } + + +} diff --git a/src/main/java/org/gcube/resourcemanagement/support/shared/types/RunningMode.java b/src/main/java/org/gcube/resourcemanagement/support/shared/types/RunningMode.java new file mode 100644 index 0000000..69ca900 --- /dev/null +++ b/src/main/java/org/gcube/resourcemanagement/support/shared/types/RunningMode.java @@ -0,0 +1,39 @@ +/**************************************************************************** + * 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: RunningMode.java + **************************************************************************** + * @author Daniele Strollo + ***************************************************************************/ + +package org.gcube.resourcemanagement.support.shared.types; + +import java.io.Serializable; + +/** + * @author Daniele Strollo (ISTI-CNR) + * + */ +public enum RunningMode implements Serializable { + PORTAL("PortalMode"), + STANDALONE("StandaloneMode"), + NOTDEFINED("NotDefined"); + + private String mode = null; + + private RunningMode(final String mode) { + this.mode = mode; + } + @Override + public final String toString() { + return this.mode; + } +} diff --git a/src/main/java/org/gcube/resourcemanagement/support/shared/types/Tuple.java b/src/main/java/org/gcube/resourcemanagement/support/shared/types/Tuple.java new file mode 100644 index 0000000..add7919 --- /dev/null +++ b/src/main/java/org/gcube/resourcemanagement/support/shared/types/Tuple.java @@ -0,0 +1,166 @@ +/**************************************************************************** + * 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: Tuple.java + **************************************************************************** + * @author Daniele Strollo + ***************************************************************************/ + +package org.gcube.resourcemanagement.support.shared.types; + +import java.io.Serializable; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; + +import com.google.gwt.user.client.rpc.IsSerializable; + +/** + * General purpose tuple representation. + * A tuple is a sequence (or ordered list) of finite length. + * + *
+ * Example:
+ *
+ * 1) Creation
+ * // single typed tuple
+ * Tuple<Long> nt = new Tuple<Long>(42L);
+ *
+ * // multi typed tuple
+ * Tuple<Object> ot = new Tuple<Object>("Lars Tackmann",
+ *    		"Age", 26);
+ *
+ * 2) Usage
+ * // get single element
+ * Integer val = (Integer) ot.get(2);
+ * // iterate tuple
+ * for (Object o : ot)
+ *   System.out.printf("'%s' ", o.toString());
+ * // print all elems
+ * System.out.printf("Object tuple: %s\n", ot.toString());
+ *
+ *
+ * 3) Operations
+ * // The elements of two tuples a and b can be joined with
+ * // union operation that returns a new tuple.
+ * Tuple c = a.union (b);
+ * 
+ * + */ +public class Tuple implements Iterable, Serializable, IsSerializable { + private static final long serialVersionUID = 5783359179069297888L; + private List content = new LinkedList(); + + /** + * @deprecated For serialization purpose use the other constructors + */ + public final List getContent() { + return content; + } + + /** + * @deprecated For serialization purpose use the other constructors + */ + public final void setContent(final List content) { + this.content = content; + } + + /** + * @deprecated For serialization purpose use the other constructors + */ + public Tuple() { + super(); + } + + public Tuple(final T... args) { + for (T t : args) { + content.add(t); + } + } + + /** + * Appends elements inside a tuple. + */ + public final void append(final T... args) { + if (content != null) { + for (T t : args) { + content.add(t); + } + } + } + + @SuppressWarnings({ "unchecked", "deprecation" }) + public final Tuple union(final Tuple t) { + Tuple retval = new Tuple(); + for (T elem : content) { + retval.append(elem); + } + for (int i = 0; i < t.size(); i++) { + retval.append(t.get(i)); + } + return retval; + } + + public final T get(final int index) { + return content.get(index); + } + + public final Iterator iterator() { + return content.iterator(); + } + + public final int size() { + return content.size(); + } + + /** + * Compares two tuples. + * The comparison is applied to all the contained elements. + * + * @param obj the {@link Tuple} element to compare + * @return true if the number of contained elements is the same and + * all the elements are equals. + */ + @SuppressWarnings("unchecked") + public final boolean equals(final Object obj) { + if (!(obj instanceof Tuple)) { + return false; + } + Tuple tuple = (Tuple) obj; + if (tuple.size() != this.content.size()) { return false; } + Iterator internalElems = this.content.iterator(); + for (T elem : tuple) { + if (!elem.equals(internalElems.next())) { + return false; + } + } + return true; + } + + @Override + public final int hashCode() { + int retval = 0; + Iterator internalElems = this.content.iterator(); + while (internalElems.hasNext()) { + retval += internalElems.next().hashCode(); + } + return retval; + } + + @Override + public final String toString() { + StringBuilder retval = new StringBuilder(); + for (Object o : content) { + retval.append(o.toString() + "/"); + } + return "(" + retval.substring(0, retval.length() - 1) + ")"; + } +} diff --git a/src/main/java/org/gcube/resourcemanagement/support/shared/UserGroup.java b/src/main/java/org/gcube/resourcemanagement/support/shared/types/UserGroup.java similarity index 95% rename from src/main/java/org/gcube/resourcemanagement/support/shared/UserGroup.java rename to src/main/java/org/gcube/resourcemanagement/support/shared/types/UserGroup.java index d98bfa0..d1699eb 100644 --- a/src/main/java/org/gcube/resourcemanagement/support/shared/UserGroup.java +++ b/src/main/java/org/gcube/resourcemanagement/support/shared/types/UserGroup.java @@ -14,7 +14,7 @@ * @author Daniele Strollo ***************************************************************************/ -package org.gcube.resourcemanagement.support.shared; +package org.gcube.resourcemanagement.support.shared.types; import java.io.Serializable; import com.google.gwt.user.client.rpc.IsSerializable; diff --git a/src/main/java/org/gcube/resourcemanagement/support/shared/types/datamodel/AtomicTreeNode.java b/src/main/java/org/gcube/resourcemanagement/support/shared/types/datamodel/AtomicTreeNode.java new file mode 100644 index 0000000..8916e6c --- /dev/null +++ b/src/main/java/org/gcube/resourcemanagement/support/shared/types/datamodel/AtomicTreeNode.java @@ -0,0 +1,100 @@ +/**************************************************************************** + * 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: AtomicTreeNode.java + **************************************************************************** + * @author Daniele Strollo + ***************************************************************************/ + +package org.gcube.resourcemanagement.support.shared.types.datamodel; + +import com.extjs.gxt.ui.client.data.BaseTreeModel; + +/** + * @author Daniele Strollo (ISTI-CNR) + * + */ +public class AtomicTreeNode extends BaseTreeModel { + private static final long serialVersionUID = 5094327834701967591L; + private static int ID = 0; + + /** + * @deprecated fr serialization only + */ + public AtomicTreeNode() { + set("id", ID++); + } + + public AtomicTreeNode(final String node) { + this(node, null); + } + + /** + * The node is used as original node of the element useful to retrieve it from the IS. + * The name is instead used for pretty printing (aliasing). + * @param node the corresponding IS node + * @param name if null the node will be used instead + */ + public AtomicTreeNode(final String node, final String name) { + set("id", ID++); + set("node", node); + if (name == null) { + set("name", node); + } else { + set("name", name); + } + } + + public AtomicTreeNode(final String node, final String name, final String icon) { + this(node, name); + set("icon", icon); + } + + public AtomicTreeNode(final String node, final String name, final AtomicTreeNode[] children) { + this(node, name); + for (int i = 0; i < children.length; i++) { + add(children[i]); + } + } + + public AtomicTreeNode(final String node, final String name, final String icon, final AtomicTreeNode[] children) { + this(node, name, children); + set("icon", icon); + } + + public final Integer getId() { + return (Integer) get("id"); + } + + public final String getName() { + return (String) get("name"); + } + + public final String getNode() { + return (String) get("node"); + } + + public final String getLabel() { + return (String) get("label"); + } + + public final String toString() { + return getName(); + } + + public final String getSubType() { + if (this.isLeaf() && this.getParent() != null) { + return this.getNode(); + } + return null; + } + +} diff --git a/src/main/java/org/gcube/resourcemanagement/support/shared/types/datamodel/CompleteResourceProfile.java b/src/main/java/org/gcube/resourcemanagement/support/shared/types/datamodel/CompleteResourceProfile.java new file mode 100644 index 0000000..f942de7 --- /dev/null +++ b/src/main/java/org/gcube/resourcemanagement/support/shared/types/datamodel/CompleteResourceProfile.java @@ -0,0 +1,82 @@ +/**************************************************************************** + * 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: ResourceProfile.java + **************************************************************************** + * @author Daniele Strollo + ***************************************************************************/ + +package org.gcube.resourcemanagement.support.shared.types.datamodel; + +import java.io.Serializable; + +import org.gcube.resourcemanagement.support.client.views.ResourceTypeDecorator; + + +import com.google.gwt.user.client.rpc.IsSerializable; + +/** + * The profile of resources is represented by its + * xml and html representations and its title (the name + * or the ID) and the type (GHN, RI, ...). + * @author Daniele Strollo (ISTI-CNR) + * + */ +public class CompleteResourceProfile implements Serializable, IsSerializable { + private static final long serialVersionUID = 1L; + private String xmlRepresentation = null; + private String htmlRepresentation = null; + private ResourceTypeDecorator type = null; + private String title = null; + private String ID = null; + + /** + * @deprecated for serialization only + */ + public CompleteResourceProfile() { + } + + + public CompleteResourceProfile(String ID, ResourceTypeDecorator type, String title, String xmlRepresentation, + String htmlRepresentation) { + super(); + this.ID = ID; + this.type = type; + this.title = title; + this.xmlRepresentation = xmlRepresentation; + this.htmlRepresentation = htmlRepresentation; + } + + + public String getXmlRepresentation() { + return xmlRepresentation; + } + + + public String getHtmlRepresentation() { + return htmlRepresentation; + } + + + public ResourceTypeDecorator getType() { + return type; + } + + + public String getTitle() { + return title; + } + + public String getID() { + return ID; + } + +} diff --git a/src/main/java/org/gcube/resourcemanagement/support/shared/types/datamodel/ResourceDescriptor.java b/src/main/java/org/gcube/resourcemanagement/support/shared/types/datamodel/ResourceDescriptor.java new file mode 100644 index 0000000..6946c5a --- /dev/null +++ b/src/main/java/org/gcube/resourcemanagement/support/shared/types/datamodel/ResourceDescriptor.java @@ -0,0 +1,113 @@ +/**************************************************************************** + * 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: ResourceDescriptor.java + **************************************************************************** + * @author Daniele Strollo + ***************************************************************************/ + +package org.gcube.resourcemanagement.support.shared.types.datamodel; + +import java.io.Serializable; + +import org.gcube.resourcemanagement.support.shared.exceptions.InvalidParameterException; +import org.gcube.resourcemanagement.support.shared.util.Assertion; + + + +import com.extjs.gxt.ui.client.data.BaseModelData; +import com.google.gwt.user.client.rpc.IsSerializable; + +/** + * All the resources that want to access resource management + * operations must be described through this data type. + * @author Daniele Strollo (ISTI-CNR) + */ +public class ResourceDescriptor extends BaseModelData implements Serializable, IsSerializable { + private static final long serialVersionUID = 6435512868470974421L; + + public ResourceDescriptor() { + super(); + } + + /** + * Creates a ResourceDescriptor + * @param type mandatory + * @param subtype can be null + * @param ID the identifier of the resource (mandatory). + * @param name the short name assigned to the resource (mandatory). + * @throws InvalidParameterException + */ + public ResourceDescriptor( + final String type, + final String subtype, + final String id, + final String name) throws InvalidParameterException { + super(); + Assertion checker = new Assertion(); + checker.validate(name != null && name.length() > 0, new InvalidParameterException("The ghnName is null or empty")); + checker.validate(id != null && id.length() > 0, new InvalidParameterException("The ID is null or empty")); + checker.validate(type != null && type.length() > 0, new InvalidParameterException("The type is null or empty")); + + this.setSubtype(subtype); + this.setType(type); + this.setID(id); + this.setName(name); + } + + public final String getType() { + return get("type"); + } + + public final String getSubtype() { + return get("subtype"); + } + + public final String getID() { + return get("ID"); + } + + public final String getName() { + return get("name"); + } + + public final void setType(final String type) { + if (type != null) { + set("type", type.trim()); + } + } + + public final void setSubtype(final String subtype) { + if (subtype != null) { + set("subtype", subtype.trim()); + } + } + + public final void setID(final String id) { + if (id != null) { + set("ID", id.trim()); + } + } + + public final void setName(final String name) { + if (name != null) { + set("name", name.trim()); + } + } + + public final void addProperty(final String property, final Object value) { + set(property, value); + } + + public final Object getProperty(final String property) { + return get(property); + } +} diff --git a/src/main/java/org/gcube/resourcemanagement/support/shared/types/datamodel/ResourceDetailModel.java b/src/main/java/org/gcube/resourcemanagement/support/shared/types/datamodel/ResourceDetailModel.java new file mode 100644 index 0000000..2e155aa --- /dev/null +++ b/src/main/java/org/gcube/resourcemanagement/support/shared/types/datamodel/ResourceDetailModel.java @@ -0,0 +1,824 @@ +/**************************************************************************** + * 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: ResourceDetailDecorator.java + **************************************************************************** + * @author Daniele Strollo + ***************************************************************************/ + +package org.gcube.resourcemanagement.support.shared.types.datamodel; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +import org.gcube.resourcemanagement.support.client.views.ResourceTypeDecorator; + +import com.extjs.gxt.ui.client.Style.HorizontalAlignment; +import com.extjs.gxt.ui.client.data.BaseModelData; +import com.extjs.gxt.ui.client.data.ModelType; +import com.extjs.gxt.ui.client.store.ListStore; +import com.extjs.gxt.ui.client.widget.form.CheckBox; +import com.extjs.gxt.ui.client.widget.form.NumberField; +import com.extjs.gxt.ui.client.widget.grid.CellEditor; +import com.extjs.gxt.ui.client.widget.grid.CheckColumnConfig; +import com.extjs.gxt.ui.client.widget.grid.ColumnConfig; +import com.extjs.gxt.ui.client.widget.grid.ColumnData; +import com.extjs.gxt.ui.client.widget.grid.ColumnModel; +import com.extjs.gxt.ui.client.widget.grid.Grid; +import com.extjs.gxt.ui.client.widget.grid.GridCellRenderer; +import com.google.gwt.core.client.GWT; +import com.google.gwt.i18n.client.NumberFormat; + +/** + * @author Massimiliano Assante (ISTI-CNR) + * @author Daniele Strollo + */ +public class ResourceDetailModel { + private static boolean initialized = false; + + public static final String SERVICE_INSTALL_KEY = "toDeploy"; + + private static HashMap RECORD_DEFINITION = null; + private static HashMap XML_MAPPING = null; + private static HashMap REQUIRED_FIELDS = null; + + private static void init() { + if (initialized) { + return; + } + initialized = true; + + RECORD_DEFINITION = new HashMap(); + XML_MAPPING = new HashMap(); + + /********************************************* + * GHN + ********************************************/ + // The column model for grid representation + List modelColumns = new ArrayList(); + + modelColumns.add(new ColumnConfig("Name", "Name", 250)); + + + + ColumnConfig status = new ColumnConfig("Status", "Status", 65); + + GridCellRenderer statusRender = new GridCellRenderer() { + @Override + public String render(BaseModelData model, String property, ColumnData config, + int rowIndex, int colIndex, ListStore store, Grid grid) { + String statusToCheck = (String)model.get(property); + String style = "gray"; + if (statusToCheck.compareTo("certified") == 0) + style = "green"; + else if (statusToCheck.compareTo("ready") == 0) + style = "orange"; + return "" + statusToCheck + ""; + } + }; + status.setRenderer(statusRender); + modelColumns.add(status); + + modelColumns.add(new ColumnConfig("LastUpdate", "Last Updated", 130)); + modelColumns.add(new ColumnConfig("gCoreVersion", "gCore v.", 50)); + modelColumns.add(new ColumnConfig("ghnVersion", "ghn v.", 40)); + + + ColumnConfig ramLeft = new ColumnConfig("VirtualAvailable", "V. Mem left", 70); + ramLeft.setAlignment(HorizontalAlignment.RIGHT); + ramLeft.setEditor(new CellEditor(new NumberField())); + final NumberFormat number = NumberFormat.getFormat("#,##0;(#,##0)"); + + GridCellRenderer ramRender = new GridCellRenderer() { + @Override + public String render(BaseModelData model, String property, ColumnData config, + int rowIndex, int colIndex, ListStore store, Grid grid) { + int val = Integer.parseInt((String)model.get(property)); + + int tot = Integer.parseInt((String) model.get("VirtualSize")); + + int percentage = (val * 100) / tot; + + String style = val < 100 ? "red" : "green"; + String toDisplay = number.format(val).replaceAll(",", "."); + toDisplay += " MB"; + + return "" + percentage + "% ("+toDisplay+")"; + } + }; + ramLeft.setRenderer(ramRender); + + modelColumns.add(ramLeft); + + + ColumnConfig localSpace = new ColumnConfig("LocalAvailableSpace", "HD Space left", 70); + localSpace.setAlignment(HorizontalAlignment.RIGHT); + localSpace.setEditor(new CellEditor(new NumberField())); + + GridCellRenderer mbRender = new GridCellRenderer() { + @Override + public String render(BaseModelData model, String property, ColumnData config, + int rowIndex, int colIndex, ListStore store, Grid grid) { + int val = Integer.parseInt((String)model.get(property)); + String style = val < 1000000 ? "red" : "green"; + String toDisplay = number.format(val); + if (toDisplay.length() > 4) + toDisplay = toDisplay.substring(0, toDisplay.length()-4).replaceAll(",", "."); + toDisplay += " MB"; + return "" + toDisplay + ""; + } + }; + + localSpace.setRenderer(mbRender); + modelColumns.add(localSpace); + + + //Optional + ColumnConfig ramTotal = new ColumnConfig("VirtualSize", "V. Memory total", 70); + ramTotal.setAlignment(HorizontalAlignment.RIGHT); + ramTotal.setEditor(new CellEditor(new NumberField())); + + GridCellRenderer ramTotRender = new GridCellRenderer() { + @Override + public String render(BaseModelData model, String property, ColumnData config, + int rowIndex, int colIndex, ListStore store, Grid grid) { + int val = Integer.parseInt((String)model.get(property)); + String toDisplay = number.format(val).replaceAll(",", "."); + toDisplay += " MB"; + return toDisplay; + } + }; + ramTotal.setRenderer(ramTotRender); + modelColumns.add(ramTotal); + + modelColumns.add(new ColumnConfig("ID", "ID", 220)); + modelColumns.add(new ColumnConfig("Uptime", "Up Time", 100)); + modelColumns.add(new ColumnConfig("LoadLast15Min", "Load Last 15 Min", 100)); + modelColumns.add(new ColumnConfig("LoadLast1Min", "Load Last 1 Min", 100)); + modelColumns.add(new ColumnConfig("LoadLast5Min", "Load Last 5 Min", 100)); + modelColumns.add(new ColumnConfig("Scopes", "Scopes", 300)); + modelColumns.add(new ColumnConfig("SubType", "SubType", 200)); + + // create the column model + ColumnModel cm = new ColumnModel(modelColumns); + // The hidden fields after the 5 column + for (int i = 7; i < modelColumns.size(); i++) { + cm.setHidden(i, true); + } + RECORD_DEFINITION.put(ResourceTypeDecorator.GHN.name(), cm); + + // defines the xml structure + ModelType type = new ModelType(); + type.setRoot("Resources"); + type.setRecordName("Resource"); + type.addField("ID"); + type.addField("Status"); + type.addField("Name"); + type.addField("Uptime"); + type.addField("LastUpdate"); + type.addField("VirtualAvailable"); + type.addField("VirtualSize"); + type.addField("gCoreVersion", "gcf-version"); + type.addField("ghnVersion", "ghn-version"); + type.addField("LocalAvailableSpace"); + type.addField("LoadLast15Min"); + type.addField("LoadLast1Min"); + type.addField("LoadLast5Min"); + type.addField("Scopes"); + + // These fields are internally used and not showable + type.addField("SubType"); + type.addField("Type"); + + XML_MAPPING.put(ResourceTypeDecorator.GHN.name(), type); + + + /********************************************* + * Collection + ********************************************/ + // The column model for grid representation + modelColumns = new ArrayList(); + modelColumns.add(new ColumnConfig("Name", "Name", 470)); + modelColumns.add(new ColumnConfig("NumberOfMembers", "Cardinality", 70)); + modelColumns.add(new ColumnConfig("LastUpdateTime", "Last Updated", 170)); + modelColumns.add(new ColumnConfig("ID", "ID", 220)); + + //Optional + modelColumns.add(new ColumnConfig("CreationTime", "Creation Time", 170)); + modelColumns.add(new ColumnConfig("Scopes", "Scopes", 300)); + modelColumns.add(new ColumnConfig("SubType", "SubType", 200)); + //modelColumns.add(new ColumnConfig("NumberOfMembers", "Number Of Members", 115)); //not available anymore + + + // create the column model + cm = new ColumnModel(modelColumns); + for (int i = 4; i < modelColumns.size(); i++) { + cm.setHidden(i, true); + } + RECORD_DEFINITION.put(ResourceTypeDecorator.Collection.name(), cm); + + // defines the xml structure + type = new ModelType(); + type.setRoot("Resources"); + type.setRecordName("Resource"); + type.addField("ID"); + type.addField("Name"); + type.addField("CreationTime"); + type.addField("LastUpdateTime"); + type.addField("NumberOfMembers"); + type.addField("Scopes"); + type.addField("SubType"); + type.addField("Type"); + + + XML_MAPPING.put(ResourceTypeDecorator.Collection.name(), type); + + + /********************************************* + * Service + ********************************************/ + // The column model for grid representation + modelColumns = new ArrayList(); + + modelColumns.add(new ColumnConfig("ServiceClass", "Service Class", 200)); + modelColumns.add(new ColumnConfig("ServiceName", "Service Name", 260)); + modelColumns.add(new ColumnConfig("Version", "Main Package Version", 100)); + //Optional + modelColumns.add(new ColumnConfig("Shareable", "Shareable", 100)); + modelColumns.add(new ColumnConfig("Scopes", "Scopes", 300)); + modelColumns.add(new ColumnConfig("ID", "ID", 220)); + modelColumns.add(new ColumnConfig("SubType", "SubType", 200)); + + // create the column model + cm = new ColumnModel(modelColumns); + for (int i = 3; i < modelColumns.size(); i++) { + cm.setHidden(i, true); + } + RECORD_DEFINITION.put(ResourceTypeDecorator.Service.name(), cm); + + // defines the xml structure + type = new ModelType(); + type.setRoot("Resources"); + type.setRecordName("Resource"); + type.addField("ID"); + type.addField("ServiceClass"); + type.addField("ServiceName"); + type.addField("Version"); + type.addField("Shareable"); + type.addField("Scopes"); + + // These fields are internally used and not showable + type.addField("SubType"); + type.addField("Type"); + + + XML_MAPPING.put(ResourceTypeDecorator.Service.name(), type); + + + + /********************************************* + * InstallableSoftware + ********************************************/ + // The column model for grid representation + modelColumns = new ArrayList(); + + // adds the checkbox to the model + CheckColumnConfig checkColumn = + new CheckColumnConfig(ResourceDetailModel.SERVICE_INSTALL_KEY, "Deploy", 60); + CellEditor checkBoxEditor = new CellEditor(new CheckBox()); + checkColumn.setEditor(checkBoxEditor); + modelColumns.add(checkColumn); + + modelColumns.add(new ColumnConfig("ServiceClass", "Service Class", 250)); + modelColumns.add(new ColumnConfig("ServiceName", "Service Name", 250)); + modelColumns.add(new ColumnConfig("Version", "Main Package Version", 100)); + //Optional + modelColumns.add(new ColumnConfig("Scopes", "Scopes", 300)); + modelColumns.add(new ColumnConfig("ID", "ID", 220)); + modelColumns.add(new ColumnConfig("SubType", "SubType", 200)); + + // create the column model + cm = new ColumnModel(modelColumns); + for (int i = 3; i < modelColumns.size(); i++) { + cm.setHidden(i, true); + } + RECORD_DEFINITION.put(ResourceTypeDecorator.InstallableSoftware.name(), cm); + + // defines the xml structure + type = new ModelType(); + type.setRoot("Resources"); + type.setRecordName("Resource"); + type.addField("ID"); + type.addField("ServiceClass"); + type.addField("ServiceName"); + type.addField("Version"); + type.addField("Scopes"); + + // These fields are internally used and not showable + type.addField("SubType"); + type.addField("Type"); + + + XML_MAPPING.put(ResourceTypeDecorator.InstallableSoftware.name(), type); + + + + /********************************************* + * RunningInstance + ********************************************/ + // The column model for grid representation + modelColumns = new ArrayList(); + modelColumns.add(new ColumnConfig("ServiceClass", "Service Class", 200)); + modelColumns.add(new ColumnConfig("ServiceName", "Service Name", 260)); + modelColumns.add(new ColumnConfig("Version", "Version", 100)); + modelColumns.add(new ColumnConfig("Status", "Status", 65)); + modelColumns.add(new ColumnConfig("GHN", "GHN", 300)); + //Optional + modelColumns.add(new ColumnConfig("Scopes", "Scopes", 300)); + modelColumns.add(new ColumnConfig("ID", "ID", 220)); + modelColumns.add(new ColumnConfig("SubType", "SubType", 200)); + + // create the column model + cm = new ColumnModel(modelColumns); + for (int i = 5; i < modelColumns.size(); i++) { + cm.setHidden(i, true); + } + RECORD_DEFINITION.put(ResourceTypeDecorator.RunningInstance.name(), cm); + + // defines the xml structure + type = new ModelType(); + type.setRoot("Resources"); + type.setRecordName("Resource"); + type.addField("ID"); + type.addField("ServiceClass"); + type.addField("ServiceName"); + type.addField("Version"); + type.addField("Status"); + // a) This is the GHNID type.addField("GHN", "/Profile/GHN/@UniqueID"); + // b) While this is its name + type.addField("GHN", "ghn-name"); + type.addField("Scopes"); + + // These fields are internally used and not showable + type.addField("SubType"); + type.addField("Type"); + + + XML_MAPPING.put(ResourceTypeDecorator.RunningInstance.name(), type); + + + + /********************************************* + * VIEW + ********************************************/ + // The column model for grid representation + modelColumns = new ArrayList(); + modelColumns.add(new ColumnConfig("ViewName", "View Name", 200)); + modelColumns.add(new ColumnConfig("SourceKey", "Source Key", 200)); + modelColumns.add(new ColumnConfig("LastUpdate", "Last Update", 270)); + modelColumns.add(new ColumnConfig("Cardinality", "Cardinality", 100)); + //Optional + modelColumns.add(new ColumnConfig("ViewType", "View Type", 170)); + modelColumns.add(new ColumnConfig("RelatedCollectionId", "Related Collection Id", 220)); + modelColumns.add(new ColumnConfig("ServiceName", "Service Name", 170)); + modelColumns.add(new ColumnConfig("Termination", "Termination Time", 270)); + + modelColumns.add(new ColumnConfig("Source", "Source", 230)); + modelColumns.add(new ColumnConfig("ServiceClass", "Service Class", 170)); + modelColumns.add(new ColumnConfig("Scopes", "Scopes", 300)); + modelColumns.add(new ColumnConfig("RI", "RI", 220)); + modelColumns.add(new ColumnConfig("SubType", "SubType", 200)); + + // create the column model + cm = new ColumnModel(modelColumns); + for (int i = 4; i < modelColumns.size(); i++) { + cm.setHidden(i, true); + } + RECORD_DEFINITION.put(ResourceTypeDecorator.VIEW.name(), cm); + + // defines the xml structure + type = new ModelType(); + type.setRoot("Resources"); + type.setRecordName("Resource"); + type.addField("ID"); + type.addField("ViewName"); + type.addField("Cardinality"); + type.addField("ViewType"); + type.addField("RelatedCollectionId"); + type.addField("Source"); + type.addField("SourceKey"); + type.addField("ServiceClass"); + type.addField("ServiceName"); + type.addField("Termination", "TerminationTimeHuman"); + type.addField("LastUpdate", "LastUpdateHuman"); + type.addField("Scopes", "/scopes"); + type.addField("SubType", "SubType"); + type.addField("RI"); + type.addField("Type"); + + + GWT.log("VIew Name: " + ResourceTypeDecorator.VIEW.name()); + XML_MAPPING.put(ResourceTypeDecorator.VIEW.name(), type); + + + /********************************************* + * GenericResource + ********************************************/ + // The column model for grid representation + modelColumns = new ArrayList(); + modelColumns.add(new ColumnConfig("Name", "Name", 500)); + modelColumns.add(new ColumnConfig("Scopes", "Scopes", 300)); + + //Optional + modelColumns.add(new ColumnConfig("ID", "ID", 220)); + modelColumns.add(new ColumnConfig("SubType", "Secondary Type", 170)); + modelColumns.add(new ColumnConfig("SubType", "SubType", 200)); + + // create the column model + cm = new ColumnModel(modelColumns); + for (int i = 2; i < modelColumns.size(); i++) { + cm.setHidden(i, true); + } + RECORD_DEFINITION.put(ResourceTypeDecorator.GenericResource.name(), cm); + + // defines the xml structure + type = new ModelType(); + type.setRoot("Resources"); + type.setRecordName("Resource"); + type.addField("ID"); + type.addField("Name"); + type.addField("Scopes"); + type.addField("SubType"); + type.addField("Type"); + + XML_MAPPING.put(ResourceTypeDecorator.GenericResource.name(), type); + + /********************************************* + * RuntimeResource + ********************************************/ + // The column model for grid representation + modelColumns = new ArrayList(); + modelColumns.add(new ColumnConfig("Name", "Name", 300)); + modelColumns.add(new ColumnConfig("Scopes", "Scopes", 300)); + + //Optional + modelColumns.add(new ColumnConfig("ID", "ID", 220)); + modelColumns.add(new ColumnConfig("SubType", "Category", 370)); + + // create the column model + cm = new ColumnModel(modelColumns); + for (int i = 3; i < modelColumns.size(); i++) { + cm.setHidden(i, true); + } + RECORD_DEFINITION.put(ResourceTypeDecorator.RuntimeResource.name(), cm); + + // defines the xml structure + type = new ModelType(); + type.setRoot("Resources"); + type.setRecordName("Resource"); + type.addField("ID"); + type.addField("Name"); + type.addField("Scopes"); + type.addField("SubType"); + type.addField("Type"); + + XML_MAPPING.put(ResourceTypeDecorator.RuntimeResource.name(), type); + + + + /********************************************* + * WSResources + ********************************************/ + // The column model for grid representation + modelColumns = new ArrayList(); + modelColumns.add(new ColumnConfig("SourceKey", "Source Key", 230)); + modelColumns.add(new ColumnConfig("ServiceName", "Service Name", 170)); + modelColumns.add(new ColumnConfig("Termination", "Termination Time", 270)); + modelColumns.add(new ColumnConfig("LastUpdate", "Last Update", 270)); + //Optional + modelColumns.add(new ColumnConfig("ID", "ID", 220)); + modelColumns.add(new ColumnConfig("Source", "Source", 230)); + modelColumns.add(new ColumnConfig("ServiceClass", "Service Class", 170)); + modelColumns.add(new ColumnConfig("Scopes", "Scopes", 300)); + modelColumns.add(new ColumnConfig("RI", "RI", 220)); + modelColumns.add(new ColumnConfig("SubType", "SubType", 200)); + + // create the column model + cm = new ColumnModel(modelColumns); + for (int i = 4; i < modelColumns.size(); i++) { + cm.setHidden(i, true); + } + RECORD_DEFINITION.put(ResourceTypeDecorator.WSResource.name(), cm); + + // defines the xml structure + type = new ModelType(); + type.setRoot("Resources"); + type.setRecordName(ResourceTypeDecorator.WSResource.name()); + + type.addField("ID"); + type.addField("Source"); + type.addField("SourceKey"); + type.addField("ServiceClass"); + type.addField("ServiceName"); + type.addField("Termination", "TerminationTimeHuman"); + type.addField("LastUpdate", "LastUpdateHuman"); + type.addField("Scopes", "/scopes"); + type.addField("SubType", "SubType"); + type.addField("RI"); + type.addField("Type"); + XML_MAPPING.put(ResourceTypeDecorator.WSResource.name(), type); + + + /********************************************* + * PROFILES OF RELATED RESOURCES: GHN + ********************************************/ + // The column model for grid representation + modelColumns = new ArrayList(); + modelColumns.add(new ColumnConfig("ServiceClass", "Service Class", 200)); + modelColumns.add(new ColumnConfig("ServiceName", "Service Name", 260)); + modelColumns.add(new ColumnConfig("ServiceVersion", "Service Version", 100)); + modelColumns.add(new ColumnConfig("MainVersion", "Main Version", 100)); + modelColumns.add(new ColumnConfig("Status", "Status", 65)); + //Optional + modelColumns.add(new ColumnConfig("ID", "ID", 220)); + + // create the column model + cm = new ColumnModel(modelColumns); + for (int i = 5; i < modelColumns.size(); i++) { + cm.setHidden(i, true); + } + RECORD_DEFINITION.put(ResourceTypeDecorator.GHNRelated.name(), cm); + + // defines the xml structure + type = new ModelType(); + type.setRoot("Resources"); + type.setRecordName("Resource"); + type.addField("ID"); + type.addField("ServiceClass"); + type.addField("ServiceName"); + type.addField("ServiceVersion"); + type.addField("MainVersion"); + type.addField("Status"); + XML_MAPPING.put(ResourceTypeDecorator.GHNRelated.name(), type); + + + /********************************************* + * PROFILES OF RELATED RESOURCES: RunningInstance + ********************************************/ + // The column model for grid representation + modelColumns = new ArrayList(); + modelColumns.add(new ColumnConfig("Key", "Name", 200)); + modelColumns.add(new ColumnConfig("Value", "Value", 260)); + // create the column model + cm = new ColumnModel(modelColumns); + RECORD_DEFINITION.put(ResourceTypeDecorator.RunningInstanceRelated.name(), cm); + + // defines the xml structure + type = new ModelType(); + type.setRoot("Resources"); + type.setRecordName("Resource"); + type.addField("Key"); + type.addField("Value"); + XML_MAPPING.put(ResourceTypeDecorator.RunningInstanceRelated.name(), type); + + + /********************************************* + * PROFILES OF RELATED RESOURCES: Service + ********************************************/ + // The column model for grid representation + modelColumns = new ArrayList(); + modelColumns.add(new ColumnConfig("RIID", "RI ID", 90)); + modelColumns.add(new ColumnConfig("ServiceStatus", "RI Status", 70)); + modelColumns.add(new ColumnConfig("RIVersion", "Serv.Version", 70)); + modelColumns.add(new ColumnConfig("ActivationTime", "RI ActivationTime", 170)); + modelColumns.add(new ColumnConfig("GHNName", "GHN Name", 100)); + modelColumns.add(new ColumnConfig("GHNStatus", "GHN Status", 70)); + + modelColumns.add(new ColumnConfig("GHNID", "GHN ID", 100)); + modelColumns.add(new ColumnConfig("GHNSite", "GHN Site", 100)); + modelColumns.add(new ColumnConfig("GHNLoad15Min", "GHNLoad15Min", 50)); + modelColumns.add(new ColumnConfig("GHNLoad5Min", "GHNLoad5Min", 50)); + modelColumns.add(new ColumnConfig("GHNLoad1Min", "GHNLoad1Min", 50)); + modelColumns.add(new ColumnConfig("GHNActivationTime", "GHNActivationTime", 100)); + modelColumns.add(new ColumnConfig("GHNLastUpdate", "GHNLastUpdate", 100)); + // create the column model + cm = new ColumnModel(modelColumns); + for (int i = 6; i < modelColumns.size(); i++) { + cm.setHidden(i, true); + } + RECORD_DEFINITION.put(ResourceTypeDecorator.ServiceRelated.name(), cm); + + // defines the xml structure + type = new ModelType(); + type.setRoot("Resources"); + type.setRecordName("Resource"); + type.addField("RIID"); + type.addField("ServiceStatus"); + type.addField("ActivationTime"); + type.addField("GHNID"); + type.addField("RIVersion"); + type.addField("GHNName"); + type.addField("GHNSite"); + type.addField("GHNStatus"); + type.addField("GHNLoad15Min"); + type.addField("GHNLoad5Min"); + type.addField("GHNLoad1Min"); + type.addField("GHNActivationTime"); + type.addField("GHNLastUpdate"); + XML_MAPPING.put(ResourceTypeDecorator.ServiceRelated.name(), type); + + /********************************************* + * MODEL FOR SWEEPER GHN + ********************************************/ + // The column model for grid representation + modelColumns = new ArrayList(); + + modelColumns.add(new ColumnConfig("Name", "Name", 130)); + modelColumns.add(new ColumnConfig("Status", "Status", 50)); + ColumnConfig minElapsed = (new ColumnConfig("UpdateMinutesElapsed", "Minutes from Update", 130)); + minElapsed.setAlignment(HorizontalAlignment.CENTER); + modelColumns.add(minElapsed); + modelColumns.add(new ColumnConfig("AllocatedRI", "#RI", 40)); + modelColumns.add(new ColumnConfig("LastUpdate", "LastUpdate", 130)); + + // hidden fields + modelColumns.add(new ColumnConfig("Actions", "Actions", 260)); + modelColumns.add(new ColumnConfig("ID", "ID", 200)); + modelColumns.add(new ColumnConfig("Type", "Type", 100)); + modelColumns.add(new ColumnConfig("Location", "Location", 100)); + modelColumns.add(new ColumnConfig("Domain", "Domain", 100)); + modelColumns.add(new ColumnConfig("IPAddress", "IPAddress", 100)); + modelColumns.add(new ColumnConfig("Scopes", "Scopes", 100)); + + + // create the column model + cm = new ColumnModel(modelColumns); + for (int i = 5; i < modelColumns.size(); i++) { + cm.setHidden(i, true); + } + + + RECORD_DEFINITION.put(ResourceTypeDecorator.Sweeper_GHN.name(), cm); + // defines the xml structure + type = new ModelType(); + type.setRoot("Resources"); + type.setRecordName("Resource"); + type.addField("ID"); + type.addField("Name"); + type.addField("Actions"); + + type.addField("Status"); + type.addField("AllocatedRI"); + type.addField("Type"); + type.addField("Location"); + type.addField("Domain"); + type.addField("IPAddress"); + type.addField("Scopes"); + type.addField("LastUpdate"); + type.addField("UpdateMinutesElapsed"); + XML_MAPPING.put(ResourceTypeDecorator.Sweeper_GHN.name(), type); + + + /********************************************* + * MODEL FOR SWEEPER RI + ********************************************/ + // The column model for grid representation + modelColumns = new ArrayList(); + + modelColumns.add(new ColumnConfig("ID", "ID", 120)); + modelColumns.add(new ColumnConfig("ServiceClass", "ServiceClass", 100)); + modelColumns.add(new ColumnConfig("ServiceName", "ServiceName", 100)); + modelColumns.add(new ColumnConfig("ghnid", "GHN ID", 120)); + + // hidden fields + modelColumns.add(new ColumnConfig("Actions", "Actions", 260)); + modelColumns.add(new ColumnConfig("ServiceStatus", "Status", 90)); + modelColumns.add(new ColumnConfig("ActivationTime", "Activation Time", 140)); + + + // create the column model + cm = new ColumnModel(modelColumns); + for (int i = 4; i < modelColumns.size(); i++) { + cm.setHidden(i, true); + } + + RECORD_DEFINITION.put(ResourceTypeDecorator.Sweeper_RI.name(), cm); + // defines the xml structure + type = new ModelType(); + type.setRoot("Resources"); + type.setRecordName("Resource"); + type.addField("ID"); + type.addField("ServiceStatus"); + type.addField("ServiceClass"); + type.addField("ServiceName"); + type.addField("ActivationTime"); + type.addField("ghnid"); + type.addField("Actions"); + XML_MAPPING.put(ResourceTypeDecorator.Sweeper_RI.name(), type); + + + + /********************************************* + * VALIDATORS + ********************************************/ + /* + * REQUIRED FIELDS + */ + + REQUIRED_FIELDS = new HashMap(); + + REQUIRED_FIELDS.put( + ResourceTypeDecorator.GHN.name(), + new String[] { + "Name", + "SubType", + "ID", + "Scopes" + }); + REQUIRED_FIELDS.put( + ResourceTypeDecorator.Collection.name(), + new String[] { + "Name", + "SubType", + "ID", + "Scopes" + }); + REQUIRED_FIELDS.put( + ResourceTypeDecorator.GenericResource.name(), + new String[] { + "Name", + "SubType", + "ID", + "Scopes" + }); + REQUIRED_FIELDS.put( + ResourceTypeDecorator.RuntimeResource.name(), + new String[] { + "Name", + "SubType", + "ID", + "Scopes" + }); + REQUIRED_FIELDS.put( + ResourceTypeDecorator.VIEW.name(), + new String[] { + "Name", + "SubType", + "ID", + "Scopes" + }); + REQUIRED_FIELDS.put( + ResourceTypeDecorator.RunningInstance.name(), + new String[] { + "ServiceClass", + "ServiceName", + "ID", + "SubType", + "Scopes", + "GHN" + }); + REQUIRED_FIELDS.put( + ResourceTypeDecorator.Service.name(), + new String[] { + "ServiceClass", + "ServiceName", + "ID", + "SubType", + "Scopes" + }); + REQUIRED_FIELDS.put( + ResourceTypeDecorator.WSResource.name(), + new String[] { + "SourceKey", + "ServiceName", + "ID", + "SubType", + "Scopes" + }); + } + + + public static final ColumnModel getRecordDefinition(final String nodeID) { + init(); + return RECORD_DEFINITION.get(nodeID); + } + + public static final ModelType getXMLMapping(final String nodeID) { + init(); + return XML_MAPPING.get(nodeID); + } + + public static final String[] getRequiredFields(final String type) { + init(); + return REQUIRED_FIELDS.get(type); + } +} diff --git a/src/main/java/org/gcube/resourcemanagement/support/shared/util/Assertion.java b/src/main/java/org/gcube/resourcemanagement/support/shared/util/Assertion.java new file mode 100644 index 0000000..5410c00 --- /dev/null +++ b/src/main/java/org/gcube/resourcemanagement/support/shared/util/Assertion.java @@ -0,0 +1,66 @@ +/**************************************************************************** + * 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: Assertion.java + **************************************************************************** + * @author Daniele Strollo + ***************************************************************************/ + +package org.gcube.resourcemanagement.support.shared.util; + +import java.io.Serializable; + +import com.google.gwt.user.client.rpc.IsSerializable; + +/** + * General purpose assertion handler. + * Assertion can be generalized to check a boolean expression and + * to raise an exception in correspondence to a failure happening + * during checking. + *
+ * Example:
+ *
+ *     Assertion<TheExceptionType> assertion = new Assertion<ParamException> ();
+ *     assertion.validate (param != null, new TheExceptionType("invalid parameter null"));
+ *
+ * or, in a more compact form:
+ *    // The exception to throw in case of failure
+ *    // during the evaluation of the expected condition
+ *    new Assertion<TheExceptionType>().validate(
+ *    	i>5,                                                     // The expected boolean condition
+ *    	new TheExceptionType("Parameter must be greater than 5")); //The error message
+ *
+ * 
+ * + * @author Daniele Strollo (ISTI-CNR) + */ +public class Assertion implements Serializable, IsSerializable { + private static final long serialVersionUID = -2007903339251667541L; + + /** + * Makes an assertion and if the expression evaluation fails, throws an + * exception of type T. + *
+	 * Example:
+	 * 	new Assertion<MyException>().validate(whatExpected, new MyException("guard failed"));
+	 * 
+ * @param assertion the boolean expression to evaluate + * @param exc the exception to throw if the condition does not hold + * @throws T the exception extending {@link java.lang.Throwable} + */ + public final void validate(final boolean assertion, final T exc) + throws T { + if (!assertion) { + throw exc; + } + } +} +