<commit_before>class WorkflowService {
    /*@ngInject*/
    constructor(experimentsAPI, processesAPI, mcbus, templates, toast, $mdDialog) {
        this.experimentsAPI = experimentsAPI;
        this.processesAPI = processesAPI;
        this.mcbus = mcbus;
        this.templates = templates;
        this.toast = toast;
        this.$mdDialog = $mdDialog;
    }

    chooseSamplesFromSource(source) {
        return this.$mdDialog.show({
            templateUrl: 'app/project/experiments/experiment/components/workflow/services/choose-source-samples-dialog.html',
            controllerAs: '$ctrl',
            controller: ChooseSamplesFromSourceDialogController,
            bindToController: true,
            multiple: true,
            locals: {
                source: source
            }
        }).then(
            (samples) => samples
        );
    }

    addProcessFromTemplateNoPopup(templateName, projectId, experimentId) {
        this.experimentsAPI.createProcessFromTemplate(projectId, experimentId, `global_${templateName}`)
            .then(
                (process) => {
                    let p = this.templates.loadTemplateFromProcess(process.template_name, process);
                    this.mcbus.send('PROCESS$ADD', p);
                }
            );
    }

    addProcessFromTemplate(templateId, projectId, experimentId, multiple = true) {
        this.experimentsAPI.createProcessFromTemplate(projectId, experimentId, `global_${templateId}`)
            .then(
                (process) => {
                    let p = this.templates.loadTemplateFromProcess(process.template_name, process);
                    this.$mdDialog.show({
                        templateUrl: 'app/project/experiments/experiment/components/workflow/new-process-dialog.html',
                        controllerAs: '$ctrl',
                        controller: NewProcessDialogController,
                        bindToController: true,
                        multiple: multiple,
                        locals: {
                            process: p
                        }
                    }).then(
                        (proc) => {
                            this.mcbus.send('PROCESS$ADD', proc);
                            //this.sendProcessChangeEvent(projectId, experimentId);
                        }
                    );
                },
                () => this.toast.error('Unable to create process from template')
            );
    }

    addSamplesToProcess(projectId, experimentId, process, samples) {
        let samplesToAdd = samples.map(s => ({
            id: s.id,
            property_set_id: s.property_set_id,
            command: 'add'
        }));

        let samplesArgs = {
            template_id: process.template_id,
            samples: samplesToAdd,
            process_id: process.id
        };

        return this.experimentsAPI.updateProcess(projectId, experimentId, process.id, samplesArgs).then(
            (process) => process
        );
    }

    addChildProcessFromTemplate(templateId, projectId, experimentId, parentProcess, multiple = true) {
        this.experimentsAPI.createProcessFromTemplate(projectId, experimentId, `global_${templateId}`)
            .then(
                (process) => {
                    let p = this.templates.loadTemplateFromProcess(process.template_name, process);

                    let samplesToAdd = parentProcess.output_samples.map(s => ({
                        id: s.id,
                        property_set_id: s.property_set_id,
                        command: 'add'
                    }));

                    let samplesArgs = {
                        template_id: process.template_id,
                        samples: samplesToAdd,
                        process_id: process.id
                    };

                    this.experimentsAPI.updateProcess(projectId, experimentId, process.id, samplesArgs).then(
                        (pUpdated) => {
                            p.input_samples = pUpdated.input_samples;
                            this.$mdDialog.show({
                                templateUrl: 'app/project/experiments/experiment/components/workflow/new-process-dialog.html',
                                controllerAs: '$ctrl',
                                controller: NewProcessDialogController,
                                bindToController: true,
                                multiple: multiple,
                                locals: {
                                    process: p
                                }
                            }).then(
                                () => this.sendProcessChangeEvent(projectId, experimentId)
                            )
                        }
                    )

                },
                () => this.toast.error('Unable to create child process')
            )
    }

    cloneProcess(projectId, experimentId, process) {
        let p = angular.copy(process);
        p.input_samples.forEach(s => s.selected = true);
        p.output_samples.forEach(s => s.selected = true);
        p.files.forEach(f => f.selected = true);
        return this.$mdDialog.show({
            templateUrl: 'app/project/experiments/experiment/components/workflow/services/clone-process-dialog.html',
            controllerAs: '$ctrl',
            controller: CloneProcessDialogController,
            bindToController: true,
            locals: {
                process: p,
                projectId: projectId
            }
        }).then(
            (cloneArgs) => {
                return this.experimentsAPI.cloneProcess(projectId, experimentId, p.id, cloneArgs).then(
                    (p) => this.mcbus.send('PROCESS$ADD', p),
                    () => this.toast.error('Error cloning process')
                )
            }
        );
    }

    sendProcessChangeEvent(projectId, experimentId) {
        this.experimentsAPI.getProcessesForExperiment(projectId, experimentId)
            .then(
                (processes) => this.mcbus.send('PROCESSES$CHANGE', processes),
                () => this.toast.error('Error retrieving processes for experiment')
            );
    }

    deleteNodeAndProcess(projectId, experimentId, processId) {
        this.processesAPI.getDeleteProcessPreConditions(projectId, processId)
            .then(
                process => {
                    let numberOfSamples = process.output_samples.length;
                    if (numberOfSamples === 0) {
                        this.deleteNodeAndProcess2(projectId, experimentId, processId);
                    } else {
                        this.confirmAndDeleteProcess(projectId, experimentId, process);
                    }
                },
                error => this.toast.error(error.data.error)
            );
    }

    confirmAndDeleteProcess(projectId, experimentId, process) {
        let processName = process.name;
        let numberOfSamples = process.output_samples.length;
        let samples = " output sample" + ((numberOfSamples !== 1) ? "s" : "");
        let processInfo = processName + " - has " + numberOfSamples + samples + ".";
        let confirm = this.$mdDialog.confirm()
            .title('This process has output samples: Delete node and Samples?')
            .textContent(processInfo)
            .ariaLabel('Please confirm - deleting node')
            .ok('Delete')
            .cancel('Cancel');

        this.$mdDialog.show(confirm).then(() => this.deleteNodeAndProcess2(projectId, experimentId, process.id));
    }

    deleteNodeAndProcess2(projectId, experimentId, processId) {
        this.experimentsAPI.deleteProcess(projectId, experimentId, processId)
            .then(
                () => this.mcbus.send('PROCESS$DELETE', processId),
                error => this.toast.error(error.data.error)
            );
    }
}

