br.ufrj.ppgi.greco.kettle.TurtleGeneratorStepMeta   A
last analyzed

Complexity

Total Complexity 35

Size/Duplication

Total Lines 296
Duplicated Lines 13.85 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 35
eloc 168
c 1
b 0
f 1
dl 41
loc 296
rs 9.6

18 Methods

Rating   Name   Duplication   Size   Complexity  
A getPrefixes() 0 2 1
A getXML() 0 16 1
C readRep(Repository,ObjectId,List,Map) 24 67 10
A setunity(String) 0 2 1
A getDialogClassName() 0 3 1
A getMapTable2() 0 2 1
A getMapTable1() 0 2 1
A getStep(StepMeta,StepDataInterface,int,TransMeta,Trans) 0 4 1
A check(List,TransMeta,StepMeta,RowMetaInterface,String[],String[],RowMetaInterface) 0 5 1
B saveRep(Repository,ObjectId,ObjectId) 0 48 8
A getunity() 0 2 1
A getMapTable() 0 2 1
A setDefault() 0 23 1
A getStepData() 0 3 1
A TurtleGeneratorStepMeta() 0 2 1
A loadXML(Node,List,Map) 17 17 2
A setPrefixes(List) 0 2 1
A getFields(RowMetaInterface,String,RowMetaInterface[],StepMeta,VariableSpace) 0 8 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
package br.ufrj.ppgi.greco.kettle;
2
3
import java.util.ArrayList;
4
import java.util.List;
5
import java.util.Map;
6
7
import org.pentaho.di.core.CheckResult;
8
import org.pentaho.di.core.CheckResultInterface;
9
import org.pentaho.di.core.Counter;
10
import org.pentaho.di.core.database.DatabaseMeta;
11
import org.pentaho.di.core.exception.KettleException;
12
import org.pentaho.di.core.exception.KettleStepException;
13
import org.pentaho.di.core.exception.KettleXMLException;
14
import org.pentaho.di.core.row.RowMetaInterface;
15
import org.pentaho.di.core.row.ValueMetaInterface;
16
import org.pentaho.di.core.row.value.ValueMetaString;
17
import org.pentaho.di.core.variables.VariableSpace;
18
import org.pentaho.di.core.xml.XMLHandler;
19
import org.pentaho.di.repository.ObjectId;
20
import org.pentaho.di.repository.Repository;
21
import org.pentaho.di.trans.Trans;
22
import org.pentaho.di.trans.TransMeta;
23
import org.pentaho.di.trans.step.BaseStepMeta;
24
import org.pentaho.di.trans.step.StepDataInterface;
25
import org.pentaho.di.trans.step.StepInterface;
26
import org.pentaho.di.trans.step.StepMeta;
27
import org.pentaho.di.trans.step.StepMetaInterface;
28
import org.w3c.dom.Node;
29
30
import br.ufrj.ppgi.greco.kettle.plugin.tools.datatable.DataTable;
31
import br.ufrj.ppgi.greco.kettle.plugin.tools.datatable.DataTableConverter;
32
33
import com.thoughtworks.xstream.XStream;
34
import com.thoughtworks.xstream.io.xml.DomDriver;
35
36
public class TurtleGeneratorStepMeta extends BaseStepMeta implements StepMetaInterface {
37
38
	// Fields for serialization
39
	public enum Field {
40
		DATA_ROOT_NODE, VERSION,
41
42
		// Table Dimensions
43
		MAP_TABLE, MAP_TABLE1, MAP_TABLE2, MAP_TABLE3,
44
45
		// TABLE 1
46
		MAP_TABLE_DIMENSIONS_FIELD_NAME, MAP_TABLE_LABELS_FIELD_NAME, MAP_TABLE_URI_FIELD_NAME, MAP_TABLE_URI_TYPE_FIELD_NAME,
47
48
		// TABLE 2
49
		MAP_TABLE_MEASURE_FIELD_NAME, MAP_TABLE_MEASURE_LABEL_FIELD_NAME, MAP_TABLE_MEASURE_URI_FIELD_NAME, MAP_TABLE_MEASURE_URI_TYPE_FIELD_NAME,
50
51
		// TABLE 3
52
		PREFIXES,
53
54
		// TABLE 4
55
		OUTPUT_UNITY_FIELD_NAME,
56
57
		// TABLE 5
58
		MAP_TABLE_HIERARCHY_FIELD_NAME, MAP_TABLE_HIERARCHY_DE_FIELD_NAME, MAP_TABLE_HIERARCHY_LABEL_FIELD_NAME, MAP_TABLE_HIERARCHY_PARA_FIELD_NAME,
59
60
	}
61
62
	// Campos do step
63
64
	// Table 1
65
	private DataTable<String> mapTable;
66
	// Table 2
67
	private DataTable<String> mapTable1;
68
	// Table 3
69
	List<List<String>> prefixes;
70
	// Table 4
71
	private String unity;
72
	// Table 5
73
	private DataTable<String> mapTable2;
74
75
	public TurtleGeneratorStepMeta() {
76
		setDefault();
77
	}
78
79
	// TODO Validar todos os campos para dar feedback ao usuario!
80
	@Override
81
	public void check(List<CheckResultInterface> remarks, TransMeta transMeta, StepMeta stepMeta, RowMetaInterface prev,
82
			String[] input, String[] output, RowMetaInterface info) {
83
		CheckResultInterface ok = new CheckResult(CheckResult.TYPE_RESULT_OK, "", stepMeta);
84
		remarks.add(ok);
85
	}
86
87
	@Override
88
	public StepInterface getStep(StepMeta stepMeta, StepDataInterface stepDataInterface, int copyNr,
89
			TransMeta transMeta, Trans trans) {
90
		return new TurtleGeneratorStep(stepMeta, stepDataInterface, copyNr, transMeta, trans);
91
	}
92
93
	@Override
94
	public StepDataInterface getStepData() {
95
		return new TurtleGeneratorStepData();
96
	}
97
98
	@Override
99
	public String getDialogClassName() {
100
		return TurtleGeneratorStepDialog.class.getName();
101
	}
102
103
	// Carregar campos a partir do XML de um .ktr
104 View Code Duplication
	@SuppressWarnings("unchecked")
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
105
	@Override
106
	public void loadXML(Node stepDomNode, List<DatabaseMeta> databases, Map<String, Counter> sequenceCounters)
107
			throws KettleXMLException {
108
		try {
109
			XStream xs = new XStream(new DomDriver());
110
			xs.alias("DataTable", DataTable.class);
111
			xs.registerConverter(new DataTableConverter());
112
113
			mapTable = (DataTable<String>) xs.fromXML(XMLHandler.getTagValue(stepDomNode, Field.MAP_TABLE.name()));
114
			mapTable1 = (DataTable<String>) xs.fromXML(XMLHandler.getTagValue(stepDomNode, Field.MAP_TABLE1.name()));
115
			mapTable2 = (DataTable<String>) xs.fromXML(XMLHandler.getTagValue(stepDomNode, Field.MAP_TABLE2.name()));
116
			prefixes = (List<List<String>>) xs.fromXML(XMLHandler.getTagValue(stepDomNode, Field.PREFIXES.name()));
117
			unity = (String) xs.fromXML(XMLHandler.getTagValue(stepDomNode, Field.OUTPUT_UNITY_FIELD_NAME.name()));
118
			
119
		} catch (Throwable e) {
120
			e.printStackTrace();
121
		}
122
	}
123
124
	// Gerar XML para salvar um .ktr
125
	@Override
126
	public String getXML() throws KettleException {
127
128
		XStream xs = new XStream();
129
		xs.alias("DataTable", DataTable.class);
130
		xs.registerConverter(new DataTableConverter());
131
		
132
		StringBuilder xml = new StringBuilder();
133
		
134
		xml.append(XMLHandler.addTagValue(Field.MAP_TABLE.name(), xs.toXML(mapTable)));
135
		xml.append(XMLHandler.addTagValue(Field.MAP_TABLE1.name(), xs.toXML(mapTable1)));
136
		xml.append(XMLHandler.addTagValue(Field.MAP_TABLE2.name(), xs.toXML(mapTable2)));
137
		xml.append(XMLHandler.addTagValue(Field.PREFIXES.name(), xs.toXML(prefixes)));
138
		xml.append(XMLHandler.addTagValue(Field.OUTPUT_UNITY_FIELD_NAME.name(), xs.toXML(unity)));
139
140
		return xml.toString();
141
	}
142
143
	// Carregar campos a partir do repositorio
144
	@Override
145
	public void readRep(Repository repository, ObjectId stepIdInRepository, List<DatabaseMeta> databases,
146
			Map<String, Counter> sequenceCounters) throws KettleException {
147
148
		try {
149
			int version = (int) repository.getStepAttributeInteger(stepIdInRepository, Field.VERSION.name());
150
151
			switch (version) {
152
			case 1:
153
				int nrLines = (int) repository.getStepAttributeInteger(stepIdInRepository, "nr_lines");
154
155
				// TABLE 1
156
				mapTable = new DataTable<String>(Field.MAP_TABLE.name(), Field.MAP_TABLE_DIMENSIONS_FIELD_NAME.name(),
157
						Field.MAP_TABLE_LABELS_FIELD_NAME.name(), Field.MAP_TABLE_URI_FIELD_NAME.name(),
158
						Field.MAP_TABLE_URI_TYPE_FIELD_NAME.name());
159
				String[] fields = mapTable.getHeader().toArray(new String[0]);
160 View Code Duplication
				for (int i = 0; i < nrLines; i++) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
161
					int nrfields = fields.length;
162
					String[] line = new String[nrfields];
163
164
					for (int f = 0; f < nrfields; f++) {
165
						line[f] = repository.getStepAttributeString(stepIdInRepository, i, fields[f]);
166
					}
167
					mapTable.add(line);
168
				}
169
170
				// TABLE 2
171
				mapTable = new DataTable<String>(Field.MAP_TABLE1.name(), Field.MAP_TABLE_MEASURE_FIELD_NAME.name(),
172
						Field.MAP_TABLE_MEASURE_LABEL_FIELD_NAME.name(), Field.MAP_TABLE_MEASURE_URI_FIELD_NAME.name(),
173
						Field.MAP_TABLE_MEASURE_URI_TYPE_FIELD_NAME.name());
174
				fields = mapTable1.getHeader().toArray(new String[0]);
175 View Code Duplication
				for (int i = 0; i < nrLines; i++) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
176
					int nrfields = fields.length;
177
					String[] line = new String[nrfields];
178
179
					for (int f = 0; f < nrfields; f++) {
180
						line[f] = repository.getStepAttributeString(stepIdInRepository, i, fields[f]);
181
					}
182
					mapTable1.add(line);
183
				}
184
185
				// Table 4
186
				unity = repository.getStepAttributeString(stepIdInRepository, Field.OUTPUT_UNITY_FIELD_NAME.name());
187
188
				// Table 5
189
				mapTable = new DataTable<String>(Field.MAP_TABLE2.name(), Field.MAP_TABLE_HIERARCHY_FIELD_NAME.name(),
190
						Field.MAP_TABLE_HIERARCHY_DE_FIELD_NAME.name(),
191
						Field.MAP_TABLE_HIERARCHY_LABEL_FIELD_NAME.name(),
192
						Field.MAP_TABLE_HIERARCHY_PARA_FIELD_NAME.name());
193
				fields = mapTable2.getHeader().toArray(new String[0]);
194 View Code Duplication
				for (int i = 0; i < nrLines; i++) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
195
					int nrfields = fields.length;
196
					String[] line = new String[nrfields];
197
198
					for (int f = 0; f < nrfields; f++) {
199
						line[f] = repository.getStepAttributeString(stepIdInRepository, i, fields[f]);
200
					}
201
					mapTable2.add(line);
202
				}
203
204
				break;
205
			default:
206
				setDefault();
207
			}
