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.Property;
26  import com.tinkerpop.frames.modules.javahandler.JavaHandler;
27  import com.tinkerpop.frames.modules.javahandler.JavaHandlerContext;
28  import eu.ehri.project.definitions.Ontology;
29  import eu.ehri.project.models.annotations.EntityType;
30  import eu.ehri.project.models.annotations.Mandatory;
31  import eu.ehri.project.models.annotations.Meta;
32  import eu.ehri.project.models.base.Annotatable;
33  import eu.ehri.project.models.base.ItemHolder;
34  import eu.ehri.project.models.base.PermissionScope;
35  import eu.ehri.project.models.base.Versioned;
36  import eu.ehri.project.models.utils.JavaHandlerUtils;
37  
38  /**
39   * Frame class representing a country. It's identifier should
40   * be represented by an ISO3166 Alpha 2 code, lower cased.
41   */
42  @EntityType(EntityClass.COUNTRY)
43  public interface Country extends PermissionScope, ItemHolder, Versioned, Annotatable {
44  
45      /**
46       * Alias function for fetching the country code identifier.
47       *
48       * @return The country code
49       */
50      @Mandatory
51      @Property(Ontology.IDENTIFIER_KEY)
52      String getCode();
53  
54      /**
55       * Fetch a count of the number of repositories in this country.
56       *
57       * @return the repository count
58       */
59      @Meta(CHILD_COUNT)
60      @JavaHandler
61      int getChildCount();
62  
63      /**
64       * Fetch all repositories in this country.
65       *
66       * @return an iterable of repository frames
67       */
68      @Adjacency(label = Ontology.REPOSITORY_HAS_COUNTRY, direction = Direction.IN)
69      Iterable<Repository> getRepositories();
70  
71      @JavaHandler
72      Iterable<DocumentaryUnit> getTopLevelDocumentaryUnits();
73  
74      /**
75       * Add a repository to this country.
76       *
77       * @param repository a repository frame
78       */
79      @JavaHandler
80      void addRepository(Repository repository);
81  
82      /**
83       * Implementation of complex methods.
84       */
85      abstract class Impl implements JavaHandlerContext<Vertex>, Country {
86  
87          @Override
88          public int getChildCount() {
89              return Math.toIntExact(gremlin().inE(Ontology.REPOSITORY_HAS_COUNTRY).count());
90          }
91  
92          @Override
93          public void addRepository(Repository repository) {
94              JavaHandlerUtils.addSingleRelationship(repository.asVertex(), it(),
95                      Ontology.REPOSITORY_HAS_COUNTRY);
96          }
97  
98          @Override
99          public Iterable<DocumentaryUnit> getTopLevelDocumentaryUnits() {
100             return frameVertices(gremlin().in(Ontology.REPOSITORY_HAS_COUNTRY)
101                     .in(Ontology.DOC_HELD_BY_REPOSITORY), DocumentaryUnit.class);
102         }
103     }
104 }