package com.onaro.sanscreen.client.view.task.actions.inventory; import com.onaro.sanscreen.task.interfaces.data.inventory.Device; import com.onaro.sanscreen.task.interfaces.data.inventory.Volume; import com.onaro.sanscreen.task.interfaces.data.inventory.InventoryException; import com.onaro.sanscreen.types.ObjectType; import com.onaro.sanscreen.client.error.CentralErrorHandler; import com.onaro.sanscreen.client.view.task.actions.inventory.InventoryTreeModel.ListNode; import java.util.*; public class VolumeTreeModelFilter extends InventoryTreeModelFilter { private String storageIp; private InventoryTreeModel.ListNode[] root; private Map> volumesByStorageId = new HashMap>(); private Set acceptedVolumeMaskStatus; /** * Filter only for storages * * @param storageIp if storage == null get all storages LUNs * alse return the specific storage LUNs */ public VolumeTreeModelFilter(String storageIp) { this.storageIp = storageIp; } public VolumeTreeModelFilter(String storageIp, Set acceptedVolumeMaskStatus) { this.storageIp = storageIp; this.acceptedVolumeMaskStatus = acceptedVolumeMaskStatus; } public void setInventoryTreeModel(InventoryTreeModel inventoryTreeModel) throws InventoryException { super.setInventoryTreeModel(inventoryTreeModel); if (storageIp != null) { InventoryTreeModel.ListNode listNode = inventoryTreeModel.getStorageListNode(); for (Iterator itr = listNode.getList().iterator(); itr.hasNext();) { final Device storage = itr.next(); if (storage.getIp().equals(storageIp)) { List list = new LinkedList(); list.add(storage); root = new InventoryTreeModel.ListNode[]{new InventoryTreeModel.SimpleListNode("Storage Devices", list)}; //$NON-NLS-1$ return; } } root = new InventoryTreeModel.ListNode[]{ }; } else { root = new InventoryTreeModel.ListNode[]{ inventoryTreeModel.getStorageListNode(), }; } } public int getChildCount(Object parent) { if (parent == root) return root.length; if (parent instanceof Device) { // Under the storage, show only the volumes list Device device = (Device) parent; if (ObjectType.STORAGE.equals(device.getType())) return 1; else return 0; } return inventoryTreeModel.getChildCount(parent); } public Object getRoot() { return root; } public Object getChild(Object parent, int index) { if (parent == root) { return root[index]; } else if (acceptedVolumeMaskStatus != null && parent instanceof Device && index == 0) { try { Device storage = (Device) parent; InventoryTreeModel.ListNode volumes = volumesByStorageId.get(storage.getId()); if (volumes == null) { @SuppressWarnings("unchecked") InventoryTreeModel.ListNode srcVolumeListNode = (InventoryTreeModel.ListNode) inventoryTreeModel.getChild(parent, index); assert "Volumes".equals(srcVolumeListNode.getType()) : "Expected list of volumes, found " + srcVolumeListNode; //$NON-NLS-1$ //$NON-NLS-2$ List srcVolumeList = srcVolumeListNode.getList(); List volumeList = new ArrayList(srcVolumeList.size()); for (int i = 0; i < srcVolumeList.size(); i++) { Volume volume = srcVolumeList.get(i); if (acceptedVolumeMaskStatus.contains(volume.getVolumeMaskingStatus())) { volumeList.add(volume); } } volumes = new InventoryTreeModel.SimpleListNode(srcVolumeListNode.getType(), volumeList, InventoryTreeModel.volumeComparator); volumesByStorageId.put(storage.getId(), volumes); } return volumes; } catch (InventoryException e) { CentralErrorHandler.handle("Failed getting inventory data from the server", this, e); //$NON-NLS-1$ return null; } } return inventoryTreeModel.getChild(parent, index); } public int getIndexOfChild(Object parent, Object child) { if (parent == null || child == null) { return -1; } if (parent == root) { for (int i = 0; i < root.length; ++i) { if (root[i] == parent) return i; } return root.length; } return inventoryTreeModel.getIndexOfChild(parent, child); } public boolean isLeaf(Object node) { return inventoryTreeModel.isLeaf(node); } }