From 2d5c9247cc0c5e220d4c8c173499024dbdaa1909 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Wed, 26 Jun 2024 17:14:42 +0200 Subject: [PATCH] Added facility class for Context --- .../contexts/impl/entities/GCubeContext.java | 182 ++++++++++++++++++ .../impl/properties/BasicInformation.java | 80 ++++++++ .../entities/facets/PolymorphismTest.java | 48 +++++ 3 files changed, 310 insertions(+) create mode 100644 src/main/java/org/gcube/resourcemanagement/contexts/impl/entities/GCubeContext.java create mode 100644 src/main/java/org/gcube/resourcemanagement/contexts/impl/properties/BasicInformation.java diff --git a/src/main/java/org/gcube/resourcemanagement/contexts/impl/entities/GCubeContext.java b/src/main/java/org/gcube/resourcemanagement/contexts/impl/entities/GCubeContext.java new file mode 100644 index 0000000..ada13ac --- /dev/null +++ b/src/main/java/org/gcube/resourcemanagement/contexts/impl/entities/GCubeContext.java @@ -0,0 +1,182 @@ +package org.gcube.resourcemanagement.contexts.impl.entities; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.SortedSet; +import java.util.TreeSet; +import java.util.UUID; + +import org.gcube.com.fasterxml.jackson.annotation.JsonGetter; +import org.gcube.com.fasterxml.jackson.annotation.JsonIgnore; +import org.gcube.com.fasterxml.jackson.annotation.JsonInclude; +import org.gcube.com.fasterxml.jackson.annotation.JsonSetter; +import org.gcube.com.fasterxml.jackson.annotation.JsonTypeName; +import org.gcube.informationsystem.contexts.impl.entities.ContextImpl; +import org.gcube.informationsystem.contexts.reference.entities.Context; +import org.gcube.informationsystem.model.reference.properties.Event; +import org.gcube.resourcemanagement.contexts.impl.properties.BasicInformation; + +/** + * @author Luca Frosini (ISTI - CNR) + */ +@JsonTypeName(value=Context.NAME) +@JsonInclude(JsonInclude.Include.NON_NULL) +public class GCubeContext extends ContextImpl { + + /** + * Generated Serial Version UID + */ + private static final long serialVersionUID = -8929392680534884169L; + + /** + * The events occurred to the Contexts. + * creation, renaming, parent change. + * Some of the event are managed by the resource-registry. + * Others can be added by an authorized client. + * This create a sort of journal. See #27707 + */ + public static final String EVENTS_PROPERTY = "events"; + + /** + * It contains the basic information for the context + */ + public static final String INFORMATION_PROPERTY = "information"; + + /** + * This information is provided to allowed user only (by role) + * The symmetric key for the context + */ + public static final String KEY_PROPERTY = "key"; + + /** + * { + * ... + * "availableAt" : [ + * "https://i-marine.d4science.org/group/alienandinvasivespecies", + * "https://services.d4science.org/group/alienandinvasivespecies" + * ] + * ... + * } + * + * For non VRE context this field could be null or could have multiple value. + * For VRE it is normally one value only (but some exception could exists) + */ + public static final String AVAILABLE_AT_PROPERTY = "availableAt"; + + protected SortedSet events; + protected BasicInformation information; + protected String key; + protected List availableAt; + + + + public GCubeContext(Context c) { + this.uuid = c.getID(); + this.metadata = c.getMetadata(); + + this.name = c.getName(); + + this.parent = c.getParent(); + this.children = c.getChildren(); + + this.state = c.getState(); + + this.additionalProperties = new HashMap<>(); + Map ap = c.getAdditionalProperties(); + for(String key : ap.keySet()) { + Object obj = ap.get(key); + switch (key) { + case EVENTS_PROPERTY: + @SuppressWarnings("unchecked") + SortedSet events = (SortedSet) obj; + setEvents(events); + break; + + case KEY_PROPERTY: + setKey((String) obj); + break; + + case INFORMATION_PROPERTY: + setInformation((BasicInformation) obj); + break; + + case AVAILABLE_AT_PROPERTY: + @SuppressWarnings("unchecked") + List availableAt = (List) obj; + setAvailableAt(availableAt); + break; + + default: + this.additionalProperties.put(key, obj); + break; + } + } + + } + + protected GCubeContext() { + super(); + } + + public GCubeContext(UUID uuid) { + this(null, uuid); + } + + public GCubeContext(String name) { + this(name, null); + } + + public GCubeContext(String name, UUID uuid) { + super(name, uuid); + } + + @JsonGetter(EVENTS_PROPERTY) + public SortedSet getEvents() { + return events; + } + + @JsonSetter(EVENTS_PROPERTY) + public void setEvents(SortedSet events) { + this.events = events; + } + + @JsonIgnore + public void addEvent(Event event) { + if(this.events==null) { + this.events = new TreeSet<>(); + } + this.events.add(event); + } + + @JsonGetter(INFORMATION_PROPERTY) + public BasicInformation getInformation() { + return information; + } + + @JsonSetter(INFORMATION_PROPERTY) + public void setInformation(BasicInformation information) { + this.information = information; + } + + @JsonGetter(KEY_PROPERTY) + public String getKey() { + return key; + } + + @JsonSetter(KEY_PROPERTY) + public void setKey(String key) { + this.key = key; + } + + @JsonGetter(AVAILABLE_AT_PROPERTY) + public List getAvailableAt() { + return availableAt; + } + + @JsonSetter(AVAILABLE_AT_PROPERTY) + public void setAvailableAt(List availableAt) { + this.availableAt = availableAt; + } + +} diff --git a/src/main/java/org/gcube/resourcemanagement/contexts/impl/properties/BasicInformation.java b/src/main/java/org/gcube/resourcemanagement/contexts/impl/properties/BasicInformation.java new file mode 100644 index 0000000..960372c --- /dev/null +++ b/src/main/java/org/gcube/resourcemanagement/contexts/impl/properties/BasicInformation.java @@ -0,0 +1,80 @@ +package org.gcube.resourcemanagement.contexts.impl.properties; + +import java.util.Date; +import java.util.HashSet; +import java.util.Set; + +import org.gcube.com.fasterxml.jackson.annotation.JsonFormat; +import org.gcube.informationsystem.base.reference.Element; + +/** + * @author Luca Frosini (ISTI - CNR) + */ +public class BasicInformation { + + protected String description; + protected Set designers; + protected Set managers; + protected Date from; + /** + * Can be null. It means no end date defined. + */ + protected Date to; + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public Set getDesigners() { + return designers; + } + + public void setDesigners(Set designers) { + this.designers = designers; + } + + public void addDesigner(String designer) { + if(this.designers==null) { + this.designers = new HashSet<>(); + } + this.designers.add(designer); + } + + public Set getManagers() { + return managers; + } + + public void setManagers(Set managers) { + this.managers = managers; + } + + public void addManager(String manager) { + if(this.managers==null) { + this.managers = new HashSet<>(); + } + this.managers.add(manager); + } + + @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = Element.DATETIME_PATTERN) + public Date getFrom() { + return from; + } + + public void setFrom(Date from) { + this.from = from; + } + + @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = Element.DATETIME_PATTERN) + public Date getTo() { + return to; + } + + public void setTo(Date to) { + this.to = to; + } + +} diff --git a/src/test/java/org/gcube/resourcemanagement/model/reference/entities/facets/PolymorphismTest.java b/src/test/java/org/gcube/resourcemanagement/model/reference/entities/facets/PolymorphismTest.java index e729545..adc6ee3 100644 --- a/src/test/java/org/gcube/resourcemanagement/model/reference/entities/facets/PolymorphismTest.java +++ b/src/test/java/org/gcube/resourcemanagement/model/reference/entities/facets/PolymorphismTest.java @@ -4,19 +4,27 @@ package org.gcube.resourcemanagement.model.reference.entities.facets; import java.io.IOException; +import java.util.Calendar; import java.util.List; +import java.util.UUID; import org.gcube.com.fasterxml.jackson.core.JsonParseException; import org.gcube.com.fasterxml.jackson.databind.JsonMappingException; import org.gcube.informationsystem.base.reference.Element; import org.gcube.informationsystem.base.reference.IdentifiableElement; +import org.gcube.informationsystem.contexts.impl.entities.ContextImpl; +import org.gcube.informationsystem.contexts.reference.entities.Context; +import org.gcube.informationsystem.model.impl.properties.EventImpl; import org.gcube.informationsystem.model.impl.properties.MetadataImpl; import org.gcube.informationsystem.model.reference.ModelElement; import org.gcube.informationsystem.model.reference.entities.Facet; import org.gcube.informationsystem.model.reference.entities.Resource; +import org.gcube.informationsystem.model.reference.properties.Event; import org.gcube.informationsystem.model.reference.properties.Metadata; import org.gcube.informationsystem.model.reference.relations.ConsistsOf; import org.gcube.informationsystem.serialization.ElementMapper; +import org.gcube.resourcemanagement.contexts.impl.entities.GCubeContext; +import org.gcube.resourcemanagement.contexts.impl.properties.BasicInformation; import org.gcube.resourcemanagement.model.impl.entities.resources.EServiceImpl; import org.gcube.resourcemanagement.model.impl.entities.resources.HostingNodeImpl; import org.gcube.resourcemanagement.model.impl.relations.isrelatedto.ActivatesImpl; @@ -109,4 +117,44 @@ public class PolymorphismTest { public void testUnmarshalling() throws JsonParseException, JsonMappingException, IOException { ElementMapper.unmarshal(Activates.class, AUX); } + + + @Test + public void testGCubeContextImplementation() throws Exception { + Context c = new ContextImpl("test"); + c.setID(UUID.randomUUID()); + c.setState("created"); + logger.debug(c.toString()); + + GCubeContext gcubeContext = new GCubeContext(c); + Event created = new EventImpl(); + created.setWhat(c.getState()); + created.setWho("luca.frosini"); + Calendar cal = Calendar.getInstance(); + cal.add(Calendar.MINUTE, -1); + created.setWhen(cal.getTime()); + gcubeContext.addEvent(created); + + + gcubeContext.setState("pending"); + Event pending = new EventImpl(); + pending.setWhat(gcubeContext.getState()); + pending.setWho("luca.frosini"); + pending.setWhen(Calendar.getInstance().getTime()); + pending.setAdditionalProperty("report", "This is a report"); + + + BasicInformation info = new BasicInformation(); + info.addDesigner("luca.frosini"); + info.addManager("luca.frosini"); + info.addManager("pasquale.pagano"); + Calendar from = Calendar.getInstance(); + info.setFrom(from.getTime()); + info.setDescription("This is a test VRE"); + gcubeContext.setInformation(info); + + gcubeContext.addEvent(pending); + logger.debug(gcubeContext.toString()); + } + }