3 Replies Latest reply on Jul 22, 2014 10:20 AM by neeraj_talreja

    Adding attachment in existing document

    neeraj_talreja

      I'm trying to add an attachment to an existing document -

                  File image = new File("animagefile");

                  AttachmentBean attachmentBean = new AttachmentBean();

                  attachmentBean.setContentType("image/jpg");

                  attachmentBean.setName("animagefile.jpg");

                  attachmentBean.setObjectType(102);

                  attachmentBean.setObjectID(1021L);

                  attachmentBean.setProperties(new HashMap<String, String>());

                  DbAttachment attachment = new DbAttachment(attachmentBean);

                  attachmentManager.createAttachment(attachment.getAttachmentContentResource(), attachment.getName(), attachment.getContentType(), attachment.getData(), image);

       

      The code above successfully add the row in the jive attachment table but some how I'm not able to see the attachment added in the document. Apparently the reference of attachment is not added in the jiveattachversion table. What I am missing here ?

       

       

      Thanks,

      Neeraj

        • Re: Adding attachment in existing document
          whoiskevin

          Not sure about the entire context you have here but you should be able to followup with using the attachmentManager.setAttachmentParent method which is the final piece of the puzzle for associating the attachment to an object in Jive (discussion, document etc).

          1 person found this helpful
            • Re: Adding attachment in existing document
              neeraj_talreja

              Thanks Kevin, I have made the code changes by adding attachment parent but I am still getting NPE, Let's see if I can make this working.

                • Re: Adding attachment in existing document
                  neeraj_talreja

                  Okay, so I tested this out and final piece of code which actually works to show attachment on document UI is -

                              AttachmentBean attachmentBean = new AttachmentBean();

                              attachmentBean.setContentType("image/jpg");

                              attachmentBean.setName("animagefile.jpg");

                              attachmentBean.setObjectType(102);

                              // 2. Replaced the ID that exists in local

                              attachmentBean.setObjectID(1001L);

                              attachmentBean.setProperties(new HashMap<String, String>());

                              DbAttachment attachment = new DbAttachment(attachmentBean);

                              JiveObject jiveObject = jiveObjectLoader.getJiveObject(102, 1001L);

                               // 3. Create attachment.

                              attachment = (DbAttachment) attachmentManager.createAttachment(attachment.getAttachmentContentResource(), attachment.getName(), attachment.getContentType(), attachment.getData(), image);

                              //4. I'm casting jiveObject here, you may well try to cast the attachment content resource as Document

                              Document doc = (Document) jiveObject;

                              // 5. Adds version to the attachment

                              doc.createAttachment(attachment);

                              // 6. Finally, save the document.

                              doc.save();