Using JSDO for update to OE Rest Server

Posted by spserna2 on 16-Jan-2015 12:27

Hi all, 

I had previous solved the sending array objects back to OE Server with Submit method, it was done by adding new object to the dataset.

Now I had another issues when trying to update the record with JSDO. The dataset contains two table reference, ttOrders and ttOrdImages

What I trying to do here are:
1. Update an existing ttOrder record with findById and assign
2. Add any new ttOrdImages record.

Below is my JSDO catalogues.

{
                "name": "BEOrders",
                "path": "\/BEOrders",
                "autoSave": false,
                "schema": {
                    "type": "object",
                    "additionalProperties": false,
                    "properties": {"dsOrders": {
                        "type": "object",
                        "additionalProperties": false,
                        "properties": {
                            "ttOrders": {
                                "type": "array",
                                "items": {
                                    "additionalProperties": false,
                                    "properties": {
                                        "_id": {"type": "string"},
                                        "_errorString": {"type": "string"},
                                        "TaskNo": {
                                            "type": "integer",
                                            "ablType": "INTEGER",
                                            "default": 0,
                                            "title": "TaskNo"
                                        },
                                        "LegRRowID": {
                                            "type": "string",
                                            "ablType": "CHARACTER",
                                            "default": "",
                                            "title": "LegRRowID"
                                        },
                                        "OrderRRowID": {
                                            "type": "string",
                                            "ablType": "CHARACTER",
                                            "default": "",
                                            "title": "OrderRRowID"
                                        },
                                        "DelColInd": {
                                            "type": "string",
                                            "ablType": "CHARACTER",
                                            "default": "",
                                            "title": "DelColInd"
                                        },
                                        "LegNo": {
                                            "type": "integer",
                                            "ablType": "INTEGER",
                                            "default": 0,
                                            "title": "LegNo"
                                        },
                                        "DisplaySeq": {
                                            "type": "integer",
                                            "ablType": "INTEGER",
                                            "default": 0,
                                            "title": "DisplaySeq"
                                        },
                                        "OrderSeq": {
                                            "type": "integer",
                                            "ablType": "INTEGER",
                                            "default": 0,
                                            "title": "OrderSeq"
                                        },
                                        "CustomerId": {
                                            "type": "string",
                                            "ablType": "CHARACTER",
                                            "default": "",
                                            "title": "CustomerId"
                                        },
                                        "CustomerName": {
                                            "type": "string",
                                            "ablType": "CHARACTER",
                                            "default": "",
                                            "title": "CustomerName"
                                        },
                                        "OrderNo": {
                                            "type": "string",
                                            "ablType": "CHARACTER",
                                            "default": "",
                                            "title": "OrderNo"
                                        },
                                        "BookingRef": {
                                            "type": "string",
                                            "ablType": "CHARACTER",
                                            "default": "",
                                            "title": "BookingRef"
                                        },
                                        "OrderRef": {
                                            "type": "string",
                                            "ablType": "CHARACTER",
                                            "default": "",
                                            "title": "OrderRef"
                                        },
                                        "DiscInd": {
                                            "type": "string",
                                            "ablType": "CHARACTER",
                                            "default": "",
                                            "title": "DiscInd"
                                        },
                                        "DiscDescription": {
                                            "type": "string",
                                            "ablType": "CHARACTER",
                                            "default": "",
                                            "title": "DiscDescription"
                                        },
                                        "DiscRetPallets": {
                                            "type": "integer",
                                            "ablType": "INTEGER",
                                            "default": 0,
                                            "title": "DiscRetPallets"
                                        },
                                        "DiscRetCases": {
                                            "type": "integer",
                                            "ablType": "INTEGER",
                                            "default": 0,
                                            "title": "DiscRetCases"
                                        },
                                        "NoOfPallets": {
                                            "type": "integer",
                                            "ablType": "INTEGER",
                                            "default": 0,
                                            "title": "NoOfPallets"
                                        }
                                    }
                                }
                            },
                            "ttOrdImages": {
                                "type": "array",
                                "items": {
                                    "additionalProperties": false,
                                    "properties": {
                                        "_id": {"type": "string"},
                                        "_errorString": {"type": "string"},
                                        "OrderRRowID": {
                                            "type": "string",
                                            "ablType": "CHARACTER",
                                            "default": "",
                                            "title": "OrderRRowID"
                                        },
                                        "ImageType": {
                                            "type": "string",
                                            "ablType": "CHARACTER",
                                            "default": "",
                                            "title": "ImageType"
                                        },
                                        "ImageNo": {
                                            "type": "integer",
                                            "ablType": "INTEGER",
                                            "default": 0,
                                            "title": "ImageNo"
                                        },
                                        "PhotoImage": {
                                            "type": "string",
                                            "contentEncoding": "base64",
                                            "ablType": "BLOB",
                                            "default": null,
                                            "title": "PhotoImage"
                                        }
                                    }
                                }
                            }
                        }
                    }}
                },
                "relations": [{
                    "relationName": "ttOrders",
                    "parentName": "ttOrders",
                    "childName": "ttOrdImages",
                    "relationFields": [{
                        "parentFieldName": "OrderRRowID",
                        "childFieldName": "OrderRRowID"
                    }]
                }],
                "operations": [
                    {
                        "path": "?filter={filter}",
                        "useBeforeImage": false,
                        "type": "read",
                        "verb": "get",
                        "params": []
                    },
                    {
                        "name": "SubmitBEOrders",
                        "path": "\/SubmitBEOrders",
                        "useBeforeImage": true,
                        "type": "submit",
                        "verb": "put",
                        "params": [{
                            "name": "dsOrders",
                            "type": "REQUEST_BODY"
                        }]
                    },
                    {
                        "name": "GetttOrders",
                        "path": "\/GetttOrders",
                        "useBeforeImage": false,
                        "type": "invoke",
                        "verb": "put",
                        "params": [{
                            "name": "filter",
                            "type": "REQUEST_BODY"
                        }]
                    },
                    {
                        "path": "",
                        "useBeforeImage": false,
                        "type": "delete",
                        "verb": "delete",
                        "params": [{
                            "name": "dsOrders",
                            "type": "REQUEST_BODY"
                        }]
                    },
                    {
                        "path": "",
                        "useBeforeImage": false,
                        "type": "update",
                        "verb": "put",
                        "params": [{
                            "name": "dsOrders",
                            "type": "REQUEST_BODY"
                        }]
                    },
                    {
                        "path": "",
                        "useBeforeImage": false,
                        "type": "create",
                        "verb": "post",
                        "params": [{
                            "name": "dsOrders",
                            "type": "REQUEST_BODY"
                        }]
                    },
                    {
                        "name": "GetttOrdImages",
                        "path": "\/GetttOrdImages",
                        "useBeforeImage": false,
                        "type": "invoke",
                        "verb": "put",
                        "params": [{
                            "name": "filter",
                            "type": "REQUEST_BODY"
                        }]
                    }
                ]
            }

