Search in sources :

Example 1 with IHopMetadataSerializer

use of org.apache.hop.metadata.api.IHopMetadataSerializer in project hop by apache.

the class CheckConnectionsDialog method open.

@Override
public IAction open() {
    Shell parent = getParent();
    shell = new Shell(parent, SWT.DIALOG_TRIM | SWT.RESIZE | SWT.MIN | SWT.MAX);
    props.setLook(shell);
    WorkflowDialog.setShellImage(shell, action);
    ModifyListener lsMod = e -> action.setChanged();
    changed = action.hasChanged();
    FormLayout formLayout = new FormLayout();
    formLayout.marginWidth = Const.FORM_MARGIN;
    formLayout.marginHeight = Const.FORM_MARGIN;
    shell.setLayout(formLayout);
    shell.setText("Check Neo4j Connections");
    int middle = props.getMiddlePct();
    int margin = Const.MARGIN;
    // Add buttons first, then the list of connections dynamically sizing
    // Put these buttons at the bottom
    // 
    Button wOk = new Button(shell, SWT.PUSH);
    wOk.setText(BaseMessages.getString(PKG, "System.Button.OK"));
    wOk.addListener(SWT.Selection, e -> ok());
    Button wCancel = new Button(shell, SWT.PUSH);
    wCancel.setText(BaseMessages.getString(PKG, "System.Button.Cancel"));
    wCancel.addListener(SWT.Selection, e -> cancel());
    BaseTransformDialog.positionBottomButtons(shell, new Button[] { wOk, wCancel }, margin, null);
    Label wlName = new Label(shell, SWT.RIGHT);
    wlName.setText("Action name");
    props.setLook(wlName);
    FormData fdlName = new FormData();
    fdlName.left = new FormAttachment(0, 0);
    fdlName.right = new FormAttachment(middle, -margin);
    fdlName.top = new FormAttachment(0, margin);
    wlName.setLayoutData(fdlName);
    wName = new Text(shell, SWT.SINGLE | SWT.LEFT | SWT.BORDER);
    props.setLook(wName);
    wName.addModifyListener(lsMod);
    FormData fdName = new FormData();
    fdName.left = new FormAttachment(middle, 0);
    fdName.top = new FormAttachment(0, margin);
    fdName.right = new FormAttachment(100, 0);
    wName.setLayoutData(fdName);
    Label wlConnections = new Label(shell, SWT.LEFT);
    wlConnections.setText(BaseMessages.getString(PKG, "CheckConnectionsDialog.Connections.Label"));
    props.setLook(wlConnections);
    FormData fdlConnections = new FormData();
    fdlConnections.left = new FormAttachment(0, 0);
    fdlConnections.right = new FormAttachment(middle, -margin);
    fdlConnections.top = new FormAttachment(wName, margin);
    wlConnections.setLayoutData(fdlConnections);
    String[] availableConnectionNames;
    try {
        IHopMetadataSerializer<NeoConnection> connectionSerializer = getMetadataProvider().getSerializer(NeoConnection.class);
        List<String> names = connectionSerializer.listObjectNames();
        Collections.sort(names);
        availableConnectionNames = names.toArray(new String[0]);
    } catch (HopException e) {
        availableConnectionNames = new String[] {};
    }
    ColumnInfo[] columns = new ColumnInfo[] { new ColumnInfo(BaseMessages.getString(PKG, "CheckConnectionsDialog.ConnectionName.Column.Label"), ColumnInfo.COLUMN_TYPE_CCOMBO, availableConnectionNames, false) };
    columns[0].setUsingVariables(true);
    wConnections = new TableView(action, shell, SWT.BORDER | SWT.FULL_SELECTION | SWT.MULTI, columns, action.getConnectionNames().size(), false, lsMod, props);
    FormData fdConnections = new FormData();
    fdConnections.left = new FormAttachment(0, 0);
    fdConnections.top = new FormAttachment(wlConnections, margin);
    fdConnections.right = new FormAttachment(100, 0);
    fdConnections.bottom = new FormAttachment(wOk, -margin * 2);
    wConnections.setLayoutData(fdConnections);
    getData();
    BaseDialog.defaultShellHandling(shell, c -> ok(), c -> cancel());
    return action;
}
Also used : Utils(org.apache.hop.core.util.Utils) TableView(org.apache.hop.ui.core.widget.TableView) IVariables(org.apache.hop.core.variables.IVariables) IActionDialog(org.apache.hop.workflow.action.IActionDialog) HopException(org.apache.hop.core.exception.HopException) WorkflowMeta(org.apache.hop.workflow.WorkflowMeta) ArrayList(java.util.ArrayList) NeoConnection(org.apache.hop.neo4j.shared.NeoConnection) WindowProperty(org.apache.hop.ui.core.gui.WindowProperty) ActionDialog(org.apache.hop.ui.workflow.action.ActionDialog) ColumnInfo(org.apache.hop.ui.core.widget.ColumnInfo) BaseMessages(org.apache.hop.i18n.BaseMessages) FormLayout(org.eclipse.swt.layout.FormLayout) WorkflowDialog(org.apache.hop.ui.workflow.dialog.WorkflowDialog) BaseDialog(org.apache.hop.ui.core.dialog.BaseDialog) FormData(org.eclipse.swt.layout.FormData) org.eclipse.swt.widgets(org.eclipse.swt.widgets) IHopMetadataSerializer(org.apache.hop.metadata.api.IHopMetadataSerializer) FormAttachment(org.eclipse.swt.layout.FormAttachment) Const(org.apache.hop.core.Const) List(java.util.List) ModifyListener(org.eclipse.swt.events.ModifyListener) SWT(org.eclipse.swt.SWT) IAction(org.apache.hop.workflow.action.IAction) Collections(java.util.Collections) BaseTransformDialog(org.apache.hop.ui.pipeline.transform.BaseTransformDialog) FormLayout(org.eclipse.swt.layout.FormLayout) FormData(org.eclipse.swt.layout.FormData) ModifyListener(org.eclipse.swt.events.ModifyListener) HopException(org.apache.hop.core.exception.HopException) ColumnInfo(org.apache.hop.ui.core.widget.ColumnInfo) NeoConnection(org.apache.hop.neo4j.shared.NeoConnection) FormAttachment(org.eclipse.swt.layout.FormAttachment) TableView(org.apache.hop.ui.core.widget.TableView)

