cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

The community will undergo maintenance on October 16th at 10:00 PM PDT and will be unavailable for up to one hour.

Updating Existing File Attachments During the Attachment Process in JAVADOC EPMDocument Applications

ozclkonur
12-Amethyst

Updating Existing File Attachments During the Attachment Process in JAVADOC EPMDocument Applications

This code performs the file attachment process, but I’m encountering an issue: when I attempt to attach the same file again, it gives a warning saying the file already exists or has been previously attached. In this scenario, the file should be updated if it already exists. I've made some modifications to handle this, but they didn't work. Could you help me with this? Should I delete the existing file first, or is it possible to directly update it?


package ext.designtech.utils;

import java.beans.PropertyVetoException;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

import wt.content.ApplicationData;
import wt.content.ContentHolder;
import wt.content.ContentRoleType;
import wt.content.ContentServerHelper;
import wt.epm.EPMDocument;
import wt.fc.Persistable;
import wt.fc.PersistenceHelper;
import wt.fc.WTObject;
import wt.method.RemoteAccess;
import wt.method.RemoteMethodServer;
import wt.pom.Transaction;
import wt.util.WTException;
import wt.util.WTPropertyVetoException;

public class UploadUtil implements RemoteAccess {

    public static void main(String[] args)
            throws WTException, PropertyVetoException, IOException, InvocationTargetException {

        RemoteMethodServer myServer = RemoteMethodServer.getDefault();
        myServer.setUserName("***");
        myServer.setPassword("***********");

        // Generic kullan?m? düzelttik
        Class[] paramTypes = {};
        Object[] paramValues = {};
        myServer.invoke("upload", UploadUtil.class.getName(), null, paramTypes, paramValues);
    }

    public static void upload() throws WTException, PropertyVetoException, IOException {

        Transaction tx = new Transaction();
        try {

            wt.fc.ObjectIdentifier oid = wt.fc.ObjectIdentifier.newObjectIdentifier("wt.epm.EPMDocument:********");

            // Cast i?lemi düzgün ?ekilde yap?ld?
            EPMDocument doc = (EPMDocument) PersistenceHelper.manager.refresh(oid);
            System.out.println(doc);

            ContentHolder ch = (ContentHolder) doc;
            ApplicationData theContent = ApplicationData.newApplicationData(ch);
            String filename = "vns-042754_2ad.pdf";
            File theFile = new File("C:\\Users\\PLM-1\\Desktop\\vns-042754_2ad.pdf");


            if(theContent == null){
               
            theContent.setFileName(filename);
            String path = "C:\\Users\\PLM-1\\Desktop\\vns-042754_2ad.pdf";
            theContent.setUploadedFromPath(path);
            theContent.setRole(ContentRoleType.SECONDARY); // Enum tipi olarak düzeltildi
            System.out.println("---Role Set---");
            theContent.setFileSize(theFile.length());
            }else{
                theContent.setFileName(filename);
                theContent.setUploadedFromPath("C:\\Users\\PLM-1\\Desktop\\vns-042754_2ad.pdf");
                theContent.setFileSize(theFile.length());
            }

            FileInputStream fis = new FileInputStream(theFile);
            System.out.println("--Upload Begin--");

            tx.start();
            theContent = ContentServerHelper.service.updateContent(ch, theContent, fis);
            tx.commit();

            ContentServerHelper.service.updateHolderFormat(doc);
            doc = (EPMDocument) PersistenceHelper.manager.refresh((Persistable) doc, true, true);
            System.out.println("---Attachment attached---");

            fis.close();
        } catch (WTPropertyVetoException e) {
            e.printStackTrace();
            tx.rollback(); // Transaction ba?ar?s?z oldu?unda geri al
        } catch (Exception e) {
            e.printStackTrace();
            tx.rollback(); // Genel hatalar için geri al
        }
    }

}
2 REPLIES 2
TDT
13-Aquamarine
13-Aquamarine
(To:ozclkonur)

Hi @ozclkonur,

 

Write a logic to get all the existing attachments in the list.

Compare the file name what you want to upload with existing list of files.

If any of the file name is matched, delete the file.

Then use the API to upload your file.

ozclkonur
12-Amethyst
(To:TDT)

I understand, but none of the changes I make within the upload function are taking effect. I change the ID, but it still tries to send the file using the first ID I initially used. I'm not sure if it's an issue with Eclipse, but I'm facing this problem. I've changed the ID 8 times to perform a different operation, yet it still triggers the first ID and attempts to send the initial data.
 
It keeps showing the following output:
INFO : wt.system.out plm-1 - wt.epm.EPMDocument:114592789
INFO : wt.system.out plm-1 - ---Role Set---
INFO : wt.system.out plm-1 - --Upload Begin--
INFO : wt.system.err plm-1 - (wt.epm.util.EPMResource/266) wt.util.WTException: Content with file name "vns-042754_2ad.pdf" already exists. Cannot add another content with same name.
 
As I mentioned, I'm changing the ID, but this issue persists.
Announcements


Top Tags