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