dustin@856: /* Copyright (C) 2013 by Bundesamt fuer Strahlenschutz
dustin@856:  * Software engineering by Intevation GmbH
dustin@856:  *
dustin@856:  * This file is Free Software under the GNU GPL (v>=3)
dustin@856:  * and comes with ABSOLUTELY NO WARRANTY! Check out
dustin@856:  * the documentation coming with IMIS-Labordaten-Application for details.
dustin@856:  */
dustin@856: 
dustin@891: /**
dustin@856:  * Window to show a confirmation dialog to delete a Probe
dustin@856:  */
dustin@856: Ext.define('Lada.view.window.DeleteProbe', {
dustin@856:     extend: 'Ext.window.Window',
dustin@856:     alias: 'widget.deleteProbe',
dustin@856: 
dustin@856:     collapsible: true,
dustin@856:     maximizable: true,
dustin@856:     autoShow: true,
dustin@856:     autoScroll: true,
dustin@856:     layout: 'fit',
dustin@856:     constrain: true,
dustin@856: 
dustin@856:     record: null,
dustin@856:     parentWindow: null,
dustin@856: 
dustin@890:     /**
dustin@890:      * This function initialises the Window
dustin@890:      */
dustin@856:     initComponent: function() {
dustin@856:         var i18n = Lada.getApplication().bundle;
dustin@856: 
dustin@856:         // add listeners to change the window appearence when it becomes inactive
dustin@856:         this.on({
dustin@856:             activate: function(){
dustin@856:                 this.getEl().removeCls('window-inactive');
dustin@856:             },
dustin@856:             deactivate: function(){
dustin@856:                 this.getEl().addCls('window-inactive');
dustin@856:             }
dustin@856:         });
dustin@856: 
dustin@856:         this.title = i18n.getMsg('delete.probe.window.title');
dustin@856:         var me = this;
dustin@856:         this.buttons = [{
dustin@856:             text: i18n.getMsg('cancel'),
dustin@856:             scope: this,
dustin@856:             handler: this.close
dustin@856:         }, {
dustin@856:             text: i18n.getMsg('delete'),
dustin@856:             handler: function() {
dustin@856: 
dustin@856:                 Ext.Ajax.request({
dustin@999:                     url: 'lada-server/rest/probe/'+me.record.get('id'),
dustin@856:                     method: 'DELETE',
dustin@856:                     success: function(response) {
dustin@856:                         var json = Ext.JSON.decode(response.responseText);
raimund@858:                         if (json.success && json.message === '200') {
raimund@858:                             Ext.Msg.show({
raimund@858:                                 title: i18n.getMsg('success'),
raimund@858:                                 autoScroll: true,
raimund@858:                                 msg: 'Probe gelöscht!',
raimund@858:                                 buttons: Ext.Msg.OK
raimund@858:                             });
raimund@858:                         }
raimund@858:                         else {
raimund@858:                             Ext.Msg.show({
raimund@858:                                 title: 'Fehler!',
raimund@858:                                 msg: 'Ein Fehler ist aufgetreten, ist die Probe nicht leer?',
raimund@858:                                 buttons: Ext.Msg.OK
raimund@858:                             });
raimund@858:                         }
raimund@858:                         me.close();
dustin@856:                     },
dustin@856:                     failure: function(response) {
dustin@856:                         var json = null;
dustin@856:                         try {
dustin@856:                             json = Ext.JSON.decode(response.responseText);
dustin@856:                         }
dustin@856:                         catch(err){
dustin@856:                             Ext.Msg.alert(Lada.getApplication().bundle.getMsg('err.msg.generic.title'),
dustin@856:                                 Lada.getApplication().bundle.getMsg('err.msg.response.body'));
dustin@856:                         }
dustin@856:                         if (json) {
dustin@856:                             if(json.errors.totalCount > 0 || json.warnings.totalCount > 0){
dustin@856:                                 formPanel.setMessages(json.errors, json.warnings);
dustin@856:                             }
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@856:                                 Ext.MessageBox.confirm('Erneutes Login erforderlich',
dustin@920:                                     'Ihre Session ist abgelaufen.<br/>'+
dustin@856:                                     'Für ein erneutes Login muss die Anwendung neu geladen werden.<br/>' +
dustin@856:                                     'Alle ungesicherten Daten gehen dabei verloren.<br/>' +
dustin@920:                                     'Soll die Anwendung jetzt neu geladen werden?', this.reload);
dustin@856:                             }
dustin@920:                             // further error handling
dustin@920:                             if(json.message){
dustin@856:                                 Ext.Msg.alert(Lada.getApplication().bundle.getMsg('err.msg.generic.title')
dustin@856:                                     +' #'+json.message,
dustin@856:                                     Lada.getApplication().bundle.getMsg(json.message));
dustin@856:                             } else {
dustin@856:                                 Ext.Msg.alert(Lada.getApplication().bundle.getMsg('err.msg.generic.title'),
dustin@856:                                     Lada.getApplication().bundle.getMsg('err.msg.generic.body'));
dustin@856:                             }
dustin@856:                         } else {
dustin@856:                             Ext.Msg.alert(Lada.getApplication().bundle.getMsg('err.msg.generic.title'),
dustin@856:                                 Lada.getApplication().bundle.getMsg('err.msg.response.body'));
dustin@856:                         }
dustin@856:                     }
dustin@856:                 });
dustin@856:             }
dustin@856:         }];
dustin@856:         this.width = 350;
dustin@856:         this.height = 250;
dustin@856: 
dustin@856:         // add listeners to change the window appearence when it becomes inactive
dustin@856:         this.on({
dustin@856:             activate: function(){
dustin@856:                 this.getEl().removeCls('window-inactive');
dustin@856:             },
dustin@856:             deactivate: function(){
dustin@856:                 this.getEl().addCls('window-inactive');
dustin@856:             },
dustin@856:             close: function () {
dustin@856:                 this.parentWindow.probenWindow = null;
dustin@856:             }
dustin@856:         });
dustin@856: 
dustin@856:         // InitialConfig is the config object passed to the constructor on
dustin@856:         // creation of this window. We need to pass it throuh to the form as
dustin@856:         // we need the "Id" param to load the correct item.
dustin@856:         this.items = [{
dustin@856:             border: 0,
dustin@856:             autoScroll: true,
dustin@856:             items: [{
dustin@856:                 xtype: 'panel',
dustin@856:                 border: 0,
dustin@856:                 margin: 5,
dustin@856:                 layout: 'fit',
dustin@856:                 html: '<p>'
dustin@856:                     + i18n.getMsg('delete.probe')
dustin@856:                     + '<br/>'
dustin@856:                     + '<br/>'
dustin@856:                     + this.record.get('probeId')
dustin@856:                     + '<br/>'
dustin@856:                     + '<br/>'
dustin@856:                     + i18n.getMsg('delete.probe.warning')
dustin@856:                     + '</p>'
dustin@856:             }]
dustin@856:         }];
dustin@856:         this.callParent(arguments);
dustin@856:     },
dustin@856: 
dustin@856:     /**
dustin@890:      * Inititalise Data
dustin@856:      */
dustin@856:     initData: function() {
dustin@856:         var i18n = Lada.getApplication().bundle;
dustin@856:         me = this;
dustin@856:     },
dustin@856: 
dustin@856:     /**
dustin@856:      * Parse ServerResponse when Proben have been generated
dustin@856:      */
dustin@856:     evalResponse: function(response) {
dustin@856:         var i18n = Lada.getApplication().bundle;
dustin@856:         var r = '';
dustin@856:             r += response.data.length;
dustin@856:             r += ' ' + i18n.getMsg('probedeleted');
dustin@856:         return r;
dustin@856:     },
dustin@856: 
dustin@890:     /**
dustin@890:      * Reload the Application
dustin@890:      */
dustin@856:     reload: function(btn) {
dustin@856:         if (btn === 'yes') {
dustin@856:             location.reload();
dustin@856:         }
dustin@856:     }
dustin@856: });
dustin@856: