raimund@594: /* Copyright (C) 2013 by Bundesamt fuer Strahlenschutz
raimund@594:  * Software engineering by Intevation GmbH
raimund@594:  *
raimund@594:  * This file is Free Software under the GNU GPL (v>=3)
raimund@594:  * and comes with ABSOLUTELY NO WARRANTY! Check out
raimund@594:  * the documentation coming with IMIS-Labordaten-Application for details.
raimund@594:  */
raimund@594: 
dustin@742: /**
dustin@742:  * This is a controller for a grid of Status
dustin@742:  */
raimund@594: Ext.define('Lada.controller.grid.Status', {
raimund@594:     extend: 'Ext.app.Controller',
raimund@594: 
dustin@742:     /**
dustin@742:      * Initialize the Controller with
dustin@742:      * 3 Listeners
dustin@742:      */
dustin@742:      init: function() {
raimund@594:         this.control({
raimund@594:             'statusgrid': {
raimund@637:                 edit: this.gridSave,
mstanko@1280:                 canceledit: this.cancelEdit
raimund@594:             },
raimund@594:             'statusgrid button[action=add]': {
raimund@594:                 click: this.add
dustin@994:             },
dustin@994:             'statusgrid button[action=reset]': {
dustin@994:                 click: this.reset
raimund@594:             }
raimund@594:         });
raimund@594:     },
raimund@594: 
dustin@742:     /**
dustin@742:      * This function is called when the grids roweditor saves
dustin@742:      * the record.
dustin@742:      * On success it refreshes the windows which contains the grid
dustin@974:      *   it also tries to refresh the ProbeWindow and the messunggrid
dustin@742:      * On failure it displays a message
dustin@742:      */
dustin@742:      gridSave: function(editor, context) {
tom@1217:         context.record.set('datum', new Date());
tom@1217:         var wert = editor.getEditor().down('combobox[displayField=wert]').value;
tom@1217:         var stufe = editor.getEditor().down('combobox[displayField=stufe]').value;
tom@1217:         var kombis = Ext.data.StoreManager.get('statuskombi');
tom@1217:         var kombiNdx = kombis.findBy(function(record, id) {
tom@1217:             if (record.raw.statusStufe.id === stufe &&
tom@1217:                 record.raw.statusWert.id === wert
tom@1217:             ) {
tom@1217:                 return true;
tom@1217:             }
tom@1217:         });
tom@1217:         context.record.set('statusKombi', kombis.getAt(kombiNdx).get('id'));
raimund@594:         context.record.save({
dustin@1025:             success: function(response) {
dustin@1025:                 var i18n = Lada.getApplication().bundle;
dustin@1025:                 var json = Ext.JSON.decode(response.responseText);
dustin@1025: 
dustin@1025:                 if(json) {
dustin@1025:                     if (!json.success) {
dustin@1025:                         if(json.message){
dustin@1025:                             Ext.Msg.alert(i18n.getMsg('err.msg.generic.title')
dustin@1025:                                 +' #'+json.message,
dustin@1025:                                 i18n.getMsg(json.message));
dustin@1025:                         } else {
dustin@1025:                             Ext.Msg.alert(i18n.getMsg('err.msg.generic.title'),
dustin@1025:                                 i18n.getMsg('err.msg.generic.body'));
dustin@1025:                         }
dustin@1025:                     } else {
dustin@1025:                         Ext.Msg.alert(i18n.getMsg('err.msg.generic.title'),
dustin@1025:                         i18n.getMsg('err.msg.generic.body'));
dustin@1025:                     }
dustin@1025:                 }
dustin@1025: 
raimund@594:                 context.grid.initData();
dustin@974:                 var win = context.grid.up('window');
dustin@974:                 win.initData();
dustin@974:                 try {
dustin@974:                     win.parentWindow.initData();
dustin@974:                     win.parentWindow.down('messunggrid').store.reload();
dustin@974:                 }
dustin@974:                 catch(e) {
dustin@974:                 }
raimund@594:             },
dustin@701:             failure: function(request, response) {
dustin@998:                 var i18n = Lada.getApplication().bundle;
dustin@701:                 var json = response.request.scope.reader.jsonData;
dustin@701:                 if (json) {
dustin@701:                     if (json.message){
dustin@998:                         Ext.Msg.alert(i18n.getMsg('err.msg.save.title')
dustin@701:                             +' #'+json.message,
dustin@998:                             i18n.getMsg(json.message));
dustin@701:                     } else {
dustin@998:                          Ext.Msg.alert(i18n.getMsg('err.msg.save.title'),
dustin@998:                             i18n.getMsg('err.msg.generic.body'));
dustin@701:                     }
dustin@701:                 } else {
dustin@998:                     Ext.Msg.alert(i18n.getMsg('err.msg.save.title'),
dustin@998:                         i18n.getMsg('err.msg.response.body'));
dustin@701:                 }
raimund@594:             }
raimund@594:         });
raimund@594:     },
raimund@594: 
dustin@742:     /**
dustin@742:      * When the edit was canceled,
dustin@742:      * the empty row might have been created by the roweditor is removed
dustin@742:      */
dustin@742:      cancelEdit: function(editor, context) {
raimund@637:         if (!context.record.get('id') ||
raimund@637:             context.record.get('id') === '') {
raimund@637:             editor.getCmp().store.remove(context.record);
raimund@637:         }
raimund@637:     },
raimund@637: 
dustin@742:     /**
dustin@742:      * This function adds a new row to add a Status
dustin@990:      *  and copies the data of the previous status into the new one
dustin@990:      *  if possible.
dustin@742:      */
dustin@742:      add: function(button) {
dustin@945:         var lastrow = button.up('statusgrid').store.count()
raimund@594: 
dustin@992:         // retrive current status from the messung.
dustin@992:         var s = button.up('window').down('messungform').getRecord().get('status');
dustin@992:         var recentStatus = button.up('statusgrid').store.getById(s);
dustin@945: 
dustin@1001:         button.up('statusgrid').statusWerteStore.reload();
dustin@1000: 
dustin@990:         //If possible copy the previous record into the new one.
dustin@990:         //this assumes the store is ordered correctly, most recent status last.
dustin@992:         // Do not copy, if current userid differs from the id of the current status
dustin@992:         if (lastrow > 0 &&
dustin@992:                 Ext.Array.contains(Lada.mst, recentStatus.get('erzeuger'))) {
dustin@992:             if (recentStatus) {
dustin@992:                 // clone the status
dustin@992:                 var record = recentStatus.copy()
dustin@992:             }
dustin@992: 
dustin@990:             record.set('id', null);
dustin@1023:             if (record.get('statusWert') === 0) {
dustin@1027:                 record.set('statusWert', '');
dustin@1023:             }
dustin@990:         } else {
dustin@990:             //create a new one
dustin@990:             var record = Ext.create('Lada.model.Status', {
dustin@990:                 messungsId: button.up('statusgrid').recordId
dustin@990:             });
raimund@1044:             if (Ext.data.StoreManager.get('messstellenFiltered').count() === 1) {
raimund@1044:                 record.set('erzeuger', Ext.data.StoreManager.get('messstellenFiltered').getAt(0).get('id'));
raimund@1044:             }
dustin@990:         }
dustin@945: 
dustin@990:         //Set the Date
dustin@990:         record.set('datum', new Date());
dustin@945: 
dustin@990:         button.up('statusgrid').store.insert(lastrow, record);
dustin@990:         button.up('statusgrid').getPlugin('rowedit').startEdit(lastrow, 1);
dustin@994:     },
dustin@994: 
dustin@994:     /**
dustin@994:      * Reset
dustin@994:      * This Function instructs the server to reset the current status
dustin@994:      * WIP / TODO
dustin@994:      *
dustin@994:      **/
dustin@994:     reset: function(button) {
dustin@998:         var me = this;
dustin@998:         var rstbutton = button;
dustin@998:         var i18n = Lada.getApplication().bundle;
dustin@998:         Ext.MessageBox.confirm(
dustin@998:             i18n.getMsg('statusgrid.reset.mbox.title'),
dustin@998:             i18n.getMsg('statusgrid.reset.mbox.text'),
dustin@998:             function(btn) {
dustin@998:                 if (btn === 'yes') {
dustin@998:                     me.doReset(rstbutton);
dustin@998:                 }
dustin@998:             });
dustin@998:      },
dustin@998: 
dustin@998:      doReset: function(button) {
dustin@998:         var i18n = Lada.getApplication().bundle;
dustin@998: 
tom@1217:         var s = button.up('window').down('messungform').getCurrentStatus();
dustin@994:         var messId = button.up('window').down('messungform').getRecord().get('id');
dustin@994: 
tom@1217:         if(!s) {
tom@1217:             Ext.Msg.alert(i18n.getMsg('err.msg.generic.title'),
tom@1217:                 i18n.getMsg('err.msg.generic.body'));
tom@1217:             return;
tom@1217:         }
tom@1225: 
dustin@994:         //Set Status to 'Resetted' (8)
tom@1217:         var kombis = Ext.data.StoreManager.get('statuskombi');
tom@1225:         var stufe = kombis.getById(s.get('statusKombi')).get('statusStufe').id;
tom@1217:         var kombiNdx = kombis.findBy(function(record, id) {
tom@1225:             return record.get('statusStufe').id === stufe
tom@1225:                 && record.get('statusWert').id === 8
tom@1217:         });
tom@1217:         var record = s.copy();
tom@1217:         record.set('datum', new Date());
tom@1217:         record.set('statusKombi', kombis.getAt(kombiNdx).get('id'));
tom@1217:         record.set('id', null);
tom@1217:         record.set('text', i18n.getMsg('statusgrid.resetText'));
dustin@994: 
dustin@994:         Ext.Ajax.request({
raimund@1007:             url: 'lada-server/rest/status',
dustin@994:             jsonData: record.getData(),
dustin@994:             method: 'POST',
dustin@994:             success: function(response) {
dustin@1025:                 var i18n = Lada.getApplication().bundle;
dustin@1025:                 var json = Ext.JSON.decode(response.responseText);
dustin@1025: 
dustin@1025:                 if(json) {
dustin@1025:                     if (!json.success) {
dustin@1025:                         if(json.message){
dustin@1025:                             Ext.Msg.alert(i18n.getMsg('err.msg.generic.title')
dustin@1025:                                 +' #'+json.message,
dustin@1025:                                 i18n.getMsg(json.message));
dustin@1025:                         } else {
dustin@1025:                             Ext.Msg.alert(i18n.getMsg('err.msg.generic.title'),
dustin@1025:                                 i18n.getMsg('err.msg.generic.body'));
dustin@1025:                         }
dustin@1025:                     }
dustin@1025:                 }
dustin@1025: 
raimund@1062:                 var win = button.up('window');
raimund@1062:                 win.initData();
dustin@996:                 button.up('grid').initData();
raimund@1062:                 try {
raimund@1062:                     win.parentWindow.initData();
raimund@1062:                     win.parentWindow.down('messunggrid').store.reload();
raimund@1062:                 }
raimund@1062:                 catch(e) {
raimund@1062:                 }
dustin@994:             },
dustin@994:             failure: function(response) {
dustin@994:                 // TODO sophisticated error handling, with understandable Texts
dustin@994:                 var json = Ext.JSON.decode(response.responseText);
dustin@994:                 if (json) {
dustin@994:                     if(json.message){
dustin@998:                         Ext.Msg.alert(i18n.getMsg('err.msg.generic.title')
dustin@994:                             +' #'+json.message,
dustin@998:                             i18n.getMsg(json.message));
dustin@994:                     } else {
dustin@994:                         Ext.Msg.alert(i18n.getMsg('err.msg.generic.title'),
dustin@994:                             i18n.getMsg('err.msg.generic.body'));
dustin@994:                     }
dustin@994:                 } else {
dustin@994:                     Ext.Msg.alert(i18n.getMsg('err.msg.generic.title'),
dustin@994:                     i18n.getMsg('err.msg.generic.body'));
dustin@994:                 }
dustin@994:             }
dustin@994:         });
dustin@998:      }
dustin@994: 
raimund@594: });