(function (module) {
    module.factory("processEdit", processEdit);
    processEdit.$inject = [];

    function processEdit() {
        /**
         * fillSetUp: will read all the setup values from process
         * and place inside template.
         *
         */
        function setUp(template, process) {
            process.setup[0].properties.forEach(function (property) {
                var i = _.indexOf(template.setup.settings[0].properties, function (template_property) {
                    return template_property.property.attribute === property.attribute
                });
                if (i > -1) {
                    template.setup.settings[0].properties[i].property.value = property.value;
                    template.setup.settings[0].properties[i].property.name = property.name;
                    template.setup.settings[0].properties[i].property.description = property.description;
                    template.setup.settings[0].properties[i].property.unit = property.unit;
                    template.setup.settings[0].properties[i].property.id = property.id;
                    template.setup.settings[0].properties[i].property.setup_id = property.setup_id;
                    template.setup.settings[0].properties[i].property._type = property._type;
                    template.setup.settings[0].properties[i].property.attribute = property.attribute;
                }
            });
            process.setup = template.setup;
            return process;
        }

        function samples(process) {
            process.input_samples = process.input_samples.map(function (sample) {
                return {
                    id: sample.id,
                    name: sample.name,
                    property_set_id: sample.property_set_id,
                    files: sample.linked_files,
                    old_properties: [],
                    new_properties: []
                }
            });
            return process;
        }

        function files(process) {
            process['input_files'] = process.input_files.map(function (file) {
                return {id: file.id, name: file.name}
            });
            process['output_files'] = process.output_files.map(function (file) {
                return {id: file.id, name: file.name}
            });
            return process;
        }

        return {
            fillProcess: function (template, process) {
                process = setUp(template, process);
                process = samples(process);
                process = files(process);
                if (!('output_samples' in process)) {
                    process.output_samples = [];
                }
                if (!('transformed_samples' in process)) {
                    process.transformed_samples = [];
                }
                return process;
            },

            addToSamplesFiles: function (files, process) {
                files.forEach(function (f) {
                    var i = _.indexOf(process.samples_files, function (item) {
                        return f.id === item.id && f.sample_id == item.sample_id;
                    });
                    if (i !== -1) {
                        process.samples_files.splice(i, 1);
                        process.samples_files.push({
                            id: f.id,
                            command: f.command,
                            name: f.name,
                            sample_id: f.sample_id
                        });
                    } else {
                        if (f.command) {
                            process.samples_files.push({
                                id: f.id,
                                command: f.command,
                                name: f.name,
                                sample_id: f.sample_id
                            });
                        }
                    }
                });
                return process;
            },

            refreshSample: function (files, sample) {
                files.forEach(function (f) {
                    if (f.command) {
                        var i = _.indexOf(sample.files, function (item) {
                            return f.id === item.id && f.sample_id == sample.id;
                        });
                        if (i !== -1) {
                            sample.files.splice(i, 1);
                            if (!(f.command === 'delete')) {
                                sample.files.push({id: f.id, command: f.command, name: f.name, linked: f.linked});
                            }
                        } else {
                            if (!(f.command === 'delete')) {
                                sample.files.push({id: f.id, command: f.command, name: f.name, linked: f.linked});
                            }
                        }
                    }
                });
                return sample;
            }
        };
    }
}(angular.module('materialscommons')));

@@ -4424,39 +4424,26 @@
     
-if (!(f.command === 'delete')) 
+sample.files.push(
 %7B%0A  
@@ -4476,90 +4476,171 @@
     
-sample.files.push(%7Bid: f.id, command: f.command, name: f.name, linked: f.linked%7D);
+id: f.id,%0A                                command: f.command,%0A                                name: f.name,%0A                                sample_id: f.sample_id,
 %0A   
@@ -4656,38 +4656,53 @@
                 
-%7D%0A
     
+linked: f.linked%0A
                 
@@ -4701,32 +4701,35 @@
             
-%7D else %7B
+        %7D);
 %0A           
@@ -4745,49 +4745,17 @@
     
-    if (!(f.command === 'delete')) %7B%0A    
+%7D else %7B%0A
     
@@ -4801,51 +4801,236 @@
 sh(%7B
-id: f.id, command: f.command, name: f.name,
+%0A                                id: f.id,%0A                                command: f.command,%0A                                name: f.name,%0A                                sample_id: f.sample_id,%0A                               
  lin
@@ -5042,19 +5042,16 @@
 f.linked
-%7D);
 %0A       
@@ -5064,32 +5064,34 @@
                %7D
+);
 %0A               

Send the sample id and the file id when linking or unlinking files.