Conditions | 12 |
Total Lines | 105 |
Code Lines | 77 |
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.DataCubeStep.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; |
||
55 | public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException { |
||
56 | |||
57 | DataCubeStepMeta meta = (DataCubeStepMeta) smi; |
||
58 | DataCubeStepData data = (DataCubeStepData) sdi; |
||
59 | |||
60 | Object[] row = getRow(); |
||
61 | if (row == null) { |
||
62 | setOutputDone(); |
||
63 | return false; |
||
64 | } |
||
65 | |||
66 | if (first) { |
||
67 | first = false; |
||
68 | |||
69 | RowMetaInterface rowMeta = getInputRowMeta(); |
||
70 | data.outputRowMeta = rowMeta.clone(); |
||
71 | |||
72 | // Adiciona os metadados do step atual |
||
73 | meta.getFields(data.outputRowMeta, getStepname(), null, null, this); |
||
74 | |||
75 | DataTable<String> vocabularyTable = meta.getVocabularyTable(); |
||
76 | for (int k = 0; k < vocabularyTable.size(); k++) { |
||
77 | String prefix = vocabularyTable.getValue(k, DataCubeStepMeta.Field.VOCABULARY_TABLE_PREFIX.name()); |
||
78 | String uri = vocabularyTable.getValue(k, DataCubeStepMeta.Field.VOCABULARY_TABLE_URI.name()); |
||
79 | putOutRow(row, meta, data, this.formatPrefix(prefix) + " " + this.formatUri(uri) + " ."); |
||
80 | } |
||
81 | |||
82 | putOutRow(row, meta, data, "", |
||
83 | "<http://purl.org/linked-data/cube> a owl:Ontology ;", |
||
84 | "rdfs:label \"Example DataCube Knowledge Base\" ;", |
||
85 | "dc:description \"This knowledgebase contains one Data Structure Definition with one Data Set. This Data Set has a couple of Components and Observations.\" .", |
||
86 | "", |
||
87 | "# Data Structure Definitions", |
||
88 | "", |
||
89 | "ex:dsd a cube:DataStructureDefinition ;", |
||
90 | " rdfs:label \"A Data Structure Definition\"@en ;", |
||
91 | " rdfs:comment \"Defines the structure of a DataSet or slice.\" ;"); |
||
92 | |||
93 | DataTable<String> dimensionTable = meta.getDimensionTable(); |
||
94 | for (int k = 0; k < dimensionTable.size(); k++) { |
||
95 | String dimURI = dimensionTable.getValue(k, DataCubeStepMeta.Field.DIMENSION_TABLE_URI.name()); |
||
96 | if (k == 0) { |
||
97 | putOutRow(row, meta, data, " cube:component " + this.formatUri(dimURI) + ","); |
||
98 | } else if (k == dimensionTable.size() - 1) { |
||
99 | putOutRow(row, meta, data, " " + this.formatUri(dimURI) + " ."); |
||
100 | } else { |
||
101 | putOutRow(row, meta, data, " " + this.formatUri(dimURI) + ","); |
||
102 | } |
||
103 | } |
||
104 | |||
105 | putOutRow(row, meta, data, "", "# Component Specifications", ""); |
||
106 | |||
107 | for (int k = 0; k < dimensionTable.size(); k++) { |
||
108 | String label = dimensionTable.getValue(k, DataCubeStepMeta.Field.DIMENSION_TABLE_LABEL.name()); |
||
109 | String dimURI = dimensionTable.getValue(k, DataCubeStepMeta.Field.DIMENSION_TABLE_URI.name()); |
||
110 | String dimName = dimensionTable.getValue(k, DataCubeStepMeta.Field.DIMENSION_TABLE_NAME.name()); |
||
111 | putOutRow(row, meta, data, |
||
112 | this.formatUri(dimURI) + " a cube:ComponentSpecification ;", |
||
113 | " rdfs:label \"" + label + "\" ;", |
||
114 | " cube: cube:dimension exProp:" + dimName + " .", ""); |
||
115 | } |
||
116 | |||
117 | putOutRow(row, meta, data, "# Data Set", |
||
118 | "rdfs:label \"A DataSet\"^^<http://www.w3.org/2001/XMLSchema#string> ;", |
||
119 | "rdfs:comment \"Represents a collection of observations and conforming to some common dimensional structure.\" ;", |
||
120 | "cube:structure ex:dsd .", "", "# Dimensions, Unit and Measure"); |
||
121 | |||
122 | for (int k = 0; k < dimensionTable.size(); k++) { |
||
123 | String type = dimensionTable.getValue(k, DataCubeStepMeta.Field.DIMENSION_TABLE_TYPE.name()); |
||
124 | String label = dimensionTable.getValue(k, DataCubeStepMeta.Field.DIMENSION_TABLE_LABEL.name()); |
||
125 | String dimName = dimensionTable.getValue(k, DataCubeStepMeta.Field.DIMENSION_TABLE_NAME.name()); |
||
126 | if (type.trim().equals("")) { // it's not a measure |
||
127 | putOutRow(row, meta, data, |
||
128 | "exProp:" + dimName + " a cube:DimensionProperty ;", |
||
129 | " rdfs:label \"" + label + "\"@en .", ""); |
||
130 | } else { |
||
131 | putOutRow(row, meta, data, "exProp:unit a cube:AttributeProperty ;", |
||
132 | " exProp:" + dimName + " a cube:MeasureProperty ;", |
||
133 | " rdfs:label \"" + label + "\"@en .", ""); |
||
134 | } |
||
135 | } |
||
136 | |||
137 | } |
||
138 | |||
139 | putOutRow(row, meta, data, "ex:ob" + i + " a cube:Observation;", |
||
140 | " cube:dataSet ex:dataset;"); |
||
141 | |||
142 | DataTable<String> dimensionTable = meta.getDimensionTable(); |
||
143 | for (int k = 0; k < dimensionTable.size(); k++) { |
||
144 | String dimName = dimensionTable.getValue(k, DataCubeStepMeta.Field.DIMENSION_TABLE_NAME.name()); |
||
145 | String type = dimensionTable.getValue(k, DataCubeStepMeta.Field.DIMENSION_TABLE_TYPE.name()); |
||
146 | String label = dimensionTable.getValue(k, DataCubeStepMeta.Field.DIMENSION_TABLE_LABEL.name()); |
||
147 | if (type.trim().equals("")) { |
||
148 | putOutRow(row, meta, data, " " + String.format(OBJ, dimName, getInputRowMeta().getString(row, dimName, ""))); |
||
149 | } else { |
||
150 | putOutRow(row, meta, data, |
||
151 | " exProp:unit \"" + label + "\";", |
||
152 | " exProp:value \"" + getInputRowMeta().getString(row, dimName, "") + "\"^^" + type + ";"); |
||
153 | } |
||
154 | } |
||
155 | |||
156 | putOutRow(row, meta, data, " rdfs:label \"\".", ""); |
||
157 | |||
158 | i++; |
||
159 | return true; |
||
160 | } |
||
172 | } |