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.utils;
21
22 import com.google.common.collect.ImmutableMap;
23 import com.google.common.collect.Maps;
24 import com.tinkerpop.frames.FramedGraph;
25 import eu.ehri.project.acl.ContentTypes;
26 import eu.ehri.project.acl.PermissionType;
27 import eu.ehri.project.core.GraphManager;
28 import eu.ehri.project.core.GraphManagerFactory;
29 import eu.ehri.project.definitions.Ontology;
30 import eu.ehri.project.models.EntityClass;
31 import eu.ehri.project.models.Group;
32 import eu.ehri.project.persistence.ActionManager;
33
34 import java.util.HashMap;
35
36 /**
37 * Initialize the graph with a minimal set of vertices. This includes:
38 * <p>
39 * <ul>
40 * <li>an admin account</li>
41 * <li>permission nodes</li>
42 * <li>content type nodes</li>
43 * </ul>
44 */
45 public class GraphInitializer {
46 private final GraphManager manager;
47
48 private static final String INIT_MESSAGE = "Initialising graph";
49
50 public GraphInitializer(FramedGraph<?> graph) {
51 manager = GraphManagerFactory.getInstance(graph);
52 }
53
54 public void initialize() throws Exception {
55
56 // Create the system node which is the head of the global event streams
57 manager.createVertex(ActionManager.GLOBAL_EVENT_ROOT, EntityClass.SYSTEM,
58 ImmutableMap.<String, Object>of(
59 // It might be useful to know when this graph was
60 // initialise. We can also put other metadata here.
61 Ontology.EVENT_TIMESTAMP, ActionManager.getTimestamp(),
62 Ontology.EVENT_LOG_MESSAGE, INIT_MESSAGE
63 ));
64
65 // Create admin account
66 manager.createVertex(Group.ADMIN_GROUP_IDENTIFIER,
67 EntityClass.GROUP, new HashMap<String, Object>() {
68 {
69 put(Ontology.IDENTIFIER_KEY, Group.ADMIN_GROUP_IDENTIFIER);
70 put(Ontology.NAME_KEY, Group.ADMIN_GROUP_NAME);
71 }
72 });
73
74 // Create permission nodes corresponding to the Enum values
75 for (PermissionType pt : PermissionType.values()) {
76 manager.createVertex(pt.getName(), EntityClass.PERMISSION,
77 Maps.<String, Object>newHashMap());
78 }
79
80 // Create content type nodes corresponding to the Enum values
81 for (ContentTypes ct : ContentTypes.values()) {
82 manager.createVertex(ct.getName(), EntityClass.CONTENT_TYPE,
83 Maps.<String, Object>newHashMap());
84 }
85 }
86 }