Search in sources :

Example 1 with AuditEvent

use of org.apache.hop.history.AuditEvent in project hop by apache.

the class ProjectsGuiPlugin method selectProject.

@GuiToolbarElement(root = HopGui.ID_MAIN_TOOLBAR, id = ID_TOOLBAR_PROJECT_COMBO, type = GuiToolbarElementType.COMBO, comboValuesMethod = "getProjectsList", extraWidth = 200, toolTip = "i18n::HopGui.Toolbar.ProjectsList.Tooltip")
public void selectProject() {
    HopGui hopGui = HopGui.getInstance();
    Combo combo = getProjectsCombo();
    if (combo == null) {
        return;
    }
    String projectName = combo.getText();
    if (StringUtils.isEmpty(projectName)) {
        return;
    }
    ProjectsConfig config = ProjectsConfigSingleton.getConfig();
    ProjectConfig projectConfig = config.findProjectConfig(projectName);
    // What is the last used environment?
    // 
    LifecycleEnvironment environment = null;
    // 
    try {
        List<AuditEvent> environmentAuditEvents = AuditManager.findEvents(ProjectsUtil.STRING_PROJECTS_AUDIT_GROUP, ProjectsUtil.STRING_ENVIRONMENT_AUDIT_TYPE, "open", 100, true);
        for (AuditEvent auditEvent : environmentAuditEvents) {
            String environmentName = auditEvent.getName();
            if (StringUtils.isNotEmpty(environmentName)) {
                environment = config.findEnvironment(environmentName);
                // 
                if (projectName.equals(environment.getProjectName())) {
                    // We found what we've been looking for
                    break;
                } else {
                    // The project doesn't to the last selected environment
                    // Since we selected the project it is the driver of the selection.
                    // Keep looking.
                    // 
                    environment = null;
                }
            }
        }
    } catch (Exception e) {
        LogChannel.UI.logError("Error reading the last used environment from the audit logs", e);
    }
    // 
    if (environment == null) {
        List<LifecycleEnvironment> environments = config.findEnvironmentsOfProject(projectName);
        if (!environments.isEmpty()) {
            environment = environments.get(0);
        }
    }
    try {
        Project project = projectConfig.loadProject(hopGui.getVariables());
        if (project != null) {
            enableHopGuiProject(projectName, project, environment);
        } else {
            hopGui.getLog().logError("Unable to find project '" + projectName + "'");
        }
    } catch (Exception e) {
        new ErrorDialog(hopGui.getShell(), BaseMessages.getString(PKG, "ProjectGuiPlugin.ChangeProject.Error.Dialog.Header"), BaseMessages.getString(PKG, "ProjectGuiPlugin.ChangeProject.Error.Dialog.Message", projectName), e);
    }
}
Also used : ProjectConfig(org.apache.hop.projects.project.ProjectConfig) Project(org.apache.hop.projects.project.Project) LifecycleEnvironment(org.apache.hop.projects.environment.LifecycleEnvironment) ProjectsConfig(org.apache.hop.projects.config.ProjectsConfig) ErrorDialog(org.apache.hop.ui.core.dialog.ErrorDialog) Combo(org.eclipse.swt.widgets.Combo) AuditEvent(org.apache.hop.history.AuditEvent) HopException(org.apache.hop.core.exception.HopException) InvocationTargetException(java.lang.reflect.InvocationTargetException) IOException(java.io.IOException) HopGui(org.apache.hop.ui.hopgui.HopGui) GuiToolbarElement(org.apache.hop.core.gui.plugin.toolbar.GuiToolbarElement)

Example 2 with AuditEvent

use of org.apache.hop.history.AuditEvent in project hop by apache.

the class ProjectsGuiPlugin method getProjectsList.

/**
 * Called by the Combo in the toolbar
 *
 * @param log
 * @param metadataProvider
 * @return
 * @throws Exception
 */