Similar to previous design, I wrap the Submit code in a Generic JavaScript Implementation as shown below :

$t.SubmitDropOrder = $t.createClass(null, {
    
    init: function(requestOptions) {
        this.__requestOptions = $.extend({}, requestOptions);
    },
    
    process: function(settings) {
        if (this.__requestOptions.echo) {
            settings.success(this.__requestOptions.echo);
        } else {
            console.log('This will start the drop orders submission process');
            
            var rowId = settings.data.dropRowId;
            var itemId = settings.data.dropItemId;
            var orderCount = parseInt(settings.data.orderCount);
            console.log('Recevived setting data: ' + rowId + "_" + itemId + "_" + orderCount);
            
            var discName = 'orderDis';
            var picName = 'orderPic';
            
            var svc_jsdo = MCBDriversService_BEOrders_JSDO.jsdo;
           
            var orderRef = svc_jsdo.ttOrders;
            var imageRef = svc_jsdo.ttOrdImages;
            console.log(svc_jsdo);
            
            var imageCounter = 1;
            
            for (var i = 1; i <= orderCount; i++) {
                
                var disData = JSON.parse(localStorage.getItem(discName + i));
                var pictureData = JSON.parse(localStorage.getItem(picName + i));
                
                console.log(disData._id);
                var orderRecord = orderRef.findById(disData._id);
                
                console.log(orderRecord);
                var ind = disData.DiscInd ? 'Y' : 'N';
                var updateData = {
                    '_id': disData._id,
                    'DiscInd': ind,
                    'DiscDescription': disData.DiscDescription,
                    'DiscRetPallets': disData.DiscRetPallets,
                    'DiscRetCases': disData.DiscRetCases
                };
                
                if(!orderRecord)
                {
                    orderRef.assign(updateData);
                }
                
                if (disData.PhotoImage) {
                    var picData = {
                        'OrderRRowId': orderRecord.OrderRRowId,
                        'ImageType': 'D',
                        'ImageNo': imageCounter++,
                        'PhotoImage': disData.PhotoImage
                    };
                    
                    imageRef.add(picData);
                }
                
                if (pictureData && pictureData.length > 0) {
                    for (var j = 0; j < pictureData.length; j++) {
                        pictureData[0].ImageNo = imageCounter++;
                        imageRef.add(pictureData[0]);
                    }
                }
            }
                        
            /* Before sending the request, save it away so we execute
             * only the function for this DataSource */
            var beforeSaveChangesFn = function(jsdo, request) {
                jsdo.unsubscribe('beforeSaveChanges', beforeSaveChangesFn);
                settings.request = request;
            };
            
            var afterSaveChangesFn = function(jsdo, success, request) {
                /* If not for the same request saved away on the before
                 * saveChanges fn, just return */
                if (request != settings.request) return;
                 
                var cStatus = 'success';
                /* Unsubscribe so this fn doesn't execute for some other
                 * Tiggr.DataSource event */
                jsdo.unsubscribe('afterSaveChanges', afterSaveChangesFn);
                
                if (success || (request.xhr.status >= 200 && request.xhr.status < 300)) {
                    settings.success(request.response);
                } else {
                    var cError = normalizeError(request);
                    
                    settings.error(request.xhr, cError);
                    cStatus = cError;
                }
                settings.complete(request.xhr, cStatus);
            };
            
            svc_jsdo.subscribe('beforeSaveChanges', beforeSaveChangesFn);
            svc_jsdo.subscribe('afterSaveChanges', afterSaveChangesFn);
            
            try {
                svc_jsdo.saveChanges(true);
            } catch (e) {
                console.log(e);
                var eError = e.message;
                settings.error(settings.request, eError, e);
                settings.complete(settings.request, 'error');
            }
            
        }
    }
    
});

