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

Complexity

Total Complexity 24

Size/Duplication

Total Lines 130
Duplicated Lines 34.62 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 24
eloc 71
c 1
b 0
f 1
dl 45
loc 130
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
B processRow(StepMetaInterface,StepDataInterface) 45 45 7
A init(StepMetaInterface,StepDataInterface) 0 6 2
D tripleWriter(Object,Object[],GraphTriplifyStepData) 0 61 13
A dispose(StepMetaInterface,StepDataInterface) 0 3 1
A GraphTriplifyStep(StepMeta,StepDataInterface,int,TransMeta,Trans) 0 3 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.lang.reflect.InvocationTargetException;
4
import java.lang.reflect.Method;
5
6
import org.pentaho.di.core.exception.KettleException;
7
import org.pentaho.di.core.exception.KettleStepException;
8
import org.pentaho.di.core.row.RowDataUtil;
9
import org.pentaho.di.core.row.RowMeta;
10
import org.pentaho.di.core.row.RowMetaInterface;
11
import org.pentaho.di.trans.Trans;
12
import org.pentaho.di.trans.TransMeta;
13
import org.pentaho.di.trans.step.BaseStep;
14
import org.pentaho.di.trans.step.StepDataInterface;
15
import org.pentaho.di.trans.step.StepInterface;
16
import org.pentaho.di.trans.step.StepMeta;
17
import org.pentaho.di.trans.step.StepMetaInterface;
18
19
/**
20
 * Step GraphTriplify.
21
 * <p />
22
 * 
23
 * @author Rogers Reiche de Mendonca
24
 * 
25
 */
26
public class GraphTriplifyStep extends BaseStep implements StepInterface {
27
	public GraphTriplifyStep(StepMeta stepMeta, StepDataInterface stepDataInterface, int copyNr, TransMeta transMeta,
28
			Trans trans) {
29
		super(stepMeta, stepDataInterface, copyNr, transMeta, trans);
30
	}
31
32
	@Override
33
	public boolean init(StepMetaInterface smi, StepDataInterface sdi) {
34
		if (super.init(smi, sdi)) {
35
			return true;
36
		} else
37
			return false;
38
	}
39
40
	@Override
41
	public void dispose(StepMetaInterface smi, StepDataInterface sdi) {
42
		super.dispose(smi, sdi);
43
	}
44
45
	/**
46
	 * Metodo chamado para cada linha que entra no step
47
	 */
48 View Code Duplication
	public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
49
		GraphTriplifyStepMeta meta = (GraphTriplifyStepMeta) smi;
50
		GraphTriplifyStepData data = (GraphTriplifyStepData) sdi;
51
52
		// Obtem linha do fluxo de entrada e termina caso nao haja mais entrada
53
		Object[] row = getRow();
54
		if (row == null) { // Nao ha mais linhas de dados
55
			setOutputDone();
56
			return false;
57
		}
58
59
		// Executa apenas uma vez. Variavel first definida na superclasse com
60
		// valor true
61
		if (first) {
62
			first = false;
63
64
			// Obtem todas as colunas ateh o step anterior.
65
			// Chamar apenas apos chamar getRow()
66
			data.outputRowMeta = new RowMeta();
67
68
			// Adiciona os metadados do step atual
69
			meta.getFields(data.outputRowMeta, getStepname(), null, null, this);
70
		}
71
72
		// Logica do step
73
		// Leitura de campos Input
74
		RowMetaInterface rowMeta = getInputRowMeta();
75
		int indexGraph = rowMeta.indexOfValue(meta.getInputGraph());
76
		Object graph = (indexGraph >= 0) ? row[indexGraph] : null;
77
78
		// Set output row
79
		Method[] methods = graph.getClass().getMethods();
80
		boolean hasListStatements = false;
81
		for (Method method : methods) {
82
			if (method.getName().equals("listStatements")) {
83
				hasListStatements = true;
84
				break;
85
			}
86
		}
87
88
		if (hasListStatements) {
89
			tripleWriter(graph, null, data);
90
		}
91
92
		return true;
93
	}
94
95
	private int tripleWriter(Object model, Object[] row, GraphTriplifyStepData data) throws KettleStepException {
96
		int numPutRows = 0;
97
98
		// StmtIterator it = model.listStatements();
99
		// Testando usar reflexion
100
		Object it = null;
101
		try {
102
			it = model.getClass().getMethod("listStatements").invoke(model);
103
			if (it == null)
104
				return 0;
105
106
			// while (it.hasNext())
107
			while ((Boolean) it.getClass().getMethod("hasNext").invoke(it)) {
108
				// Statement stmt = it.next();
109
				Object stmt = it.getClass().getMethod("next").invoke(it);
110
111
				incrementLinesInput();
112
113
				// String subject = stmt.getSubject().toString();
114
				String subject = stmt.getClass().getMethod("getSubject").invoke(stmt).toString();
115
				// String predicate = stmt.getPredicate().toString();
116
				String predicate = stmt.getClass().getMethod("getPredicate").invoke(stmt).toString();
117
				// String object = stmt.getObject().toString();
118
				String object = stmt.getClass().getMethod("getObject").invoke(stmt).toString();
119
120
				if (subject != null && !subject.isEmpty() && predicate != null && !predicate.isEmpty()
121
						&& object != null) {
122
					// Monta linha com a tripla
123
					Object[] outputRow = row;
124
					int i = 0;
125
					outputRow = RowDataUtil.addValueData(outputRow, i++, subject);
126
					outputRow = RowDataUtil.addValueData(outputRow, i++, predicate);
127
					outputRow = RowDataUtil.addValueData(outputRow, i++, object);
128
129
					// Joga tripla no fluxo
130
					putRow(data.outputRowMeta, outputRow);
131
132
					numPutRows++;
133
				} else {
134
					String triplaIgnorada = stmt.getClass().getMethod("getString").invoke(stmt).toString();
135
					logBasic("Tripla ignorada: " + triplaIgnorada);
136
				}
137
			}
138
		} catch (IllegalAccessException e) {
139
			// TODO Auto-generated catch block
140
			e.printStackTrace();
141
		} catch (IllegalArgumentException e) {
142
			// TODO Auto-generated catch block
143
			e.printStackTrace();
144
		} catch (InvocationTargetException e) {
145
			// TODO Auto-generated catch block
146
			e.printStackTrace();
147
		} catch (NoSuchMethodException e) {
148
			// TODO Auto-generated catch block
149
			e.printStackTrace();
150
		} catch (SecurityException e) {
151
			// TODO Auto-generated catch block
152
			e.printStackTrace();
153
		}
154
155
		return numPutRows;
156
	}
157
}
158