Example 2 with IHopMetadataSerializer

use of org.apache.hop.metadata.api.IHopMetadataSerializer in project hop by apache.

the class MemoryMetadataProvider method getSerializer.

@Override
public <T extends IHopMetadata> IHopMetadataSerializer<T> getSerializer(Class<T> managedClass) throws HopException {
    IHopMetadataSerializer<IHopMetadata> serializer = serializerMap.get(managedClass.getName());
    if (serializer == null) {
        HopMetadata hopMetadata = managedClass.getAnnotation(HopMetadata.class);
        String description = managedClass.getSimpleName();
        if (hopMetadata != null) {
            description = hopMetadata.name();
        }
        serializer = (IHopMetadataSerializer<IHopMetadata>) new MemoryMetadataSerializer<>(this, managedClass, variables, description);
        serializerMap.put(managedClass.getName(), serializer);
    }
    return (IHopMetadataSerializer<T>) serializer;
}
Also used : IHopMetadata(org.apache.hop.metadata.api.IHopMetadata) HopMetadata(org.apache.hop.metadata.api.HopMetadata) IHopMetadata(org.apache.hop.metadata.api.IHopMetadata) IHopMetadataSerializer(org.apache.hop.metadata.api.IHopMetadataSerializer)

Example 3 with IHopMetadataSerializer

use of org.apache.hop.metadata.api.IHopMetadataSerializer in project hop by apache.

the class CypherScript method execute.

