raimund@548: /* Copyright (C) 2013 by Bundesamt fuer Strahlenschutz
raimund@548:  * Software engineering by Intevation GmbH
raimund@548:  *
raimund@548:  * This file is Free Software under the GNU GPL (v>=3)
raimund@548:  * and comes with ABSOLUTELY NO WARRANTY! Check out
raimund@548:  * the documentation coming with IMIS-Labordaten-Application for details.
raimund@548:  */
raimund@548: 
raimund@548: /**
raimund@548:  * Controller for filter result grid.
raimund@548:  */
raimund@548: Ext.define('Lada.controller.FilterResult', {
raimund@548:     extend: 'Ext.app.Controller',
raimund@548:     requires: [
tom@1227:         'Lada.view.window.FileUpload',
dustin@754:         'Lada.view.window.ProbeEdit',
raimund@1083:         'Lada.view.window.ProbeCreate',
dustin@940:         'Lada.view.window.Messprogramm',
dustin@940:         'Lada.view.window.GenProbenFromMessprogramm'
raimund@548:     ],
raimund@548: 
dustin@742:     /**
dustin@810:      * Initialize the Controller with listeners
dustin@742:      */
raimund@548:     init: function() {
raimund@548:         this.control({
raimund@548:             'filterresultgrid': {
raimund@548:                 itemdblclick: this.editItem
raimund@548:             },
dustin@754:             'filterresultgrid toolbar button[action=addProbe]': {
dustin@754:                 click: this.addProbeItem
raimund@548:             },
dustin@755:             'filterresultgrid toolbar button[action=addMessprogramm]': {
dustin@755:                 click: this.addMessprogrammItem
dustin@755:             },
dustin@860:             'filterresultgrid toolbar button[action=genProbenFromMessprogramm]': {
dustin@860:                 click: this.genProbenFromMessprogramm
dustin@860:             },
raimund@548:             'filterresultgrid toolbar button[action=import]': {
raimund@548:                 click: this.uploadFile
raimund@548:             },
raimund@548:             'filterresultgrid toolbar button[action=export]': {
raimund@548:                 click: this.downloadFile
dustin@923:             },
dustin@923:             'filterresultgrid toolbar button[action=print]': {
dustin@923:                 click: this.printSelection
raimund@548:             }
raimund@548:         });
raimund@548:         this.callParent(arguments);
raimund@548:     },
raimund@548: 
dustin@742:     /**
dustin@742:      * This function is called after a Row in the
dustin@742:      * {@link Lada.view.grid.FilterResult}
dustin@742:      * was double-clicked.
dustin@742:      * The function opens a {@link Lada.view.window.ProbeEdit}
dustin@895:      * or a {@link Lada.view.window.Messprogramm}.
dustin@754:      * To determine which window has to be opened, the function
dustin@754:      * analyse the records modelname.
dustin@742:      */
raimund@548:     editItem: function(grid, record) {
dustin@754:         var mname = record.store.model.modelName || '';
dustin@754:         var winname = '';
dustin@754: 
dustin@754:         //Based upon the Model that was loaded, act differently
dustin@754:         if (mname == 'Lada.model.ProbeList'){
dustin@754:             winname = 'Lada.view.window.ProbeEdit';
dustin@754:         }
dustin@754:         else if (mname == 'Lada.model.MessprogrammList'){
dustin@759:             winname = 'Lada.view.window.Messprogramm';
dustin@754:         }
dustin@754:         if (winname){
dustin@754:             var win = Ext.create(winname, {
dustin@814:                 record: record,
dustin@814:                 style: 'z-index: -1;' //Fixes an Issue where windows could not be created in IE8
dustin@754:              });
dustin@754:             win.show();
dustin@754:             win.initData();
dustin@754:         }
dustin@754:         else {
dustin@754:             console.log('The model is unknown.'
dustin@754:                 +'No window was configured to display the data.'
dustin@754:                 +'I retrieved a model named:' + mname
dustin@754:             );
dustin@754:         }
dustin@754:     },
dustin@754: 
dustin@754:     /**
dustin@754:      * This function opens a new window to create a Probe
dustin@754:      * {@link Lada.view.window.ProbeCreate}
dustin@754:      */
dustin@754:     addProbeItem: function() {
dustin@754:         var win = Ext.create('Lada.view.window.ProbeCreate');
raimund@548:         win.show();
raimund@548:         win.initData();
raimund@548:     },
raimund@548: 
dustin@742:     /**
dustin@742:      * This function opens a new window to create a Probe
dustin@895:      * {@link Lada.view.window.Messprogramm}
dustin@742:      */
dustin@754:     addMessprogrammItem: function() {
dustin@759:         var win = Ext.create('Lada.view.window.Messprogramm');
dustin@755:         win.show();
dustin@755:         win.initData();
raimund@548:     },
raimund@548: 
dustin@742:     /**
dustin@860:      * This button creates a window to generate Proben
dustin@860:      * from a selected messprogramm.
dustin@860:      */
dustin@860:     genProbenFromMessprogramm: function(button) {
dustin@860:         var grid = button.up('grid');
dustin@860:         var selection = grid.getView().getSelectionModel().getSelection();
dustin@860:         var i18n = Lada.getApplication().bundle;
dustin@860:         var proben = [];
dustin@860:         for (var i = 0; i < selection.length; i++) {
dustin@860:             proben.push(selection[i].get('id'));
dustin@860:         }
dustin@860:         var me = this;
dustin@860: 
dustin@860:         var winname = 'Lada.view.window.GenProbenFromMessprogramm';
dustin@860:         for (p in proben) {
dustin@860:             grid.setLoading(true);
dustin@860:             Ext.ClassManager.get('Lada.model.Messprogramm').load(proben[p], {
dustin@860:                 failure: function(record, action) {
dustin@860:                     me.setLoading(false);
dustin@860:                     // TODO
dustin@860:                     console.log('An unhandled Failure occured. See following Response and Record');
dustin@860:                     console.log(action);
dustin@860:                     console.log(record);
dustin@860:                     },
dustin@860:                 success: function(record, response) {
dustin@860:                     grid.setLoading(false);
dustin@860: 
dustin@860:                     var win = Ext.create(winname, {
dustin@860:                         record: record,
dustin@860:                         parentWindow: null
dustin@860:                     });
dustin@860:                     win.show();
dustin@860:                     win.initData();
dustin@860:                 },
dustin@860:                 scope: this
dustin@860:             });
dustin@860:         }
dustin@860:     },
dustin@860: 
dustin@860:     /**
dustin@742:      * This function opens a {@link Lada.view.window.FileUpload}
dustin@742:      * window to upload a LAF-File
dustin@742:      */
raimund@725:     uploadFile: function() {
raimund@725:         var win = Ext.create('Lada.view.window.FileUpload', {
raimund@725:             title: 'Datenimport',
raimund@725:             modal: true
raimund@725:         });
raimund@548: 
raimund@725:         win.show();
raimund@548:     },
raimund@548: 
dustin@742:     /**
dustin@742:      * This function can be used to Download the items which
dustin@742:      * were selected in the {@link Lada.view.grid.FilterResult}
dustin@742:      * The Download does not work with Internet Explorers older than v.10
dustin@742:      */
raimund@548:     downloadFile: function(button) {
raimund@725:         var grid = button.up('grid');
raimund@725:         var selection = grid.getView().getSelectionModel().getSelection();
dustin@754:         var i18n = Lada.getApplication().bundle;
raimund@725:         var proben = [];
raimund@725:         for (var i = 0; i < selection.length; i++) {
raimund@725:             proben.push(selection[i].get('id'));
raimund@725:         }
dustin@825:         var me = this;
raimund@725:         Ext.Ajax.request({
dustin@999:             url: 'lada-server/data/export/laf',
raimund@725:             jsonData: {'proben': proben},
raimund@725:             success: function(response) {
raimund@725:                 var content = response.responseText;
raimund@725:                 var blob = new Blob([content],{type: 'text/plain'});
raimund@725:                 saveAs(blob, 'export.laf');
raimund@725:             },
dustin@812:             failure: function(response) {
dustin@920:                 /*
dustin@920:                 SSO will send a 302 if the Client is not authenticated
dustin@920:                 unfortunately this seems to be filtered by the browser.
dustin@920:                 We assume that a 302 was send when the follwing statement
dustin@920:                 is true.
dustin@920:                 */
dustin@920:                 if (response.status == 0 && response.responseText === "") {
dustin@920:                     Ext.MessageBox.confirm('Erneutes Login erforderlich',
dustin@920:                         'Ihre Session ist abgelaufen.<br/>'+
dustin@920:                         'Für ein erneutes Login muss die Anwendung neu geladen werden.<br/>' +
dustin@920:                         'Alle ungesicherten Daten gehen dabei verloren.<br/>' +
dustin@920:                         'Soll die Anwendung jetzt neu geladen werden?', this.reload);
dustin@920:                 }
dustin@920:                 // further error handling
dustin@812:                 var json = Ext.JSON.decode(response.responseText);
dustin@812:                 if (json) {
dustin@812:                     if(json.errors.totalCount > 0 || json.warnings.totalCount > 0){
dustin@812:                         formPanel.setMessages(json.errors, json.warnings);
dustin@812:                     }
dustin@920:                     if(json.message){
dustin@812:                         Ext.Msg.alert(Lada.getApplication().bundle.getMsg('err.msg.generic.title')
dustin@812:                             +' #'+json.message,
dustin@812:                             Lada.getApplication().bundle.getMsg(json.message));
dustin@812:                     } else {
dustin@812:                         Ext.Msg.alert(i18n.getMsg('err.msg.generic.title'),
dustin@812:                             i18n.getMsg('err.msg.laf.filecreatefailed'));
dustin@812:                     }
dustin@812:                 } else {
dustin@812:                     Ext.Msg.alert(i18n.getMsg('err.msg.generic.title'),
dustin@754:                     i18n.getMsg('err.msg.laf.filecreatefailed'));
dustin@812:                 }
raimund@725:             }
raimund@725:         });
dustin@812:     },
dustin@812: 
dustin@923:     /**
dustin@923:      * Send the selection to a Printservice
dustin@923:      */
dustin@923:     printSelection: function(button) {
dustin@939: 
dustin@939:         //disable Button and setLoading...
dustin@939:         button.disable();
dustin@939:         button.setLoading(true);
dustin@939: 
dustin@923:         var grid = button.up('grid');
dustin@923:         var selection = grid.getView().getSelectionModel().getSelection();
dustin@923:         var i18n = Lada.getApplication().bundle;
dustin@934:         var me = this;
dustin@934:         var columns = [];
dustin@937:         var columnNames = [];
dustin@938:         var visibleColumns = [];
dustin@938:         var displayName = '';
dustin@934:         var data = [];
dustin@937: 
dustin@934:         // Write the columns to an array
dustin@934:         try {
dustin@934:             for (key in selection[0].data) {
dustin@937:                 // Do not write owner or readonly or id
dustin@937:                 if (["owner", "readonly", "id"].indexOf(key) == -1){
dustin@934:                     columns.push(key);
dustin@934:                 }
dustin@934:             }
dustin@937:         }
dustin@937:         catch (e) {
dustin@937:             console.log(e);
dustin@937:         }
dustin@937: 
dustin@937:         //Retrieve visible columns' id's and names.
dustin@938:         // and set displayName
dustin@937:         try {
dustin@937:             var grid = button.up('filterresultgrid');
dustin@937:             var cman = grid.columnManager;
dustin@937:             var cols = cman.getColumns();
dustin@938: 
dustin@938:             displayName = grid.down('tbtext').text;
dustin@938: 
dustin@937:             for (key in cols) {
dustin@937:                 if (cols[key].dataIndex) {
dustin@937:                     visibleColumns[cols[key].dataIndex] = cols[key].text;
dustin@937:                 }
dustin@937:             }
dustin@937:         }
dustin@937:         catch (e) {
dustin@936:             console.log(e);
dustin@923:         }
dustin@934: 
dustin@936: 
dustin@934:         // Retrieve Data from selection
dustin@934:         try {
dustin@934:             for (item in selection) {
dustin@934:                 var row = selection[item].data;
dustin@934:                 var out = [];
dustin@934:                 //Lookup every column and write to data array.
dustin@934:                 for (key in columns){
dustin@934:                     var attr = columns[key];
dustin@937:                     //Only write data to output when the column is not hidden.
dustin@937:                     if (row[attr] != null &&
dustin@937:                         visibleColumns[attr] != null) {
dustin@936:                         out.push(row[attr].toString());
dustin@936:                     }
dustin@937:                     else if (visibleColumns[attr] != null) {
dustin@936:                         out.push('');
dustin@936:                     }
dustin@934:                 }
dustin@934:                 data.push(out);
dustin@934:             }
dustin@937:         }
dustin@937:         catch (e){
dustin@936:             console.log(e);
dustin@934:         }
dustin@937: 
dustin@937:         //Retrieve the names of the columns.
dustin@937:         try {
dustin@937:             var grid = button.up('filterresultgrid');
dustin@937:             var cman = grid.columnManager;
dustin@937:             var cols = cman.getColumns();
dustin@937:             //Iterate columns and find column names for the key...
dustin@937:             // This WILL run into bad behaviour when column-keys exist twice.
dustin@937:             for (key in columns){
dustin@937:                 for (k in cols){
dustin@937:                     if (cols[k].dataIndex == columns[key]){
dustin@937:                         columnNames.push(cols[k].text);
dustin@937:                         break;
dustin@937:                     }
dustin@937:                 }
dustin@937:             }
dustin@937:         }
dustin@937:         catch (e) {
dustin@937:             console.log(e);
dustin@937:         }
dustin@934: 
dustin@934:         var printData = {
dustin@934:             'layout': 'A4 landscape',
dustin@934:             'outputFormat': 'pdf',
dustin@934:             'attributes': {
dustin@934:                 'title': 'Auszug aus LADA',
dustin@938:                 'displayName': displayName,
dustin@937:                 'table': {
dustin@937:                     'columns': columnNames,
dustin@937:                     'data': data
dustin@937:                 }
dustin@934:             }
dustin@934:         }
dustin@934: 
dustin@923:         Ext.Ajax.request({
dustin@934:             url: 'lada-printer/buildreport.pdf',
dustin@934:             //configure a proxy in apache conf!
dustin@934:             jsonData: printData,
dustin@934:             binary: true,
dustin@923:             success: function(response) {
dustin@934:                 var content = response.responseBytes;
dustin@934:                 var filetype = response.getResponseHeader('Content-Type');
dustin@934:                 var blob = new Blob([content],{type: filetype});
dustin@923:                 saveAs(blob, 'lada-print.pdf');
dustin@939:                 button.enable();
dustin@939:                 button.setLoading(false);
dustin@923:             },
dustin@923:             failure: function(response) {
dustin@923:                 console.log('failure');
dustin@923:                 // Error handling
dustin@923:                 // TODO
dustin@934:                 //console.log(response.responseText)
dustin@939:                 button.enable();
dustin@939:                 button.setLoading(false);
dustin@923:                 if (response.responseText) {
dustin@923:                     try {
dustin@923:                         var json = Ext.JSON.decode(response.responseText);
dustin@923:                     }
dustin@923:                     catch(e){
dustin@923:                         console.log(e);
dustin@923:                     }
dustin@923:                 }
dustin@923:                 if (json) {
dustin@923:                     if(json.errors.totalCount > 0 || json.warnings.totalCount > 0){
dustin@923:                         formPanel.setMessages(json.errors, json.warnings);
dustin@923:                     }
dustin@923:                     if(json.message){
dustin@923:                         Ext.Msg.alert(Lada.getApplication().bundle.getMsg('err.msg.generic.title')
dustin@923:                             +' #'+json.message,
dustin@923:                             Lada.getApplication().bundle.getMsg(json.message));
dustin@923:                     } else {
dustin@923:                         Ext.Msg.alert(i18n.getMsg('err.msg.generic.title'),
dustin@923:                             i18n.getMsg('err.msg.print.noContact'));
dustin@923:                     }
dustin@923:                 } else {
dustin@923:                     Ext.Msg.alert(i18n.getMsg('err.msg.generic.title'),
dustin@923:                     i18n.getMsg('err.msg.print.noContact'));
dustin@923:                 }
dustin@923:             }
dustin@923:         });
dustin@923:     },
dustin@923: 
dustin@812:     reload: function(btn) {
dustin@812:         if (btn === 'yes') {
dustin@812:             location.reload();
dustin@812:         }
raimund@548:     }
raimund@548: });