All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.ikasan.dashboard.ui.scheduler.component.SchedulerJobUploadDialog Maven / Gradle / Ivy

There is a newer version: 4.0.1
Show newest version
package org.ikasan.dashboard.ui.scheduler.component;


import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.vaadin.flow.component.ClickEvent;
import com.vaadin.flow.component.ComponentEventListener;
import com.vaadin.flow.component.UI;
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.confirmdialog.ConfirmDialog;
import com.vaadin.flow.component.html.H3;
import com.vaadin.flow.component.orderedlayout.FlexComponent;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.component.upload.Upload;
import com.vaadin.flow.component.upload.receivers.MemoryBuffer;
import org.ikasan.dashboard.ui.general.component.AbstractCloseableResizableDialog;
import org.ikasan.dashboard.ui.general.component.NotificationHelper;
import org.ikasan.dashboard.ui.scheduler.util.NewSchedulerJobEventBroadcaster;
import org.ikasan.dashboard.ui.util.ComponentSecurityVisibility;
import org.ikasan.dashboard.ui.util.SecurityConstants;
import org.ikasan.job.orchestration.util.ObjectMapperFactory;
import org.ikasan.security.service.authentication.IkasanAuthentication;
import org.ikasan.spec.scheduled.context.model.ContextTemplate;
import org.ikasan.spec.scheduled.job.model.*;
import org.ikasan.spec.scheduled.job.service.SchedulerJobService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.context.SecurityContextHolder;

import java.io.IOException;
import java.io.InputStream;

public class SchedulerJobUploadDialog extends AbstractCloseableResizableDialog {
    Logger logger = LoggerFactory.getLogger(SchedulerJobUploadDialog.class);

    private byte[] uploadedJob;
    private SchedulerJobService schedulerJobService;
    private ContextTemplate contextTemplate;
    private Class schedulerJobClass;
    private String label;

    private ObjectMapper objectMapper;

    /**
     * Constructor
     */
    public SchedulerJobUploadDialog(ContextTemplate contextTemplate, SchedulerJobService schedulerJobService
        , Class schedulerJobClass, String label)
    {
        this.contextTemplate = contextTemplate;
        if(this.contextTemplate == null) {
            throw new IllegalArgumentException("contextTemplate cannot be null!");
        }
        this.schedulerJobService = schedulerJobService;
        if(this.schedulerJobService == null) {
            throw new IllegalArgumentException("schedulerJobService cannot be null!");
        }
        this.schedulerJobClass = schedulerJobClass;
        if(this.schedulerJobClass == null) {
            throw new IllegalArgumentException("schedulerJobClass cannot be null!");
        }
        this.label = label;
        if(this.label == null) {
            throw new IllegalArgumentException("label cannot be null!");
        }

        objectMapper = ObjectMapperFactory.newInstance();
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, true);

