/*
* Embedded Jopr Project
* Copyright (C) 2006-2009 Red Hat, Inc.
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
package org.jboss.on.embedded.ui;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.faces.model.SelectItem;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jetbrains.annotations.NotNull;
import org.rhq.core.domain.measurement.Availability;
import org.rhq.core.domain.resource.Resource;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.on.embedded.bean.ResourceListItem;
import org.jboss.on.embedded.manager.ResourceManager;
import org.jboss.on.embedded.manager.ResourceManagerFactory;
import org.jboss.on.embedded.ui.nav.JONTreeNode;
/**
* Class to hold common methods used by Seam Action classes
*
* @author Jessica Sant
*/
@Name("commonActionUtil")
public class CommonActionUtil
{
private final Log LOG = LogFactory.getLog(CommonActionUtil.class);
@In
private NavigationAction navigationAction;
/**
* Retrieves the ResourceListItem for the Resource specified by the current navigation path. This path must resolve
* to an id
*
* @return a resource list item representing the resource
*/
// TODO consider renaming this to something like getResourceByPath
// and also consider splitting out the retrieval of availability since
// not all clients need this
public
@NotNull
ResourceListItem getCurrentResourceListItem()
{
ResourceListItem resourceListItem;
Resource resource = getCurrentResource();
ResourceManager resourceManager = ResourceManagerFactory.resourceManager();
Availability availability = resourceManager.getAvailability(resource);
resourceListItem = new ResourceListItem(resource, availability);
return resourceListItem;
}
/**
* Retrieves the Resource specified by the current navigation path. This path must resolve to an id.
*
* @return the Resource specified by the current navigation path
*/
public
@NotNull
Resource getCurrentResource()
{
// there are plenty of other implementations we could use here
// e.g. navigationAction.getSelectedNode().getResource()
String path = navigationAction.getCurrentPath();
int resourceId;
try
{
// Temporary fix for tab error in Control and Metrics for Root Element
if (path.equals("/"))
{
resourceId = 0;
LOG.info("Application just started, so the Resource path is showing as '/'");
}
else
{
resourceId = Integer.valueOf(path);
}
}
catch (NumberFormatException e)
{
throw new RuntimeException("Could not parse path [" + path + "] into a numeric Resource id.");
}
// we've got an id so lets go and retrieve the corresponding resource
ResourceManager resourceManager = ResourceManagerFactory.resourceManager();
Resource resource = resourceManager.getResource(resourceId);
if (resource == null)
{
throw new IllegalStateException("Resource with id " + resourceId + " not found.");
}
return resource;
}
public List<SelectItem> asSelectItemList()
{
JONTreeNode selectedNode = navigationAction.getSelectedNode();
Collection<JONTreeNode> c = selectedNode.getChildNodes();
List<SelectItem> result = new ArrayList<SelectItem>();
if (c != null)
{
result.add(new SelectItem("All Types"));
for (JONTreeNode node : c)
{
result.add(new SelectItem(node.getPath(), node.getName()));
}
}
return result;
}
}