public List<String> getProjectsList(ILogChannel log, IHopMetadataProvider metadataProvider) throws Exception {
    List<String> names = ProjectsConfigSingleton.getConfig().listProjectConfigNames();
    Map<String, Date> lastUsedMap = new HashMap<>();
    names.stream().forEach(name -> lastUsedMap.put(name, new GregorianCalendar(1900, Calendar.JANUARY, 1).getTime()));
    // Get the list of events from the Audit Manager...
    // 
    List<AuditEvent> projectOpenEvents = AuditManager.findEvents(ProjectsUtil.STRING_PROJECTS_AUDIT_GROUP, ProjectsUtil.STRING_PROJECT_AUDIT_TYPE, "open", 100, true);
    for (AuditEvent projectOpenEvent : projectOpenEvents) {
        lastUsedMap.put(projectOpenEvent.getName(), projectOpenEvent.getDate());
    }
    // Reverse sort by last used date of a project...
    // 
    Collections.sort(names, new Comparator<String>() {

        @Override
        public int compare(String name1, String name2) {
            int cmp = -lastUsedMap.get(name1).compareTo(lastUsedMap.get(name2));
            if (cmp == 0) {
                cmp = name1.compareToIgnoreCase(name2);
            }
            return cmp;
        }
    });
    return names;
}
Also used : AuditEvent(org.apache.hop.history.AuditEvent)

Example 3 with AuditEvent

use of org.apache.hop.history.AuditEvent in project hop by apache.

the class HopGuiFileDelegate method fileOpenRecent.

/**
 * Show all the recent files in a new dialog...
 */
public void fileOpenRecent() {
    // Get the recent files for the active perspective...
    // 
    IHopPerspective perspective = hopGui.getActivePerspective();
    try {
        // Let's limit ourselves to 100 operations...
        // 
        List<AuditEvent> events = AuditManager.findEvents(HopNamespace.getNamespace(), "file", "open", 100, true);
        Set<String> filenames = new HashSet<>();
        List<RowMetaAndData> rows = new ArrayList<>();
        IRowMeta rowMeta = new RowMeta();
        rowMeta.addValueMeta(new ValueMetaString("filename"));
        rowMeta.addValueMeta(new ValueMetaString("operation"));
        rowMeta.addValueMeta(new ValueMetaString("date"));
        for (AuditEvent event : events) {
            String filename = event.getName();
            if (!filenames.contains(filename)) {
                filenames.add(filename);
                String operation = event.getOperation();
                String dateString = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(event.getDate());
                rows.add(new RowMetaAndData(rowMeta, new Object[] { filename, operation, dateString }));
            }
        }
        SelectRowDialog rowDialog = new SelectRowDialog(hopGui.getShell(), hopGui.getVariables(), SWT.NONE, rows);
        rowDialog.setTitle("Select the file to open");
        RowMetaAndData row = rowDialog.open();
        if (row != null) {
            String filename = row.getString("filename", null);
            hopGui.fileDelegate.fileOpen(filename);
        }
    } catch (Exception e) {
        new ErrorDialog(hopGui.getShell(), "Error", "Error getting list of recently opened files", e);
    }
}
Also used : ValueMetaString(org.apache.hop.core.row.value.ValueMetaString) RowMeta(org.apache.hop.core.row.RowMeta) IRowMeta(org.apache.hop.core.row.IRowMeta) IRowMeta(org.apache.hop.core.row.IRowMeta) ArrayList(java.util.ArrayList) ErrorDialog(org.apache.hop.ui.core.dialog.ErrorDialog) ValueMetaString(org.apache.hop.core.row.value.ValueMetaString) SelectRowDialog(org.apache.hop.ui.core.dialog.SelectRowDialog) HopException(org.apache.hop.core.exception.HopException) RowMetaAndData(org.apache.hop.core.RowMetaAndData) IHopPerspective(org.apache.hop.ui.hopgui.perspective.IHopPerspective) AuditEvent(org.apache.hop.history.AuditEvent) FileObject(org.apache.commons.vfs2.FileObject) SimpleDateFormat(java.text.SimpleDateFormat) HashSet(java.util.HashSet)

Example 4 with AuditEvent

use of org.apache.hop.history.AuditEvent in project hop by apache.

the class ProjectsGuiPlugin method enableHopGuiProject.

