1 |
779bac69
|
Marc NICOLAS
|
|
2 |
|
|
|
3 |
|
|
|
4 |
|
|
|
5 |
|
|
|
6 |
|
|
|
7 |
|
|
package fr.ias.sitools.converters;
|
8 |
|
|
|
9 |
|
|
import fr.cnes.sitools.common.validator.ConstraintViolation;
|
10 |
|
|
import fr.cnes.sitools.common.validator.ConstraintViolationLevel;
|
11 |
|
|
import fr.cnes.sitools.common.validator.Validator;
|
12 |
|
|
import fr.cnes.sitools.dataset.converter.business.AbstractConverter;
|
13 |
|
|
import fr.cnes.sitools.dataset.converter.model.ConverterParameter;
|
14 |
|
|
import fr.cnes.sitools.dataset.converter.model.ConverterParameterType;
|
15 |
|
|
import fr.cnes.sitools.datasource.jdbc.model.AttributeValue;
|
16 |
|
|
import fr.cnes.sitools.datasource.jdbc.model.Record;
|
17 |
|
|
import java.util.HashSet;
|
18 |
|
|
import java.util.Map;
|
19 |
|
|
import java.util.Set;
|
20 |
|
|
import java.util.logging.Level;
|
21 |
|
|
import java.util.logging.Logger;
|
22 |
|
|
|
23 |
|
|
|
24 |
|
|
|
25 |
|
|
|
26 |
|
|
|
27 |
|
|
public class FileSizeConverter extends AbstractConverter {
|
28 |
|
|
|
29 |
|
|
|
30 |
|
|
private static final Logger LOGGER = Logger.getLogger(FileSizeConverter.class.getName());
|
31 |
|
|
|
32 |
|
|
public FileSizeConverter() {
|
33 |
|
|
|
34 |
|
|
this.setName("FileSizeConverter");
|
35 |
|
|
this.setDescription("A converter applying an unit conversion from bytes into Kb or Mb or Gb");
|
36 |
|
|
this.setClassAuthor("Marc NICOLAS");
|
37 |
|
|
this.setClassOwner("IAS");
|
38 |
|
|
this.setClassVersion("0.1");
|
39 |
|
|
|
40 |
|
|
ConverterParameter colFileSize = new ConverterParameter("colFileSizeInBytes", "row of dataset with the filesize in bytes",ConverterParameterType.CONVERTER_PARAMETER_IN);
|
41 |
|
|
ConverterParameter colFileSizeConvert = new ConverterParameter("colFileSizeConvert", "File Size converted", ConverterParameterType.CONVERTER_PARAMETER_OUT);
|
42 |
|
|
this.addParam(colFileSize);
|
43 |
|
|
this.addParam(colFileSizeConvert);
|
44 |
|
|
|
45 |
|
|
LOGGER.log(Level.INFO, "Converter :{0} version {1}", new Object[] { this.getName(), this.getClassVersion() });
|
46 |
|
|
}
|
47 |
|
|
|
48 |
|
|
@Override
|
49 |
|
|
public Record getConversionOf(Record record) throws Exception {
|
50 |
|
|
Record out = record;
|
51 |
|
|
String result = "";
|
52 |
|
|
|
53 |
|
|
AttributeValue attrOut = this.getOutParam("colFileSizeConvert", out);
|
54 |
|
|
|
55 |
|
|
AttributeValue attrIn = this.getInParam("colFileSizeInBytes", out);
|
56 |
|
|
|
57 |
|
|
if (!isNull(attrIn) && !isNull(attrOut)) {
|
58 |
|
|
try{
|
59 |
|
|
double input = Double.parseDouble(attrIn.getValue().toString());
|
60 |
|
|
if(input>0 && input<1024){
|
61 |
|
|
result = (double)Math.round(input*10)/10 +" o";
|
62 |
|
|
}else if(input >= 1024 && input < Math.pow(1024, 2) ){
|
63 |
|
|
result = (double)Math.round((input/1024)*10)/10+" Ko";
|
64 |
|
|
}else if(input >=Math.pow(1024, 2) && input < Math.pow(1024, 3)){
|
65 |
|
|
result = (double)Math.round((input/Math.pow(1024, 2))*10)/10+" Mo" ;
|
66 |
|
|
}else if( input >= Math.pow(1024, 3) && input < Math.pow(1024, 4)){
|
67 |
|
|
result = (double)Math.round((input/Math.pow(1024, 3))*10)/10+" Go" ;
|
68 |
|
|
}
|
69 |
|
|
|
70 |
|
|
if(!result.isEmpty()){
|
71 |
|
|
attrOut.setValue(result);
|
72 |
|
|
}
|
73 |
|
|
}catch (Exception e) {
|
74 |
|
|
attrOut.setValue(Double.NaN);
|
75 |
|
|
}
|
76 |
|
|
}
|
77 |
|
|
|
78 |
|
|
return out;
|
79 |
|
|
}
|
80 |
|
|
|
81 |
|
|
@Override
|
82 |
|
|
public Validator<AbstractConverter> getValidator() {
|
83 |
|
|
|
84 |
|
|
return new Validator<AbstractConverter>() {
|
85 |
|
|
|
86 |
|
|
@Override
|
87 |
|
|
public Set<ConstraintViolation> validate(AbstractConverter item) {
|
88 |
|
|
Set<ConstraintViolation> constraints = new HashSet<ConstraintViolation>();
|
89 |
|
|
Map<String, ConverterParameter> params = item.getParametersMap();
|
90 |
|
|
ConverterParameter param = params.get("colFileSizeInBytes");
|
91 |
|
|
|
92 |
|
|
if(param.getAttachedColumn().isEmpty()){
|
93 |
|
|
ConstraintViolation constraint = new ConstraintViolation();
|
94 |
|
|
constraint.setMessage("You must choose the column with filesize in bytes data");
|
95 |
|
|
constraint.setLevel(ConstraintViolationLevel.CRITICAL);
|
96 |
|
|
constraint.setValueName(param.getName());
|
97 |
|
|
constraints.add(constraint);
|
98 |
|
|
}
|
99 |
|
|
|
100 |
|
|
param = params.get("colFileSizeConvert");
|
101 |
|
|
|
102 |
|
|
if(param.getAttachedColumn().isEmpty()){
|
103 |
|
|
ConstraintViolation constraint = new ConstraintViolation();
|
104 |
|
|
constraint.setMessage("You must choose the output column");
|
105 |
|
|
constraint.setLevel(ConstraintViolationLevel.CRITICAL);
|
106 |
|
|
constraint.setValueName(param.getName());
|
107 |
|
|
constraints.add(constraint);
|
108 |
|
|
}
|
109 |
|
|
|
110 |
|
|
return constraints;
|
111 |
|
|
}
|
112 |
|
|
};
|
113 |
|
|
}
|
114 |
|
|
|
115 |
|
|
|
116 |
|
|
} |