partkeepr

fork of partkeepr
git clone https://git.e1e0.net/partkeepr.git
Log | Files | Refs | Submodules | README | LICENSE

ServiceCall.js (4631B)


      1 Ext.define('PartKeepr.ServiceCall', {
      2     extend: 'Ext.util.Observable',
      3 
      4     service: null,
      5     call: null,
      6 
      7     sHandler: null,
      8     parameters: {},
      9     loadMessage: null,
     10     anonymous: false,
     11 
     12     constructor: function (service, call)
     13     {
     14         this.setService(service);
     15         this.setCall(call);
     16         this.parameters = {};
     17     },
     18 
     19     /**
     20      * <p>This method activates anonymous mode.</p>
     21      * <p>Anonymous mode defines that the service is called without passing a valid session. Usually, the only anonymous call is to authenticate a user.</p>
     22      */
     23     enableAnonymous: function ()
     24     {
     25         this.anonymous = true;
     26     },
     27     /**
     28      * <p>This method deactivates anonymous mode.</p>
     29      */
     30     disableAnonymous: function ()
     31     {
     32         this.anonymous = false;
     33     },
     34     setService: function (service)
     35     {
     36         this.service = service;
     37     },
     38     setCall: function (call)
     39     {
     40         this.call = call;
     41     },
     42     setParameter: function (parameter, value)
     43     {
     44         this.parameters[parameter] = value;
     45     },
     46     setParameters: function (obj)
     47     {
     48         Ext.apply(this.parameters, obj);
     49     },
     50     setLoadMessage: function (message)
     51     {
     52         this.loadMessage = message;
     53     },
     54     setHandler: function (handler)
     55     {
     56         this.sHandler = handler;
     57     },
     58     doCall: function ()
     59     {
     60         /* Update the status bar to indicate that the call is in progress. */
     61         PartKeepr.getApplication().getStatusbar().startLoad(this.loadMessage);
     62 
     63         this.parameters._format = "json";
     64 
     65         var headers = {
     66             "call": this.call,
     67             "lang": Ext.getLocale()
     68         };
     69 
     70         if (!this.anonymous) {
     71             var provider = PartKeepr.Auth.AuthenticationProvider.getAuthenticationProvider();
     72 
     73             Ext.apply(headers, provider.getHeaders());
     74         }
     75 
     76         Ext.Ajax.request({
     77             url: PartKeepr.getBasePath() + '/' + this.service + "/" + this.call,
     78             success: Ext.bind(this.onSuccess, this),
     79             failure: Ext.bind(this.onError, this),
     80             method: "POST",
     81             jsonData: this.parameters,
     82             headers: headers
     83         });
     84     },
     85     onSuccess: function (responseObj, options)
     86     {
     87         PartKeepr.getApplication().getStatusbar().endLoad();
     88 
     89         try {
     90             var response = Ext.decode(responseObj.responseText);
     91         } catch (ex) {
     92 
     93             PartKeepr.ExceptionWindow.showException(responseObj);
     94             return;
     95         }
     96 
     97 
     98         /* Check the status */
     99         if (response.status == "error") {
    100             this.displayError(response.exception);
    101             PartKeepr.getApplication().getStatusbar().setStatus({
    102                 text: this.getErrorMessage(response.exception),
    103                 iconCls: 'x-status-error',
    104                 clear: {
    105                     useDefaults: true,
    106                     anim: false
    107                 }
    108             });
    109             return;
    110         }
    111 
    112         /* Check the status */
    113         if (response.status == "systemerror") {
    114             this.displaySystemError(response);
    115             PartKeepr.getApplication().getStatusbar().setStatus({
    116                 text: this.getErrorMessage(response),
    117                 iconCls: 'x-status-error',
    118                 clear: {
    119                     useDefaults: true,
    120                     anim: false
    121                 }
    122             });
    123 
    124 
    125             return;
    126         }
    127 
    128 
    129         if (this.sHandler) {
    130             this.sHandler(response);
    131         }
    132     },
    133     onError: function (response, options)
    134     {
    135         var request;
    136 
    137         PartKeepr.ExceptionWindow.showException(response);
    138         PartKeepr.getApplication().getStatusbar().endLoad();
    139     },
    140     displayError: function (obj)
    141     {
    142         Ext.Msg.show({
    143             title: i18n("Error"),
    144             msg: this.getErrorMessage(obj),
    145             buttons: Ext.MessageBox.OK,
    146             icon: Ext.MessageBox.ERROR
    147         });
    148     },
    149     getErrorMessage: function (obj)
    150     {
    151         var errorMsg;
    152 
    153         if (obj.message === "") {
    154             errorMsg = obj.exception;
    155         } else {
    156             errorMsg = obj.message;
    157         }
    158 
    159         return errorMsg;
    160     },
    161     displaySystemError: function (obj)
    162     {
    163         var errorMsg;
    164 
    165         errorMsg = "Error Message: " + obj.message + "<br>";
    166         errorMsg += "Exception:" + obj.exception + "<br>";
    167         errorMsg += "Backtrace:<br>" + str_replace("\n", "<br>", obj.backtrace);
    168 
    169         Ext.Msg.maxWidth = 800;
    170 
    171         Ext.Msg.show({
    172             title: i18n("System Error"),
    173             msg: errorMsg,
    174             buttons: Ext.MessageBox.OK,
    175             icon: Ext.MessageBox.ERROR
    176 
    177         });
    178     }
    179 
    180 });