-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathPostgresRichVersionFactory.java
More file actions
169 lines (140 loc) · 6.29 KB
/
Copy pathPostgresRichVersionFactory.java
File metadata and controls
169 lines (140 loc) · 6.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/**
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dao.models.postgres;
import dao.models.RichVersionFactory;
import dao.versions.postgres.PostgresVersionFactory;
import db.DbClient;
import db.DbDataContainer;
import db.PostgresClient;
import db.PostgresResults;
import exceptions.GroundException;
import exceptions.GroundVersionNotFoundException;
import models.models.RichVersion;
import models.models.StructureVersion;
import models.models.Tag;
import models.versions.GroundType;
import util.ElasticSearch;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public abstract class PostgresRichVersionFactory<T extends RichVersion>
extends PostgresVersionFactory<T>
implements RichVersionFactory<T> {
private final PostgresClient dbClient;
private final PostgresStructureVersionFactory structureVersionFactory;
private final PostgresTagFactory tagFactory;
/**
* Constructor for the Postgres rich version factory.
*
* @param dbClient the Postgres client
* @param structureVersionFactory the singleton PostgresStructureVerisonFactory
* @param tagFactory the singleton PostgresTagFactory
*/
public PostgresRichVersionFactory(PostgresClient dbClient,
PostgresStructureVersionFactory structureVersionFactory,
PostgresTagFactory tagFactory) {
super(dbClient);
this.dbClient = dbClient;
this.structureVersionFactory = structureVersionFactory;
this.tagFactory = tagFactory;
}
/**
* Persist rich version data in the database.
*
* @param id the id of the rich version
* @param tags tags associated with this version
* @param structureVersionId the id of the StructureVersion associated with this version
* @param reference an optional external reference
* @param referenceParameters access parameters for the reference
* @throws GroundException an error while persisting data
*/
@Override
public void insertIntoDatabase(long id,
Map<String, Tag> tags,
long structureVersionId,
String reference,
Map<String, String> referenceParameters) throws GroundException {
super.insertIntoDatabase(id);
if (structureVersionId != -1) {
StructureVersion structureVersion = this.structureVersionFactory
.retrieveFromDatabase(structureVersionId);
RichVersionFactory.checkStructureTags(structureVersion, tags);
}
List<DbDataContainer> insertions = new ArrayList<>();
insertions.add(new DbDataContainer("id", GroundType.LONG, id));
insertions.add(new DbDataContainer("structure_version_id", GroundType.LONG,
structureVersionId));
insertions.add(new DbDataContainer("reference", GroundType.STRING, reference));
this.dbClient.insert("rich_version", insertions);
for (String key : tags.keySet()) {
Tag tag = tags.get(key);
ElasticSearch.insertElasticSearch(tag, "rich_version");
List<DbDataContainer> tagInsertion = new ArrayList<>();
tagInsertion.add(new DbDataContainer("rich_version_id", GroundType.LONG, id));
tagInsertion.add(new DbDataContainer("key", GroundType.STRING, key));
if (tag.getValue() != null) {
tagInsertion.add(new DbDataContainer("value", GroundType.STRING,
tag.getValue().toString()));
tagInsertion.add(new DbDataContainer("type", GroundType.STRING,
tag.getValueType().toString()));
} else {
tagInsertion.add(new DbDataContainer("value", GroundType.STRING, null));
tagInsertion.add(new DbDataContainer("type", GroundType.STRING, null));
}
this.dbClient.insert("rich_version_tag", tagInsertion);
}
for (String key : referenceParameters.keySet()) {
List<DbDataContainer> parameterInsertion = new ArrayList<>();
parameterInsertion.add(new DbDataContainer("rich_version_id", GroundType.LONG, id));
parameterInsertion.add(new DbDataContainer("key", GroundType.STRING, key));
parameterInsertion.add(new DbDataContainer("value", GroundType.STRING,
referenceParameters.get(key)));
this.dbClient.insert("rich_version_external_parameter", parameterInsertion);
}
}
/**
* Retrieve rich version data from the database.
*
* @param id the id of the rich version
* @return the retrieved rich version
* @throws GroundException either the rich version didn't exist or couldn't be retrieved
*/
public RichVersion retrieveRichVersionData(long id) throws GroundException {
List<DbDataContainer> predicates = new ArrayList<>();
predicates.add(new DbDataContainer("id", GroundType.LONG, id));
PostgresResults resultSet = this.dbClient.equalitySelect("rich_version",
DbClient.SELECT_STAR,
predicates);
if (resultSet.isEmpty()) {
throw new GroundVersionNotFoundException(RichVersion.class, id);
}
List<DbDataContainer> parameterPredicates = new ArrayList<>();
parameterPredicates.add(new DbDataContainer("rich_version_id", GroundType.LONG, id));
Map<String, String> referenceParameters = new HashMap<>();
PostgresResults parameterSet = this.dbClient.equalitySelect("rich_version_external_parameter",
DbClient.SELECT_STAR, parameterPredicates);
if (!parameterSet.isEmpty()) {
do {
referenceParameters.put(parameterSet.getString(2), parameterSet.getString(3));
} while (parameterSet.next());
}
Map<String, Tag> tags = tagFactory.retrieveFromDatabaseByVersionId(id);
String reference = resultSet.getString(3);
long structureVersionId = resultSet.getLong(2);
structureVersionId = structureVersionId == 0 ? -1 : structureVersionId;
return new RichVersion(id, tags, structureVersionId, reference,
referenceParameters);
}
}