/*
* 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.bean.history.operation;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import org.rhq.core.domain.configuration.Configuration;
import org.rhq.core.domain.operation.OperationDefinition;
import org.rhq.core.domain.operation.OperationHistory;
import org.rhq.core.domain.operation.ResourceOperationHistory;
import org.rhq.core.domain.resource.Resource;
import org.rhq.core.domain.resource.ResourceType;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.on.embedded.manager.history.AbstractHistoryManager;
import org.jboss.on.embedded.manager.history.operation.OperationHistoryManager;
/**
* @author Mark Spritzler
*/
@Name("historyManager")
@Scope(ScopeType.APPLICATION)
public class OperationHistoryManagerBean extends AbstractHistoryManager<OperationHistory>
implements OperationHistoryManager
{
private AtomicInteger jobNameId = new AtomicInteger(0);
public OperationHistory addOperationHistory(String operationName,
Configuration parameters,
Resource resource,
ResourceType resourceType)
{
OperationHistory operationHistory = null;
OperationDefinition operationDefinition = getOperationDefinition(operationName, resourceType);
if (operationDefinition != null)
{
int id = jobNameId.incrementAndGet();
String jobName = operationDefinition.getName() + "-" + id;
operationHistory =
new ResourceOperationHistory(jobName,
"",
resource.getName(),
operationDefinition,
parameters,
resource,
null); //GroupOperationHistory
operationHistory.setId(id);
addToMaps(operationHistory, resource, resourceType);
}
return operationHistory;
}
private static OperationDefinition getOperationDefinition(String operationName, ResourceType resourceType)
{
Set<OperationDefinition> operationDefinitions = resourceType.getOperationDefinitions();
OperationDefinition operationDefinition = null;
for (OperationDefinition definition : operationDefinitions)
{
if (definition.getName().equals(operationName))
{
operationDefinition = definition;
}
}
return operationDefinition;
}
protected String getJobId(OperationHistory history)
{
return history.getJobId().toString();
}
}