我有一个自定义数据库,我可以从中获取 GUID,然后从 CE 中删除该文档(如果可用)。与 GUID 关联的所有记录都将从表中删除。
此外,该方法是同步的,以使多个线程能够加速该过程。随着记录数量的增加,批量删除肯定需要时间。输出消息仍需整理。
/**
* This method "HARD" deletes the document from CE in <B>BATCH</B>.
* @param filenetDocGUID
* @throws Exception
*/
public static synchronized String batchDeleteDocumentFromCE_DB(List<String> filenetDocGUID, String QUERY) throws Exception {
System.out.println("Start of batchDeleteDocumentFromCE_DB() method");
System.out.println("Input Parameter filenetDocGUID is : " + filenetDocGUID);
long startTime = System.currentTimeMillis();
Document document = null;
UserContext uc = null;
ObjectStore os = null;
Subject subject = null;
VersionSeries vs = null;
UpdatingBatch ub = null;
int deletedRecordsFromDB [] = null ;
List<String> deletedRecordsFromCE = null;
PreparedStatement preparedStatement = null;
Connection dbConnection = null;
String messageBody = "<table border=\"1\" align=\"center\"><tr><td>GUID</td><td>DB Records</td><td>CE Records</td></tr>";
List<String> processedfilenetDocGUID = null;
try {
if (filenetDocGUID != null) {
getCESession(); // This will get CE Session
os = ceSessionData.getObjectStore();
System.out.println("Domain fetched from CESession static reference is : " + ceSessionData.getDomain());
// The RefreshMode parameter indicates that the property cache for this instance is to be
// refreshed with the updated data.
ub = UpdatingBatch.createUpdatingBatchInstance(ceSessionData.getDomain(), RefreshMode.NO_REFRESH);
System.out.println("Batch update object created");
subject = ceSessionData.getSubject();
System.out.println("Subject fetched from CESession static reference.");
uc = UserContext.get();
uc.pushSubject(subject);
dbConnection = getDataBaseConnection();
dbConnection.setAutoCommit(false); // auto commit is set to false so that DB is not committed before deleting the records
preparedStatement = dbConnection.prepareStatement(QUERY);
//QUERY will be for the db from where you need to remove the data
deletedRecordsFromCE = new ArrayList<String>();
if (os != null)
{
processedfilenetDocGUID = new ArrayList<String>();
for(String GUID : filenetDocGUID)
{
try
{
//get document versions from CE
document = Factory.Document.fetchInstance(os, GUID.trim(), null);
vs = document.get_VersionSeries();
vs.delete();
ub.add(vs, null);// add documents to the batch
//prepare statement for deleting records from DB with the GUID
preparedStatement.setString(1, GUID.trim());
preparedStatement.addBatch(); // add records to dB batch for deletion
deletedRecordsFromCE.add("1");
processedfilenetDocGUID.add(GUID);
}
catch(Exception exp)
{
if(exp.getMessage().contains("The requested item was not found.")||exp.getMessage().contains("E_OBJECT_NOT_FOUND"))
{
log.error("The requested item with GUID : "+GUID.trim()+" was not found in CE.");
//If document is not available in CE, then delete all records associated with it from DB
//document not present in CE but available in DB so delete all the DB records referring to this GUID
//prepare statement for deleting records from DB with the GUID
preparedStatement.setString(1, GUID.trim());
preparedStatement.addBatch();
deletedRecordsFromCE.add("The requested item was not found.");
processedfilenetDocGUID.add(GUID);
}
else
{
log.error("Exception occurred while fetching Document Version Series from DB");
}
}
}
deletedRecordsFromDB = preparedStatement.executeBatch();
System.out.println("Batch Records deleted from DB >>>>>>>>>>>>>>>>>>>>");
if(ub.hasPendingExecute())
{
//execute the batch only if there are records in the batch
ub.updateBatch();
System.out.println("Batch Records deleted from CE>>>>>>>>>>>>>>>>>>>>>>>>>>");
}
else
{
System.out.println("No records to be deleted from CE>>>>>>>>>>>>>>>>>>>>>>>>>>");
}
dbConnection.commit();
System.out.println("DB delete records commit successful");
//below will create a html to view the report for deletion
for(int i=0; i<processedfilenetDocGUID.size(); i++)
{
messageBody = messageBody+("<tr><td>"+processedfilenetDocGUID.get(i)+"</td>");
if(deletedRecordsFromCE.get(i)!=null)
{
//only if document was present in CE or document version series was not obtained from CE
messageBody = messageBody+("<td>"+deletedRecordsFromDB[i]+"</td><td>"+deletedRecordsFromCE.get(i)+"</td>");
}
else
{
//if any other exception occurs then neglect the record and proceed with others
messageBody = messageBody+("<td>"+deletedRecordsFromDB[i]+"</td><td>Exception occured while deleting from CE.</td>");
}
messageBody = messageBody+(messageBody + "</tr>");
}
}
}
}
catch (Exception e)
{
System.out.println("Exception in deleteDocumentFromCE() Method of CEManager.class");
dbConnection.rollback(); // rollback from dB in case of issues
log.error(e.getMessage());
throw new Exception("System Error occurred while deleting the document in CE.. "+e.getMessage());
}
finally
{
messageBody = messageBody+("<tr><td colspan=\"3\">Time taken for Batch in msec : "+(System.currentTimeMillis()-startTime)+"</td></tr>");
messageBody = messageBody+("<tr><td colspan=\"3\">Records deleted : "+processedfilenetDocGUID.size()+" from input "+filenetDocGUID.size()+"</td></tr>");
messageBody = messageBody+("</table>");
System.out.println("Message : "+messageBody);
document = null;
vs = null;
ub = null;
deletedRecordsFromCE = null;
closeDBResources(dbConnection, preparedStatement, null);
if (uc != null)
uc.popSubject();
}
System.out.println("End of batchDeleteDocumentFromCE_DB() method.");
return messageBody;
}