Skip to content

Instantly share code, notes, and snippets.

@amosfong
Created September 4, 2012 09:41
Show Gist options
  • Save amosfong/3619230 to your computer and use it in GitHub Desktop.
Save amosfong/3619230 to your computer and use it in GitHub Desktop.
Groovy script to delete all user's comments and message board posts and then deactivates him
import com.liferay.portal.model.User;
import com.liferay.portal.service.UserLocalServiceUtil;
import com.liferay.portlet.messageboards.model.MBMessage;
import com.liferay.portlet.messageboards.model.MBThread;
import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
import com.liferay.portlet.messageboards.service.MBThreadLocalServiceUtil;
import com.liferay.portlet.messageboards.NoSuchMessageException;
import com.liferay.portlet.messageboards.NoSuchThreadException;
// Screen Names of users you want to delete
String[] screenNames = [""];
// Either Print log to screen or to catalina.out
boolean printLogToScreen = true;
// Don't modify anything below this
long[] classNameIds = [10083, 10037, 10076, 11327273];
for (String screenName : screenNames) {
User user = UserLocalServiceUtil.getUserByScreenName(1, screenName);
log("############################", printLogToScreen);
log("Cleaning up User: " + user.getFullName() + " - " + screenName, printLogToScreen);
List<MBMessage> messages = MBMessageLocalServiceUtil.getUserDiscussionMessages(user.getUserId(), classNameIds, -1, -1, -1, null);
for (MBMessage message : messages) {
log("Deleting Discussion: " + message.getClassNameId() + " - " + message.getMessageId(), printLogToScreen);
MBMessageLocalServiceUtil.deleteDiscussionMessage(message.getMessageId());
}
messages = MBMessageLocalServiceUtil.getGroupMessages(14, user.getUserId(), -1, -1, -1);
for (MBMessage message : messages) {
try {
MBThread thread = MBThreadLocalServiceUtil.getThread(message.getThreadId());
// Message is a root message
if (thread.getRootMessageId() == message.getMessageId()) {
log("Deleting Thread: " + message.getSubject(), printLogToScreen);
MBThreadLocalServiceUtil.deleteThread(thread.getThreadId());
continue;
}
MBMessageLocalServiceUtil.deleteMessage(message.getMessageId());
log("Deleting Post: " + message.getSubject(), printLogToScreen);
}
catch (NoSuchMessageException nsme) {
log("Post Already deleted: " + message.getSubject(), printLogToScreen);
}
catch (NoSuchThreadException nste) {
log("Thread Already deleted: " + message.getSubject(), printLogToScreen);
}
}
log("Deactivating User", printLogToScreen);
UserLocalServiceUtil.updateActive(user.getUserId(), false);
}
def log(String log, boolean printLogToScreen) {
if (printLogToScreen) {
out.println(log);
}
else {
System.out.println(log);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment