partkeepr

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

HydraProxy.js (7038B)


      1 Ext.define("PartKeepr.data.HydraProxy", {
      2     extend: 'Ext.data.proxy.Rest',
      3     alias: 'proxy.Hydra',
      4 
      5     timeout: 300000,
      6 
      7     reader: {
      8         type: 'hydra'
      9     },
     10     writer: {
     11         type: 'jsonwithassociations',
     12         writeAllFields: true
     13     },
     14     appendId: false,
     15     limitParam: "itemsPerPage",
     16     defaultListenerScope: true,
     17     sortParam: "order",
     18     headers: {},
     19 
     20     /**
     21      * An ID which should be ignored when loading items. Usually we use the item ID as URL as per JSON-LD spec,
     22      * but sometimes you might require loading an item from the url parameter instead.
     23      *
     24      * This is mainly a workaround for ExtJS trees because we need a virtual root node for which the ID cannot be
     25      * changed.
     26      */
     27     ignoreLoadId: null,
     28 
     29     /**
     30      * If true, ignores IDs when updating/deletes entries. This is mostly used for entities where no primary key exists.
     31      */
     32     ignoreIds: false,
     33 
     34     constructor: function (config)
     35     {
     36         config.url = PartKeepr.getBasePath() + config.url;
     37         this.callParent(arguments);
     38     },
     39     listeners: {
     40         exception: function (reader, response)
     41         {
     42             this.showException(response);
     43         }
     44     },
     45     getHeaders: function ()
     46     {
     47         var headers = this.callParent(arguments);
     48 
     49         var provider = PartKeepr.Auth.AuthenticationProvider.getAuthenticationProvider();
     50 
     51         Ext.apply(headers, provider.getHeaders());
     52 
     53         return headers;
     54     },
     55     buildUrl: function (request)
     56     {
     57         var operation = request.getOperation();
     58 
     59         // Set the URI to the ID, as JSON-LD operates on IRIs.
     60         if (request.getAction() == "read") {
     61             if (operation.getId()) {
     62                 if (operation.getId() !== this.ignoreLoadId) {
     63                     request.setUrl(operation.getId());
     64                 }
     65             }
     66         }
     67 
     68         if (request.getAction() == "update") {
     69             if (request.getRecords().length != 1) {
     70                 throw "The amount of records updating must be exactly one";
     71             }
     72 
     73             if (!this.ignoreIds) {
     74                 this.api.update = request.getRecords()[0].getId();
     75             }
     76         }
     77 
     78         if (request.getAction() == "destroy") {
     79             if (request.getRecords().length != 1) {
     80                 throw "The amount of records updating must be exactly one";
     81             }
     82 
     83             if (!this.ignoreIds) {
     84                 this.api.destroy = request.getRecords()[0].getId();
     85             }
     86         }
     87 
     88         return this.callParent([request]);
     89     },
     90     /**
     91      * Calls a specific action on the record.
     92      * @todo Document on how we call actions on entities
     93      *
     94      *
     95      */
     96     callAction: function (record, action, method, parameters, callback, reload)
     97     {
     98         var url;
     99 
    100         if (action !== null) {
    101             url = record.getId() + "/" + action;
    102         } else {
    103             url = record.getId();
    104         }
    105         var request = Ext.create("Ext.data.Request");
    106 
    107         request.setMethod(method);
    108         request.setUrl(url);
    109         if (Ext.isObject(parameters)) {
    110             request.setParams(parameters);
    111         }
    112 
    113         request.setHeaders(this.getHeaders());
    114 
    115         request.setCallback(function (options, success, response)
    116         {
    117             this.processCallActionResponse(options, success, response);
    118 
    119             if (reload) {
    120                 record.load();
    121             }
    122 
    123             if (Ext.isFunction(callback)) {
    124                 callback(options, success, response);
    125             }
    126         }.bind(this));
    127 
    128         this.sendRequest(request);
    129     },
    130     /**
    131      * Encodes the array of {@link Ext.util.Filter} objects into a string to be sent in the request url. By default,
    132      * this simply JSON-encodes the filter data.
    133      *
    134      * Additionally converts any model instances to the ID representation in order to save bytes during a request.
    135      *
    136      * @param {Ext.util.Filter[]} filters The array of {@link Ext.util.Filter Filter} objects
    137      * @return {String} The encoded filters
    138      */
    139     encodeFilters: function (filters)
    140     {
    141         var out = [],
    142             length = filters.length,
    143             i, filter, j;
    144 
    145         for (i = 0; i < length; i++) {
    146             filter = filters[i].serialize();
    147 
    148             if (Object.prototype.toString.call(filter.value) === '[object Array]') {
    149                 for (j = 0; j < filter.value.length; j++) {
    150                     if (filter.value[j].isModel && filter.value[j].isModel === true) {
    151                         filter.value[j] = filter.value[j].getId();
    152                     }
    153                 }
    154             } else {
    155                 if (typeof filter.value === "object" && filter.value !== null) {
    156                     if (filter.value.isModel && filter.value.isModel === true) {
    157                         filter.value = filter.value.getId();
    158                     }
    159                 }
    160             }
    161             out[i] = filter;
    162         }
    163 
    164         return this.applyEncoding(out);
    165     },
    166     /**
    167      * Calls a specific action on the collection
    168      * @todo Document on how we call actions on entities
    169      *
    170      *
    171      */
    172     callCollectionAction: function (action, method, parameters, callback, ignoreException)
    173     {
    174         var url;
    175 
    176         if (action !== null) {
    177             url = this.url + "/" + action;
    178         } else {
    179             url = this.url;
    180         }
    181 
    182         var request = Ext.create("Ext.data.Request");
    183 
    184         request.setMethod(method);
    185         request.setUrl(url);
    186         if (Ext.isObject(parameters)) {
    187             request.setParams(parameters);
    188         }
    189 
    190         request.setHeaders(this.getHeaders());
    191 
    192         request.setCallback(function (options, success, response)
    193         {
    194             this.processCallActionResponse(options, success, response, ignoreException, action);
    195 
    196             if (Ext.isFunction(callback)) {
    197                 callback(options, success, response, request);
    198             }
    199         }.bind(this));
    200 
    201         this.sendRequest(request);
    202     },
    203     processCallActionResponse: function (options, success, response, ignoreException, action)
    204     {
    205         var i;
    206 
    207         if (success === true) {
    208             var actions = PartKeepr.getApplication().getSystemPreference("partkeepr.actions", []);
    209 
    210             for (i = 0; i < actions.length; i++) {
    211                 if (this.getModel().$className === actions[i].baseEntity && action == actions[i].action) {
    212                     PartKeepr.BatchJobBundle.Entity.BatchJob.load(actions[i].batchJob, {
    213                         scope: this,
    214                         success: this.onBatchJobLoaded
    215                     });
    216                 }
    217             }
    218             return;
    219         }
    220 
    221         if (!ignoreException) {
    222             this.showException(response);
    223         }
    224     },
    225     onBatchJobLoaded: function (record) {
    226         var j = Ext.create("PartKeepr.Components.BatchJob.BatchJobExecutionWindow", {
    227             batchJob: record
    228         });
    229         j.show();
    230     },
    231     showException: function (response)
    232     {
    233         PartKeepr.ExceptionWindow.showException(response);
    234     }
    235 });