+ DashboardConstants.TAB_PATH
+ tabId
+ DashboardConstants.CURRENT_GADGET_LAYOUT_PATH;
}
Resource gadgetLayout;
if (registry.resourceExists(gadgetLayoutPath)) {
gadgetLayout = registry.get(gadgetLayoutPath);
} else {
gadgetLayout = registry.newCollection();
}
//get the column which the new gadget should be added to have a equal distribution <column, numberOfGadgets>
HashMap<Integer, Integer> gadgets = new HashMap<Integer, Integer>();
String layout = getGadgetLayout(userId, tabId, dashboardName);
if (layout.equals("NA")) {
layout = "{layout:[]}";
}
// to hold the sorted result
HashMap<Integer, Integer> map = new LinkedHashMap<Integer, Integer>();
JSONObject json = null;
try {
json = new JSONObject(layout);
} catch (Exception e) {
log.error("JSONParser unsuccessful : ", e);
}
for (int i = 0; i < json.getJSONArray("layout").length(); i++) {
JSONArray numberOfGadgetsInColumn = json.getJSONArray("layout").getJSONObject(i).getJSONArray("layout");
gadgets.put(i, numberOfGadgetsInColumn.length());
}
List<Integer> mapKeys = new ArrayList<Integer>(gadgets.keySet());
List<Integer> mapValues = new ArrayList<Integer>(gadgets.values());
TreeSet<Integer> sortedSet = new TreeSet<Integer>(mapValues);
Object[] sortedArray = sortedSet.toArray();
int size = sortedArray.length;
for (int i = 0; i < size; i++) {
map.put
(mapKeys.get(mapValues.indexOf(sortedArray[i])),
Integer.parseInt(sortedArray[i].toString()));
}
List<Integer> ref = new ArrayList<Integer>(map.keySet());
// Retrieving the gadget layout
String currentLayout = gadgetLayout
.getProperty(DashboardConstants.CURRENT_GADGET_LAYOUT);
int colNum = 0;
if (ref.size() != 0) {
colNum = ref.get(0);
}
gadgetLayout.setProperty(DashboardConstants.CURRENT_GADGET_LAYOUT,
insertGadgetToJsonLayout(currentLayout, gadgetId, colNum).toString());
registry.put(gadgetLayoutPath, gadgetLayout);
// Done