logger.debug("New comment from {} : {} - {}", new Object[] {userId, title, description});
// TODO - check values
// save comment
ResourceResolver resolver = null;
try {
resolver = factory.getAdministrativeResourceResolver(null);
final Resource reqResource = resolver.getResource(request.getResource().getPath());
final Map<String, Object> properties = new HashMap<String, Object>();
properties.put(ResourceResolver.PROPERTY_RESOURCE_TYPE, SlingshotConstants.RESOURCETYPE_COMMENT);
properties.put(SlingshotConstants.PROPERTY_TITLE, title);
properties.put(SlingshotConstants.PROPERTY_DESCRIPTION, description);
properties.put(SlingshotConstants.PROPERTY_USER, userId);
// we try it five times
PersistenceException exception = null;
Resource newResource = null;
for(int i=0; i<5; i++) {
try {
exception = null;
final String name = ResourceUtil.createUniqueChildName(reqResource, Util.filter(title));
newResource = resolver.create(reqResource, name, properties);
resolver.commit();
break;
} catch ( final PersistenceException pe) {
resolver.revert();
resolver.refresh();
exception = pe;
}
}
if ( exception != null ) {
throw exception;
}
// order node at the top (if jcr based)
final Node newNode = newResource.adaptTo(Node.class);
if ( newNode != null ) {
try {
final Node parent = newNode.getParent();
final Node firstNode = parent.getNodes().nextNode();
if ( !firstNode.getName().equals(newNode.getName()) ) {
parent.orderBefore(newNode.getName(), firstNode.getName());
newNode.getSession().save();
}
} catch ( final RepositoryException re) {
logger.error("Unable to order comment to the top", re);
}
}
} catch ( final LoginException le ) {
throw new ServletException("Unable to login", le);
} finally {
if ( resolver != null ) {
resolver.close();
}
}
// send redirect at the end
final String path = request.getResource().getParent().getPath();