EntityTransaction tx = null;
try {
tx = em.getTransaction();
tx.begin();
Image img = null;
Directory directory = null;
// search if directory exists, or create it
try {
TypedQuery<Directory> query = em.createQuery("select directory from Directory directory where directory.path = ?1",
Directory.class).setParameter(1, dir);
directory = query.getSingleResult();
if (!directory.isIndexingAllowedBoolean()) {
getServletContext().log("Cannot record image data - indexing not allowed");
return;
}
} catch (NoResultException ex) {
getServletContext().log("No directory data for: " + dir, ex);
directory = ModelFactory.newDirectory(path, null, null, em, tx);
em.persist(directory);
}
// RBAC
AccessController.checkAuthorization(directory, request, Action.editImg, em);
// search if image exists or create it
try {
img = em
.createQuery(
"select image from Image image where image.name = ?1 and image.directory.path = ?2",
Image.class).setParameter(1, name)
.setParameter(2, dir).getSingleResult();
// Update image data and persist it
img.setDescription(imgData.getDesc());
img.setRating(imgData.getRating());
if (imgData.getDate() != null) {img.setDate(new java.sql.Date(imgData.getDate().getTime()));}
updateAttributes(img, imgData.getAttributes(), em);
em.merge(img);
} catch (NoResultException ex) {
getServletContext().log(
"No image data for: " + dir + " / " + name, ex);
img = new Image(
name,
imgData.getDesc(),
directory,
imgData.getRating(),
(imgData.getDate() != null ? new java.sql.Date(imgData.getDate().getTime()) : directory.getDate()));
updateAttributes(img, imgData.getAttributes(), em);
em.persist(img);
}
} catch (AuthenticationException ex) {
getServletContext().log("Unauthorized user", ex);