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;
21  
22  import com.tinkerpop.blueprints.Direction;
23  import com.tinkerpop.blueprints.Vertex;
24  import com.tinkerpop.frames.Adjacency;
25  import com.tinkerpop.frames.modules.javahandler.JavaHandler;
26  import com.tinkerpop.frames.modules.javahandler.JavaHandlerContext;
27  import com.tinkerpop.pipes.util.Pipeline;
28  import eu.ehri.project.definitions.Ontology;
29  import eu.ehri.project.models.annotations.EntityType;
30  import eu.ehri.project.models.annotations.Fetch;
31  import eu.ehri.project.models.annotations.Mandatory;
32  import eu.ehri.project.models.annotations.Meta;
33  import eu.ehri.project.models.base.Annotatable;
34  import eu.ehri.project.models.base.Described;
35  import eu.ehri.project.models.base.ItemHolder;
36  import eu.ehri.project.models.base.Versioned;
37  import eu.ehri.project.models.base.Watchable;
38  import eu.ehri.project.models.utils.JavaHandlerUtils;
39  
40  
41  /**
42   * A frame class for graph nodes representing repository
43   * items.
44   */
45  @EntityType(EntityClass.REPOSITORY)
46  public interface Repository extends Described, ItemHolder, Watchable, Versioned, Annotatable {
47  
48      /**
49       * Count the number of top-level documentary unit items within
50       * this repository.
51       *
52       * @return the number of top-level items
53       */
54      @Meta(CHILD_COUNT)
55      @JavaHandler
56      int getChildCount();
57  
58      /**
59       * Fetch all top-level documentary unit items within this
60       * repository.
61       *
62       * @return an iterable of top-level items
63       */
64      @Adjacency(label = Ontology.DOC_HELD_BY_REPOSITORY, direction = Direction.IN)
65      Iterable<DocumentaryUnit> getTopLevelDocumentaryUnits();
66  
67      /**
68       * Fetch items at <b>all</b> levels (including children of top-level
69       * items and their children, recursively.)
70       *
71       * @return an iterable of documentary unit items
72       */
73      @JavaHandler
74      Iterable<DocumentaryUnit> getAllDocumentaryUnits();
75  
76      /**
77       * Add a documentary unit as a top-level item in this
78       * repository.
79       *
80       * @param unit a documentary unit item
81       */
82      @JavaHandler
83      void addTopLevelDocumentaryUnit(DocumentaryUnit unit);
84  
85      /**
86       * Fetch the country in which this repository resides.
87       *
88       * @return a country frame
89       */
90      @Mandatory
91      @Fetch(Ontology.REPOSITORY_HAS_COUNTRY)
92      @Adjacency(label = Ontology.REPOSITORY_HAS_COUNTRY, direction = Direction.OUT)
93      Country getCountry();
94  
95      /**
96       * The the country in which this repository resides.
97       *
98       * @param country a country frame
99       */
100     @JavaHandler
101     void setCountry(Country country);
102 
103     /**
104      * Implementation of complex methods.
105      */
106     abstract class Impl implements JavaHandlerContext<Vertex>, Repository {
107 
108         public int getChildCount() {
109             return Math.toIntExact(gremlin().inE(Ontology.DOC_HELD_BY_REPOSITORY).count());
110         }
111 
112         public void addTopLevelDocumentaryUnit(DocumentaryUnit unit) {
113             JavaHandlerUtils.addSingleRelationship(unit.asVertex(), it(),
114                     Ontology.DOC_HELD_BY_REPOSITORY);
115         }
116 
117         public void setCountry(Country country) {
118             country.addRepository(frame(it(), Repository.class));
119         }
120 
121         public Iterable<DocumentaryUnit> getAllDocumentaryUnits() {
122             Pipeline<Vertex, Vertex> otherPipe = gremlin().as("n").in(Ontology.DOC_IS_CHILD_OF)
123                     .loop("n", JavaHandlerUtils.noopLoopFunc, JavaHandlerUtils.noopLoopFunc);
124 
125             return frameVertices(gremlin().in(Ontology.DOC_HELD_BY_REPOSITORY)
126                     .cast(Vertex.class).copySplit(gremlin(), otherPipe)
127                     .fairMerge().cast(Vertex.class));
128         }
129     }
130 }