208
		} catch (Exception e) {
209
			throw new KettleException(
210
					"Unable to read step information from the repository for id_step=" + stepIdInRepository, e);
211
		}
212
213
	}
214
215
	@Override
216
	public void saveRep(Repository repository, ObjectId idOfTransformation, ObjectId idOfStep) throws KettleException {
217
218
		try {
219
			repository.saveStepAttribute(idOfTransformation, idOfStep, Field.VERSION.name(), 1);
220
221
			// TABLE 1
222
			int linhas = (int) mapTable.size();
223
			int colunas = mapTable.getHeader().size();
224
			repository.saveStepAttribute(idOfTransformation, idOfStep, "nr_lines", linhas);
225
			for (int i = 0; i < linhas; i++) {
226
				for (int f = 0; f < colunas; f++) {
227
					repository.saveStepAttribute(idOfTransformation, idOfStep, i, mapTable.getHeader().get(f),
228
							mapTable.getValue(i, f));
229
				}
230
			}
231
232
			// TABLE 2
233
			linhas = (int) mapTable1.size();
234
			colunas = mapTable1.getHeader().size();
235
			repository.saveStepAttribute(idOfTransformation, idOfStep, "nr_lines", linhas);
236
			for (int i = 0; i < linhas; i++) {
237
				for (int f = 0; f < colunas; f++) {
238
					repository.saveStepAttribute(idOfTransformation, idOfStep, i, mapTable1.getHeader().get(f),
239
							mapTable1.getValue(i, f));
240
				}
241
			}
242
243
			// Table 3
244
			repository.saveStepAttribute(idOfTransformation, idOfStep, Field.PREFIXES.name(),
245
					new XStream().toXML(prefixes));
246
247
			// Table 4
248
			repository.saveStepAttribute(idOfTransformation, idOfStep, Field.OUTPUT_UNITY_FIELD_NAME.name(), unity);
249
250
			// TABLE 5
251
			linhas = (int) mapTable2.size();
252
			colunas = mapTable2.getHeader().size();
253
			repository.saveStepAttribute(idOfTransformation, idOfStep, "nr_lines", linhas);
254
			for (int i = 0; i < linhas; i++) {
255
				for (int f = 0; f < colunas; f++) {
256
					repository.saveStepAttribute(idOfTransformation, idOfStep, i, mapTable2.getHeader().get(f),
257
							mapTable2.getValue(i, f));
258
				}
259
			}
260
261
		} catch (Exception e) {
262
			throw new KettleException("Unable to save step information to the repository for id_step= " + idOfStep, e);
263
		}
264
	}
