374 lines
14 KiB
Java
374 lines
14 KiB
Java
package org.gcube.informationsystem.service;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.Collection;
|
|
import java.util.Iterator;
|
|
import java.util.List;
|
|
import java.util.UUID;
|
|
|
|
import org.gcube.com.fasterxml.jackson.databind.ObjectMapper;
|
|
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.informationsystem.base.reference.AccessType;
|
|
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.resourceregistry.publisher.ResourceRegistryPublisher;
|
|
import org.gcube.informationsystem.resourceregistry.publisher.ResourceRegistryPublisherFactory;
|
|
import org.gcube.informationsystem.serialization.ElementMapper;
|
|
import org.gcube.informationsystem.service.dto.ContextDTO;
|
|
import org.gcube.informationsystem.service.dto.FacetPropGui;
|
|
import org.gcube.informationsystem.service.dto.FacetPropertyDTO;
|
|
import org.gcube.informationsystem.service.dto.FacetSpecDTO;
|
|
import org.gcube.informationsystem.service.dto.FacetTypeDTO;
|
|
import org.gcube.informationsystem.service.dto.ResourceTypeDTO;
|
|
import org.gcube.informationsystem.tree.Tree;
|
|
import org.gcube.informationsystem.types.PropertyTypeName;
|
|
import org.gcube.informationsystem.types.PropertyTypeName.BaseType;
|
|
import org.gcube.informationsystem.types.impl.entities.FacetTypeImpl;
|
|
import org.gcube.informationsystem.types.impl.entities.ResourceTypeImpl;
|
|
import org.gcube.informationsystem.types.knowledge.TypeInformation;
|
|
import org.gcube.informationsystem.types.reference.Type;
|
|
import org.gcube.informationsystem.types.reference.properties.LinkedEntity;
|
|
import org.gcube.informationsystem.types.reference.properties.PropertyDefinition;
|
|
import org.gcube.informationsystem.utils.UUIDUtility;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import com.jayway.jsonpath.JsonPath;
|
|
import com.nimbusds.jose.shaded.json.JSONUtil;
|
|
|
|
import net.minidev.json.JSONArray;
|
|
|
|
/**
|
|
* Service class for managing InformationSystem resources.
|
|
*/
|
|
@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);
|
|
SecretManagerProvider.instance.reset();
|
|
SecretManager secretManager = new SecretManager();
|
|
Secret secret = new JWTSecret(umaToken);
|
|
secretManager.addSecret(secret);
|
|
secretManager.set();
|
|
SecretManagerProvider.instance.set(secretManager);
|
|
|
|
}
|
|
|
|
|
|
public String executeQueryTemplate(String name) throws Exception {
|
|
ResourceRegistryClient resourceRegistryClient= ResourceRegistryClientFactory.create();
|
|
try {
|
|
String res = resourceRegistryClient.runQueryTemplate(name);
|
|
return res;
|
|
}catch(Exception e) {
|
|
log.error("ERROR IN GETRESOURCETYPES");
|
|
throw e;
|
|
}
|
|
}
|
|
|
|
|
|
private String extractPath(Context ctx) {
|
|
String res = "/"+ctx.getName();
|
|
if(ctx.getParent()==null) {
|
|
return res;
|
|
}
|
|
Context dad = ctx.getParent().getSource();
|
|
res = "/"+ctx.getParent().getSource().getName()+res;
|
|
if(dad.getParent()==null) {
|
|
return res;
|
|
}
|
|
Context gdad = dad.getParent().getSource();
|
|
res = "/"+dad.getParent().getSource().getName()+res;
|
|
if(gdad.getParent()==null) {
|
|
return res;
|
|
}
|
|
Context g2dad = gdad.getParent().getSource();
|
|
res = "/"+gdad.getParent().getSource().getName()+res;
|
|
if(g2dad.getParent()==null) {
|
|
return res;
|
|
}
|
|
return res;
|
|
}
|
|
|
|
|
|
public FacetSpecDTO getFacetSpecification(String facetName) throws Exception {
|
|
|
|
String currentCtx = SecretManagerProvider.instance.get().getContext();
|
|
ResourceRegistryClient resourceRegistryClient= ResourceRegistryClientFactory.create(currentCtx);
|
|
String jsonResult = resourceRegistryClient.getType(facetName, true);
|
|
ObjectMapper objectMapper = new ObjectMapper();
|
|
//resourceRegistryClient returns an array of one element
|
|
FacetTypeImpl ft = objectMapper.readValue(jsonResult, FacetTypeImpl[].class)[0];
|
|
ArrayList<FacetPropertyDTO> properties = new ArrayList<FacetPropertyDTO>();
|
|
ArrayList<FacetPropGui> propsGui = new ArrayList<FacetPropGui>();
|
|
FacetSpecDTO fsdto = new FacetSpecDTO();
|
|
fsdto.setName(ft.getName());
|
|
fsdto.setDescription(ft.getDescription());
|
|
|
|
if(ft.getProperties()!=null ) {
|
|
for(PropertyDefinition pd:ft.getProperties() ) {
|
|
try {
|
|
FacetPropertyDTO prop = new FacetPropertyDTO();
|
|
prop.setDescription(pd.getDescription());
|
|
prop.setMandatory(pd.isMandatory());
|
|
//prop.setMax(pd.getMax());
|
|
//prop.setMin(pd.getMin());
|
|
prop.setName(pd.getName());
|
|
prop.setNotnull(pd.isNotnull());
|
|
prop.setPropertyType(pd.getPropertyType());
|
|
//prop.setReadonly(pd.isReadonly());
|
|
prop.setRegexp(pd.getRegexp());
|
|
prop.setType(pd.getTypeName());
|
|
propsGui.add(prop.toFacetPropGui());
|
|
}catch(Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}
|
|
//fsdto.setProperties(properties);
|
|
fsdto.setGuiProps(propsGui);
|
|
return fsdto;
|
|
}
|
|
|
|
|
|
public ResourceTypeDTO getResourceType(String typeName) throws Exception {
|
|
|
|
String currentCtx = SecretManagerProvider.instance.get().getContext();
|
|
log.debug("getResourceType : [currentCtx=]",currentCtx);
|
|
ResourceRegistryClient resourceRegistryClient= ResourceRegistryClientFactory.create(currentCtx);
|
|
//questo è un array di un elemento
|
|
String jsonResult = resourceRegistryClient.getType(typeName, true);
|
|
ObjectMapper objectMapper = new ObjectMapper();
|
|
//resourceRegistryClient returns an array of one element
|
|
ResourceTypeImpl rt = objectMapper.readValue(jsonResult, ResourceTypeImpl[].class)[0];
|
|
String name = rt.getName();
|
|
String id = rt.getID().toString();
|
|
String desc = rt.getDescription();
|
|
boolean abst = rt.isAbstract();
|
|
ResourceTypeDTO dto = new ResourceTypeDTO(name, id, desc, abst);
|
|
List<LinkedEntity> fcts = rt.getFacets();
|
|
if(fcts!=null) {
|
|
ArrayList<FacetTypeDTO> facetTypes = new ArrayList<FacetTypeDTO>();
|
|
//TODO: RIEMPI LE FACET
|
|
for(LinkedEntity le: fcts) {
|
|
FacetTypeDTO ftDto = new FacetTypeDTO(le.getRelation(),le.getTarget(),le.getDescription(),le.getMin(),le.getMax());
|
|
facetTypes.add(new FacetTypeDTO(le.getRelation(),le.getTarget(),le.getDescription(),le.getMin(),le.getMax()));
|
|
}
|
|
dto.setFacetTypes(facetTypes);
|
|
}
|
|
return dto;
|
|
}
|
|
|
|
|
|
|
|
public String[] getFacetRelationsOptions(String parentRelation) throws Exception {
|
|
|
|
JSONArray res = new JSONArray();
|
|
String tmp = this.getResourceTypeJson(parentRelation);
|
|
try {
|
|
res = JsonPath.parse(tmp).read("$[*].name");
|
|
}catch(Exception e) {
|
|
log.error(e.getMessage());
|
|
e.printStackTrace();
|
|
}
|
|
|
|
return this.toStringArray(res);
|
|
}
|
|
|
|
private String[] toStringArray(JSONArray jsonArray) {
|
|
ArrayList<String> list = new ArrayList<>();
|
|
for (int i = 0; i < jsonArray.size(); i++) {
|
|
String str = jsonArray.get(i).toString();
|
|
list.add(str);
|
|
}
|
|
return list.toArray(new String[0]);
|
|
}
|
|
|
|
|
|
/*
|
|
//questa ritorna anche le facet figlie che possono essere usate nella select per il campo relation
|
|
//in caso si parta da zero si sceglie ConsistsOf
|
|
//String jsonResult = resourceRegistryClient.getType("IsIdentifiedBy", true);
|
|
*/
|
|
public String getResourceTypeJson(String typeName) throws Exception {
|
|
String currentCtx = SecretManagerProvider.instance.get().getContext();
|
|
ResourceRegistryClient resourceRegistryClient= ResourceRegistryClientFactory.create(currentCtx);
|
|
String jsonResult = resourceRegistryClient.getType(typeName, true);
|
|
return jsonResult;
|
|
}
|
|
|
|
|
|
public List<ContextDTO> getAllContexts() throws Exception {
|
|
ArrayList<ContextDTO> res = new ArrayList<ContextDTO>();
|
|
//log.debug("GetAllContext: [rootCtx=]",rootCtx);
|
|
ResourceRegistryClient resourceRegistryClient= ResourceRegistryClientFactory.create();
|
|
List<Context>contexts=resourceRegistryClient.getAllContext();
|
|
for(Context ctx:contexts) {
|
|
String pnt = " - "; //for the root
|
|
//TODO: da dove tiro fuori il path?
|
|
ContextDTO dto = new ContextDTO();
|
|
dto.setId(ctx.getID().toString());
|
|
dto.setName(ctx.getName());
|
|
if(ctx.getParent()!=null) {
|
|
if(ctx.getParent().getSource()!=null) {
|
|
pnt = ctx.getParent().getSource().getName();
|
|
}
|
|
}
|
|
dto.setParent(pnt);
|
|
dto.setPath(extractPath(ctx));
|
|
res.add(dto);
|
|
}
|
|
log.debug("AllContexts: {}",contexts);
|
|
return res;
|
|
}
|
|
|
|
/*
|
|
* per prendere tutti i tipi (albero a sin)
|
|
*/
|
|
public List<Type> getResourceTypes() throws Exception {
|
|
|
|
String currentCtx = SecretManagerProvider.instance.get().getContext();
|
|
log.debug("getResourceTypes : [currentCtx=]",currentCtx);
|
|
ResourceRegistryClient resourceRegistryClient= ResourceRegistryClientFactory.create(currentCtx);
|
|
List<Type> types = resourceRegistryClient.getType(Resource.class, true);
|
|
log.debug("getResourceTypes:",types);
|
|
return types;
|
|
|
|
}
|
|
|
|
public ArrayList<ResourceTypeDTO> getResourceTypesTree() throws Exception {
|
|
String currentCtx = SecretManagerProvider.instance.get().getContext();
|
|
ResourceRegistryClient resourceRegistryClient= ResourceRegistryClientFactory.create(currentCtx);
|
|
ModelKnowledge <Type, TypeInformation> modelKnowledge = resourceRegistryClient.getModelKnowledge();
|
|
Type resourcesType = modelKnowledge.getTypeByName(AccessType.RESOURCE.getName());
|
|
Tree<Type> typeTree = modelKnowledge.getTree(resourcesType.getAccessType());
|
|
SimpleTreeMaker treeMaker = new SimpleTreeMaker();
|
|
typeTree.elaborate(treeMaker);
|
|
ArrayList<ResourceTypeDTO> orderedNodes = treeMaker.getNodeList();
|
|
fatherIds = new ArrayList<>();
|
|
for(ResourceTypeDTO node:orderedNodes) {
|
|
//to set HostingNode as default type in the resources tree
|
|
// if(node.getName().equals(Constants.RES_TYPE_HOSTINGNODE)) {
|
|
// node.setIsSelected(true);
|
|
// }
|
|
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;
|
|
}
|
|
|
|
|
|
/*
|
|
* Fetches the instance of a certain type for a given UUID
|
|
*/
|
|
public String getResource(String type, String uid) throws Exception {
|
|
String raw = "";
|
|
String currentCtx = SecretManagerProvider.instance.get().getContext();
|
|
ResourceRegistryClient resourceRegistryClient= ResourceRegistryClientFactory.create(currentCtx);
|
|
resourceRegistryClient.setIncludeMeta(false);
|
|
UUID coso = UUID.fromString(uid);
|
|
raw = resourceRegistryClient.getInstance(type, coso);
|
|
return raw;
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
* Fetches all the resource instances for a given type
|
|
*/
|
|
public List<Resource> getResourceInstances(String resourceType) throws Exception {
|
|
String currentCtx = SecretManagerProvider.instance.get().getContext();
|
|
List<Resource> instancesAsObject;
|
|
log.debug("getResourceInstances : [currentCtx=]",currentCtx);
|
|
ResourceRegistryClient resourceRegistryClient = ResourceRegistryClientFactory.create(currentCtx);
|
|
resourceRegistryClient.setIncludeMeta(true);
|
|
//this fetches all the instances (otherwise 10 is the default value)
|
|
resourceRegistryClient.setOffset(0);
|
|
resourceRegistryClient.setLimit(-1);
|
|
String instances = resourceRegistryClient.getInstances(resourceType, false);
|
|
instancesAsObject = ElementMapper.unmarshalList(Resource.class, instances);
|
|
return instancesAsObject;
|
|
}
|
|
|
|
|
|
|
|
//CRUD - 1
|
|
public void createResourceInstance(String resourceType,String jsonDescription) throws Exception {
|
|
String currentCtx = SecretManagerProvider.instance.get().getContext();
|
|
ResourceRegistryPublisher publisher = ResourceRegistryPublisherFactory.create(currentCtx);
|
|
//String jsonDescription="";
|
|
publisher.createResource(jsonDescription);
|
|
//publisher.createResource(new Resource)
|
|
|
|
}
|
|
|
|
//CRUD - 2
|
|
public void updateResourceInstance(String resourceType, String uuid) throws Exception {
|
|
String currentCtx = SecretManagerProvider.instance.get().getContext();
|
|
//TODO: uso la getResource (riga 206)? Però restituisce una String (JSON)...
|
|
//publisher.update(resourceTypeString, jsonString);
|
|
}
|
|
|
|
//CRUD - 3
|
|
public boolean deleteResourceInstance(String resourceType, String uuid) throws Exception {
|
|
String currentCtx = SecretManagerProvider.instance.get().getContext();
|
|
ResourceRegistryPublisher publisher = ResourceRegistryPublisherFactory.create(currentCtx);
|
|
UUIDUtility uuidUtil = new UUIDUtility();
|
|
return publisher.deleteResource(resourceType, uuidUtil.fromString(uuid));
|
|
|
|
//TODO: GESTIRE ERRORI!
|
|
}
|
|
|
|
//CRUD - 4
|
|
//TODO: vedi se questo va passato da un REST di JHipster
|
|
public void retrieveResourceInstance(String resourceType) throws Exception {
|
|
String currentCtx = SecretManagerProvider.instance.get().getContext();
|
|
ResourceRegistryPublisher publisher = ResourceRegistryPublisherFactory.create(currentCtx);
|
|
//publisher.addResourceToContext(null, null, null)
|
|
//TODO: quale metodo usare per ottenere una risorsa partendo da UUID?
|
|
//publisher.delete(resourceType, uuidString);
|
|
}
|
|
|
|
|
|
}
|