public static void enableHopGuiProject(String projectName, Project project, LifecycleEnvironment environment) throws HopException {
    try {
        HopGui hopGui = HopGui.getInstance();
        // Before we switch the namespace in HopGui, save the state of the perspectives
        // 
        hopGui.auditDelegate.writeLastOpenFiles();
        // 
        if (!hopGui.fileDelegate.saveGuardAllFiles()) {
            // Abort the project change
            return;
        }
        // Close 'm all
        // 
        hopGui.fileDelegate.closeAllFiles();
        // This is called only in HopGui so we want to start with a new set of variables
        // It avoids variables from one project showing up in another
        // 
        IVariables variables = Variables.getADefaultVariableSpace();
        // See if there's an environment associated with the current project
        // In that case, apply the variables in those files
        // 
        List<String> configurationFiles = new ArrayList<>();
        if (environment != null) {
            configurationFiles.addAll(environment.getConfigurationFiles());
        }
        // Set the variables and give HopGui the new metadata provider(s) for the project.
        // 
        String environmentName = environment == null ? null : environment.getName();
        ProjectsUtil.enableProject(hopGui.getLog(), projectName, project, variables, configurationFiles, environmentName, hopGui);
        // HopGui now has a new metadata provider set.
        // Only now we can get the variables from the defined run configs.
        // Set them with default values just to make them show up.
        // 
        IHopMetadataSerializer<PipelineRunConfiguration> runConfigSerializer = hopGui.getMetadataProvider().getSerializer(PipelineRunConfiguration.class);
        for (PipelineRunConfiguration runConfig : runConfigSerializer.loadAll()) {
            for (VariableValueDescription variableValueDescription : runConfig.getConfigurationVariables()) {
                variables.setVariable(variableValueDescription.getName(), "");
            }
        }
        // We need to change the currently set variables in the newly loaded files
        // 
        hopGui.setVariables(variables);
        // Re-open last open files for the namespace
        // 
        hopGui.auditDelegate.openLastFiles();
        // Clear last used, fill it with something useful.
        // 
        IVariables hopGuiVariables = Variables.getADefaultVariableSpace();
        hopGui.setVariables(hopGuiVariables);
        for (String variable : variables.getVariableNames()) {
            String value = variables.getVariable(variable);
            if (!variable.startsWith(Const.INTERNAL_VARIABLE_PREFIX)) {
                hopGuiVariables.setVariable(variable, value);
            }
        }
        // Refresh the currently active file
        // 
        hopGui.getActivePerspective().getActiveFileTypeHandler().updateGui();
        // Update the toolbar combos
        // 
        ProjectsGuiPlugin.selectProjectInList(projectName);
        ProjectsGuiPlugin.selectEnvironmentInList(environment == null ? null : environment.getName());
        // Also add this as an event so we know what the project usage history is
        // 
        AuditEvent prjUsedEvent = new AuditEvent(ProjectsUtil.STRING_PROJECTS_AUDIT_GROUP, ProjectsUtil.STRING_PROJECT_AUDIT_TYPE, projectName, "open", new Date());
        AuditManager.getActive().storeEvent(prjUsedEvent);
        // Now use that event to refresh the list...
        // 
        refreshProjectsList();
        ProjectsGuiPlugin.selectProjectInList(projectName);
        if (environment != null) {
            // Also add this as an event so we know what the project usage history is
            // 
            AuditEvent envUsedEvent = new AuditEvent(ProjectsUtil.STRING_PROJECTS_AUDIT_GROUP, ProjectsUtil.STRING_ENVIRONMENT_AUDIT_TYPE, environment.getName(), "open", new Date());
            AuditManager.getActive().storeEvent(envUsedEvent);
        }
        // Send out an event notifying that a new project is activated...
        // The metadata has changed so fire those events as well
        // 
        hopGui.getEventsHandler().fire(projectName, HopGuiEvents.ProjectActivated.name());
        hopGui.getEventsHandler().fire(projectName, HopGuiEvents.MetadataChanged.name());
        // Inform the outside world that we're enabled an other project
        // 
        ExtensionPointHandler.callExtensionPoint(LogChannel.GENERAL, hopGuiVariables, HopExtensionPoint.HopGuiProjectAfterEnabled.name(), project);
    } catch (Exception e) {
        throw new HopException("Error enabling project '" + projectName + "' in HopGui", e);
    }
}
Also used : VariableValueDescription(org.apache.hop.core.variables.VariableValueDescription) HopException(org.apache.hop.core.exception.HopException) IVariables(org.apache.hop.core.variables.IVariables) LocalPipelineRunConfiguration(org.apache.hop.pipeline.engines.local.LocalPipelineRunConfiguration) PipelineRunConfiguration(org.apache.hop.pipeline.config.PipelineRunConfiguration) AuditEvent(org.apache.hop.history.AuditEvent) HopException(org.apache.hop.core.exception.HopException) InvocationTargetException(java.lang.reflect.InvocationTargetException) IOException(java.io.IOException) HopGui(org.apache.hop.ui.hopgui.HopGui)

Example 5 with AuditEvent

use of org.apache.hop.history.AuditEvent in project hop by apache.

the class HopGuiStartProjectLoad method callExtensionPoint.

