package rocks.inspectit.ui.rcp.handlers;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.Command;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.IHandler;
import org.eclipse.core.commands.NotEnabledException;
import org.eclipse.core.commands.NotHandledException;
import org.eclipse.core.commands.common.NotDefinedException;
import org.eclipse.core.expressions.IEvaluationContext;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.swt.widgets.Event;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.commands.ICommandService;
import org.eclipse.ui.handlers.HandlerUtil;
import org.eclipse.ui.handlers.IHandlerService;
import rocks.inspectit.shared.all.communication.data.InvocationSequenceData;
import rocks.inspectit.shared.all.communication.data.SqlStatementData;
import rocks.inspectit.ui.rcp.editor.inputdefinition.EditorPropertiesData;
import rocks.inspectit.ui.rcp.editor.inputdefinition.EditorPropertiesData.PartType;
import rocks.inspectit.ui.rcp.editor.inputdefinition.InputDefinition;
import rocks.inspectit.ui.rcp.editor.inputdefinition.InputDefinition.IdDefinition;
import rocks.inspectit.ui.rcp.editor.inputdefinition.extra.InputDefinitionExtrasMarkerFactory;
import rocks.inspectit.ui.rcp.editor.inputdefinition.extra.SqlStatementInputDefinitionExtra;
import rocks.inspectit.ui.rcp.editor.root.AbstractRootEditor;
import rocks.inspectit.ui.rcp.model.SensorTypeEnum;
import rocks.inspectit.ui.rcp.repository.RepositoryDefinition;
/**
* Handler for navigation to the aggregated SQL data.
*
* @author Ivan Senic
*
*/
public class NavigateToAggregatedSqlDataHandler extends AbstractHandler implements IHandler {
/**
* {@inheritDoc}
*/
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
StructuredSelection selection = (StructuredSelection) HandlerUtil.getCurrentSelectionChecked(event);
AbstractRootEditor rootEditor = (AbstractRootEditor) HandlerUtil.getActiveEditor(event);
RepositoryDefinition repositoryDefinition = rootEditor.getInputDefinition().getRepositoryDefinition();
Object selectedObject = selection.getFirstElement();
SqlStatementData dataToNavigateTo = null;
if (selectedObject instanceof SqlStatementData) {
dataToNavigateTo = (SqlStatementData) selectedObject;
} else if (selectedObject instanceof InvocationSequenceData) {
dataToNavigateTo = ((InvocationSequenceData) selectedObject).getSqlStatementData();
}
if (null != dataToNavigateTo) {
InputDefinition inputDefinition = new InputDefinition();
inputDefinition.setRepositoryDefinition(repositoryDefinition);
inputDefinition.setId(SensorTypeEnum.SQL);
EditorPropertiesData editorPropertiesData = new EditorPropertiesData();
editorPropertiesData.setSensorImage(SensorTypeEnum.SQL.getImage());
editorPropertiesData.setSensorName("Aggregated SQL Statements");
editorPropertiesData.setViewName(dataToNavigateTo.getSql());
editorPropertiesData.setPartNameFlag(PartType.SENSOR);
inputDefinition.setEditorPropertiesData(editorPropertiesData);
IdDefinition idDefinition = new IdDefinition();
idDefinition.setPlatformId(dataToNavigateTo.getPlatformIdent());
inputDefinition.setIdDefinition(idDefinition);
SqlStatementInputDefinitionExtra sqlStatementInputDefinitionExtra = new SqlStatementInputDefinitionExtra();
sqlStatementInputDefinitionExtra.setSql(dataToNavigateTo.getSql());
inputDefinition.addInputDefinitonExtra(InputDefinitionExtrasMarkerFactory.SQL_STATEMENT_EXTRAS_MARKER, sqlStatementInputDefinitionExtra);
// open the view via command
IHandlerService handlerService = (IHandlerService) PlatformUI.getWorkbench().getService(IHandlerService.class);
ICommandService commandService = (ICommandService) PlatformUI.getWorkbench().getService(ICommandService.class);
Command command = commandService.getCommand(OpenViewHandler.COMMAND);
ExecutionEvent executionEvent = handlerService.createExecutionEvent(command, new Event());
IEvaluationContext context = (IEvaluationContext) executionEvent.getApplicationContext();
context.addVariable(OpenViewHandler.INPUT, inputDefinition);
try {
command.executeWithChecks(executionEvent);
} catch (NotDefinedException | NotEnabledException | NotHandledException e) {
throw new ExecutionException("Error opening the aggregated SQL data view.", e);
}
}
return null;
}
}