class NewProcessDialogController {
    /*@ngInject*/
    constructor($mdDialog, processesAPI, $stateParams) {
        this.$mdDialog = $mdDialog;
        this.processesAPI = processesAPI;
        this.projectId = $stateParams.project_id;
        this.processName = this.process.name;
    }

    done() {
        this.processesAPI.getProcess(this.projectId, this.process.id).then(
            (p) => this.$mdDialog.hide(p)
        );
    }<commit_msg>Integrate mcprojstore into some of the calls that update process information<commit_after>class WorkflowService {
    /*@ngInject*/
    constructor(experimentsAPI, processesAPI, mcbus, templates, toast, $mdDialog, mcprojstore) {
        this.experimentsAPI = experimentsAPI;
        this.processesAPI = processesAPI;
        this.mcbus = mcbus;
        this.templates = templates;
        this.toast = toast;
        this.$mdDialog = $mdDialog;
        this.mcprojstore = mcprojstore;
    }

    chooseSamplesFromSource(source) {
        return this.$mdDialog.show({
            templateUrl: 'app/project/experiments/experiment/components/workflow/services/choose-source-samples-dialog.html',
            controllerAs: '$ctrl',
            controller: ChooseSamplesFromSourceDialogController,
            bindToController: true,
            multiple: true,
            locals: {
                source: source
            }
        }).then(
            (samples) => samples
        );
    }

    addProcessFromTemplateNoPopup(templateName, projectId, experimentId) {
        this.experimentsAPI.createProcessFromTemplate(projectId, experimentId, `global_${templateName}`)
            .then(
                (process) => {
                    let p = this.templates.loadTemplateFromProcess(process.template_name, process);
                    this.mcprojstore.addProcess(p).then(
                        () => this.mcbus.send('PROCESS$ADD', p)
                    )
                }
            );
    }

    addProcessFromTemplate(templateId, projectId, experimentId, multiple = true) {
        this.experimentsAPI.createProcessFromTemplate(projectId, experimentId, `global_${templateId}`)
            .then(
                (process) => {
                    let p = this.templates.loadTemplateFromProcess(process.template_name, process);
                    this.$mdDialog.show({
                        templateUrl: 'app/project/experiments/experiment/components/workflow/new-process-dialog.html',
                        controllerAs: '$ctrl',
                        controller: NewProcessDialogController,
                        bindToController: true,
                        multiple: multiple,
                        locals: {
                            process: p
                        }
                    }).then(
                        (proc) => this.mcprojstore.addProcess(p).then(() => this.mcbus.send('PROCESS$ADD', proc))
                    );
                },
                () => this.toast.error('Unable to create process from template')
            );
    }

    addSamplesToProcess(projectId, experimentId, process, samples) {
        let samplesToAdd = samples.map(s => ({
            id: s.id,
            property_set_id: s.property_set_id,
            command: 'add'
        }));

        let samplesArgs = {
            template_id: process.template_id,
            samples: samplesToAdd,
            process_id: process.id
        };

        return this.experimentsAPI.updateProcess(projectId, experimentId, process.id, samplesArgs).then(
            (process) => this.mcprojstore.addProcess(process).then(() => process));
    }

    addChildProcessFromTemplate(templateId, projectId, experimentId, parentProcess, multiple = true) {
        this.experimentsAPI.createProcessFromTemplate(projectId, experimentId, `global_${templateId}`)
            .then(
                (process) => {
                    let p = this.templates.loadTemplateFromProcess(process.template_name, process);

                    let samplesToAdd = parentProcess.output_samples.map(s => ({
                        id: s.id,
                        property_set_id: s.property_set_id,
                        command: 'add'
                    }));

                    let samplesArgs = {
                        template_id: process.template_id,
                        samples: samplesToAdd,
                        process_id: process.id
                    };

                    this.experimentsAPI.updateProcess(projectId, experimentId, process.id, samplesArgs).then(
                        (pUpdated) => {
                            p.input_samples = pUpdated.input_samples;
                            this.$mdDialog.show({
                                templateUrl: 'app/project/experiments/experiment/components/workflow/new-process-dialog.html',
                                controllerAs: '$ctrl',
                                controller: NewProcessDialogController,
                                bindToController: true,
                                multiple: multiple,
                                locals: {
                                    process: p
                                }
                            }).then(
                                () => this.sendProcessChangeEvent(projectId, experimentId)
                            )
                        }
                    )

                },
                () => this.toast.error('Unable to create child process')
            )
    }

    cloneProcess(projectId, experimentId, process) {
        let p = angular.copy(process);
        p.input_samples.forEach(s => s.selected = true);
        p.output_samples.forEach(s => s.selected = true);
        p.files.forEach(f => f.selected = true);
        return this.$mdDialog.show({
            templateUrl: 'app/project/experiments/experiment/components/workflow/services/clone-process-dialog.html',
            controllerAs: '$ctrl',
            controller: CloneProcessDialogController,
            bindToController: true,
            locals: {
                process: p,
                projectId: projectId
            }
        }).then(
            (cloneArgs) => {
                return this.experimentsAPI.cloneProcess(projectId, experimentId, p.id, cloneArgs).then(
                    (p) => this.mcbus.send('PROCESS$ADD', p),
                    () => this.toast.error('Error cloning process')
                )
            }
        );
    }

    sendProcessChangeEvent(projectId, experimentId) {
        this.experimentsAPI.getProcessesForExperiment(projectId, experimentId)
            .then(
                (processes) => this.mcbus.send('PROCESSES$CHANGE', processes),
                () => this.toast.error('Error retrieving processes for experiment')
            );
    }

    deleteNodeAndProcess(projectId, experimentId, processId) {
        this.processesAPI.getDeleteProcessPreConditions(projectId, processId)
            .then(
                process => {
                    let numberOfSamples = process.output_samples.length;
                    if (numberOfSamples === 0) {
                        this.deleteNodeAndProcess2(projectId, experimentId, processId);
                    } else {
                        this.confirmAndDeleteProcess(projectId, experimentId, process);
                    }
                },
                error => this.toast.error(error.data.error)
            );
    }

    confirmAndDeleteProcess(projectId, experimentId, process) {
        let processName = process.name;
        let numberOfSamples = process.output_samples.length;
        let samples = " output sample" + ((numberOfSamples !== 1) ? "s" : "");
        let processInfo = processName + " - has " + numberOfSamples + samples + ".";
        let confirm = this.$mdDialog.confirm()
            .title('This process has output samples: Delete node and Samples?')
            .textContent(processInfo)
            .ariaLabel('Please confirm - deleting node')
            .ok('Delete')
            .cancel('Cancel');

        this.$mdDialog.show(confirm).then(() => this.deleteNodeAndProcess2(projectId, experimentId, process.id));
    }

    deleteNodeAndProcess2(projectId, experimentId, processId) {
        this.experimentsAPI.deleteProcess(projectId, experimentId, processId)
            .then(
                () => this.mcprojstore.removeProcessById(processId).then(() => this.mcbus.send('PROCESS$DELETE', processId)),
                error => this.toast.error(error.data.error)
            );
    }
}

class NewProcessDialogController {
    /*@ngInject*/
    constructor($mdDialog, processesAPI, $stateParams) {
        this.$mdDialog = $mdDialog;
        this.processesAPI = processesAPI;
        this.projectId = $stateParams.project_id;
        this.processName = this.process.name;
    }

    done() {
        this.processesAPI.getProcess(this.projectId, this.process.id).then(
            (p) => this.$mdDialog.hide(p)
        );
    }
{'fraction_non_alphanumeric': 0.1017421189855416, 'fraction_numerical': 0.0005925574780753733, 'mean_word_length': 1.5853247549019607, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 59, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4675425', 'n_tokens_mistral': 4262, 'n_tokens_neox': 4071, 'n_words': 687}