View Javadoc

1   package eu.ehri.project.persistence;
2   
3   import com.google.common.collect.Iterables;
4   import com.tinkerpop.blueprints.CloseableIterable;
5   import com.tinkerpop.blueprints.util.WrappingCloseableIterable;
6   import com.tinkerpop.frames.FramedGraph;
7   import eu.ehri.project.core.GraphManager;
8   import eu.ehri.project.core.GraphManagerFactory;
9   import eu.ehri.project.definitions.EventTypes;
10  import eu.ehri.project.definitions.Ontology;
11  import eu.ehri.project.models.EntityClass;
12  import eu.ehri.project.models.events.SystemEvent;
13  import eu.ehri.project.models.events.Version;
14  import org.slf4j.Logger;
15  import org.slf4j.LoggerFactory;
16  
17  import java.util.Optional;
18  
19  public class VersionManager {
20  
21      private static final Logger logger = LoggerFactory.getLogger(ActionManager.class);
22  
23      private final FramedGraph<?> graph;
24      private final GraphManager manager;
25  
26      public VersionManager(FramedGraph<?> graph) {
27          this.graph = graph;
28          this.manager = GraphManagerFactory.getInstance(graph);
29      }
30  
31      /**
32       * If an item was deleted, obtain the last version before it
33       * was removed.
34       *
35       * @param id the item's ID
36       * @return a version frame
37       */
38      public Optional<Version> versionAtDeletion(String id) {
39          try (CloseableIterable<Version> versions = manager
40                  .getEntities(Ontology.VERSION_ENTITY_ID, id, EntityClass.VERSION, Version.class)) {
41              for (Version v : versions) {
42                  SystemEvent event = v.getTriggeringEvent();
43                  if (event != null && EventTypes.deletion.equals(event.getEventType())) {
44                      return Optional.of(v);
45                  }
46              }
47          }
48          return Optional.empty();
49      }
50  
51      /**
52       * Get the last version for all deleted items.
53       *
54       * @param type the entity class
55       * @return an iterable of Version frames
56       */
57      public CloseableIterable<Version> versionsAtDeletion(EntityClass type, String from, String until) {
58          CloseableIterable<Version> versions = manager.getEntities(Ontology.VERSION_ENTITY_CLASS, type.getName(),
59                  EntityClass.VERSION, Version.class);
60          return new WrappingCloseableIterable<>(Iterables.filter(versions, v -> {
61              SystemEvent latestEvent = v.getTriggeringEvent();
62              try {
63                  return latestEvent.getEventType().equals(EventTypes.deletion)
64                          && (from == null || from.compareTo(latestEvent.getTimestamp()) < 0)
65                          && (until == null || until.compareTo(latestEvent.getTimestamp()) >= 0);
66              } catch (NullPointerException e) {
67                  // Shouldn't happen, but just to be safe...
68                  return false;
69              }
70          }));
71      }
72  }