View Javadoc

1   /*
2    * Copyright 2015 Data Archiving and Networked Services (an institute of
3    * Koninklijke Nederlandse Akademie van Wetenschappen), King's College London,
4    * Georg-August-Universitaet Goettingen Stiftung Oeffentlichen Rechts
5    *
6    * Licensed under the EUPL, Version 1.1 or – as soon they will be approved by
7    * the European Commission - subsequent versions of the EUPL (the "Licence");
8    * You may not use this work except in compliance with the Licence.
9    * You may obtain a copy of the Licence at:
10   *
11   * https://joinup.ec.europa.eu/software/page/eupl
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the Licence is distributed on an "AS IS" basis,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the Licence for the specific language governing
17   * permissions and limitations under the Licence.
18   */
19  
20  package eu.ehri.project.models.events;
21  
22  import com.tinkerpop.blueprints.Direction;
23  import com.tinkerpop.blueprints.Vertex;
24  import com.tinkerpop.frames.Adjacency;
25  import com.tinkerpop.frames.Property;
26  import com.tinkerpop.frames.modules.javahandler.JavaHandler;
27  import com.tinkerpop.frames.modules.javahandler.JavaHandlerContext;
28  import com.tinkerpop.pipes.util.Pipeline;
29  import eu.ehri.project.definitions.Ontology;
30  import eu.ehri.project.models.EntityClass;
31  import eu.ehri.project.models.annotations.EntityType;
32  import eu.ehri.project.models.annotations.Fetch;
33  import eu.ehri.project.models.annotations.Mandatory;
34  import eu.ehri.project.models.base.Accessible;
35  import eu.ehri.project.models.utils.JavaHandlerUtils;
36  
37  /**
38   * Frame class representing a serialized version of
39   * some other node.
40   */
41  @EntityType(EntityClass.VERSION)
42  public interface Version extends Accessible {
43  
44      /**
45       * Fetch the class of the entity that this version pertains to.
46       *
47       * @return an entity class enum value
48       */
49      @Mandatory
50      @Property(Ontology.VERSION_ENTITY_CLASS)
51      String getEntityType();
52  
53      /**
54       * Fetch the ID of the entity that this version pertains to.
55       *
56       * @return an ID string
57       */
58      @Mandatory
59      @Property(Ontology.VERSION_ENTITY_ID)
60      String getEntityId();
61  
62      /**
63       * Fetch a serialized snapshot of the item's data in JSON format.
64       *
65       * @return JSON data representing a sub-graph
66       */
67      @Property(Ontology.VERSION_ENTITY_DATA)
68      String getEntityData();
69  
70      /**
71       * Fetch the event that triggered this version.
72       *
73       * @return a system event instance
74       */
75      @Fetch(value = Ontology.VERSION_HAS_EVENT, ifLevel = 0)
76      @Adjacency(label = Ontology.VERSION_HAS_EVENT, direction = Direction.OUT)
77      SystemEvent getTriggeringEvent();
78  
79      /**
80       * Loops up through the chain of versions until the latest and fetches
81       * the item to which the events refer. If it has been
82       *
83       * @return the entity to which this version refers.
84       */
85      @JavaHandler
86      Accessible getEntity();
87  
88      /**
89       * Implementation of complex methods.
90       */
91      abstract class Impl implements JavaHandlerContext<Vertex>, Version {
92          public Accessible getEntity() {
93              Pipeline<Vertex,Vertex> out =  gremlin().as("n").in(Ontology.ENTITY_HAS_PRIOR_VERSION)
94                      .loop("n", JavaHandlerUtils.noopLoopFunc,
95                              vertexLoopBundle -> !vertexLoopBundle.getObject().getVertices(Direction.IN,
96                              Ontology.ENTITY_HAS_PRIOR_VERSION).iterator().hasNext());
97              return (Accessible)(out.hasNext() ? frame(out.next()) : null);
98          }
99      }
100 }