view app/controller/Proben.js @ 427:7d80feaa4a7f

The proxy to REST for Verwaltungseinheiten
author Torsten Irländer <torsten.irlaender@intevation.de>
date Fri, 08 Nov 2013 12:34:37 +0100
parents 85238577adc9
children bd2b26c249e2
line wrap: on
line source
/**
 * Controller for Proben
 */

function buildImportReport(filename, msg, errors, warnings) {
    var out = Array();
    if (msg != 200) {
        out.push("Der Import der Datei " + filename + " war nicht erfolgreich.");
    } else {
        out.push("Der Import der Datei " + filename + " war erfolgreich.");
    }
    out.push("Bei dem Import sind folgende Fehler und Warnungen aufgetreten");
    out.push("<br/>");
    out.push("<strong>Fehler:</strong>");
    out.push("<br/>");
    if (errors) {
        out.push("<ol>");
        for (var key in errors) {
            out.push("<li>"+key)
            var msgs = errors[key];
            out.push("<ol>");
            for (var i = msgs.length - 1; i >= 0; i--){
                out.push("<li>"+msgs[i].key+" ("+msgs[i].code+"): "+msgs[i].value+"</li>")
            };
            out.push("</ol>");
            out.push("</li>");
        }
        out.push("</ol>");
    } else {
        out.push("Keine Fehler");
        out.push("<br/>");
    }
    out.push("<strong>Warnungen:</strong>");
    out.push("<br/>");
    if (warnings) {
        out.push("<ol>");
        for (var key in warnings) {
            out.push("<li>"+key)
            var msgs = warnings[key];
            out.push("<ol>");
            for (var i = msgs.length - 1; i >= 0; i--){
                out.push("<li>"+msgs[i].key+" ("+msgs[i].code+"): "+msgs[i].value+"</li>")
            };
            out.push("</ol>");
            out.push("</li>");
        }
        out.push("</ol>");
    } else {
        out.push("Keine Warnungen");
        out.push("<br/>");
    }
    return out.join("");
}