265
266
	// Inicializacoes default
267
	@Override
268
	public void setDefault() {
269
270
		// TABLE 1
271
		mapTable = new DataTable<String>(Field.MAP_TABLE.name(), Field.MAP_TABLE_DIMENSIONS_FIELD_NAME.name(),
272
				Field.MAP_TABLE_LABELS_FIELD_NAME.name(), Field.MAP_TABLE_URI_FIELD_NAME.name(),
273
				Field.MAP_TABLE_URI_TYPE_FIELD_NAME.name());
274
275
		// TABLE 2
276
		mapTable1 = new DataTable<String>(Field.MAP_TABLE1.name(), Field.MAP_TABLE_MEASURE_FIELD_NAME.name(),
277
				Field.MAP_TABLE_MEASURE_LABEL_FIELD_NAME.name(), Field.MAP_TABLE_MEASURE_URI_FIELD_NAME.name(),
278
				Field.MAP_TABLE_MEASURE_URI_TYPE_FIELD_NAME.name());
279
280
		// TABLE 3
281
		prefixes = new ArrayList<List<String>>();
282
283
		// TABLE 4
284
		unity = "Descrição da unidade tratada no DataSet";
285
286
		// TABLE 5
287
		mapTable2 = new DataTable<String>(Field.MAP_TABLE2.name(), Field.MAP_TABLE_HIERARCHY_FIELD_NAME.name(),
288
				Field.MAP_TABLE_HIERARCHY_DE_FIELD_NAME.name(), Field.MAP_TABLE_HIERARCHY_LABEL_FIELD_NAME.name(),
289
				Field.MAP_TABLE_HIERARCHY_PARA_FIELD_NAME.name());
290
	}
291
292
	/**
293
	 * It describes what each output row is going to look like
294
	 */
295
	@Override
296
	public void getFields(RowMetaInterface inputRowMeta, String name, RowMetaInterface[] info, StepMeta nextStep,
297
			VariableSpace space) throws KettleStepException {
298
		inputRowMeta.clear();
299
		
300
		ValueMetaInterface field = new ValueMetaString("Turtle_File");
301
		field.setOrigin(name);
302
		inputRowMeta.addValueMeta(field);
303
	}
304
305
	// Getters & Setters
306
	public List<List<String>> getPrefixes() {
307
		return prefixes;
308
	}
309
310
	public void setPrefixes(List<List<String>> prefixes) {
311
		this.prefixes = prefixes;
312
	}
313
314
	public DataTable<String> getMapTable() {
315
		return mapTable;
316
	}
317
318
	public DataTable<String> getMapTable1() {
319
		return mapTable1;
320
	}
321
322
	public DataTable<String> getMapTable2() {
323
		return mapTable2;
324
	}
325
326
	public String getunity() {
327
		return unity;
328
	}
329
330
	public void setunity(String unity) {
331
		this.unity = unity;
332
	}
333
}