* @param datamart the datamart
*/
private void decorateMap(SVGDocument masterMap, SVGDocument targetMap, DataMart datamart) {
IDataStore dataStore = datamart.getDataStore();
IDataStoreMetaData dataStoreMeta = dataStore.getMetaData();
List measureFieldsMeta = dataStoreMeta.findFieldMeta("ROLE", "MEASURE");
String[] kpiNames = new String[measureFieldsMeta.size()];
for(int i = 0; i < kpiNames.length; i++) {
IFieldMetaData filedMeta = (IFieldMetaData)measureFieldsMeta.get(i);
kpiNames[i] = filedMeta.getName();
}
//int selectedKpiIndex = dataStoreMeta.getFieldIndex( getSelectedMeasureName() );
String selectedKpiName = getSelectedMeasureName(); //kpiNames[selectedKpiIndex];
Assert.assertTrue(selectedKpiName != null, "default_kpi attribute cannot be null. Please add it to MEASURES tag in your template file");
Measure measure = getMeasure( selectedKpiName );
Number lb_value = null;
Number ub_value = null;
String lb_color = null;
String ub_color = null;
String null_values_color = null;
String[] trasholdCalculationPercParams = null;
Integer num_group = null;
Integer trasholdCalculationUniformParams = null;
String colorRangeCalculationGradParams = null;
String[] col_kpi_array = null;
Number[] trash_kpi_array = null;
Number[] kpi_ordered_values = null;
dataStore.sortRecords( dataStoreMeta.getFieldIndex(selectedKpiName) );
List orderedKpiValuesSet = dataStore.getFieldValues( dataStoreMeta.getFieldIndex(selectedKpiName) );
//Set orderedKpiValuesSet = datamart.getOrderedKpiValuesSet( selectedKpiName );
kpi_ordered_values = (Number[])orderedKpiValuesSet.toArray(new Number[0]);
if( measure.getTresholdLb() == null
|| measure.getTresholdLb().trim().equalsIgnoreCase("")
|| measure.getTresholdLb().equalsIgnoreCase("none") ) {
lb_value = null;
} else {
lb_value = Double.parseDouble( measure.getTresholdLb() );
}
if( measure.getTresholdUb() == null
|| measure.getTresholdUb().trim().equalsIgnoreCase("")
|| measure.getTresholdUb().equalsIgnoreCase("none") ) {
ub_value = null;
} else {
ub_value = Double.parseDouble( measure.getTresholdUb() );
}
lb_color = measure.getColurOutboundCol();
ub_color = measure.getColurOutboundCol();
null_values_color = measure.getColurNullCol();
String numGroupAttr = measure.getTresholdCalculatorParameters().getProperty("GROUPS_NUMBER");
if( numGroupAttr != null ) {
num_group = Integer.parseInt( numGroupAttr );
trasholdCalculationUniformParams = num_group;
}
colorRangeCalculationGradParams = measure.getColurCalculatorParameters().getProperty("BASE_COLOR");
//////////////////////////////////////////////////////////////////////////
// SetTrashHolds
///////////////
if(lb_value == null) {
lb_value = kpi_ordered_values[0];
}
if(ub_value == null) {
ub_value = kpi_ordered_values[kpi_ordered_values.length-1];
}
if(lb_value.doubleValue() > ub_value.doubleValue()) {
Number t = lb_value;
ub_value = lb_value;
lb_value = t;
}
if(ub_value.doubleValue() < kpi_ordered_values[0].doubleValue() || lb_value.doubleValue() > kpi_ordered_values[kpi_ordered_values.length-1].doubleValue()) {
lb_value = kpi_ordered_values[0];
ub_value = kpi_ordered_values[kpi_ordered_values.length-1];
}
if( measure.getTresholdCalculatorType().equalsIgnoreCase("quantile") ) {
trash_kpi_array = new Number[num_group + 1];
int diff_value_num = 0;
int start_index = -1;
if(kpi_ordered_values[0].doubleValue() >= lb_value.doubleValue() && kpi_ordered_values[kpi_ordered_values.length-1].doubleValue() <= ub_value.doubleValue()) {
diff_value_num = kpi_ordered_values.length;
start_index = 0;
} else {
for(int j = 0; j < kpi_ordered_values.length; j++) {
if(kpi_ordered_values[j].doubleValue() >= lb_value.doubleValue() && kpi_ordered_values[j].doubleValue() <= ub_value.doubleValue()) {
start_index = (start_index == -1?j:start_index);
diff_value_num++;
}
}
}
if(diff_value_num < num_group) num_group = diff_value_num;
int blockSize = (int)Math.floor( diff_value_num / num_group );
trash_kpi_array[0] = lb_value;
for(int j = 1; j < num_group; j++){
trash_kpi_array[j] = kpi_ordered_values[start_index + (j*blockSize)];
}
trash_kpi_array[num_group] = ub_value;
} else if ( measure.getTresholdCalculatorType().equalsIgnoreCase("perc") ) {
double range = ub_value.doubleValue() - lb_value.doubleValue();
trasholdCalculationPercParams = getTresholdsArray(measure.getColumnId());
trash_kpi_array = new Number[trasholdCalculationPercParams.length + 1];
trash_kpi_array[0] = lb_value;
for(int j = 0; j < trasholdCalculationPercParams.length; j++) {
double groupSize = (range / 100.0) * Double.parseDouble(trasholdCalculationPercParams[j]);
trash_kpi_array[j+1] = trash_kpi_array[j].doubleValue() + groupSize;
}
trash_kpi_array[ trash_kpi_array.length - 1] = ub_value;
num_group = trash_kpi_array.length - 1;
} else if ( measure.getTresholdCalculatorType().equalsIgnoreCase("uniform") ) {
trash_kpi_array = new Number[ trasholdCalculationUniformParams.intValue() + 1 ];
double perc = 100 / (trasholdCalculationUniformParams.doubleValue());
trasholdCalculationPercParams = new String[trasholdCalculationUniformParams.intValue() + 1];
for(int j = 0; j < trasholdCalculationPercParams.length; j++) {
trasholdCalculationPercParams[j] = "" + perc;
}
double range = ub_value.doubleValue() - lb_value.doubleValue();
trash_kpi_array[0] = lb_value;
for(int j = 0; j < trash_kpi_array.length-2; j++) {
double groupSize = (range / 100.0) * Double.parseDouble(trasholdCalculationPercParams[j]);
trash_kpi_array[j+1] = trash_kpi_array[j].doubleValue() + groupSize;
}
trash_kpi_array[ trash_kpi_array.length-1 ] = ub_value;
num_group = trasholdCalculationPercParams.length - 1;
} else if ( measure.getTresholdCalculatorType().equalsIgnoreCase("static") ) {
String[] trasholdsArray = getTresholdsArray( selectedKpiName );
trash_kpi_array = new Number[trasholdsArray.length];
for(int j = 0; j < trasholdsArray.length; j++) {
trash_kpi_array[j] = new Double( trasholdsArray[j] );
}
} else {
//setQuantileTrasholds(kpi_names[i]);
}
if(num_group == null) { // static case, num_group is calculated from bounds
num_group = new Integer(trash_kpi_array.length-1);
}
if(measure.getColurCalculatorType().equalsIgnoreCase("static")) {
col_kpi_array = getColoursArray( selectedKpiName );
} else if(measure.getColurCalculatorType().equalsIgnoreCase("gradient") || measure.getColurCalculatorType().equalsIgnoreCase("grad")) {
col_kpi_array = getGradientColourRange(colorRangeCalculationGradParams, num_group);
} else {
col_kpi_array = getGradientColourRange(colorRangeCalculationGradParams, num_group);
}
logger.debug( Arrays.toString( col_kpi_array ) );
Element targetLayer = targetMap.getElementById(datamart.getTargetFeatureName());
NodeList nodeList = targetLayer.getChildNodes();
for(int i = 0; i < nodeList.getLength(); i++){
Node childNode = (Node)nodeList.item(i);
if(childNode instanceof Element) {
SVGElement child = (SVGElement)childNode;
String childId = child.getId();
String column_id = childId.replaceAll(datamart.getTargetFeatureName() + "_", "");
IRecord record = dataStore.getRecordByID( column_id );
//Map attributes = (Map)datamart.getAttributeseById(column_id);
String targetColor = null;
Number kpyValue = null;
if(record != null) {
IField field = record.getFieldAt( dataStoreMeta.getFieldIndex(selectedKpiName) );
String kpyValueAttr = "" + field.getValue();
//String kpyValueAttr = (String)attributes.get( selectedKpiName );
if(kpyValueAttr == null) {
targetColor = null_values_color;
} else {
kpyValue = Double.parseDouble(kpyValueAttr);
if(kpyValue.doubleValue() < lb_value.doubleValue()) {
targetColor = lb_color;
} else if(kpyValue.doubleValue() > ub_value.doubleValue()) {
targetColor = ub_color;
} else if(kpyValue.doubleValue() == ub_value.doubleValue()) {
targetColor = col_kpi_array[trash_kpi_array.length-2];
} else {
for (int j = 0; j < trash_kpi_array.length-1; j++) {
if (kpyValue.doubleValue() >= trash_kpi_array[j].doubleValue() && kpyValue.doubleValue() < trash_kpi_array[j+1].doubleValue()) {
targetColor = col_kpi_array[j];
break;
}
}
}
}
}
if(targetColor != null) {
if(child.getNodeName().equals("path")
|| child.getNodeName().equals("polygon")
|| child.getNodeName().equals("ellipse")
|| child.getNodeName().equals("circle")
|| child.getNodeName().equals("rect")
) {
child.setAttribute("fill", targetColor );
} else if(child.getNodeName().equals("line")
|| child.getNodeName().equals("polyline")
) {
child.setAttribute("stroke", targetColor );
}
String opacity = measure.getColurCalculatorParameters().getProperty("opacity");
if(opacity != null) {
child.setAttribute("opacity", opacity );
}
}
}
}
// add label
//Map values = datamart.getValues();
//Iterator it = values.keySet().iterator();
Iterator it = dataStore.iterator();
while(it.hasNext()) {
IRecord record = (IRecord)it.next();
IField field = null;
field = record.getFieldAt( dataStoreMeta.getIdFieldIndex() );
String id = (String)field.getValue();
//String id = (String)it.next();
//Map kpiValueMap = (Map)values.get(id);
String centroideId = "centroidi_" + datamart.getTargetFeatureName() + "_" + id;
Element centroide = targetMap.getElementById( centroideId );
if( centroide != null ) {
List fields = record.getFields();
int line = 0;
Element labelGroup = null;
if(fields.size()>0) labelGroup = masterMap.createElement("g");
boolean isFirst = true;
for(int i = 0; i < fields.size(); i++) {
if(i == dataStoreMeta.getIdFieldIndex()) continue;
field = (IField)fields.get(i);
String fieldName = dataStoreMeta.getFieldName(i);
//String tmpKpiName = (String)kpiValueIterator.next();
Measure kpi = getMeasure( fieldName );
String kpiValue = "" + field.getValue();