The problem is findById() keep returning null records, I have previously read the data on the page level. When I used assign on table reference level , it said that working record is undefined.

I even tried to use ROW and UPDATE service generated by Mobile App Builder on the UI level for ttOrders and they all succeed, iis the JSDO accessing the memory differently?

Is there any initiation code that I need before trying to use assign() method ?

I have tried calling fill() as well, however when I log the entire JSDO object into console, the records property of my table reference is always undefined.

I will be grateful if anyone could point me to the right direction.

Thank you.

Posted by spserna2 on 19-Jan-2015 05:05

Hi all,

I think I know why findById() does not work

In the documentation, documentation.progress.com/.../index.html

Note: The value assigned to _id for any given record object can change with each invocation of the fill( ) or saveChanges( ) methods.

The '_id' value that I stored is different from the id value in JSDO memory, when I called another read method on the master page.

In order to find back the correct record, use find() method and compare against another property that are static (rowId, orderNo).

documentation.progress.com/.../index.html

I have used following code to get back my data

               var findRecordFn = function(jsrecord) {
                    return jsrecord.data.DisplaySeq === disData.DisplaySeq;
                };
                
                console.log(disData.DisplaySeq);
                var orderRecord = orderRef.find(findRecordFn);

Also, the record property in the table reference been undefined is not relevant in this scenario, the data property is what hold the data.

Thank you.

 

All Replies

Posted by spserna2 on 19-Jan-2015 05:05

Hi all,

I think I know why findById() does not work

In the documentation, documentation.progress.com/.../index.html

Note: The value assigned to _id for any given record object can change with each invocation of the fill( ) or saveChanges( ) methods.

The '_id' value that I stored is different from the id value in JSDO memory, when I called another read method on the master page.

In order to find back the correct record, use find() method and compare against another property that are static (rowId, orderNo).

documentation.progress.com/.../index.html

I have used following code to get back my data

               var findRecordFn = function(jsrecord) {
                    return jsrecord.data.DisplaySeq === disData.DisplaySeq;
                };
                
                console.log(disData.DisplaySeq);
                var orderRecord = orderRef.find(findRecordFn);

Also, the record property in the table reference been undefined is not relevant in this scenario, the data property is what hold the data.

Thank you.

 

Posted by Evan Bleicher on 16-Feb-2015 08:14

It is good to hear that the OpenEdge documentation was able to provide you with a resolution to your issue.  Thank you for posting this information to the Community and thereby assisting others who may have a similar issue.

This thread is closed