@Override
public Result execute(Result result, int nr) throws HopException {
    IHopMetadataSerializer<NeoConnection> serializer = getMetadataProvider().getSerializer(NeoConnection.class);
    // Replace variables & parameters
    // 
    NeoConnection connection;
    String realConnectionName = resolve(connectionName);
    try {
        if (StringUtils.isEmpty(realConnectionName)) {
            throw new HopException("The Neo4j connection name is not set");
        }
        connection = serializer.load(realConnectionName);
        if (connection == null) {
            throw new HopException("Unable to find connection with name '" + realConnectionName + "'");
        }
    } catch (Exception e) {
        result.setResult(false);
        result.increaseErrors(1L);
        throw new HopException("Unable to gencsv or find connection with name '" + realConnectionName + "'", e);
    }
    String realScript;
    if (replacingVariables) {
        realScript = resolve(script);
    } else {
        realScript = script;
    }
    int nrExecuted;
    try (Driver driver = connection.getDriver(log, this)) {
        // 
        try (Session session = connection.getSession(log, driver, this)) {
            TransactionWork<Integer> transactionWork = transaction -> {
                int executed = 0;
                try {
                    // Split the script into parts : semi-colon at the start of a separate line
                    // 
                    String[] commands = realScript.split("\\r?\\n;");
                    for (String command : commands) {
                        // Cleanup command: replace leading and trailing whitespaces and newlines
                        // 
                        String cypher = command.replaceFirst("^\\s+", "").replaceFirst("\\s+$", "");
                        // 
                        if (StringUtils.isNotEmpty(cypher)) {
                            transaction.run(cypher);
                            executed++;
                            log.logDetailed("Executed cypher statement: " + cypher);
                        }
                    }
                    // All statements executed successfully so commit
                    // 
                    transaction.commit();
                } catch (Exception e) {
                    log.logError("Error executing cypher statements...", e);
                    result.increaseErrors(1L);
                    transaction.rollback();
                    result.setResult(false);
                }
                return executed;
            };
            nrExecuted = session.writeTransaction(transactionWork);
        }
    }
    if (result.getNrErrors() == 0) {
        logBasic("Neo4j script executed " + nrExecuted + " statements without error");
    } else {
        logBasic("Neo4j script executed with error(s)");
    }
    return result;
}
Also used : Session(org.neo4j.driver.Session) Action(org.apache.hop.core.annotations.Action) StringUtils(org.apache.commons.lang.StringUtils) Driver(org.neo4j.driver.Driver) IVariables(org.apache.hop.core.variables.IVariables) XmlHandler(org.apache.hop.core.xml.XmlHandler) HopException(org.apache.hop.core.exception.HopException) IHopMetadataProvider(org.apache.hop.metadata.api.IHopMetadataProvider) HopXmlException(org.apache.hop.core.exception.HopXmlException) IHopMetadataSerializer(org.apache.hop.metadata.api.IHopMetadataSerializer) TransactionWork(org.neo4j.driver.TransactionWork) NeoConnection(org.apache.hop.neo4j.shared.NeoConnection) Node(org.w3c.dom.Node) Result(org.apache.hop.core.Result) ActionBase(org.apache.hop.workflow.action.ActionBase) IAction(org.apache.hop.workflow.action.IAction) NeoConnection(org.apache.hop.neo4j.shared.NeoConnection) HopException(org.apache.hop.core.exception.HopException) Driver(org.neo4j.driver.Driver) HopException(org.apache.hop.core.exception.HopException) HopXmlException(org.apache.hop.core.exception.HopXmlException) Session(org.neo4j.driver.Session)

Aggregations

IHopMetadataSerializer (org.apache.hop.metadata.api.IHopMetadataSerializer)3 HopException (org.apache.hop.core.exception.HopException)2 IVariables (org.apache.hop.core.variables.IVariables)2 NeoConnection (org.apache.hop.neo4j.shared.NeoConnection)2 IAction (org.apache.hop.workflow.action.IAction)2 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 List (java.util.List)1 StringUtils (org.apache.commons.lang.StringUtils)1 Const (org.apache.hop.core.Const)1 Result (org.apache.hop.core.Result)1 Action (org.apache.hop.core.annotations.Action)1 HopXmlException (org.apache.hop.core.exception.HopXmlException)1 Utils (org.apache.hop.core.util.Utils)1 XmlHandler (org.apache.hop.core.xml.XmlHandler)1 BaseMessages (org.apache.hop.i18n.BaseMessages)1 HopMetadata (org.apache.hop.metadata.api.HopMetadata)1 IHopMetadata (org.apache.hop.metadata.api.IHopMetadata)1 IHopMetadataProvider (org.apache.hop.metadata.api.IHopMetadataProvider)1 BaseDialog (org.apache.hop.ui.core.dialog.BaseDialog)1