Conditions | 23 |
Total Lines | 99 |
Code Lines | 56 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 1 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like br.ufrj.ppgi.greco.kettle.DataPropertyMappingStep.processRow(StepMetaInterface,StepDataInterface) 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; |
||
52 | public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException { |
||
53 | |||
54 | DataPropertyMappingStepMeta meta = (DataPropertyMappingStepMeta) smi; |
||
55 | DataPropertyMappingStepData data = (DataPropertyMappingStepData) sdi; |
||
56 | |||
57 | // Obtem linha do fluxo de entrada e termina caso nao haja mais entrada |
||
58 | Object[] row = getRow(); |
||
59 | if (row == null) { // Nao ha mais linhas de dados |
||
60 | setOutputDone(); |
||
61 | return false; |
||
62 | } |
||
63 | |||
64 | if (first) { // Executa apenas uma vez. Variavel first definida na |
||
65 | // superclasse |
||
66 | first = false; |
||
67 | |||
68 | // Obtem todas as colunas ate o step anterior. |
||
69 | // Chamar apenas apos chamar getRow() |
||
70 | RowMetaInterface rowMeta = getInputRowMeta(); |
||
71 | data.outputRowMeta = rowMeta.clone(); |
||
72 | |||
73 | // Adiciona os metadados do step atual |
||
74 | meta.getFields(data.outputRowMeta, getStepname(), null, null, this); |
||
75 | |||
76 | // TODO Outras opera��es que devem ser executadas apenas uma vez |
||
77 | } |
||
78 | |||
79 | /* |
||
80 | * Logica do step: leitura de campos de entrada e internos e geracao do |
||
81 | * campo de saida |
||
82 | */ |
||
83 | |||
84 | // Add rdf:type |
||
85 | String subject = getInputRowMeta().getString(row, meta.getSubjectUriFieldName(), ""); |
||
86 | |||
87 | List<String> typesUri = meta.getRdfTypeUris(); |
||
88 | Iterator<String> it = typesUri.iterator(); |
||
89 | while (it.hasNext()) { |
||
90 | String type = (String) it.next(); |
||
91 | putOutRow(row, meta, data, subject, "http://www.w3.org/1999/02/22-rdf-syntax-ns#type", type); |
||
92 | } |
||
93 | |||
94 | // Add data properties |
||
95 | DataTable<String> table = meta.getMapTable(); |
||
96 | for (int i = 0; i < table.size(); i++) { |
||
97 | |||
98 | String predicateField = table.getValue(i, DataPropertyMappingStepMeta.Field.MAP_TABLE_PREDICATE_URI.name()); |
||
99 | String predicate = getInputRowMeta().getString(row, predicateField, predicateField); |
||
100 | |||
101 | String objectField = table.getValue(i, |
||
102 | DataPropertyMappingStepMeta.Field.MAP_TABLE_OBJECT_FIELD_NAME.name()); |
||
103 | int index = getInputRowMeta().indexOfValue(objectField); |
||
104 | |||
105 | String datatype = table.getValue(i, DataPropertyMappingStepMeta.Field.MAP_TABLE_TYPED_LITERAL.name()); |
||
106 | |||
107 | String langtagValue = table.getValue(i, DataPropertyMappingStepMeta.Field.MAP_TABLE_LANGUAGE_TAG.name()); |
||
108 | String langtagField = table.getValue(i, |
||
109 | DataPropertyMappingStepMeta.Field.MAP_TABLE_LANGTAG_FIELD_NAME.name()); |
||
110 | |||
111 | String langtag = null; |
||
112 | if (langtagField != null && !langtagField.isEmpty()) { |
||
113 | langtag = getInputRowMeta().getString(row, getInputRowMeta().indexOfValue(langtagField)); |
||
114 | } |
||
115 | if (langtag == null || langtag.isEmpty()) |
||
116 | langtag = langtagValue; |
||
117 | |||
118 | String object = null; |
||
119 | |||
120 | try { |
||
121 | if ("xsd:float".equals(datatype) || "xsd:double".equals(datatype) || "xsd:decimal".equals(datatype)) { |
||
122 | object = new DecimalFormat("0.0#########", new DecimalFormatSymbols(Locale.US)) |
||
123 | .format(getInputRowMeta().getNumber(row, index)); |
||
124 | } else if ("xsd:dateTime".equals(datatype)) { |
||
125 | object = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'") |
||
126 | .format(getInputRowMeta().getDate(row, index)); |
||
127 | } else if ("xsd:date".equals(datatype)) { |
||
128 | object = new SimpleDateFormat("yyyy-MM-dd").format(getInputRowMeta().getDate(row, index)); |
||
129 | } else if ("xsd:integer".equals(datatype)) { |
||
130 | object = getInputRowMeta().getInteger(row, index).toString(); |
||
131 | } else { |
||
132 | object = getInputRowMeta().getString(row, index); |
||
133 | } |
||
134 | } catch (Exception e) { |
||
135 | object = ""; |
||
136 | } |
||
137 | |||
138 | // Rogers (Jul./2012): Quando o repositorio e' database, o valor do |
||
139 | // datatype quando vazio e' null. |
||
140 | if (datatype != null) { |
||
141 | datatype = datatype.replace("xsd:", "http://www.w3.org/2001/XMLSchema#"); |
||
142 | } |
||
143 | |||
144 | if (subject != null && predicate != null && object != null && !"".equals(subject) && !"".equals(predicate) |
||
145 | && !"".equals(object)) { |
||
146 | putOutRow(row, meta, data, subject, predicate, object, datatype, langtag); |
||
147 | } |
||
148 | } |
||
149 | |||
150 | return true; |
||
151 | } |
||
204 |