HttpServletResponse response) throws ServletException, IOException {
getServletContext().log(
"-> AttributeListSvr.doDelete()\nParameters= "
+ request.getParameterMap().toString());
// Extract request data
JsonAttributeMove data = null;
try {
data = JsonAttributeMove.decode(request.getInputStream());
}
catch (Exception ex) {
getServletContext().log("Cannot get POSTed administrative command", ex);
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
return;
}
getServletContext().log("Process attribute delete: " + data.getKey1());
// Delete attribute into database
EntityManager em = ModelFactory.getEntityManager();
EntityTransaction tx = null;
try {
tx = em.getTransaction();
tx.begin();
Attribute attribute = em.createQuery(
"select attribute from Attribute attribute where attribute.name = ?1", Attribute.class).setParameter(1, data.getKey1())
.getSingleResult();
// before removing attribute, unlink children
List<Attribute> children = em.createQuery("select attribute from Attribute attribute where attribute.parent.name = ?1", Attribute.class).
setParameter(1, data.getKey1()).getResultList();
for (Attribute child: children) {
child.setParent(attribute.getParent());
}
// delete association with directories
for (Directory dir: attribute.getDirectories()) {
dir.getAttributes().remove(attribute);
}
attribute.getDirectories().clear();
// delete association with images
for (Image img: attribute.getImages()) {
img.getAttributes().remove(attribute);
}
attribute.getImages().clear();
// delete attribute
em.remove(attribute);
tx.commit();
} catch (Exception ex) {
getServletContext().log("Cannot delete unknown attribute: " + data.getKey1(), ex);
tx.rollback();
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
finally {
em.close();