        this.init();
    }

    private void init()
    {
        this.setModal(true);

        VerticalLayout verticalLayout = new VerticalLayout();

        H3 uploadContextHeader = new H3(this.label);

        HorizontalLayout header = new HorizontalLayout();
        header.setWidthFull();
        header.setHeight("40px");
        header.add(uploadContextHeader);
        header.setVerticalComponentAlignment(FlexComponent.Alignment.CENTER, uploadContextHeader);
        header.getElement().getStyle().set("padding-bottom", "40px");

        verticalLayout.add(header);

        IkasanAuthentication authentication = (IkasanAuthentication) SecurityContextHolder.getContext().getAuthentication();

        MemoryBuffer fileBuffer = new MemoryBuffer();
        Upload upload = new Upload(fileBuffer);
        upload.setMaxFiles(1);
        upload.addFinishedListener(event -> {
            InputStream inputStream =
                fileBuffer.getInputStream();

            try
            {
                uploadedJob = new byte[inputStream.available()];
                inputStream.read(uploadedJob);
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        });

        Button saveButton = new Button(getTranslation("button.save", UI.getCurrent().getLocale()));
        saveButton.addClickListener((ComponentEventListener>) buttonClickEvent -> {
            if (this.schedulerJobClass.getName().equals(InternalEventDrivenJob.class.getName())) {
                InternalEventDrivenJob schedulerJob;
                try {
                    schedulerJob = (InternalEventDrivenJob) objectMapper
                        .readValue(uploadedJob, this.schedulerJobClass);
                }
                catch (Exception e) {
                    NotificationHelper.showErrorNotification(getTranslation("error.bad-job-template-format", UI.getCurrent().getLocale()));
                    return;
                }

                if(!this.isJobUnique(schedulerJob)) {
                    this.openConfirmDialogForExistingJob(schedulerJob, authentication.getName());
                }
                else {
                    this.saveJob(schedulerJob, authentication.getName());
                }
            }
            else if(this.schedulerJobClass.getName().equals(FileEventDrivenJob.class.getName())) {
                FileEventDrivenJob schedulerJob;
                try {
                    schedulerJob = (FileEventDrivenJob) objectMapper
                        .readValue(uploadedJob, this.schedulerJobClass);
                }
                catch (Exception e) {
                    NotificationHelper.showErrorNotification(getTranslation("error.bad-job-template-format", UI.getCurrent().getLocale()));
                    return;
                }

                if(!this.isJobUnique(schedulerJob)) {
                    this.openConfirmDialogForExistingJob(schedulerJob, authentication.getName());
                }
                else {
                    this.saveJob(schedulerJob, authentication.getName());
                }
            }
            else if(this.schedulerJobClass.getName().equals(QuartzScheduleDrivenJob.class.getName())) {
                QuartzScheduleDrivenJob schedulerJob;
                try {
                    schedulerJob = (QuartzScheduleDrivenJob) objectMapper
                        .readValue(uploadedJob, this.schedulerJobClass);
                }
                catch (Exception e) {
                    NotificationHelper.showErrorNotification(getTranslation("error.bad-job-template-format", UI.getCurrent().getLocale()));
                    return;
                }

                if(!this.isJobUnique(schedulerJob)) {
                    this.openConfirmDialogForExistingJob(schedulerJob, authentication.getName());
                }
                else {
                    this.saveJob(schedulerJob, authentication.getName());
                }
            }

            this.close();
        });

        ComponentSecurityVisibility.applySecurity(saveButton, SecurityConstants.ALL_AUTHORITY,
            SecurityConstants.SCHEDULER_WRITE, SecurityConstants.SCHEDULER_ADMIN,
            SecurityConstants.SCHEDULER_ALL_ADMIN, SecurityConstants.SCHEDULER_ALL_WRITE);

        Button cancelButton = new Button(getTranslation("button.cancel", UI.getCurrent().getLocale()));
        cancelButton.addClickListener(buttonClickEvent -> this.close());

        HorizontalLayout buttonLayout = new HorizontalLayout();
        buttonLayout.add(saveButton, cancelButton);

        verticalLayout.add(upload, buttonLayout);
        verticalLayout.setHorizontalComponentAlignment(FlexComponent.Alignment.CENTER, upload, buttonLayout);
        this.content.add(verticalLayout);
        super.showResize(false);
        super.setResizable(false);
        super.setWidth("600px");
        super.setHeight("300px");
    }

    private boolean isJobUnique(SchedulerJob schedulerJob) {
        SchedulerJobRecord schedulerJobRecord = this.schedulerJobService
            .findByContextNameAndJobName(this.contextTemplate.getName(), schedulerJob.getJobName());

        return schedulerJobRecord == null;
    }

    private void openConfirmDialogForExistingJob(SchedulerJob schedulerJob, String actor) {
        ConfirmDialog confirmDialog = new ConfirmDialog();
        confirmDialog.setHeader(getTranslation("confirm-dialog.job-exists-header", UI.getCurrent().getLocale()));
        confirmDialog.setText(getTranslation("confirm-dialog.job-exists-text", UI.getCurrent().getLocale()));
        confirmDialog.setCancelable(true);
        confirmDialog.open();
        confirmDialog.addConfirmListener(event -> this.saveJob(schedulerJob, actor));
    }

    private void saveJob(SchedulerJob schedulerJob, String actor) {
        try {
            if (schedulerJob instanceof InternalEventDrivenJob) {
                this.schedulerJobService.saveInternalEventDrivenJob((InternalEventDrivenJob) schedulerJob, actor);
                NewSchedulerJobEventBroadcaster.broadcast(schedulerJob);
            } else if (schedulerJob instanceof FileEventDrivenJob) {
                this.schedulerJobService.saveFileEventDrivenJob((FileEventDrivenJob) schedulerJob, actor);
                NewSchedulerJobEventBroadcaster.broadcast(schedulerJob);
            } else if (schedulerJob instanceof QuartzScheduleDrivenJob) {
                this.schedulerJobService.saveQuartzScheduledJob((QuartzScheduleDrivenJob) schedulerJob, actor);
                NewSchedulerJobEventBroadcaster.broadcast(schedulerJob);
            }
        }
        catch (Exception e) {
            e.printStackTrace();
            NotificationHelper.showUserNotification(getTranslation("error.job-upload", UI.getCurrent().getLocale()));
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy