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: [
dustin@754:         'Lada.view.window.ProbeEdit',
dustin@759:         'Lada.view.window.Messprogramm'
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@810:             'store': {
dustin@810:                 beforeload: this.loadingAnimationOn,
dustin@810:                 load:   this.loadingAnimationOff
dustin@810:             },
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:             },
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
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@754:      * or a {@link Lada.view.window.MessprogrammEdit}.
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@754:      * {@link Lada.view.window.MessprogrammCreate}
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@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({
raimund@725:             method: 'POST',
dustin@727:             url: 'lada-server/export/laf',
raimund@725:             jsonData: {'proben': proben},
raimund@725:             headers: {'X-OPENID-PARAMS': Lada.openIDParams},
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@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@812:                     // TODO Move this handling of 699 and 698 to a more central place!
dustin@812:                     // TODO i18n
dustin@812:                     if (json.message === "699" || json.message === "698") {
dustin@812:                         /* This is the unauthorized message with the authentication
dustin@812:                             * redirect in the data */
dustin@812: 
dustin@812:                         /* We decided to handle this with a redirect to the identity
dustin@812:                             * provider. In which case we have no other option then to
dustin@812:                             * handle it here with relaunch. */
dustin@812:                         Ext.MessageBox.confirm('Erneutes Login erforderlich',
dustin@812:                             'Der Server konnte die Anfrage nicht authentifizieren.<br/>'+
dustin@812:                             'Für ein erneutes Login muss die Anwendung neu geladen werden.<br/>' +
dustin@812:                             'Alle ungesicherten Daten gehen dabei verloren.<br/>' +
dustin@825:                             'Soll die Anwendung jetzt neu geladen werden?', me.reload);
dustin@812:                     }
dustin@812:                     else 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@812:     reload: function(btn) {
dustin@812:         if (btn === 'yes') {
dustin@812:             location.reload();
dustin@812:         }
raimund@548:     }
raimund@548: });