Completed code to retrieve and display the resource types as a nested

tree.
This commit is contained in:
Maria Teresa Paratore 2023-11-08 19:49:54 +01:00
parent 0b7a13aa2c
commit 452f19eb74
5 changed files with 211 additions and 89 deletions

View File

@ -3,6 +3,7 @@ package org.gcube.fullstackapps.informationsystemmonitor.service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
@ -14,6 +15,7 @@ import org.gcube.common.authorization.utils.manager.SecretManager;
import org.gcube.common.authorization.utils.manager.SecretManagerProvider;
import org.gcube.common.authorization.utils.secret.JWTSecret;
import org.gcube.common.authorization.utils.secret.Secret;
import org.gcube.fullstackapps.informationsystemmonitor.service.dto.ResourceTypeDTO;
import org.gcube.informationsystem.base.reference.AccessType;
import org.gcube.informationsystem.base.reference.Element;
import org.gcube.informationsystem.contexts.reference.entities.Context;
@ -43,7 +45,7 @@ import org.springframework.stereotype.Service;
public class InformationSystemService {
private static final Logger log = LoggerFactory.getLogger(InformationSystemService.class);
private ArrayList<String> fatherIds;
public void setUma(String umaToken) throws Exception {
log.debug("Set Uma: [umaToken={}]",umaToken);
@ -88,26 +90,79 @@ public class InformationSystemService {
}
return list;
}
/*
* String fakeTree =
"Resource"
+ "__GCubeResource"
+ "___Actor"
+ "____LegalBody"
+ "____Person"
+ "___ConfigurationTemplate"
+ "____Configuration"
+ "___Dataset"
+ "____ConcreteDataset"
+ "___Schema"
+ "___Service"
+ "____EService"
+ "_____RunningPlugin"
+ "____HostingNode"
+ "____VirtualMachine"
+ "____VirtualService"
+ "___Site"
+ "___Software"
+ "____Plugin_ ";
*/
public ArrayList<SimpleTreeNode> getResourceTypesTree() throws Exception {
public ArrayList<ResourceTypeDTO> getResourceTypesTree() throws Exception {
//public ResourceTypeDTO getResourceTypesTree() throws Exception {
String currentCtx = SecretManagerProvider.instance.get().getContext();
//log.debug("getResourceTypes : [currentCtx=]",currentCtx);
ResourceRegistryClient resourceRegistryClient= ResourceRegistryClientFactory.create(currentCtx);
AccessType[] modelTypes = AccessType.getModelTypes();
ModelKnowledge <Type, TypeInformation> modelKnowledge = resourceRegistryClient.getModelKnowledge();
Type resourcesType = modelKnowledge.getTypeByName(AccessType.RESOURCE.getName());
Tree<Type> typeTree = modelKnowledge.getTree(resourcesType.getAccessType());
/*
String rootName = typeTree.getRootNode().getNodeElement().getName();
String rootId = typeTree.getRootNode().getNodeElement().getID().toString();
Set<Node<Type>> children = typeTree.getRootNode().getChildrenNodes();
*/
SimpleTreeMaker treeMaker = new SimpleTreeMaker();
typeTree.elaborate(treeMaker);
return treeMaker.getNodeList();
ArrayList<ResourceTypeDTO> orderedNodes = treeMaker.getNodeList();
fatherIds = new ArrayList<>();
for(ResourceTypeDTO node:orderedNodes) {
if(!node.getChildren().isEmpty()) {
fatherIds.add(node.getId());
}
}
return cleanTree(orderedNodes);
}
private ArrayList<ResourceTypeDTO> cleanTree(ArrayList<ResourceTypeDTO> ordered) {
String currentId = null;
ResourceTypeDTO candidateFatherNode = null;
for(int i=ordered.size()-1; i>=0; i--) {
ResourceTypeDTO currentNode = ordered.get(i);
currentId = currentNode.getId();
if(fatherIds.contains(currentId)) {
for(int j=0; j<ordered.size(); j++) {
Iterator<ResourceTypeDTO> it = ordered.get(j).getChildren().iterator();
while(it.hasNext()) {
candidateFatherNode = it.next();
if(candidateFatherNode.getId().equals(currentId)){
for(ResourceTypeDTO child: currentNode.getChildren()) {
candidateFatherNode.addChild(child);
}
//TODO: check this
ordered.remove(currentNode);
fatherIds.remove(currentId);
}
}
}
}
}
return ordered;
}

View File

@ -1,9 +1,15 @@
package org.gcube.fullstackapps.informationsystemmonitor.service;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.Set;
import org.gcube.fullstackapps.informationsystemmonitor.service.dto.ResourceTypeDTO;
import org.gcube.informationsystem.tree.Node;
import org.gcube.informationsystem.tree.NodeElaborator;
import org.gcube.informationsystem.types.reference.Type;
@ -14,40 +20,80 @@ public class SimpleTreeMaker implements NodeElaborator<Type> {
private static final Logger log = LoggerFactory.getLogger(SimpleTreeMaker.class);
private SimpleTreeNode myNode;
private ResourceTypeDTO myNode;
//private SimpleTreeNode[] children;
//ordered tree node list
private ArrayList<SimpleTreeNode> nodeList;
private ArrayList<ResourceTypeDTO> nodeList;
Map<String, ResourceTypeDTO> nodeMap;
public SimpleTreeMaker() {
//this.myTree = new SimpleTreeNode();
this.nodeList = new ArrayList<ResourceTypeDTO>();
this.nodeMap = new HashMap<>();
}
public SimpleTreeNode getSimpleTree() {
public Map<String, ResourceTypeDTO> getNodeMap() {
return nodeMap;
}
public ResourceTypeDTO getSimpleTree() {
return myNode;
}
public ArrayList<SimpleTreeNode> getNodeList() {
public ArrayList<ResourceTypeDTO> getNodeList() {
return nodeList;
}
@Override
public void elaborate(Node<Type> node, int level) throws Exception {
myNode = new ResourceTypeDTO(node.getNodeElement().getName(), node.getNodeElement().getID().toString());
if(node.getParent()!=null) {
myNode.setFatherId(node.getParent().getNodeElement().getID().toString());
}else {
myNode.setFatherId(null);
}
myNode = new SimpleTreeNode(node.getNodeElement().getName(), node.getNodeElement().getID().toString());
for(Node<Type> nt:node.getChildrenNodes()) {
String name = nt.getNodeElement().getName();
String id = nt.getNodeElement().getID().toString();
SimpleTreeNode cn = new SimpleTreeNode(name, id);
myNode.getChildNodes().add(cn);
ResourceTypeDTO cn = new ResourceTypeDTO(name, id);
cn.setFatherId(nt.getParent().getNodeElement().getID().toString());
myNode.getChildren().add(cn);
}
/*
log.debug("+++++NAME: "+myNode.getValue());
log.debug("+++++NODES: ");
//myTree.showTreeNodes();
log.debug("+++++++++++++++++");
*/
if(!myNode.getChildren().isEmpty()) {
nodeList.add(myNode);
}
////////////////////
cleanDuplicates(nodeList);
return;
}
private void cleanDuplicates(ArrayList<ResourceTypeDTO> allNodes) {
//Map<String, ResourceTypeDTO> tmpMap = new HashMap<>();
for(ResourceTypeDTO node:allNodes) {
if(nodeMap.containsKey(node.getId())) {
nodeMap.remove(node.getId());
}else {
nodeMap.put(node.getId(), node);
}
}
}
private void visitNodes(ResourceTypeDTO node) {
Queue<ResourceTypeDTO> queue = new ArrayDeque<>();
queue.add(node);
ResourceTypeDTO currentNode;
Set<ResourceTypeDTO> alreadyVisited = new HashSet<>();
log.debug("Visited nodes: ");
while (!queue.isEmpty()) {
currentNode = queue.remove();
log.debug(currentNode.getName() + " | ");
alreadyVisited.add(currentNode);
queue.addAll(currentNode.getChildren());
queue.removeAll(alreadyVisited);
}
}
}

View File

@ -1,15 +1,84 @@
package org.gcube.fullstackapps.informationsystemmonitor.service.dto;
import java.util.ArrayDeque;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Set;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ResourceTypeDTO {
private String id;
private static final Logger log = LoggerFactory.getLogger(ResourceTypeDTO.class);
//TODO: vedi se a regime può servire ID
private String name;
private ResourceTypeDTO[] children;
}
private String id;
private String fatherId;
public String getFatherId() {
return fatherId;
}
public void setFatherId(String fatherId) {
this.fatherId = fatherId;
}
private List<ResourceTypeDTO> children;
public ResourceTypeDTO(String name, String id) {
this.name = name;
this.id = id;
this.children = new LinkedList<>();
}
public void addChild(ResourceTypeDTO childNode) {
this.children.add(childNode);
}
public void showTreeNodes() {
visitNodes(this);
}
public String getName() {
return name;
}
public String getId() {
return id;
}
public void setName(String name) {
this.name = name;
}
public void setId(String id) {
this.id = id;
}
public List<ResourceTypeDTO> getChildren() {
return children;
}
public void visitNodes(ResourceTypeDTO node) {
Queue<ResourceTypeDTO> queue = new ArrayDeque<>();
queue.add(node);
ResourceTypeDTO currentNode;
Set<ResourceTypeDTO> alreadyVisited = new HashSet<>();
log.debug("Visited nodes: ");
while (!queue.isEmpty()) {
currentNode = queue.remove();
log.debug(currentNode.getName() + " | ");
alreadyVisited.add(currentNode);
queue.addAll(currentNode.getChildren());
queue.removeAll(alreadyVisited);
}
}
}

View File

@ -2,34 +2,18 @@ package org.gcube.fullstackapps.informationsystemmonitor.web.rest;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import javax.annotation.Nullable;
import org.gcube.com.fasterxml.jackson.databind.JsonNode;
import org.gcube.com.fasterxml.jackson.databind.ObjectMapper;
import org.gcube.common.authorization.utils.manager.SecretManagerProvider;
import org.gcube.fullstackapps.informationsystemmonitor.config.TokenManager;
import org.gcube.fullstackapps.informationsystemmonitor.service.InformationSystemService;
import org.gcube.fullstackapps.informationsystemmonitor.service.SimpleTreeNode;
import org.gcube.fullstackapps.informationsystemmonitor.service.dto.ResourceTypeDTO;
import org.gcube.informationsystem.base.reference.AccessType;
import org.gcube.informationsystem.base.reference.Element;
import org.gcube.informationsystem.contexts.reference.entities.Context;
import org.gcube.informationsystem.model.knowledge.ModelKnowledge;
import org.gcube.informationsystem.model.reference.entities.Resource;
import org.gcube.informationsystem.resourceregistry.client.ResourceRegistryClient;
import org.gcube.informationsystem.resourceregistry.client.ResourceRegistryClientFactory;
import org.gcube.informationsystem.serialization.ElementMapper;
import org.gcube.informationsystem.tree.Node;
import org.gcube.informationsystem.tree.NodeInformation;
import org.gcube.informationsystem.tree.Tree;
import org.gcube.informationsystem.types.knowledge.TypeInformation;
import org.gcube.informationsystem.types.reference.Type;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
@ -41,7 +25,6 @@ import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.util.UriUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.java.Log;
import tech.jhipster.web.util.HeaderUtil;
/**
@ -88,35 +71,15 @@ public class InformationSystemResource {
@GetMapping("/resourcetypes")
public ResponseEntity<String> resourceTypes(@RequestParam @Nullable String currentContext) {
log.debug("Request resource types");
List<ResourceTypeDTO> typeDtos = new ArrayList<ResourceTypeDTO>();
try {
informationSystemService.setUma(createUmaToken(currentContext));
/////////////
/*
Tree<Type> resourceTree = (Tree<Type>) informationSystemService.getResourcesTree();
NodeInformation<Type> ni = resourceTree.getNodeInformation();
log.debug("nodeIdentifier..."+ni.toString());
Node<Type> root = resourceTree.getRootNode();
log.debug("rootStr..."+root.getNodeElement().toString());
*/
////////////
////////PROVA
ArrayList<ResourceTypeDTO> treeNode = informationSystemService.getResourceTypesTree();
ArrayList<SimpleTreeNode> treeNodes = informationSystemService.getResourceTypesTree();
//TODO: costruire ResourceTypeDTO[] a partire da treeNodes
List<Type> resTypes = informationSystemService.getResourceTypes();
Iterator<Type> it = resTypes.iterator();
while (it.hasNext()) {
Type t = it.next();
//at the moment, an empty array
ResourceTypeDTO[] children = new ResourceTypeDTO[0];
ResourceTypeDTO dto = new ResourceTypeDTO(t.getID().toString(),t.getName(),children);
typeDtos.add(dto);
}
ObjectMapper objectMapper = new ObjectMapper();
String sc = objectMapper.writeValueAsString(typeDtos);
//String sc = objectMapper.writeValueAsString(tree);
String sc = objectMapper.writeValueAsString(treeNode);
return ResponseEntity.ok().body(sc);
} catch (Exception e) {
log.error("****ERRORE*************");
@ -127,18 +90,6 @@ public class InformationSystemResource {
}
}
private boolean isInstanceOf(JsonNode jsonNode, String type) {
Set<String> types = getTypeAndSubtypes(type);
String instanceType = jsonNode.get(Element.TYPE_PROPERTY).asText();
return types.contains(instanceType);
}
private Set<String> getTypeAndSubtypes(String type) {
Set<String> set = new HashSet<>();
set.add(type);
return set;
}
/*
* ritorna le istanze delle risorse di un certo tipo

View File

@ -1,3 +1,4 @@
<div class="row">
<div class="d-flex flex-row py-4" id="riga-superiore-controlli">
<div id="btn-group" class="col-md-3 mt-3">