This repository has been archived by the owner on Nov 22, 2019. It is now read-only.
/
RestEntityFactory.java
368 lines (321 loc) · 18.9 KB
/
RestEntityFactory.java
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*
* Copyright Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags and
* the COPYRIGHT.txt file distributed with this work.
*
* 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 org.komodo.rest.relational;
import java.net.URI;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map.Entry;
import java.util.Properties;
import org.komodo.openshift.BuildStatus;
import org.komodo.relational.RelationalModelFactory;
import org.komodo.relational.connection.Connection;
import org.komodo.relational.dataservice.Dataservice;
import org.komodo.relational.model.Column;
import org.komodo.relational.model.Model;
import org.komodo.relational.model.Table;
import org.komodo.relational.model.View;
import org.komodo.relational.template.Template;
import org.komodo.relational.template.TemplateEntry;
import org.komodo.relational.vdb.Condition;
import org.komodo.relational.vdb.DataRole;
import org.komodo.relational.vdb.Mask;
import org.komodo.relational.vdb.ModelSource;
import org.komodo.relational.vdb.Permission;
import org.komodo.relational.vdb.Translator;
import org.komodo.relational.vdb.Vdb;
import org.komodo.relational.vdb.VdbImport;
import org.komodo.relational.workspace.WorkspaceManager;
import org.komodo.rest.KomodoRestV1Application.V1Constants;
import org.komodo.rest.RestBasicEntity;
import org.komodo.rest.relational.connection.RestConnection;
import org.komodo.rest.relational.dataservice.RestDataservice;
import org.komodo.rest.relational.response.RestSyndesisDataSource;
import org.komodo.rest.relational.response.RestVdb;
import org.komodo.rest.relational.response.RestVdbCondition;
import org.komodo.rest.relational.response.RestVdbDataRole;
import org.komodo.rest.relational.response.RestVdbImport;
import org.komodo.rest.relational.response.RestVdbMask;
import org.komodo.rest.relational.response.RestVdbModel;
import org.komodo.rest.relational.response.RestVdbModelSource;
import org.komodo.rest.relational.response.RestVdbModelTable;
import org.komodo.rest.relational.response.RestVdbModelTableColumn;
import org.komodo.rest.relational.response.RestVdbModelView;
import org.komodo.rest.relational.response.RestVdbPermission;
import org.komodo.rest.relational.response.RestVdbTranslator;
import org.komodo.rest.relational.response.metadata.RestMetadataConnection;
import org.komodo.rest.relational.response.metadata.RestMetadataTemplate;
import org.komodo.rest.relational.response.metadata.RestMetadataTemplateEntry;
import org.komodo.rest.relational.response.metadata.RestMetadataVdb;
import org.komodo.rest.relational.response.metadata.RestMetadataVdbTranslator;
import org.komodo.rest.relational.response.virtualization.RestVirtualizationStatus;
import org.komodo.spi.KException;
import org.komodo.spi.lexicon.vdb.VdbLexicon;
import org.komodo.spi.repository.KomodoObject;
import org.komodo.spi.repository.KomodoType;
import org.komodo.spi.repository.Repository;
import org.komodo.spi.repository.Repository.UnitOfWork;
import org.komodo.spi.repository.Repository.UnitOfWork.State;
import org.komodo.spi.runtime.SyndesisDataSource;
import org.komodo.spi.runtime.TeiidDataSource;
import org.komodo.spi.runtime.TeiidPropertyDefinition;
import org.komodo.spi.runtime.TeiidTranslator;
import org.komodo.spi.runtime.TeiidVdb;
import org.komodo.utils.ArgCheck;
import org.komodo.utils.KLog;
/**
*
*/
public class RestEntityFactory implements V1Constants {
private static final KLog LOGGER = KLog.getLogger();
private void checkTransaction(UnitOfWork transaction) {
ArgCheck.isNotNull( transaction, "transaction" ); //$NON-NLS-1$
ArgCheck.isTrue( ( transaction.getState() == State.NOT_STARTED ), "transaction state is not NOT_STARTED" ); //$NON-NLS-1$
}
/**
* @param kObject the object
* @param baseUri the base uri
* @param transaction the transaction
* @param properties extra properties
* @return the rest object for the given kObject
* @throws KException if error occurs
*/
@SuppressWarnings( "unchecked" )
public <T extends RestBasicEntity> T create(KomodoObject kObject, URI baseUri, UnitOfWork transaction, KomodoProperties properties)
throws KException {
checkTransaction(transaction);
WorkspaceManager wsMgr = WorkspaceManager.getInstance(kObject.getRepository(), transaction);
KomodoType kType = kObject.getTypeIdentifier(transaction);
switch (kType) {
case VDB:
Vdb vdb = wsMgr.resolve(transaction, kObject, Vdb.class);
Boolean exportXml = properties.getProperty(VDB_EXPORT_XML_PROPERTY, Boolean.FALSE);
return (T)new RestVdb(baseUri, vdb, exportXml, transaction);
case VDB_CONDITION:
Condition condition = wsMgr.resolve(transaction, kObject, Condition.class);
return (T)new RestVdbCondition(baseUri, condition, transaction);
case VDB_DATA_ROLE:
DataRole dataRole = (kObject instanceof DataRole) ? (DataRole)kObject : wsMgr.resolve(transaction,
kObject,
DataRole.class);
return (T)new RestVdbDataRole(baseUri, dataRole, transaction);
case VDB_IMPORT:
VdbImport vdbImport = wsMgr.resolve(transaction, kObject, VdbImport.class);
return (T)new RestVdbImport(baseUri, vdbImport, transaction);
case VDB_MASK:
Mask mask = wsMgr.resolve(transaction, kObject, Mask.class);
return (T)new RestVdbMask(baseUri, mask, transaction);
case MODEL:
Model model = wsMgr.resolve(transaction, kObject, Model.class);
return (T)new RestVdbModel(baseUri, model, transaction);
case VDB_MODEL_SOURCE:
ModelSource source = wsMgr.resolve(transaction, kObject, ModelSource.class);
return (T)new RestVdbModelSource(baseUri, source, transaction);
case VIEW:
View view = wsMgr.resolve(transaction, kObject, View.class);
return (T)new RestVdbModelView(baseUri, view, transaction);
case TABLE:
Table table = wsMgr.resolve(transaction, kObject, Table.class);
return (T)new RestVdbModelTable(baseUri, table, transaction);
case COLUMN:
Column column = wsMgr.resolve(transaction, kObject, Column.class);
return (T)new RestVdbModelTableColumn(baseUri, column, transaction);
case VDB_PERMISSION:
Permission permission = wsMgr.resolve(transaction, kObject, Permission.class);
return (T)new RestVdbPermission(baseUri, permission, transaction);
case VDB_TRANSLATOR:
Translator translator = wsMgr.resolve(transaction, kObject, Translator.class);
return (T)new RestVdbTranslator(baseUri, translator, transaction);
case CONNECTION:
Connection connection = wsMgr.resolve(transaction, kObject, Connection.class);
return (T)new RestConnection(baseUri, connection, transaction);
case DATASERVICE:
Dataservice dataService = wsMgr.resolve(transaction, kObject, Dataservice.class);
return (T)new RestDataservice(baseUri, dataService, false, transaction);
case UNKNOWN:
return null;
default:
return (T)new RestBasicEntity(baseUri, kObject, transaction);
}
}
/**
* @param kObject the object
* @param baseUri the base uri
* @param transaction the transaction
* @return the rest object for the given kObject
* @throws KException if error occurs
*/
public <T extends RestBasicEntity> T create(KomodoObject kObject, URI baseUri, UnitOfWork transaction) throws KException {
return create(kObject, baseUri, transaction, new KomodoProperties());
}
/**
* @param basicEntity the entity to be resolved
* @param klazz the class to resolve it to
* @return the resolved class or null
*/
public static <T extends RestBasicEntity> T resolve(RestBasicEntity basicEntity, Class<T> klazz) {
try {
T instance = klazz.newInstance();
basicEntity.clone(instance);
return instance;
} catch (Exception ex) {
LOGGER.error("Failure to resolve entity", ex); //$NON-NLS-1$
return null;
}
}
private KomodoObject createTemporaryParent(UnitOfWork transaction, Repository repository, String primaryType) throws KException {
String wkspPath = repository.komodoWorkspace(transaction).getAbsolutePath();
String timeNow = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd_HH-mm-ss-SSS"));
return repository.add(transaction, wkspPath, timeNow, primaryType);
}
public RestMetadataVdb createMetadataVdb(UnitOfWork transaction, Repository repository,
TeiidVdb teiidVdb, URI baseUri) throws Exception {
checkTransaction(transaction);
ArgCheck.isTrue(transaction.isRollbackOnly(), "transaction should be rollback-only");
KomodoObject parent = createTemporaryParent(transaction, repository, null);
KomodoObject vdbObj = parent.getObjectFactory().exportTeiidVdb(transaction, parent, teiidVdb);
WorkspaceManager wsMgr = WorkspaceManager.getInstance(repository, transaction);
Vdb vdb = wsMgr.resolve(transaction, vdbObj, Vdb.class);
return new RestMetadataVdb(baseUri, vdb, transaction, false);
}
public RestMetadataVdbTranslator createMetadataTranslator(UnitOfWork transaction, Repository repository,
TeiidTranslator teiidTranslator, URI baseUri)
throws Exception {
checkTransaction(transaction);
ArgCheck.isTrue(transaction.isRollbackOnly(), "transaction should be rollback-only");
KomodoObject parent = createTemporaryParent(transaction, repository, VdbLexicon.Vdb.VIRTUAL_DATABASE);
WorkspaceManager mgr = WorkspaceManager.getInstance(repository, transaction);
Vdb parentVdb = mgr.resolve(transaction, parent, Vdb.class);
String type = teiidTranslator.getType() != null ? teiidTranslator.getType() : teiidTranslator.getName();
Translator translator = RelationalModelFactory.createTranslator(transaction, repository, parentVdb, teiidTranslator.getName(), type);
translator.setDescription(transaction, teiidTranslator.getDescription());
Properties props = teiidTranslator.getProperties();
for (Entry<Object, Object> entry : props.entrySet()) {
translator.setProperty(transaction, entry.getKey().toString(), entry.getValue());
}
return new RestMetadataVdbTranslator(baseUri, translator, transaction);
}
public RestMetadataConnection createMetadataDataSource(UnitOfWork transaction, Repository repository,
TeiidDataSource teiidDataSource, URI baseUri)
throws Exception {
checkTransaction(transaction);
KomodoObject parent = createTemporaryParent(transaction, repository, null);
Connection connection = RelationalModelFactory.createConnection(transaction, repository,parent.getAbsolutePath(), teiidDataSource.getName());
connection.setDriverName(transaction, teiidDataSource.getType());
connection.setJndiName(transaction, teiidDataSource.getJndiName());
for (Entry<Object, Object> property : teiidDataSource.getProperties().entrySet()) {
String key = property.getKey().toString();
if (TeiidDataSource.DATASOURCE_DRIVERNAME.equals(key) ||
TeiidDataSource.DATASOURCE_JNDINAME.equals(key))
continue; // Already set as explicit fields
connection.setProperty(transaction, key, property.getValue());
}
return new RestMetadataConnection(baseUri, connection, transaction);
}
/*
* Get the default value for the Managed ConnectionFactory class
* @param propDefns the collection of property definitions
* @return default value of the ManagedConnectionFactory, null if not found.
*/
private String getManagedConnectionFactoryClassDefault (Collection<TeiidPropertyDefinition> propDefns) {
String resultValue = null;
for(TeiidPropertyDefinition pDefn : propDefns) {
if(pDefn.getName().equalsIgnoreCase(Template.CONN_FACTORY_CLASS_KEY)) {
resultValue=(String)pDefn.getDefaultValue();
break;
}
}
return resultValue;
}
private TemplateEntry createMetadataTemplateEntry(UnitOfWork transaction, Template template, String factoryClass,
TeiidPropertyDefinition definition) throws KException {
TemplateEntry templateEntry = template.addEntry(transaction, definition.getName());
templateEntry.setDescription(transaction, definition.getDescription());
templateEntry.setDisplayName(transaction, definition.getDisplayName());
Collection<String> allowedValues = definition.getAllowedValues();
if (allowedValues != null && ! allowedValues.isEmpty()) {
List<Object> valuesList = new ArrayList<Object>();
for (String value : allowedValues)
valuesList.add(value);
templateEntry.setAllowedValues(transaction, valuesList);
}
templateEntry.setCategory(transaction, definition.getCategory());
templateEntry.setDefaultValue(transaction, definition.getDefaultValue());
templateEntry.setTypeClassName(transaction, definition.getPropertyTypeClassName());
templateEntry.setConstrainedToAllowedValues(transaction, definition.isConstrainedToAllowedValues());
templateEntry.setAdvanced(transaction, definition.isAdvanced());
templateEntry.setMasked(transaction, definition.isMasked());
templateEntry.setModifiable(transaction, definition.isModifiable());
templateEntry.setRequired(transaction, definition.isRequired());
templateEntry.setCustomProperties(transaction, definition.getProperties());
// Copy the 'managedconnectionfactory-class' default value into the 'class-name' default value
if(definition.getName().equals(Template.CLASSNAME_KEY)) {
templateEntry.setDefaultValue(transaction, factoryClass);
templateEntry.setRequired(transaction, true);
templateEntry.setModifiable(transaction, false);
}
return templateEntry;
}
public RestMetadataTemplate createMetadataTemplate(UnitOfWork transaction, Repository repository,
String templateName,
Collection<TeiidPropertyDefinition> propertyDefns,
URI baseUri)
throws Exception {
checkTransaction(transaction);
ArgCheck.isTrue(transaction.isRollbackOnly(), "transaction should be rollback-only");
KomodoObject parent = createTemporaryParent(transaction, repository, null);
Template template = RelationalModelFactory.createTemplate(transaction, repository, parent, templateName);
// Get the Managed connection factory class for rars
String factoryClass = getManagedConnectionFactoryClassDefault(propertyDefns);
for (TeiidPropertyDefinition definition : propertyDefns) {
createMetadataTemplateEntry(transaction, template, factoryClass, definition);
}
return new RestMetadataTemplate(baseUri, template, transaction);
}
public List<RestMetadataTemplateEntry> createMetadataTemplateEntry(UnitOfWork transaction, Repository repository,
Collection<TeiidPropertyDefinition> propertyDefns,
URI baseUri)
throws Exception {
checkTransaction(transaction);
ArgCheck.isTrue(transaction.isRollbackOnly(), "transaction should be rollback-only");
KomodoObject parent = createTemporaryParent(transaction, repository, null);
Template template = RelationalModelFactory.createTemplate(transaction, repository, parent, "tempTemplate");
// Get the Managed connection factory class for rars
String factoryClass = getManagedConnectionFactoryClassDefault(propertyDefns);
List<RestMetadataTemplateEntry> restEntries = new ArrayList<>();
for (TeiidPropertyDefinition definition : propertyDefns) {
TemplateEntry entry = createMetadataTemplateEntry(transaction, template, factoryClass, definition);
RestMetadataTemplateEntry restEntry = new RestMetadataTemplateEntry(baseUri, entry, transaction);
restEntries.add(restEntry);
}
return restEntries;
}
public RestSyndesisDataSource createServiceCatalogDataSource(UnitOfWork transaction, Repository repository,
SyndesisDataSource datasource, URI baseUri) throws Exception {
checkTransaction(transaction);
ArgCheck.isTrue(transaction.isRollbackOnly(), "transaction should be rollback-only");
// TODO: phantomjinx what needs to be done here?
KomodoObject parent = createTemporaryParent(transaction, repository, null);
return new RestSyndesisDataSource(baseUri, parent, datasource, transaction);
}
public RestVirtualizationStatus createBuildStatus(BuildStatus status, URI baseUri) throws Exception {
return new RestVirtualizationStatus(baseUri, status);
}
}