torsten@472: /* Copyright (C) 2013 by Bundesamt fuer Strahlenschutz torsten@472: * Software engineering by Intevation GmbH torsten@472: * torsten@472: * This file is Free Software under the GNU GPL (v>=3) torsten@472: * and comes with ABSOLUTELY NO WARRANTY! Check out raimund@497: * the documentation coming with IMIS-Labordaten-Application for details. torsten@472: */ torsten@472: torsten@293: /** torsten@293: * Controller for Proben torsten@293: */ torsten@379: raimund@500: // TODO: Move these functions into the controller. torsten@465: function numOfErrors(proben) { torsten@465: var errors = 0; torsten@465: for (var key in proben) { torsten@465: if (proben[key].length > 0) { torsten@465: errors = errors + 1; torsten@465: } torsten@465: } torsten@465: return errors; torsten@465: } torsten@465: raimund@500: // TODO: Use tpl to generate the html document. torsten@379: function buildImportReport(filename, msg, errors, warnings) { raimund@497: var out = []; torsten@465: // There is a entry for each imported proben in the errors dict (might be torsten@465: // empty) raimund@497: var numProben = (Object.keys(errors).length > 0); raimund@497: var numErrors = (numOfErrors(errors)); raimund@497: var hasWarnings = (Object.keys(warnings).length > 0); raimund@497: if (msg !== 200) { raimund@497: out.push('Der Import der Datei ' + raimund@497: filename + raimund@497: ' war nicht erfolgreich. Der Importvorgang konnte aufgrund' + raimund@497: 'eines Fehlers im Server nicht beendet werden.'); raimund@497: } raimund@497: else { raimund@497: if (numErrors === numProben) { raimund@497: out.push('Der Import der Datei ' raimund@497: + filename + ' war nicht erfolgreich.'); torsten@397: } raimund@497: else if (numErrors === 0) { raimund@497: out.push('Der Import der Datei ' + filename + ' war erfolgreich.'); raimund@497: } raimund@497: else { raimund@497: out.push('Der Import der Datei ' raimund@497: + filename + ' war nicht oder nur teilweise erfolgreich.'); raimund@497: } raimund@497: out.push(' Bei dem Import sind folgende Fehler und Warnungen ' + raimund@497: 'aufgetreten:'); raimund@497: out.push('
'); raimund@497: var key; raimund@497: var msgs; raimund@497: if (numErrors) { raimund@497: out.push('Fehler:'); raimund@497: out.push('
'); raimund@497: out.push('
    '); raimund@497: for (key in errors) { raimund@497: out.push('
  1. Probe: ' + key); raimund@497: msgs = errors[key]; raimund@497: out.push('
      '); raimund@497: for (var i = msgs.length - 1; i >= 0; i--) { raimund@497: out.push('
    1. ' + msgs[i].key + raimund@497: ' (' + Lada.getApplication().bundle.getMsg( raimund@497: msgs[i].code.toString()) + raimund@497: '): ' + msgs[i].value + '
    2. '); raimund@497: } raimund@497: out.push('
    '); raimund@497: out.push('
  2. '); torsten@463: } raimund@497: out.push('
'); raimund@497: out.push('
'); torsten@397: } raimund@497: if (hasWarnings) { raimund@497: out.push('Warnungen:'); raimund@497: out.push('
'); raimund@497: out.push('
    '); raimund@497: for (key in warnings) { raimund@497: out.push('
  1. ' + key); raimund@497: msgs = warnings[key]; raimund@497: out.push('
      '); raimund@497: for (var i = msgs.length - 1; i >= 0; i--) { raimund@497: out.push('
    1. ' + msgs[i].key + ' (' + Lada.getApplication().bundle.getMsg(msgs[i].code.toString())+'): '+msgs[i].value+'
    2. ') raimund@497: } raimund@497: out.push('
    '); raimund@497: out.push('
  2. '); torsten@463: } raimund@497: out.push('
