dustin@758: /* Copyright (C) 2013 by Bundesamt fuer Strahlenschutz
dustin@758:  * Software engineering by Intevation GmbH
dustin@758:  *
dustin@758:  * This file is Free Software under the GNU GPL (v>=3)
dustin@758:  * and comes with ABSOLUTELY NO WARRANTY! Check out
dustin@758:  * the documentation coming with IMIS-Labordaten-Application for details.
dustin@758:  */
dustin@758: 
dustin@758: /**
dustin@758:  * This is a controller for a grid of Messmethode
dustin@758:  */
dustin@758: Ext.define('Lada.controller.grid.Messmethode', {
dustin@758:     extend: 'Ext.app.Controller',
dustin@766:     record: null,
dustin@758: 
dustin@758:     /**
dustin@758:      * Inhitialize the controller
dustin@758:      * It has 3 listeners
dustin@758:      */
dustin@758:     init: function() {
dustin@758:         this.control({
dustin@758:             'messmethodengrid': {
dustin@758:                 edit: this.gridSave,
dustin@764:                 canceledit: this.cancelEdit,
dustin@768:                 select: this.selectRow,
dustin@768:                 deselect: this.deselectRow
dustin@758:             },
dustin@758:             'messmethodengrid button[action=add]': {
dustin@758:                 click: this.add
dustin@758:             },
dustin@758:             'messmethodengrid button[action=delete]': {
dustin@758:                 click: this.remove
dustin@766:             },
dustin@766:             //Nuklidegrid
dustin@766:             'nuklidegrid': {
dustin@766:                 edit: this.gridSaveNuklid,
dustin@766:                 canceledit: this.cancelEdit
dustin@766:             },
dustin@766:             'nuklidegrid button[action=add]': {
dustin@766:                 click: this.addNuklid
dustin@766:             },
dustin@766:             'nuklidegrid button[action=remove]': {
dustin@766:                 click: this.removeNuklid
dustin@758:             }
dustin@758:         });
dustin@758:     },
dustin@758: 
dustin@758:     /**
dustin@758:      * This function is called when the grids roweditor saves
dustin@758:      * the record.
dustin@758:      * On success it refreshes the windows which contains the grid
dustin@758:      * On failure it displays a message
dustin@758:      */
dustin@758:      gridSave: function(editor, context) {
dustin@758:         context.record.save({
dustin@758:             success: function() {
dustin@758:                 context.grid.initData();
dustin@758:                 context.grid.up('window').initData();
dustin@758:             },
dustin@758:             failure: function(request, response) {
dustin@758:                 var json = response.request.scope.reader.jsonData;
dustin@758:                 if (json) {
dustin@758:                     if (json.message){
dustin@758:                         Ext.Msg.alert(Lada.getApplication().bundle.getMsg('err.msg.save.title')
dustin@758:                             +' #'+json.message,
dustin@758:                             Lada.getApplication().bundle.getMsg(json.message));
dustin@758:                     } else {
dustin@758:                          Ext.Msg.alert(Lada.getApplication().bundle.getMsg('err.msg.save.title'),
dustin@758:                             Lada.getApplication().bundle.getMsg('err.msg.generic.body'));
dustin@758:                     }
dustin@758:                 } else {
dustin@758:                     Ext.Msg.alert(Lada.getApplication().bundle.getMsg('err.msg.save.title'),
dustin@758:                         Lada.getApplication().bundle.getMsg('err.msg.response.body'));
dustin@758:                 }
dustin@758:             }
dustin@758:         });
dustin@758:     },
dustin@758: 
dustin@758:     /**
dustin@766:      * This function is called when the Nuklide-grids roweditor saves
dustin@766:      * the record.
dustin@766:      * It adds the nuklid to the messgroessen-array of the messmethode
dustin@766:      * record.
dustin@766:      * On success it refreshes the windows which contains the grid
dustin@766:      * On failure it displays a message
dustin@766:      */
dustin@766:     gridSaveNuklid: function(editor, context) {
dustin@766:         console.log(context);
dustin@767:         var modified = false;
dustin@767:         var id = context.newValues.id;
dustin@767:         var mg = this.record.get('messgroessen');
dustin@767: 
dustin@767:         //Test if this Nuklid already exists.
dustin@767:         if (Array.isArray(id)){
dustin@767:             for (i in id) {
dustin@767:                 //Only insert if value does not exist
dustin@767:                 if (! Ext.Array.contains(mg, id[i])) {
dustin@767:                     modified = true;
dustin@767:                 }
dustin@767:             }
dustin@767:         }
dustin@767:         else {
dustin@767:             if (! Ext.Array.contains(mg, id)) {
dustin@767:                 mg.push(id);
dustin@767:                 modified = true;
dustin@767:             }
dustin@767:         }
dustin@767: 
dustin@767:         if (modified) {
dustin@767:             this.syncArray(context.store);
dustin@767:         }
dustin@767:         else {
dustin@767:             editor.getCmp().store.remove(context.record);
dustin@767:         }
dustin@766:     },
dustin@766: 
dustin@766:     /**
dustin@758:      * When the edit was canceled,
dustin@758:      * the empty row might have been created by the roweditor is removed
dustin@758:      */
dustin@766:     cancelEdit: function(editor, context) {
dustin@758:         if (!context.record.get('id') ||
dustin@758:             context.record.get('id') === '') {
dustin@758:             editor.getCmp().store.remove(context.record);
dustin@758:         }
dustin@758:     },
dustin@758: 
dustin@758:     /**
dustin@764:      * When a row is selected,
dustin@764:      * the nuklid-grid will be updated
dustin@764:      * to display the nuklide which are possible for this
dustin@764:      * Messmethod
dustin@764:      */
dustin@764:     selectRow: function(row, record, index) {
dustin@766:         //save the record to this object. which makes it accessible
dustin@766:         //for nuklideGrid releated functions.
dustin@766:         this.record = record;
dustin@766:         var nuklide = record.get('messgroessen');
dustin@766: 
dustin@764:         var ngrid = row.view.up('window').down('nuklidegrid');
dustin@766: 
dustin@766:         var mmtmessgroessenstore = this.buildNuklideStore(nuklide);
dustin@766:         //Set Store
dustin@766:         ngrid.setData(mmtmessgroessenstore);
dustin@766: 
dustin@766:         //Enable Editing
dustin@766:         ngrid.setReadOnly(false);
dustin@766:     },
dustin@766: 
dustin@766:     /**
dustin@768:      * Clear the nuklideGrid when a MMT Row is deselected
dustin@768:      */
dustin@768:     deselectRow: function(row, record, index){
dustin@768:         var ngrid = row.view.up('window').down('nuklidegrid');
dustin@768:         ngrid.initData();
dustin@768:     },
dustin@768: 
dustin@768:     /**
dustin@766:      * This function syncs the Messmethoden-Messgroessen Array
dustin@766:      * With the Nuklide Store.
dustin@766:      * It simply overwrites the Array
dustin@766:      */
dustin@766:     syncArray: function(store) {
dustin@766:         var mg = new Array();
dustin@766:         console.log('syncarray');
dustin@766:         console.log(store);
dustin@766:         var item;
dustin@766:         for (item in store.data.items){
dustin@766:             mg.push(store.data.items[item].get('id'));
dustin@766:         }
dustin@766: 
dustin@766:         // Ext.Array.contains(mg, id[i])
dustin@766:         this.record.set('messgroessen', mg);
dustin@766:         var me = this;
dustin@766:         this.record.save({
dustin@766:             success: function() {
dustin@766:                 console.log('Success');
dustin@766:                 console.log(me.record.get('messgroessen'));
dustin@766:                 console.log(mg);
dustin@766:             },
dustin@766:             failure: function(request, response) {
dustin@766:                 var json = response.request.scope.reader.jsonData;
dustin@766:                 if (json) {
dustin@766:                     if (json.message){
dustin@766:                         Ext.Msg.alert(Lada.getApplication().bundle.getMsg('err.msg.save.title')
dustin@766:                             +' #'+json.message,
dustin@766:                             Lada.getApplication().bundle.getMsg(json.message));
dustin@766:                     } else {
dustin@766:                         Ext.Msg.alert(Lada.getApplication().bundle.getMsg('err.msg.save.title'),
dustin@766:                             Lada.getApplication().bundle.getMsg('err.msg.generic.body'));
dustin@766:                     }
dustin@766:                 } else {
dustin@766:                     Ext.Msg.alert(Lada.getApplication().bundle.getMsg('err.msg.save.title'),
dustin@766:                         Lada.getApplication().bundle.getMsg('err.msg.response.body'));
dustin@766:                 }
dustin@766:             }
dustin@766:         });
dustin@766: 
dustin@766:     },
dustin@766: 
dustin@766:     /**
dustin@766:      * Return a MessgroessenStore created from nuklide array
dustin@766:      */
dustin@766:     buildNuklideStore: function(nuklide) {
dustin@766:         // Create a fully populated Messgroessen Store
dustin@764:         var store = Ext.data.StoreManager.get('messgroessen');
dustin@764:         if (!store) {
dustin@764:             store = Ext.create('Lada.store.Messgroessen');
dustin@764:         }
dustin@766: 
dustin@766:         //Create an empty Messgroessen Store which will be populated with the
dustin@766:         //Messgroessen defined in the Messmethoden record.
dustin@766:         var mmtmessgroessenstore = Ext.create('Ext.data.Store', {
dustin@786:             model: 'Lada.model.Messgroesse'
dustin@765:         });
dustin@765: 
dustin@766:         // Copy every Record from Messgroessenstore to the empty Store
dustin@766:         // which was defined in the messmethode record
dustin@766:         for (n in nuklide) {
dustin@766:             mmtmessgroessenstore.add(store.getById(nuklide[n]));
dustin@766:         }
dustin@766:         return mmtmessgroessenstore;
dustin@766:     },
dustin@766: 
dustin@766:     /**
dustin@766:      * This function adds a new row in the NuklidGrid
dustin@766:      */
dustin@766:     addNuklid: function(button) {
dustin@766:         var record = Ext.create('Lada.model.Messgroesse');
dustin@766:         button.up('nuklidegrid').store.insert(0, record);
dustin@766:         button.up('nuklidegrid').rowEditing.startEdit(0, 0);
dustin@766:     },
dustin@766: 
dustin@766:     /**
dustin@766:      * A row can be removed from the Nuklidgrid with the remove
dustin@766:      * function. It asks the user for confirmation
dustin@766:      * If the removal was confirmed, it reloads the parent window on success,
dustin@766:      * on failure, an error message is shown.
dustin@766:      */
dustin@766:     removeNuklid: function(button) {
dustin@766:         var grid = button.up('grid');
dustin@766:         var selection = grid.getView().getSelectionModel().getSelection()[0];
dustin@766:         grid.getStore().remove(selection);
dustin@766:         var store = grid.getStore();
dustin@766:         this.syncArray(store);
dustin@764:     },
dustin@764: 
dustin@764:     /**
dustin@758:      * This function adds a new row
dustin@758:      */
dustin@758:     add: function(button) {
dustin@758:         var record = Ext.create('Lada.model.MmtMessprogramm');
dustin@972:         if (!record.get('letzteAenderung')) {
dustin@972:             record.data.letzteAenderung = new Date();
dustin@972:         }
dustin@758:         record.set('messprogrammId', button.up('messmethodengrid').recordId);
dustin@758:         button.up('messmethodengrid').store.insert(0, record);
dustin@758:         button.up('messmethodengrid').rowEditing.startEdit(0, 0);
dustin@758:     },
dustin@758: 
dustin@758:     /**
dustin@758:      * A row can be removed from the grid with the remove
dustin@758:      * function. It asks the user for confirmation
dustin@758:      * If the removal was confirmed, it reloads the parent window on success,
dustin@758:      * on failure, an error message is shown.
dustin@758:      */
dustin@758:     remove: function(button) {
dustin@758:         var grid = button.up('grid');
dustin@758:         //TODO i18n
dustin@758:         var selection = grid.getView().getSelectionModel().getSelection()[0];
dustin@758:         Ext.MessageBox.confirm('Löschen', 'Sind Sie sicher?', function(btn) {
dustin@758:             if (btn === 'yes') {
dustin@758:                 selection.destroy({
dustin@758:                     success: function() {
dustin@758:                         button.up('window').initData();
dustin@758:                     },
dustin@758:                     failure: function(request, response) {
dustin@758:                         var json = response.request.scope.reader.jsonData;
dustin@758:                         if (json) {
dustin@758:                             if (json.message){
dustin@758:                                 Ext.Msg.alert(Lada.getApplication().bundle.getMsg('err.msg.delete.title')
dustin@758:                                     +' #'+json.message,
dustin@758:                                     Lada.getApplication().bundle.getMsg(json.message));
dustin@758:                             } else {
dustin@758:                                 Ext.Msg.alert(Lada.getApplication().bundle.getMsg('err.msg.delete.title'),
dustin@758:                                     Lada.getApplication().bundle.getMsg('err.msg.generic.body'));
dustin@758:                             }
dustin@758:                         } else {
dustin@758:                             Ext.Msg.alert(Lada.getApplication().bundle.getMsg('err.msg.delete.title'),
dustin@758:                                 Lada.getApplication().bundle.getMsg('err.msg.response.body'));
dustin@758:                         }
dustin@758:                     }
dustin@758:                 });
dustin@758:             }
dustin@758:         });
dustin@758:     }
dustin@758: });