Created
June 20, 2014 15:07
-
-
Save cfalzone/29a240334ab235278a85 to your computer and use it in GitHub Desktop.
Deleting content and related file field in dotCMS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.aquent; | |
import java.util.List; | |
import com.dotmarketing.business.APILocator; | |
import com.dotmarketing.business.UserAPI; | |
import com.dotmarketing.db.HibernateUtil; | |
import com.dotmarketing.portlets.contentlet.business.ContentletAPI; | |
import com.dotmarketing.portlets.contentlet.model.Contentlet; | |
import com.dotmarketing.util.Logger; | |
import com.liferay.portal.model.User; | |
public class Test { | |
private ContentletAPI conAPI = APILocator.getContentletAPI(); | |
private UserAPI userAPI = APILocator.getUserAPI(); | |
public void deleteContentletAndFile(String idToDelete) { | |
// First let's get the contentlet | |
String query = "+identifier:"+idToDelete; | |
Contentlet con; | |
User sysUser; | |
try { | |
sysUser = userAPI.getSystemUser(); | |
List<Contentlet> cons = conAPI.search(query, 1, 0, "modDate", sysUser, false); | |
if(cons.size() > 0) { | |
con = cons.get(0); | |
} else { | |
Logger.error(this, "Contentlet with identifier: "+idToDelete+" not found"); | |
return; | |
} | |
} catch(Exception e) { | |
Logger.error(this, "Exception searching for contentlet with identifier: "+idToDelete, e); | |
return; | |
} | |
// Now get the file field's value | |
// I am not 100% sure if this returns an inode or identifier. I think inode. | |
// Or it might be something else entirely, I did not test this | |
String fileInode = con.getStringProperty("fileFieldVarName"); | |
Contentlet fileCon; | |
String fileQuery = "+(inode:"+fileInode+" identifier:"+fileInode+")"; | |
try { | |
List<Contentlet> cons = conAPI.search(fileQuery, 1, 0, "modDate", sysUser, false); | |
if(cons.size() > 0) { | |
fileCon = cons.get(0); | |
} else { | |
Logger.error(this, "File with identifier or inode: "+fileInode+" not found"); | |
return; | |
} | |
} catch(Exception e) { | |
Logger.error(this, "Exception searching for file with inode or identifier: "+fileInode, e); | |
return; | |
} | |
// Now we can delete both | |
try { | |
HibernateUtil.startTransaction(); | |
conAPI.delete(fileCon, sysUser, false); | |
conAPI.delete(con, sysUser, false); | |
HibernateUtil.commitTransaction(); | |
} catch (Exception e) { | |
Logger.error(this, "Exception trying to delete the content with identifier "+idToDelete | |
+" or file with inode or identifier "+fileInode, e); | |
try { | |
HibernateUtil.rollbackTransaction(); | |
} catch (Exception e1) { | |
Logger.error(this, "Exception rolling back transaction", e1); | |
} | |
return; | |
} | |
Logger.info(this, "Content with identifier "+idToDelete+" and file with inode or identifier: " | |
+fileInode+" has been deleted"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment