1 package de.dlr.shepard.neo4Core.dao;
2
3 import de.dlr.shepard.neo4Core.entities.Version;
4 import de.dlr.shepard.util.CypherQueryHelper;
5 import de.dlr.shepard.util.CypherQueryHelper.Neighborhood;
6 import jakarta.enterprise.context.RequestScoped;
7 import java.util.ArrayList;
8 import java.util.HashMap;
9 import java.util.HashSet;
10 import java.util.Iterator;
11 import java.util.List;
12 import java.util.Map;
13 import java.util.UUID;
14
15 @RequestScoped
16 public class VersionDAO extends GenericDAO<Version> {
17
18
19
20
21
22
23
24 public Version find(UUID id) {
25 Version version = session.load(getEntityType(), id, DEPTH_ENTITY);
26 return version;
27 }
28
29 @Override
30 public Class<Version> getEntityType() {
31 return Version.class;
32 }
33
34 public List<Version> findAllVersions(long collectionId) {
35 ArrayList<Version> result = new ArrayList<Version>();
36 HashSet<Version> resultHashSet = new HashSet<Version>();
37 Map<String, Object> paramsMap = new HashMap<>();
38 String query = "";
39 query = query + "MATCH (col:Collection)-[]->(ver:Version) WHERE col.shepardId = " + collectionId + " ";
40 query = query + CypherQueryHelper.getReturnPart("ver", Neighborhood.EVERYTHING);
41
42 var resultSet = findByQuery(query, paramsMap);
43 Iterator<Version> it = resultSet.iterator();
44 while (it.hasNext()) {
45 Version next = it.next();
46 resultHashSet.add(find(next.getUid()));
47 }
48 for (Version ver : resultHashSet) result.add(ver);
49 return result;
50 }
51
52 public Version findHEADVersion(long collectionId) {
53 Version ret = null;
54 String query =
55 "MATCH (c:Collection)-[:has_version]->(v:Version) WHERE c.shepardId = " +
56 collectionId +
57 " AND " +
58 CypherQueryHelper.getVersionHeadPart("v") +
59 " " +
60 CypherQueryHelper.getReturnPart("v", Neighborhood.EVERYTHING);
61 Map<String, Object> paramsMap = new HashMap<>();
62 var resultSet = findByQuery(query, paramsMap);
63 Iterator<Version> it = resultSet.iterator();
64 if (it.hasNext()) {
65 ret = it.next();
66 }
67 return ret;
68 }
69
70 public Version findVersionLightByNeo4jId(long neo4jId) {
71 Version ret = null;
72 String query = "MATCH (ve:VersionableEntity)-[:has_version]->(v) WHERE id(ve) = " + neo4jId + " RETURN v";
73 Map<String, Object> paramsMap = new HashMap<>();
74 var resultSet = findByQuery(query, paramsMap);
75 Iterator<Version> it = resultSet.iterator();
76 if (it.hasNext()) {
77 ret = it.next();
78 }
79 return ret;
80 }
81
82 public void createLink(long versionableEntityId, UUID versionUID) {
83 String query =
84 "MATCH (ve:VersionableEntity), (v:Version) WHERE id(ve) = " +
85 versionableEntityId +
86 " AND v.uid = '" +
87 versionUID +
88 "' CREATE (ve)-[:has_version]->(v)";
89 Map<String, Object> paramsMap = new HashMap<>();
90 runQuery(query, paramsMap);
91 }
92
93 public boolean copyDataObjectsWithParentsAndPredecessors(UUID sourceVersionUID, UUID targetVersionUID) {
94 copyDataObjects(sourceVersionUID, targetVersionUID);
95 copyChildRelations(sourceVersionUID, targetVersionUID);
96 copySuccessorRelations(sourceVersionUID, targetVersionUID);
97 return true;
98 }
99
100 public void removeSuperflousHasDataObjects(UUID sourceVersionUID, UUID targetVersionUID) {
101 StringBuffer queryBuffer = new StringBuffer();
102 queryBuffer.append(
103 "MATCH (v_target:Version)<-[:has_version]-(col_target:Collection)-[hd:has_dataobject]->(do_source:DataObject)-[:has_version]->(v_source:Version)"
104 );
105 queryBuffer.append(
106 "WHERE v_source.uid = '" + sourceVersionUID + "' AND v_target.uid = '" + targetVersionUID + "' "
107 );
108 queryBuffer.append(" DELETE hd");
109 }
110
111 public void copyDataObjects(UUID sourceVersionUID, UUID targetVersionUID) {
112 StringBuffer queryBuffer = new StringBuffer();
113 queryBuffer.append(
114 "MATCH (do_source:DataObject)-[:has_version]->(v_source:Version)-[:has_predecessor]->(v_target:Version)<-[:has_version]-(col_target:Collection)"
115 );
116 queryBuffer.append(
117 " WHERE v_source.uid = '" + sourceVersionUID + "' AND v_target.uid = '" + targetVersionUID + "' "
118 );
119 queryBuffer.append(" CREATE (col_target)-[:has_dataobject]->(do_target:DataObject)-[:has_version]->(v_target) ");
120 queryBuffer.append(" SET do_target = do_source");
121 Map<String, Object> paramsMap = new HashMap<>();
122 runQuery(queryBuffer.toString(), paramsMap);
123 }
124
125 public void copyChildRelations(UUID sourceVersionUID, UUID targetVersionUID) {
126 StringBuffer queryBuffer = new StringBuffer();
127 queryBuffer.append(
128 "MATCH (do_source_parent:DataObject)-[:has_child]->(do_source_child:DataObject)-[:has_version]->(v_source:Version)-[:has_predecessor]->(v_target:Version)<-[:has_version]-(do_target_parent:DataObject), "
129 );
130 queryBuffer.append("(v_target)<-[:has_version]-(do_target_child:DataObject) ");
131 queryBuffer.append(
132 " WHERE v_source.uid = '" + sourceVersionUID + "' AND v_target.uid = '" + targetVersionUID + "' "
133 );
134 queryBuffer.append(
135 " AND do_source_parent.shepardId=do_target_parent.shepardId AND do_source_child.shepardId=do_target_child.shepardId "
136 );
137 queryBuffer.append(" CREATE (do_target_parent)-[:has_child]->(do_target_child)");
138 Map<String, Object> paramsMap = new HashMap<>();
139 runQuery(queryBuffer.toString(), paramsMap);
140 }
141
142 public void copySuccessorRelations(UUID sourceVersionUID, UUID targetVersionUID) {
143 StringBuffer queryBuffer = new StringBuffer();
144 queryBuffer.append(
145 "MATCH (do_source_predecessor:DataObject)-[:has_successor]->(do_source_successor:DataObject)-[:has_version]->(v_source:Version)-[:has_predecessor]->(v_target:Version)<-[:has_version]-(do_target_predecessor:DataObject), "
146 );
147 queryBuffer.append("(v_target)<-[:has_version]-(do_target_successor:DataObject) ");
148 queryBuffer.append(
149 " WHERE v_source.uid = '" + sourceVersionUID + "' AND v_target.uid = '" + targetVersionUID + "' "
150 );
151 queryBuffer.append(
152 " AND do_source_predecessor.shepardId=do_target_predecessor.shepardId AND do_source_successor.shepardId=do_target_successor.shepardId "
153 );
154 queryBuffer.append(" CREATE (do_target_predecessor)-[:has_successor]->(do_target_successor)");
155 Map<String, Object> paramsMap = new HashMap<>();
156 runQuery(queryBuffer.toString(), paramsMap);
157 }
158 }