br.ufrj.ppgi.greco.kettle.LinkDiscoveryToolStepMeta   C
last analyzed

Complexity

Total Complexity 56

Size/Duplication

Total Lines 419
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 56
eloc 267
c 1
b 0
f 1
dl 0
loc 419
rs 5.5199

46 Methods

Rating   Name   Duplication   Size   Complexity  
A getDialogClassName() 0 3 1
A getStepData() 0 2 1
A loadXML(Node,List,Map) 0 36 2
A LinkDiscoveryToolStepMeta() 0 2 1
A saveRep(Repository,ObjectId,ObjectId) 0 40 2
A setDefault() 0 22 1
A readRep(Repository,ObjectId,List,Map) 0 41 2
A getStep(StepMeta,StepDataInterface,int,TransMeta,Trans) 0 3 1
A getXML() 0 31 1
A check(List,TransMeta,StepMeta,RowMetaInterface,String[],String[],RowMetaInterface) 0 5 1
A getPrefixes() 0 2 1
A setLinkageType(String) 0 2 1
A setSparqlOutput(boolean) 0 2 1
A setSourceGraph(String) 0 2 1
A getSourceGraph() 0 2 1
A getOutputGraph() 0 2 1
A setSourceRestriction(String) 0 2 1
A isSparqlOutput() 0 2 1
A getConfigFile() 0 2 1
A getTargetEndpoint() 0 2 1
A getFilePath() 0 2 1
A setMetrics(DataTable) 0 2 1
A setSourceEndpoint(String) 0 2 1
A setTargetRestriction(String) 0 2 1
A getOutputEndpoint() 0 2 1
A getAggregationType() 0 2 1
A getTargetGraph() 0 2 1
A getOutputFolder() 0 2 1
A setOutputFilename(String) 0 2 1
A getSourceEndpoint() 0 2 1
A getFileType(String) 0 9 5
A getLinkageType() 0 2 1
A getFields(RowMetaInterface,String,RowMetaInterface[],StepMeta,VariableSpace) 0 16 1
A setTargetEndpoint(String) 0 2 1
A setOutputEndpoint(String) 0 2 1
A setPrefixes(DataTable) 0 2 1
A setAggregationType(String) 0 2 1
A setOutputGraph(String) 0 2 1
A setConfigFile(String) 0 2 1
A getMetrics() 0 2 1
A getSourceRestriction() 0 2 1
A getTargetRestriction() 0 2 1
A getOutputFilename() 0 2 1
A isSparqlEndpoint(String) 0 6 3
A setTargetGraph(String) 0 2 1
A setOutputFolder(String) 0 4 2

How to fix   Complexity   

Complexity

