0011666: Some fixes for Expressodriver
[tine20] / tine20 / Expressodriver / js / ExceptionHandler.js
1 /*
2  * Tine 2.0
3  *
4  * @package     Expressodriver
5  * @license     http://www.gnu.org/licenses/agpl.html AGPL Version 3
6  * @copyright   Copyright (c) 2007-2014 Metaways Infosystems GmbH (http://www.metaways.de)
7  * @copyright   Copyright (c) 2014 Serpro (http://www.serpro.gov.br)
8  * @author      Marcelo Teixeira <marcelo.teixeira@serpro.gov.br>
9  * @author      Edgar de Lucca <edgar.lucca@serpro.gov.br>
10  *
11  */
12
13 Ext.ns('Tine.Expressodriver');
14
15 /**
16  * generic exception handler for expressodriver
17  *
18  * @namespace Tine.Expressodriver
19  * @param {Tine.Exception} exception
20  * @param {Object} request
21  */
22 Tine.Expressodriver.handleRequestException = function(exception, request) {
23
24     var app = Tine.Tinebase.appMgr.get('Expressodriver'),
25         existingFilenames = [],
26         nonExistantFilenames = [],
27         i,
28         filenameWithoutPath = null;
29
30     switch(exception.code) {
31         // overwrite default 503 handling and add a link to the wiki
32         case 503:
33             Ext.MessageBox.show({
34                 buttons: Ext.Msg.OK,
35                 icon: Ext.MessageBox.WARNING,
36                 title: _('Service Unavailable'),
37                 msg: String.format(app.i18n._('The Expressodriver is not configured correctly. Please refer to the {0}Tine 2.0 Admin FAQ{1} for configuration advice or contact your administrator.'),
38                     '<a href="http://www.tine20.org/wiki/index.php/Admin_FAQ#The_message_.22filesdir_config_value_not_set.22_appears_in_the_logfile_and_I_can.27t_open_the_Expressodriver" target="_blank">',
39                     '</a>')
40             });
41             break;
42
43         case 901:
44             if (request) {
45                 Tine.log.debug('Tine.Expressodriver.handleRequestException - request exception:');
46                 Tine.log.debug(exception);
47
48                 if (exception.existingnodesinfo) {
49                     for (i = 0; i < exception.existingnodesinfo.length; i++) {
50                         existingFilenames.push(exception.existingnodesinfo[i].name);
51                     }
52                 }
53
54                 this.conflictConfirmWin = Tine.widgets.dialog.FileListDialog.openWindow({
55                     modal: true,
56                     allowCancel: false,
57                     height: 180,
58                     width: 300,
59                     title: app.i18n._('Files already exists') + '. ' + app.i18n._('Do you want to replace the following file(s)?'),
60                     text: existingFilenames.join('<br />'),
61                     scope: this,
62                     handler: function(button) {
63                         var params = request.params,
64                             uploadKey = exception.uploadKeyArray;
65                         params.method = request.method;
66                         params.forceOverwrite = true;
67
68                         if (button == 'no') {
69                             if (params.method == 'Expressodriver.moveNodes') {
70                                 // reload grid
71                                 var app = Tine.Tinebase.appMgr.get('Expressodriver');
72                                 app.getMainScreen().getCenterPanel().grid.getStore().reload();
73                                 // do nothing, other nodes has been moved already
74                                 return;
75                             }
76
77                             Tine.log.debug('Tine.Expressodriver.handleRequestException::' + params.method + ' -> only non-existant nodes.');
78
79                             Ext.each(params.filenames, function(filename) {
80                                 filenameWithoutPath = filename.match(/[^\/]*$/);
81                                 if (filenameWithoutPath && existingFilenames.indexOf(filenameWithoutPath[0]) === -1) {
82                                     nonExistantFilenames.push(filename);
83                                 }
84                             });
85
86                             params.filenames = nonExistantFilenames;
87
88                             uploadKey = nonExistantFilenames;
89                         } else {
90                             Tine.log.debug('Tine.Expressodriver.handleRequestException::' + params.method + ' -> replace all existing nodes.');
91                         }
92
93                         if (params.method == 'Expressodriver.copyNodes' || params.method == 'Expressodriver.moveNodes' ) {
94                             Tine.Expressodriver.fileRecordBackend.copyNodes(null, null, null, params);
95                         } else if (params.method == 'Expressodriver.createNodes' ) {
96                             Tine.Expressodriver.fileRecordBackend.createNodes(params, uploadKey, exception.addToGridStore);
97                         }
98                     }
99                 });
100
101             } else {
102                 Ext.Msg.show({
103                   title:   app.i18n._('Failure on create folder'),
104                   msg:     app.i18n._('Item with this name already exists!'),
105                   icon:    Ext.MessageBox.ERROR,
106                   buttons: Ext.Msg.OK
107                });
108             }
109             break;
110         case 902: // Expressodriver_Exception_DestinationIsOwnChild
111             Ext.MessageBox.show({
112                 buttons: Ext.Msg.OK,
113                 icon: Ext.MessageBox.ERROR,
114                 title: app.i18n._(exception.title),
115                 msg: app.i18n._(exception.message)
116             });
117             break;
118         case 903: // Expressodriver_Exception_DestinationIsSameNode
119             Ext.MessageBox.show({
120                 buttons: Ext.Msg.OK,
121                 icon: Ext.MessageBox.INFO,
122                 title: app.i18n._(exception.title),
123                 msg: app.i18n._(exception.message)
124             });
125             break;
126         case 904: // Expressodriver_Exception_CredentialsRequired
127             var passwordDialog = new Tine.Expressodriver.CredentialsDialog();
128             passwordDialog.adapterName = exception.adaptername;
129             passwordDialog.show();
130             break;
131         default:
132             Tine.Tinebase.ExceptionHandler.handleRequestException(exception);
133             break;
134     }
135 };