raimund@1082: /* Copyright (C) 2013 by Bundesamt fuer Strahlenschutz
raimund@1082:  * Software engineering by Intevation GmbH
raimund@1082:  *
raimund@1082:  * This file is Free Software under the GNU GPL (v>=3)
raimund@1082:  * and comes with ABSOLUTELY NO WARRANTY! Check out
raimund@1082:  * the documentation coming with IMIS-Labordaten-Application for details.
raimund@1082:  */
raimund@1082: 
raimund@1082: /**
raimund@1082:  * This is a simple Window to set the Status for multiple Messungen on bulk.
raimund@1082:  */
raimund@1082: Ext.define('Lada.view.window.SetStatus', {
raimund@1082:     extend: 'Ext.window.Window',
raimund@1082:     alias: 'setstatuswindow',
raimund@1082: 
raimund@1082:     requires: [
raimund@1082:         'Lada.view.widget.Status'
raimund@1082:     ],
raimund@1082: 
raimund@1082:     grid: null,
raimund@1082:     selection: null,
raimund@1082: 
raimund@1082:     modal: true,
raimund@1082:     closable: false,
raimund@1082:     resultMessage: '',
raimund@1082: 
raimund@1082:     /**
raimund@1082:      * This function initialises the Window
raimund@1082:      */
raimund@1082:     initComponent: function() {
raimund@1082:         var i18n = Lada.getApplication().bundle;
raimund@1082:         var me = this;
tom@1230:         var statusWerteStore = Ext.create('Lada.store.StatusWerte');
tom@1230:         statusWerteStore.load({
tom@1230:             params: {
tom@1230:                 messungsId: Ext.Array.pluck(this.selection, 'id').toString()
tom@1230:             }
tom@1230:         });
tom@1230: 
raimund@1082:         this.items = [{
tom@1228:             xtype: 'form',
raimund@1082:             name: 'valueselection',
raimund@1082:             border: 0,
raimund@1082:             items: [{
raimund@1082:                 xtype: 'fieldset',
raimund@1082:                 title: 'Status für ' + this.selection.length + ' Messung(en) setzen',
raimund@1082:                 margin: '5, 5, 10, 5',
raimund@1082:                 items: [{
raimund@1082:                     xtype: 'combobox',
raimund@1082:                     store: Ext.data.StoreManager.get('messstellenFiltered'),
raimund@1082:                     displayField: 'messStelle',
raimund@1082:                     valueField: 'id',
raimund@1082:                     allowBlank: false,
raimund@1082:                     queryMode: 'local',
raimund@1082:                     editable: false,
raimund@1082:                     width: 300,
raimund@1082:                     labelWidth: 100,
raimund@1082:                     emptyText: 'Wählen Sie einen Erzeuger aus.',
raimund@1082:                     fieldLabel: i18n.getMsg('statusgrid.header.erzeuger')
raimund@1082:                 }, {
raimund@1082:                     xtype: 'statuswert',
tom@1230:                     store: statusWerteStore,
raimund@1082:                     allowBlank: false,
raimund@1082:                     width: 300,
raimund@1082:                     labelWidth: 100,
raimund@1082:                     fieldLabel: i18n.getMsg('statusgrid.header.statusWert')
raimund@1082:                 }, {
tom@1230:                     xtype: 'combobox',
tom@1230:                     name: 'statusstufe',
tom@1230:                     store: Ext.data.StoreManager.get('statusstufe'),
tom@1230:                     displayField: 'stufe',
tom@1230:                     valueField: 'id',
tom@1230:                     allowBlank: false,
tom@1230:                     editable: false,
tom@1230:                     forceSelection: true,
tom@1230:                     width: 300,
tom@1230:                     labelWidth: 100,
tom@1230:                     fieldLabel: i18n.getMsg('statusgrid.header.statusStufe')
tom@1230:                 }, {
raimund@1082:                     xtype: 'textarea',
raimund@1082:                     width: 300,
raimund@1082:                     height: 100,
raimund@1082:                     labelWidth: 100,
raimund@1082:                     fieldLabel: i18n.getMsg('statusgrid.header.text'),
raimund@1082:                     emptyText: 'Geben Sie einen Kommentar ein.'
raimund@1082:                 }]
tom@1228:             }],
tom@1228:             buttons: [{
tom@1228:                 text: i18n.getMsg('statusSetzen'),
tom@1228:                 name: 'start',
tom@1228:                 icon: 'resources/img/mail-mark-notjunk.png',
tom@1228:                 formBind: true,
tom@1228:                 disabled: true,
tom@1228:                 handler: this.setStatus
tom@1228:             }, {
tom@1228:                 text: i18n.getMsg('cancel'),
tom@1228:                 name: 'abort',
tom@1228:                 handler: this.closeWindow
raimund@1082:             }]
raimund@1082:         }, {
raimund@1082:             xtype: 'panel',
raimund@1082:             hidden: true,
raimund@1082:             margin: '5, 5, 5, 5',
raimund@1082:             overflow: 'auto',
raimund@1082:             name: 'result'
raimund@1082:         }, {
raimund@1082:             xtype: 'progressbar',
raimund@1082:             margin: '5, 5, 5, 5',
raimund@1082:             hidden: true,
raimund@1082:             text: 'Verarbeite Statusänderungen'
raimund@1082:         }];
raimund@1082:         this.buttons = [{
raimund@1082:             text: i18n.getMsg('close'),
raimund@1082:             name: 'close',
raimund@1082:             hidden: true,
raimund@1082:             handler: this.closeWindow
raimund@1082:         }];
raimund@1082: 
raimund@1082:         this.callParent(arguments);
tom@1228: 
tom@1228:         // Initially validate to indicate mandatory fields clearly.
tom@1228:         this.down('form').isValid();
raimund@1082:     },
raimund@1082: 
raimund@1082:     /**
raimund@1082:      * @private
raimund@1082:      * A handler for a Abort-Button
raimund@1082:      */
raimund@1082:     closeWindow: function(button) {
raimund@1082:         var win = button.up('window');
raimund@1082:         win.close();
raimund@1082:     },
raimund@1082: 
raimund@1082:     /**
raimund@1082:      * @private
raimund@1082:      * A handler to setStatus on Bulk.
raimund@1082:      */
raimund@1082:     setStatus: function(button) {
raimund@1082:         var win = button.up('window');
raimund@1082:         win.down('panel').disable();
raimund@1082:         win.down('button[name=start]').disable();
raimund@1082:         win.down('button[name=abort]').disable();
raimund@1082:         var progress = win.down('progressbar');
raimund@1082:         progress.show();
raimund@1082:         win.send();
raimund@1082:     },
raimund@1082: 
raimund@1082:     send: function() {
raimund@1082:         var i18n = Lada.getApplication().bundle;
raimund@1082:         var me = this;
raimund@1082:         var progress = me.down('progressbar');
raimund@1082:         var progressText = progress.getText();
raimund@1082:         var count = 0;
tom@1230: 
tom@1230:         var wert = me.down('statuswert').getValue();
tom@1230:         var stufe = me.down('[name=statusstufe]').getValue();
tom@1230:         var kombis = Ext.data.StoreManager.get('statuskombi');
tom@1230:         var kombiIdx = kombis.findBy(function(record) {
tom@1230:             return record.get('statusStufe').id === stufe
tom@1230:                 && record.get('statusWert').id === wert;
tom@1230:         });
tom@1230:         if (kombiIdx < 0) {
tom@1230:             Ext.Msg.alert(i18n.getMsg('err.msg.generic.title'),
tom@1230:                 'Unerlaubte Kombination aus Status und Stufe');
tom@1230:             me.down('button[name=close]').show();
tom@1230:             return;
tom@1230:         }
tom@1230: 
raimund@1082:         for (var i = 0; i < this.selection.length; i++) {
raimund@1082:             var data = Ext.create('Lada.model.Status', {
raimund@1082:                 messungsId: this.selection[i].get('id'),
tom@1230:                 mstId: this.down('combobox').getValue(),
raimund@1082:                 datum: new Date(),
tom@1230:                 statusKombi: kombis.getAt(kombiIdx).get('id'),
raimund@1082:                 text: this.down('textarea').getValue()
raimund@1082:             });
raimund@1082:             Ext.Ajax.request({
raimund@1082:                 url: 'lada-server/rest/status',
raimund@1082:                 method: 'POST',
raimund@1082:                 jsonData: data.raw,
raimund@1082:                 success: function(response) {
raimund@1082:                     var json = Ext.JSON.decode(response.responseText);
raimund@1082:                     me.resultMessage += '<strong>' + i18n.getMsg('messung') + ': ';
raimund@1082:                     var sel = me.selection[count];
raimund@1082:                     me.resultMessage += sel.get('hauptprobenNr') + ' - ' + sel.get('nebenprobenNr') + '</strong><br><dd>';
raimund@1099:                     me.resultMessage += i18n.getMsg('status-' + json.message) + '</dd><br>';
raimund@1082:                     count++;
raimund@1082:                     progress.updateProgress(count / me.selection.length, progressText + ' (' + count + ')');
raimund@1082:                     if (count === me.selection.length) {
raimund@1082:                         var result = me.down('panel[name=result]');
raimund@1082:                         var values = me.down('panel[name=valueselection]');
raimund@1082:                         me.down('button[name=start]').hide();
raimund@1082:                         me.down('button[name=abort]').hide();
raimund@1082:                         me.down('button[name=close]').show();
raimund@1082:                         result.setSize(values.getWidth(), values.getHeight());
raimund@1082:                         result.getEl().setHTML(me.resultMessage);
raimund@1082:                         result.show();
raimund@1082:                         values.hide();
raimund@1082:                     }
raimund@1082:                 },
raimund@1082:                 failure: function(response) {
raimund@1082:                     console.log(response);
raimund@1082:                     count++;
raimund@1082:                     progress.updateProgress(count / me.selection.length);
raimund@1082:                     if (count === me.selection.length) {
raimund@1082:                         me.close();
raimund@1082:                     }
raimund@1082:                 }
raimund@1082:             });
raimund@1082:         }
raimund@1082:     }
raimund@1082: });
raimund@1082: