SPEL: How to upload attachment using SPEL

Document created by cdtj Champion on Feb 2, 2017
Version 1Show Document
  • View in full screen mode

I would like to share method found while developing java app, not sure that someone will need it in real life, but...

 

Calling example

// Attaching txt from local disk to Issue
void run() {
     int newAttmntId;
     newAttmntId = newAttmnt("c:\\1234.txt", "doc_rep:1002")
     if (newAttmntId < 1) {
          switch (val) {
               case 0: printf("Unknown error"); break;
               case 1: printf("Failed to get file name"); break;
               case 2: printf("Failed to get file extension"); break;
               default: printf("I'm to lazy to fill this list"); break;
          }
     } else {
          send_wait(0, top_object(), "call_attr", "api", "insert_object", (uuid)NULL, "lrel_attachments_issues", NULL, 0,
                         "iss", "iss:12345678",
                         "attmnt", newAttmntId
                    );
     }
}

 

Entire code

i'm not using file attaching to make this code searcheable:

int newAttmnt(string filePath, string repPersid) {
     object boplog, rep, attmntObj, gl;
     string fileName, fileExt, escapedPath, attmntFileName, attmntRep, attmntRelFilePath, fileSlice[25], fileExtSlice[25];
     int i, j, uploadSession, fileSize, zipFlag;

     // Create new rep object
     rep = (object)format("@|rep_daemon:%s|top|0|0", getenv("NX_SERVER"));

     // Escape file path
     escapedPath = gsub(filePath, "\\\\", "/");
     printf("escaped path: %s\n", escapedPath);

     // Zipping
     zipFlag = 1;

     // Get file name
     split(fileSlice, escapedPath, "/");
     j = 24;
     while ( (j >= 0) && (strlen(fileSlice[j]) == 0) ) {
          j--;
     }
     if (j > -1)
          fileName = fileSlice[j];
     else
          return -1;
     printf("file : %s\n", fileName);

     // Get file extension
     split(fileExtSlice, fileName, "\\.");
     j = 24;
     while ( (j >= 0) && (strlen(fileExtSlice[j]) == 0) ) {
          j--;
     }
     if (j > -1)
          fileExt = fileExtSlice[j];
     else
          return -2;
     printf("file ext : %s\n", fileExt);

     // Get group leader
     send_wait(0, top_object(), "get_co_group");
     if (msg_error()) {
          for (i=0;i<msg_length();i++) {
               printf("\tERROR[%d]: %s\n", i, (string)msg[i]);
          }
          return -3;
     } else {
          gl = msg[0];
     }

     // Get new dob [attmnt]
     send_wait(0, top_object(), "call_attr", "attmnt", "get_new_dob", NULL, NULL, gl);
     if (msg_error()) {
          return -4;
     } else {
          attmntObj = msg[0];
     }

     // Open upload session, filename format > attmnt.id + "_" + file name
     send_wait(0, rep, "open_upload_session", format("%d_%s", attmntObj.id, fileName), repPersid, 0, 1, zipFlag);
     if (msg_error()) {
          for (i=0;i<msg_length();i++) {
               printf("\tERROR[%d]: %s\n", i, (string)msg[i]);
          }
          return -5;
     } else {
          uploadSession = msg[0];
          attmntFileName = msg[1];
          attmntRelFilePath = msg[2];
          attmntRep = msg[3];          
     }

     // Uploading file to local repository
     send_wait(0, rep, "upload_local_file", uploadSession, escapedPath);
     if (msg_error()) {
          for (i=0;i<msg_length();i++) {
               printf("\tERROR[%d]: %s\n", i, (string)msg[i]);
          }
          return -6;
     } else {
          // msg[0] contains rel_file_path folder
          fileSize = msg[1];
     }

     // Closing upload session
     send_wait(0, rep, "close_upload_session", uploadSession);
     if (msg_error()) {
          for (i=0;i<msg_length();i++) {
               printf("\tERROR[%d]: %s\n", i, (string)msg[i]);
          }
          return -7;
     }
     
     send_wait(0, attmntObj, "call_attr", "attmnt_name", "set_val", fileName);
     send_wait(0, attmntObj, "call_attr", "orig_file_name", "set_val", fileName);
     send_wait(0, attmntObj, "call_attr", "link_only", "set_val", 0);
     send_wait(0, attmntObj, "call_attr", "file_type", "set_val", fileExt);
     send_wait(0, attmntObj, "call_attr", "file_size", "set_val", fileSize);
     send_wait(0, attmntObj, "call_attr", "file_name", "set_val", attmntFileName);
     send_wait(0, attmntObj, "call_attr", "repository", "set_val", attmntRep);
     send_wait(0, attmntObj, "call_attr", "rel_file_path", "set_val", attmntRelFilePath);
     send_wait(0, attmntObj, "call_attr", "zip_flag", "set_val", zipFlag);
     send_wait(0, attmntObj, "call_attr", "status", "set_val", "INSTALLED");
     send_wait(0, gl, "checkin");
     if (msg_error()) {
          for (i=0;i<msg_length();i++) {
               printf("\tERROR[%d]: %s\n", i, (string)msg[i]);
          }
          return -8;
     } else {
          return attmntObj.id;
     }
}
4 people found this helpful

Attachments

    Outcomes