package com.onaro.sanscreen.client.view.changes.format; import java.text.NumberFormat; import java.text.ParseException; import java.util.Locale; import java.util.ResourceBundle; import com.onaro.sanscreen.server.changes.data.core.VolumePropertyChange; import org.apache.commons.lang3.StringUtils; import com.onaro.commons.text.SmallDoubleFormat; import com.onaro.commons.util.Unit; import com.onaro.sanscreen.client.Messages; import com.onaro.sanscreen.server.changes.Change; import com.onaro.sanscreen.server.changes.ChangeConstants; import com.onaro.util.IllegalInitException; public class VolumeFormatter extends BaseFormatter { private static NumberFormat NUM_FORMAT; static { // NUM_FORMAT = DecimalFormat.getNumberInstance(); /* Doubles from the server use String.valueOf(double) which always uses a comma thousand's separator and a decimal point which is the same as US English. So create an instance based on that locale. Note: some capacities from the server arrive as strings and we don't translate them. For example: Volumes 0CF1 (8.43GB), 0CF2 (8.43GB), 0CF3 (8.43GB), 0CF4 (8.43GB) are down. Total capacity is 33,72GB The list of volumes with their capacities are not reformatted by the client but the total is. So we have an inconsistency. */ NUM_FORMAT = NumberFormat.getInstance(Locale.US); NUM_FORMAT.setMaximumFractionDigits(2); } public VolumeFormatter(String key, ResourceBundle bundle) throws IllegalInitException { super(key, bundle); } protected String getValue(String attributeName, Change change,boolean allowNull, ChangeFormatPattern pattern) throws IllegalInitException { String origValue = super.getValue(attributeName, change, allowNull,pattern); if (ChangeConstants.PROPERTY_NAME.equals(attributeName) && VolumePropertyChange.Property.CAPACITY_MB.name().equals(origValue)) { return Messages.INSTANCE.getCapacityTooltipLabel(); } String rvValue = origValue; //the value of the volume capacity is formatted in GBs if (isCapacityProperty(attributeName, change)) { rvValue = "0"; //$NON-NLS-1$ } else if (isCapacity(attributeName)) { rvValue = ""; //$NON-NLS-1$ } if (origValue != null && (isCapacityProperty(attributeName, change) || isCapacity(attributeName))) { double capacity = parseCapacity(origValue, attributeName); if(capacity > 0){ rvValue = fixCapacity(capacity); } } if (StringUtils.isNotBlank(rvValue) && shouldAppendUnit(attributeName, change)) { rvValue = new StringBuilder(rvValue).append(' ').append(Messages.INSTANCE.getGBSuffix()).toString(); } return rvValue; } protected double parseCapacity(String value, String attributeName) { try { double capacityMB = NUM_FORMAT.parse(value).doubleValue(); return Unit.convert(capacityMB, Unit.MEGABYTE, Unit.GIGABYTE); } catch (ParseException e) { assert false : "Failed to parse attribute["+attributeName+"] with value["+value+"] as double."; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ return -1; } } protected boolean shouldAppendUnit(String attributeName, Change change) throws IllegalInitException { return isCapacity(attributeName) || isCapacityProperty(attributeName, change); } protected boolean isCapacity(String attributeName) { return attributeName.contains(ChangeConstants.CAPACITY);//capacity should always be reformated } protected boolean isCapacityProperty(String attributeName, Change change) throws IllegalInitException { return ((ChangeConstants.OLD_VALUE.equals(attributeName) || ChangeConstants.NEW_VALUE.equals(attributeName)) && //should be reformated only if value of capacity or free properties (getValue(ChangeConstants.PROPERTY_NAME,change).toUpperCase().contains("CAPACITY") || //$NON-NLS-1$ "Free".equalsIgnoreCase(getValue(ChangeConstants.PROPERTY_NAME,change)))); //$NON-NLS-1$ } protected String fixCapacity(double capacity) { return new SmallDoubleFormat().format(capacity); } }