Complex classes like br.ufrj.ppgi.greco.kettle.LinkDiscoveryToolStepMeta often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
package br.ufrj.ppgi.greco.kettle;
2
3
import java.nio.file.Path;
4
import java.nio.file.Paths;
5
import java.util.List;
6
import java.util.Map;
7
8
import org.pentaho.di.core.CheckResult;
9
import org.pentaho.di.core.CheckResultInterface;
10
import org.pentaho.di.core.Counter;
11
import org.pentaho.di.core.database.DatabaseMeta;
12
import org.pentaho.di.core.exception.KettleException;
13
import org.pentaho.di.core.exception.KettleStepException;
14
import org.pentaho.di.core.exception.KettleXMLException;
15
import org.pentaho.di.core.row.RowMetaInterface;
16
import org.pentaho.di.core.row.ValueMetaInterface;
17
import org.pentaho.di.core.row.value.ValueMetaString;
18
import org.pentaho.di.core.variables.VariableSpace;
19
import org.pentaho.di.core.xml.XMLHandler;
20
import org.pentaho.di.repository.ObjectId;
21
import org.pentaho.di.repository.Repository;
22
import org.pentaho.di.trans.Trans;
23
import org.pentaho.di.trans.TransMeta;
24
import org.pentaho.di.trans.step.BaseStepMeta;
25
import org.pentaho.di.trans.step.StepDataInterface;
26
import org.pentaho.di.trans.step.StepInterface;
27
import org.pentaho.di.trans.step.StepMeta;
28
import org.pentaho.di.trans.step.StepMetaInterface;
29
import org.w3c.dom.Node;
30
31
import br.ufrj.ppgi.greco.kettle.plugin.tools.datatable.DataTable;
32
import br.ufrj.ppgi.greco.kettle.plugin.tools.datatable.DataTableConverter;
33
34
import com.thoughtworks.xstream.XStream;
35
import com.thoughtworks.xstream.io.xml.DomDriver;
36
37
public class LinkDiscoveryToolStepMeta extends BaseStepMeta implements StepMetaInterface {
38
39
	public enum Field {
40
		CONFIG_FILE,
41
42
		// Data Sources
43
		SOURCE_ENDPOINT, SOURCE_GRAPH, SOURCE_RESTRICTION, TARGET_ENDPOINT, TARGET_GRAPH, TARGET_RESTRICTION,
44
45
		// Prefixes
46
		PREFIXES_TABLE, PREFIXES_TABLE_PREFIX, PREFIXES_TABLE_NAMESPACE,
47
48
		// Linkage Rules
49
		LINKAGE_TYPE, AGGREGATION_TYPE, METRICS_TABLE, METRICS_TABLE_SOURCE, METRICS_TABLE_TARGET, METRICS_TABLE_METRIC,
50
51
		// Output
52
		OUTPUT_FOLDER, OUTPUT_FILENAME, OUTPUT_GRAPH, OUTPUT_SPARQL, OUTPUT_ENDPOINT
53
	}
54
55
	private String configFile;
56
57
	private String sourceEndpoint;
58
	private String sourceGraph;
59
	private String sourceRestriction;
60
61
	private String targetEndpoint;
62
	private String targetGraph;
63
	private String targetRestriction;
64
65
	private DataTable<String> prefixes;
66
67
	private String linkageType;
68
	private String aggregationType;
69
	private DataTable<String> metrics;
70
71
	private boolean sparqlOutput;
72
	private String outputFolder;
73
	private String outputFilename;
74
	private String outputGraph;
75
	private String outputEndpoint;
76
77
	public LinkDiscoveryToolStepMeta() {
78
		setDefault();
79
	}
80
81
	@Override
82
	public void check(List<CheckResultInterface> remarks, TransMeta transMeta, StepMeta stepMeta, RowMetaInterface prev,
83
			String[] input, String[] output, RowMetaInterface info) {
84
		CheckResultInterface ok = new CheckResult(CheckResult.TYPE_RESULT_OK, "", stepMeta);
85
		remarks.add(ok);
86
	}
87
88
	public StepInterface getStep(StepMeta stepMeta, StepDataInterface stepDataInterface, int copyNr,
89
			TransMeta transMeta, Trans trans) {
90
		return new LinkDiscoveryToolStep(stepMeta, stepDataInterface, copyNr, transMeta, trans);
91
	}
92
93
	public StepDataInterface getStepData() {
94
		return new LinkDiscoveryToolStepData();
95
	}
96
97
	@Override
98
	public String getDialogClassName() {
99
		return LinkDiscoveryToolStepDialog.class.getName();
100
	}
101
102
	@SuppressWarnings("unchecked")
103
	@Override
104
	public void loadXML(Node stepDomNode, List<DatabaseMeta> databases, Map<String, Counter> sequenceCounters)
105
			throws KettleXMLException {
106
107
		try {
108
			XStream xs = new XStream(new DomDriver());
109
			xs.alias("DataTable", DataTable.class);
110
			xs.registerConverter(new DataTableConverter());
111
112
			this.configFile = (String) XMLHandler.getTagValue(stepDomNode, Field.CONFIG_FILE.name());
113
114
			this.sourceEndpoint = (String) XMLHandler.getTagValue(stepDomNode, Field.SOURCE_ENDPOINT.name());
115
			this.sourceGraph = (String) XMLHandler.getTagValue(stepDomNode, Field.SOURCE_GRAPH.name());
116
			this.sourceRestriction = (String) XMLHandler.getTagValue(stepDomNode, Field.SOURCE_RESTRICTION.name());
117
118
			this.targetEndpoint = (String) XMLHandler.getTagValue(stepDomNode, Field.TARGET_ENDPOINT.name());
119
			this.targetGraph = (String) XMLHandler.getTagValue(stepDomNode, Field.TARGET_GRAPH.name());
120
			this.targetRestriction = (String) XMLHandler.getTagValue(stepDomNode, Field.TARGET_RESTRICTION.name());
121
122
			this.prefixes = (DataTable<String>) xs
123
					.fromXML(XMLHandler.getTagValue(stepDomNode, Field.PREFIXES_TABLE.name()));
124
125
			this.linkageType = (String) XMLHandler.getTagValue(stepDomNode, Field.LINKAGE_TYPE.name());
126
			this.aggregationType = (String) XMLHandler.getTagValue(stepDomNode, Field.AGGREGATION_TYPE.name());
127
			this.metrics = (DataTable<String>) xs
128
					.fromXML(XMLHandler.getTagValue(stepDomNode, Field.METRICS_TABLE.name()));
129
130
			this.sparqlOutput = "Y".equals(XMLHandler.getTagValue(stepDomNode, Field.OUTPUT_SPARQL.name()));
131
			this.outputFolder = (String) XMLHandler.getTagValue(stepDomNode, Field.OUTPUT_FOLDER.name());
132
			this.outputFilename = (String) XMLHandler.getTagValue(stepDomNode, Field.OUTPUT_FILENAME.name());
133
			this.outputEndpoint = (String) XMLHandler.getTagValue(stepDomNode, Field.OUTPUT_ENDPOINT.name());
134
			this.outputGraph = (String) XMLHandler.getTagValue(stepDomNode, Field.OUTPUT_GRAPH.name());
135
136
		} catch (Throwable e) {
137
			e.printStackTrace();
138
		}
139
	}
140
141
	@Override
142
	public String getXML() throws KettleException {
143
		XStream xs = new XStream(new DomDriver());
144
		xs.alias("DataTable", DataTable.class);
145
		xs.registerConverter(new DataTableConverter());
146
147
		StringBuilder xml = new StringBuilder();
148
149
		xml.append(XMLHandler.addTagValue(Field.CONFIG_FILE.name(), this.configFile));
150
151
		xml.append(XMLHandler.addTagValue(Field.SOURCE_ENDPOINT.name(), this.sourceEndpoint));
152
		xml.append(XMLHandler.addTagValue(Field.SOURCE_GRAPH.name(), this.sourceGraph));
153
		xml.append(XMLHandler.addTagValue(Field.SOURCE_RESTRICTION.name(), this.sourceRestriction));
154
155
		xml.append(XMLHandler.addTagValue(Field.TARGET_ENDPOINT.name(), this.targetEndpoint));
156
		xml.append(XMLHandler.addTagValue(Field.TARGET_GRAPH.name(), this.targetGraph));
157
		xml.append(XMLHandler.addTagValue(Field.TARGET_RESTRICTION.name(), this.targetRestriction));
158
159
		xml.append(XMLHandler.addTagValue(Field.PREFIXES_TABLE.name(), xs.toXML(this.prefixes)));
160
161
		xml.append(XMLHandler.addTagValue(Field.LINKAGE_TYPE.name(), this.linkageType));
162
		xml.append(XMLHandler.addTagValue(Field.AGGREGATION_TYPE.name(), this.aggregationType));
163
		xml.append(XMLHandler.addTagValue(Field.METRICS_TABLE.name(), xs.toXML(this.metrics)));
164
165
		xml.append(XMLHandler.addTagValue(Field.OUTPUT_SPARQL.name(), this.sparqlOutput));
166
		xml.append(XMLHandler.addTagValue(Field.OUTPUT_FOLDER.name(), this.outputFolder));
167
		xml.append(XMLHandler.addTagValue(Field.OUTPUT_FILENAME.name(), this.outputFilename));
168
		xml.append(XMLHandler.addTagValue(Field.OUTPUT_GRAPH.name(), this.outputGraph));
169
		xml.append(XMLHandler.addTagValue(Field.OUTPUT_ENDPOINT.name(), this.outputEndpoint));
170
171
		return xml.toString();
172
	}
173
174
	@SuppressWarnings("unchecked")
175
	@Override
176
	public void readRep(Repository repository, ObjectId stepIdInRepository, List<DatabaseMeta> databases,
177
			Map<String, Counter> sequenceCounters) throws KettleException {
178
		try {
179
180
			XStream xs = new XStream(new DomDriver());
181
			xs.alias("DataTable", DataTable.class);
182
			xs.registerConverter(new DataTableConverter());
183
184
			this.configFile = repository.getStepAttributeString(stepIdInRepository, Field.CONFIG_FILE.name());
185
186
			this.sourceEndpoint = repository.getStepAttributeString(stepIdInRepository, Field.SOURCE_ENDPOINT.name());
187
			this.sourceGraph = repository.getStepAttributeString(stepIdInRepository, Field.SOURCE_GRAPH.name());
188
			this.sourceRestriction = repository.getStepAttributeString(stepIdInRepository,
189
					Field.SOURCE_RESTRICTION.name());
190
191
			this.targetEndpoint = repository.getStepAttributeString(stepIdInRepository, Field.TARGET_ENDPOINT.name());
192
			this.targetGraph = repository.getStepAttributeString(stepIdInRepository, Field.TARGET_GRAPH.name());
193
			this.targetRestriction = repository.getStepAttributeString(stepIdInRepository,
194
					Field.TARGET_RESTRICTION.name());
195
196
			this.prefixes = (DataTable<String>) xs
197
					.fromXML(repository.getStepAttributeString(stepIdInRepository, Field.PREFIXES_TABLE.name()));
198
199
			this.linkageType = repository.getStepAttributeString(stepIdInRepository, Field.LINKAGE_TYPE.name());
200
			this.aggregationType = (String) repository.getStepAttributeString(stepIdInRepository,
201
					Field.AGGREGATION_TYPE.name());
202
			this.metrics = (DataTable<String>) xs
203
					.fromXML(repository.getStepAttributeString(stepIdInRepository, Field.METRICS_TABLE.name()));
204
205
			this.sparqlOutput = "Y"
206
					.equals(repository.getStepAttributeString(stepIdInRepository, Field.OUTPUT_SPARQL.name()));
207
			this.outputFolder = repository.getStepAttributeString(stepIdInRepository, Field.OUTPUT_FOLDER.name());
208
			this.outputFilename = repository.getStepAttributeString(stepIdInRepository, Field.OUTPUT_FILENAME.name());
209
			this.outputGraph = repository.getStepAttributeString(stepIdInRepository, Field.OUTPUT_GRAPH.name());
210
			this.outputEndpoint = repository.getStepAttributeString(stepIdInRepository, Field.OUTPUT_ENDPOINT.name());
211
212
		} catch (Exception e) {
213
			throw new KettleException(
214
					"Unable to read step information from the repository for id_step=" + stepIdInRepository, e);
215
		}
216
217
	}
218
219
	@Override
220
	public void saveRep(Repository repository, ObjectId idOfTransformation, ObjectId idOfStep) throws KettleException {
221
		try {
222
223
			XStream xs = new XStream(new DomDriver());
224
			xs.alias("DataTable", DataTable.class);
225
			xs.registerConverter(new DataTableConverter());
226
227
			repository.saveStepAttribute(idOfTransformation, idOfStep, Field.CONFIG_FILE.name(), this.configFile);
228
229
			repository.saveStepAttribute(idOfTransformation, idOfStep, Field.SOURCE_ENDPOINT.name(),
230
					this.sourceEndpoint);
231
			repository.saveStepAttribute(idOfTransformation, idOfStep, Field.SOURCE_GRAPH.name(), this.sourceGraph);
232
			repository.saveStepAttribute(idOfTransformation, idOfStep, Field.SOURCE_RESTRICTION.name(),
233
					this.sourceRestriction);
234
235
			repository.saveStepAttribute(idOfTransformation, idOfStep, Field.TARGET_ENDPOINT.name(),
236
					this.targetEndpoint);
237
			repository.saveStepAttribute(idOfTransformation, idOfStep, Field.TARGET_GRAPH.name(), this.targetGraph);
238
			repository.saveStepAttribute(idOfTransformation, idOfStep, Field.TARGET_RESTRICTION.name(),
239
					this.targetRestriction);
240
241
			repository.saveStepAttribute(idOfTransformation, idOfStep, Field.PREFIXES_TABLE.name(),
242
					xs.toXML(this.prefixes));
243
244
			repository.saveStepAttribute(idOfTransformation, idOfStep, Field.LINKAGE_TYPE.name(), this.linkageType);
245
			repository.saveStepAttribute(idOfTransformation, idOfStep, Field.AGGREGATION_TYPE.name(),
246
					this.aggregationType);
247
			repository.saveStepAttribute(idOfTransformation, idOfStep, Field.METRICS_TABLE.name(),
248
					xs.toXML(this.metrics));
249
250
			repository.saveStepAttribute(idOfTransformation, idOfStep, Field.OUTPUT_SPARQL.name(), this.sparqlOutput);
251
			repository.saveStepAttribute(idOfTransformation, idOfStep, Field.OUTPUT_FOLDER.name(), this.outputFolder);
252
			repository.saveStepAttribute(idOfTransformation, idOfStep, Field.OUTPUT_FILENAME.name(),
253
					this.outputFilename);
254
			repository.saveStepAttribute(idOfTransformation, idOfStep, Field.OUTPUT_GRAPH.name(), this.outputGraph);
255
			repository.saveStepAttribute(idOfTransformation, idOfStep, Field.OUTPUT_ENDPOINT.name(), this.outputEndpoint);
256
257
		} catch (Exception e) {
258
			throw new KettleException("Unable to save step information to the repository for id_step=" + idOfStep, e);
259
		}
260
	}
261
262
	public void setDefault() {
263
		this.sourceEndpoint = "";
264
		this.sourceGraph = "";
265
		this.sourceRestriction = "";
266
		this.targetEndpoint = "";
267
		this.targetGraph = "";
268
		this.targetRestriction = "";
269
270
		this.prefixes = new DataTable<String>(Field.PREFIXES_TABLE.name(), Field.PREFIXES_TABLE_PREFIX.name(),
271
				Field.PREFIXES_TABLE_NAMESPACE.name());
272
273
		this.linkageType = "owl:sameAs";
274
		this.aggregationType = "";
275
		this.metrics = new DataTable<String>(Field.METRICS_TABLE.name(), Field.METRICS_TABLE_SOURCE.name(),
276
				Field.METRICS_TABLE_TARGET.name(), Field.METRICS_TABLE_METRIC.name());
277
278
		this.outputFolder = "";
279
		this.outputFilename = "ntriples.nt";
280
		this.configFile = "";
281
		this.outputGraph = "";
282
		this.sparqlOutput = false;
283
		this.outputEndpoint = "";
284
	}
285
286
	@Override
287
	public void getFields(RowMetaInterface inputRowMeta, String name, RowMetaInterface[] info, StepMeta nextStep,
288
			VariableSpace space) throws KettleStepException {
289
		ValueMetaInterface field = null;
290
291
		field = new ValueMetaString("subject");
292
		field.setOrigin(name);
293
		inputRowMeta.addValueMeta(field);
294
295
		field = new ValueMetaString("predicate");
296
		field.setOrigin(name);
297
		inputRowMeta.addValueMeta(field);
298
299
		field = new ValueMetaString("object");
300
		field.setOrigin(name);
301
		inputRowMeta.addValueMeta(field);
302
	}
303
304
	public String getConfigFile() {
305
		return configFile;
306
	}
307
308
	public void setConfigFile(String configFile) {
309
		this.configFile = configFile;
310
	}
311
312
	public String getSourceEndpoint() {
313
		return sourceEndpoint;
314
	}
315
316
	public void setSourceEndpoint(String sourceEndpoint) {
317
		this.sourceEndpoint = sourceEndpoint;
318
	}
319
320
	public String getSourceGraph() {
321
		return sourceGraph;
322
	}
323
324
	public void setSourceGraph(String sourceGraph) {
325
		this.sourceGraph = sourceGraph;
326
	}
327
328
	public String getSourceRestriction() {
329
		return sourceRestriction;
330
	}
331
332
	public void setSourceRestriction(String sourceRestriction) {
333
		this.sourceRestriction = sourceRestriction;
334
	}
335
336
	public String getTargetEndpoint() {
337
		return targetEndpoint;
338
	}
339
340
	public void setTargetEndpoint(String targetEndpoint) {
341
		this.targetEndpoint = targetEndpoint;
342
	}
343
344
	public String getTargetGraph() {
345
		return targetGraph;
346
	}
347
348
	public void setTargetGraph(String targetGraph) {
349
		this.targetGraph = targetGraph;
350
	}
351
352
	public String getTargetRestriction() {
353
		return targetRestriction;
354
	}
355
356
	public void setTargetRestriction(String targetRestriction) {
357
		this.targetRestriction = targetRestriction;
358
	}
359
360
	public DataTable<String> getPrefixes() {
361
		return prefixes;
362
	}
363
364
	public void setPrefixes(DataTable<String> prefixes) {
365
		this.prefixes = prefixes;
366
	}
367
368
	public String getLinkageType() {
369
		return linkageType;
370
	}
371
372
	public void setLinkageType(String linkageType) {
373
		this.linkageType = linkageType;
374
	}
375
376
	public String getAggregationType() {
377
		return aggregationType;
378
	}
379
380
	public void setAggregationType(String aggregationType) {
381
		this.aggregationType = aggregationType;
382
	}
383
384
	public DataTable<String> getMetrics() {
385
		return metrics;
386
	}
387
388
	public void setMetrics(DataTable<String> metrics) {
389
		this.metrics = metrics;
390
	}
391
392
	public String getOutputFolder() {
393
		return outputFolder;
394
	}
395
396
	public void setOutputFolder(String outputFolder) {
397
		Path p = Paths.get(outputFolder);
398
		this.setOutputFilename(p.getFileName().toString());
399
		this.outputFolder = p.getParent() != null ? p.getParent().toString() : "";
400
	}
401
402
	public String getOutputFilename() {
403
		return outputFilename;
404
	}
405
406
	public void setOutputFilename(String outputFilename) {
407
		this.outputFilename = outputFilename;
408
	}
409
410
	public String getFilePath() {
411
		return Paths.get(this.getOutputFolder(), this.getOutputFilename()).toString();
412
	}
413
414
	public boolean isSparqlEndpoint(String location) {
415
		if (location != null && !location.isEmpty()) {
416
			String s = location.trim().toLowerCase();
417
			return s.startsWith("http://") || s.startsWith("https://") || s.startsWith("www.");
418
		}
419
		return false;
420
	}
421
422
	public String getFileType(String location) {
423
		if (location != null && !location.isEmpty()) {
424
			String s = location.trim().toLowerCase();
425
			if (s.lastIndexOf(".") != -1 && s.lastIndexOf(".") != 0)
426
				return s.substring(s.lastIndexOf(".") + 1);
427
			else
428
				return "";
429
		} else {
430
			return "";
431
		}
432
	}
433
434
	public boolean isSparqlOutput() {
435
		return sparqlOutput;
436
	}
437
438
	public void setSparqlOutput(boolean sparqlOutput) {
439
		this.sparqlOutput = sparqlOutput;
440
	}
441
442
	public String getOutputGraph() {
443
		return outputGraph;
444
	}
445
446
	public void setOutputGraph(String outputGraph) {
447
		this.outputGraph = outputGraph;
448
	}
449
450
	public String getOutputEndpoint() {
451
		return outputEndpoint;
452
	}
453
454
	public void setOutputEndpoint(String outputEndpoint) {
455
		this.outputEndpoint = outputEndpoint;
456
	}
457
458
}
459