Ext.define('Lada.controller.Proben', {
    extend: 'Lada.controller.Base',
    views: [
        'proben.Edit',
        'proben.Create',
        'proben.Import'
    ],
    stores: [
        'Proben',
        'Zusatzwerte',
        'Probenzusatzwerte',
        'Kommentare',
        'Orte',
        'Messungen'
    ],
    init: function() {
        console.log('Initialising the Proben controller');
        this.callParent();
    },
    addListeners: function() {
        this.control({
            'probenlist': {
                itemdblclick: this.editItem
            },
            'probenlist toolbar button[action=add]': {
                click: this.addItem
            },
            'probenlist toolbar button[action=import]': {
                click: this.selectUploadFile
            },
            'probenlist toolbar button[action=export]': {
                click: this.downloadFile
            },
            'probencreate button[action=save]': {
                click: this.saveItem
            },
            'probenedit button[action=save]': {
                click: this.saveItem
            },
            'probenimport button[action=save]': {
                click: this.uploadItem
            },
            'probencreate form': {
                savesuccess: this.createSuccess,
                savefailure: this.createFailure
            },
            'probenedit form': {
                savesuccess: this.editSuccess,
                savefailure: this.editFailure
            }
        });
    },
    addItem: function(button) {
        console.log('Adding new Probe');
        var view = Ext.widget('probencreate');
    },
    /**
     * Opens a window with a file chooser to select the file to upload
     * @private
     */
    selectUploadFile: function(button) {
        console.log('Importing');
        var view = Ext.widget('probenimport');
    },
    /** Uploads the selected file the the server
     * @private
     */
    uploadItem: function(button) {
        var win = button.up('window');
        var form = win.down('form');
        if(form.isValid()){
            form.submit({
                url: 'server/rest/import/laf',
                waitMsg: 'Importiere...',
                // TODO: Handle the response correct. o must must contain the
                // filename (None) <2013-08-13 16:17>
                success: function(fp, resp) {
                    var errors = resp.result.data.errors;
                    var warnings = resp.result.data.warnings;
                    var filename = resp.result.data.filename;
                    var message = resp.result.message;
                    var dialogbody = buildImportReport(filename, message, errors, warnings)
                    var filename = resp.result.data.filename;
                    Ext.Msg.alert('Erfolg', dialogbody);
                    win.close();
                },
                failure: function(fp, resp) {
                    var errors = resp.result.data.errors;
                    var warnings = resp.result.data.warnings;
                    var filename = resp.result.data.filename;
                    var message = resp.result.message;
                    var dialogbody = buildImportReport(filename, message, errors, warnings)
                    Ext.Msg.alert('Fehler', dialogbody);
                    win.close();
                }
            });
        }
    },
    /**
     * Will download the selected Probe in LAF format in a new window (tab).
     */
    downloadFile: function(button) {
        var grid = button.up('grid');
        var selection = grid.getView().getSelectionModel().getSelection();
        var hiddenItems = [];
        for (var i = 0; i < selection.length; i++) {
            hiddenItems.push({xtype: 'hiddenfield', name: 'probeId', value: selection[i].get('probeId')});
        }
        console.log('create download form.');
        var hiddenForm = Ext.create('Ext.form.Panel', {
            title: 'hiddenForm',
            standardSubmit: true,
            url: 'server/rest/export/laf',
            timeout: 120,
            height: 0,
            width: 0,
            hidden: true,
            items: hiddenItems
        });
        hiddenForm.getForm().submit();
    },
    editItem: function(grid, record) {
        console.log('Editing Probe');
        var id = record.get('probeId');
        // Load Zusatzwerte
        var pstore = this.getProbenzusatzwerteStore();
        pstore.load();
        var zstore = this.getZusatzwerteStore();
        zstore.load({
            params: {
                probeId: id
            }
        });
        // Load Kommentare
        var kstore = this.getKommentareStore();
        kstore.load({
            params: {
                probeId: id
            }
        });
        // Load Orte
        var ostore = this.getOrteStore();
        ostore.load({
            params: {
                probeId: id
            }
        });
        // Load Orte
        var mstore = this.getMessungenStore();
        mstore.load({
            params: {
                probeId: id
            }
        });
        var view = Ext.widget('probenedit', {modelId: id});
        console.log("Loaded Probe with ID " + record.getId()); //outputs ID
    },
    createSuccess: function(form, record, response) {
        // Close Createdialog
        var win = form.up('window');
        win.close();
        var resp = Ext.decode(response.responseText);
        var warn = Array();
        var err = Array();
        if (resp.warnings) {
            warn.push("<strong>Warnungen:</strong><br>");
            for (var key in resp.warnings) {
                warn.push(key + ": ");
                warn.push(Lada.getApplication().bundle.getMsg(resp.warnings[key]) + "<br>");
            }
        }
        if (resp.errors) {
            err.push("<strong>Fehler:</strong><br>");
            for (var key in resp.errors) {
                err.push(key + ": ");
                err.push(Lada.getApplication().bundle.getMsg(resp.warnings[key]) + "<br>");
            }
        }
        var msg = warn.join("") + err.join("");
        Ext.MessageBox.alert("Ergebnis", msg);
        // Reload store
        var store = this.getProbenStore();
        store.reload({
            scope: this,
            callback: function(records, operation, success) {
                console.log('Reloaded store');
                // Open Editdialog
                var json = Ext.decode(response.responseText);
                if (json) {
                    var probeId = json.data.probeId;
                    var probe = store.findRecord("probeId", probeId);
                    this.editItem(null, probe);
                }
            }
        });
    },
    editSuccess: function(form, record, response) {
        // Reload store
        var store = this.getProbenStore();
        store.reload();
        var win = form.up('window');
        win.close();
    }
});

http://lada.wald.intevation.org