'); torsten@463: } torsten@397: } raimund@497: return out.join(''); torsten@379: } torsten@379: torsten@1: Ext.define('Lada.controller.Proben', { torsten@293: extend: 'Lada.controller.Base', raimund@491: torsten@4: views: [ torsten@65: 'proben.Edit', torsten@357: 'proben.Create', torsten@357: 'proben.Import' torsten@4: ], raimund@491: torsten@13: stores: [ torsten@122: 'Proben', torsten@135: 'Zusatzwerte', raimund@490: 'KommentareP', torsten@157: 'Orte', torsten@157: 'Messungen' torsten@13: ], raimund@491: torsten@1: init: function() { raimund@491: this.callParent(arguments); torsten@293: }, raimund@491: torsten@293: addListeners: function() { torsten@1: this.control({ torsten@18: 'probenlist': { torsten@293: itemdblclick: this.editItem torsten@18: }, torsten@64: 'probenlist toolbar button[action=add]': { torsten@293: click: this.addItem torsten@64: }, torsten@357: 'probenlist toolbar button[action=import]': { torsten@357: click: this.selectUploadFile torsten@357: }, torsten@381: 'probenlist toolbar button[action=export]': { torsten@381: click: this.downloadFile torsten@381: }, torsten@270: 'probencreate button[action=save]': { torsten@293: click: this.saveItem torsten@270: }, torsten@270: 'probenedit button[action=save]': { torsten@293: click: this.saveItem torsten@270: }, torsten@357: 'probenimport button[action=save]': { torsten@357: click: this.uploadItem torsten@357: }, torsten@70: 'probencreate form': { torsten@70: savesuccess: this.createSuccess, torsten@70: savefailure: this.createFailure torsten@70: }, torsten@70: 'probenedit form': { torsten@70: savesuccess: this.editSuccess, torsten@70: savefailure: this.editFailure torsten@1: } torsten@1: }); torsten@1: }, raimund@491: raimund@497: addItem: function() { raimund@497: Ext.widget('probencreate'); torsten@64: }, raimund@491: torsten@357: /** torsten@357: * Opens a window with a file chooser to select the file to upload torsten@357: * @private torsten@357: */ raimund@497: selectUploadFile: function() { raimund@497: Ext.widget('probenimport'); torsten@357: }, raimund@491: torsten@357: /** Uploads the selected file the the server torsten@357: * @private torsten@357: */ torsten@357: uploadItem: function(button) { torsten@357: var win = button.up('window'); torsten@357: var form = win.down('form'); raimund@491: if (form.isValid()) { torsten@357: form.submit({ rrenkert@376: url: 'server/rest/import/laf', torsten@357: waitMsg: 'Importiere...', torsten@357: // TODO: Handle the response correct. o must must contain the torsten@357: // filename (None) <2013-08-13 16:17> torsten@357: success: function(fp, resp) { torsten@397: var errors = resp.result.data.errors; torsten@397: var warnings = resp.result.data.warnings; torsten@397: var filename = resp.result.data.filename; torsten@397: var message = resp.result.message; raimund@497: var dialogbody = raimund@497: buildImportReport(filename, message, errors, warnings); torsten@397: Ext.Msg.alert('Erfolg', dialogbody); torsten@357: win.close(); torsten@357: }, torsten@357: failure: function(fp, resp) { torsten@382: var errors = resp.result.data.errors; torsten@382: var warnings = resp.result.data.warnings; torsten@382: var filename = resp.result.data.filename; torsten@382: var message = resp.result.message; raimund@497: var dialogbody = raimund@497: buildImportReport(filename, message, errors, warnings); torsten@379: Ext.Msg.alert('Fehler', dialogbody); torsten@357: win.close(); torsten@357: } torsten@357: }); torsten@357: } torsten@357: }, raimund@491: torsten@381: /** torsten@381: * Will download the selected Probe in LAF format in a new window (tab). torsten@381: */ torsten@381: downloadFile: function(button) { torsten@381: var grid = button.up('grid'); rrenkert@400: var selection = grid.getView().getSelectionModel().getSelection(); rrenkert@400: var hiddenItems = []; rrenkert@400: for (var i = 0; i < selection.length; i++) { raimund@491: hiddenItems.push({ raimund@491: xtype: 'hiddenfield', raimund@491: name: 'probeId', raimund@491: value: selection[i].get('probeId') raimund@491: }); rrenkert@400: } rrenkert@400: var hiddenForm = Ext.create('Ext.form.Panel', { rrenkert@400: title: 'hiddenForm', rrenkert@400: standardSubmit: true, rrenkert@400: url: 'server/rest/export/laf', rrenkert@400: timeout: 120, rrenkert@400: height: 0, rrenkert@400: width: 0, rrenkert@400: hidden: true, rrenkert@400: items: hiddenItems rrenkert@400: }); rrenkert@400: hiddenForm.getForm().submit(); torsten@381: }, raimund@491: torsten@293: editItem: function(grid, record) { raimund@490: var id = record.get('id'); torsten@122: // Load Zusatzwerte raimund@496: var pstore = Ext.data.StoreManager.get('staProbenzusaetze'); torsten@135: pstore.load(); torsten@122: var zstore = this.getZusatzwerteStore(); torsten@122: zstore.load({ torsten@122: params: { torsten@139: probeId: id torsten@122: } torsten@122: }); torsten@149: // Load Kommentare raimund@490: var kstore = this.getKommentarePStore(); torsten@144: kstore.load({ torsten@144: params: { torsten@144: probeId: id torsten@144: } torsten@144: }); torsten@149: // Load Orte torsten@149: var ostore = this.getOrteStore(); torsten@149: ostore.load({ torsten@149: params: { torsten@149: probeId: id torsten@149: } torsten@149: }); torsten@157: // Load Orte torsten@157: var mstore = this.getMessungenStore(); torsten@157: mstore.load({ torsten@157: params: { torsten@157: probeId: id torsten@157: } torsten@157: }); raimund@491: var view = Ext.widget('probenedit', { raimund@491: modelId: id raimund@491: }); raimund@490: view.show(); torsten@18: }, raimund@491: torsten@255: createSuccess: function(form, record, response) { torsten@256: // Close Createdialog torsten@69: var win = form.up('window'); torsten@18: win.close(); raimund@448: var store = this.getProbenStore(); raimund@497: // Load or reload the probenstore. raimund@448: if (store.getCount() === 0) { raimund@448: store.load({ raimund@448: scope: this, raimund@497: callback: function() { raimund@448: // Open Editdialog raimund@448: var json = Ext.decode(response.responseText); raimund@448: if (json) { raimund@448: var probeId = json.data.probeId; raimund@497: var probe = store.findRecord('probeId', probeId); raimund@448: this.editItem(null, probe); raimund@448: } raimund@448: } raimund@448: }); rrenkert@405: } raimund@448: else { raimund@448: store.reload({ raimund@448: scope: this, raimund@497: callback: function() { raimund@448: // Open Editdialog raimund@448: var json = Ext.decode(response.responseText); raimund@448: if (json) { raimund@448: var probeId = json.data.probeId; raimund@497: var probe = store.findRecord('probeId', probeId); raimund@448: this.editItem(null, probe); raimund@448: } torsten@256: } raimund@448: }); raimund@448: } torsten@70: }, raimund@491: raimund@497: editSuccess: function(form) { torsten@86: // Reload store torsten@86: var store = this.getProbenStore(); torsten@86: store.reload(); torsten@70: var win = form.up('window'); torsten@70: win.close(); torsten@1: } torsten@1: });