@Override
public void callExtensionPoint(ILogChannel logChannelInterface, IVariables variables, Object o) throws HopException {
    HopGui hopGui = HopGui.getInstance();
    try {
        ProjectsConfig config = ProjectsConfigSingleton.getConfig();
        // 
        if (ProjectsConfigSingleton.getConfig().isEnabled()) {
            logChannelInterface.logBasic("Projects enabled");
            // What is the last used project?
            // 
            String lastProjectName = null;
            // Let's see in the audit logs what the last opened project is.
            // 
            List<AuditEvent> auditEvents = AuditManager.findEvents(ProjectsUtil.STRING_PROJECTS_AUDIT_GROUP, ProjectsUtil.STRING_PROJECT_AUDIT_TYPE, "open", 1, true);
            if (auditEvents.isEmpty()) {
                lastProjectName = config.getDefaultProject();
            } else {
                logChannelInterface.logDetailed("Audit events found for hop-gui/project : " + auditEvents.size());
                AuditEvent lastEvent = auditEvents.get(0);
                long eventTime = lastEvent.getDate().getTime();
                lastProjectName = lastEvent.getName();
                if (config.findProjectConfig(lastProjectName) == null) {
                    // The last existing project to open was not found.
                    // 
                    lastProjectName = null;
                }
            }
            if (StringUtils.isNotEmpty(lastProjectName)) {
                ProjectConfig projectConfig = config.findProjectConfig(lastProjectName);
                if (projectConfig != null) {
                    Project project = projectConfig.loadProject(variables);
                    logChannelInterface.logBasic("Enabling project : '" + lastProjectName + "'");
                    LifecycleEnvironment lastEnvironment = null;
                    // What was the last environment for this project?
                    // 
                    List<AuditEvent> envEvents = AuditManager.findEvents(ProjectsUtil.STRING_PROJECTS_AUDIT_GROUP, ProjectsUtil.STRING_ENVIRONMENT_AUDIT_TYPE, "open", 100, true);
                    // 
                    for (AuditEvent envEvent : envEvents) {
                        LifecycleEnvironment environment = config.findEnvironment(envEvent.getName());
                        if (environment != null && lastProjectName.equals(environment.getProjectName())) {
                            lastEnvironment = environment;
                            break;
                        }
                    }
                    // Set system variables for HOP_HOME, HOP_METADATA_FOLDER, ...
                    // Sets the namespace in HopGui to the name of the project
                    // 
                    ProjectsGuiPlugin.enableHopGuiProject(lastProjectName, project, lastEnvironment);
                    // Don't open the files twice
                    // 
                    HopGui.getInstance().setOpeningLastFiles(false);
                }
            }
        } else {
            logChannelInterface.logBasic("No last projects history found");
        }
    } catch (Exception e) {
        new ErrorDialog(hopGui.getShell(), "Error", "Error initializing the Projects system", e);
    }
}
Also used : ProjectConfig(org.apache.hop.projects.project.ProjectConfig) Project(org.apache.hop.projects.project.Project) LifecycleEnvironment(org.apache.hop.projects.environment.LifecycleEnvironment) ProjectsConfig(org.apache.hop.projects.config.ProjectsConfig) ErrorDialog(org.apache.hop.ui.core.dialog.ErrorDialog) AuditEvent(org.apache.hop.history.AuditEvent) HopException(org.apache.hop.core.exception.HopException) HopGui(org.apache.hop.ui.hopgui.HopGui)

Aggregations

AuditEvent (org.apache.hop.history.AuditEvent)5 HopException (org.apache.hop.core.exception.HopException)4 ErrorDialog (org.apache.hop.ui.core.dialog.ErrorDialog)3 HopGui (org.apache.hop.ui.hopgui.HopGui)3 IOException (java.io.IOException)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 ProjectsConfig (org.apache.hop.projects.config.ProjectsConfig)2 LifecycleEnvironment (org.apache.hop.projects.environment.LifecycleEnvironment)2 Project (org.apache.hop.projects.project.Project)2 ProjectConfig (org.apache.hop.projects.project.ProjectConfig)2 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 FileObject (org.apache.commons.vfs2.FileObject)1 RowMetaAndData (org.apache.hop.core.RowMetaAndData)1 GuiToolbarElement (org.apache.hop.core.gui.plugin.toolbar.GuiToolbarElement)1 IRowMeta (org.apache.hop.core.row.IRowMeta)1 RowMeta (org.apache.hop.core.row.RowMeta)1 ValueMetaString (org.apache.hop.core.row.value.ValueMetaString)1 IVariables (org.apache.hop.core.variables.IVariables)1