Skip to Content
0
Former Member
Dec 10, 2020 at 12:23 PM

Handling attachments in MDK app

258 Views

Hello all,

I have tried to follow the details provided by Ashish Jain in the link

https://answers.sap.com/questions/13151554/sap-mdk-fetch-attachments-from-backend-through-oda.html

for handling attachments in my MDK offline application for creating notifications. I have some doubts:

I am using CreateEntity and then CreateRelatedMedia(if attachment exists) actions to create my notifications. At first, I am not capturing the file names of the attachments user uploads, but just capture the value for the Attachment control in the Media property. To try, I tried to pass property FileName, but then also it did not help.

Below are my actions code:

CreateEntity action:

{
    "ActionResult": {
        "_Name": "create"
    },
    "OnFailure": "/ZME_CREATENOTIFICATION/Actions/CreateNotificationEntityFailureMessage.action",
    "OnSuccess": "/ZME_CREATENOTIFICATION/Rules/CheckIfAttachmentExists.rule.js",

    "Headers": {
        "OfflineOData.RemoveAfterUpload": "true"
    },

    
"Properties": {
        "Description": "#Page:NotifCreatePage/#Control:DescFormCellSimpleProperty/#Value",
        "Equipment":  "/ZME_CREATENOTIFICATION/Rules/EquipInLPOrSP.rule.js",
        "FuncLoc": "/ZME_CREATENOTIFICATION/Rules/FuncLocInLPOrSP.rule.js",
        "NotifType": "#Page:NotifCreatePage/#Control:NotTypeFormCellListPicker/#SelectedValue",
        "Priority": "#Page:NotifCreatePage/#Control:PriorityFormCellListPicker/#SelectedValue",
        "ShortText": "#Page:NotifCreatePage/#Control:ShortTextFormCellSimpleProperty/#Value"
    },
    
    "ShowActivityIndicator": true,
    "Target": {
        "EntitySet": "PMNotificationSet",
        "Service": "/ZME_CREATENOTIFICATION/Services/ZME_PM_CREATE_NOTIFICATION_SRV.service"
    },
    "_Type": "Action.Type.ODataService.CreateEntity"
}

CreateRelatedMedia action:

{


    "IsOnlineRequest": false,
    "Media": "#Page:NotifCreatePage/#Control:AttachmentNotifFormCell/#Value",
    "OnFailure": "/ZME_CREATENOTIFICATION/Actions/CreateNotificationFailureMessage.action",
    "OnSuccess": "/ZME_CREATENOTIFICATION/Actions/CreateNotificationSuccessMessage.action",
    "Headers": {
        "OfflineOData.RemoveAfterUpload": "false"
    },
    "ParentLink": {
        "Property": "NotifToAttachmentNav",
        "Target": {
            "EntitySet": "PMNotificationSet",
            "ReadLink": "#ActionResults:create/#Property:data/#Property:@odata.readLink"
        }
    },
    "Properties": {
        "Notification": "#ActionResults:create/#Property:data/#Property:Notification",
        "FileName": "AttachPic"


    },
    "Target": {
        "EntitySet": "PMAttachmentSet",
        "Service": "/ZME_CREATENOTIFICATION/Services/ZME_PM_CREATE_NOTIFICATION_SRV.service"
    },
    "_Type": "Action.Type.ODataService.CreateRelatedMedia"
}
 

Now, these notifications, both synced and unsynced to the backed are to be displayed in the Notifications list page. user can edit the non synced notifications and just view the synced ones. My issue is showing the attachments, by clicking on the ObjectCell where I show my notifications.How is the file name to be handled, when the file is uploaded using FormCell.Attachment control? I see that we need the file name in the DownloadOrOpenDocument.js. (const filename = bindingObject.FileName+".jpg";)

let fs = require('tns-core-modules/file-system');
export default function DownloadOrOpenDocument(sectionedTableProxy) {
  const pageProxy = sectionedTableProxy.getPageProxy();
  const bindingObject = pageProxy.getActionBinding();
  const readLink = bindingObject["@odata.readLink"];
  const filename = bindingObject.ProductId+".jpg";
  const serviceName = "/MDKMedia/Services/MDKMedia.service";
  const entitySet = 'Products';
  // first we need to decide if the media exists locally or needs to be downloaded
  return sectionedTableProxy.isMediaLocal(serviceName, entitySet, readLink).then((mediaIsLocal) => {
    const tempDir = fs.knownFolders.temp();
    const attachmentPath = fs.path.join(tempDir.path, filename);
    const fileExists = fs.File.exists(attachmentPath);
    if (mediaIsLocal && fileExists) {
      // the media has been downloaded, we can open it -> the path needs to be provided in the action definition
      // or it should come from binding
      // persist the media data to local sandbox, so we can open it with the document viewer
      if (!bindingObject) {
        return pageProxy.executeAction("/MDKMedia/Actions/ODataDownloadFailure.action");
      }
      if (readLink) {
          pageProxy.setActionBinding({
            'FileName': attachmentPath
          });
          return pageProxy.executeAction("/MDKMedia/Actions/OpenRelatedDocument.action")
      }
    } else if (mediaIsLocal) {
      //  The media is on the offline store but the file hasn't been saved
      if (pageProxy.getClientData()[readLink]) {
        // we have the stream in client data, save and open
        const attachmentFile = fs.File.fromPath(attachmentPath);
        attachmentFile.writeSync(pageProxy.getClientData()[readLink], function (err) {
          attachmentFile.remove();
          return pageProxy.executeAction('/MDKMedia/Actions/ODataDownloadFailure.action');
        });
        pageProxy.setActionBinding({
          'FileName': attachmentPath
        });
        return pageProxy.executeAction('/MDKMedia/Actions/OpenRelatedDocument.action');
      } 
    } 
    else {
      if (sectionedTableProxy.downloadInProgressForReadLink(readLink)) {
        return Promise.resolve();
      } else {
        return sectionedTableProxy.executeAction("/MDKMedia/Actions/DownloadDocumentStreams.action").catch(error => {
        });
      }
    }
  });
}

Please help.

Thanks

Aanchal