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
}
}
}