getServletContext().log(
"-> ImageDataSvr.doPost()\nParameters= "
+ request.getParameterMap().toString());
// Extract request parameters
String path=null, name=null, dir=null;
JsonImgData imgData = null;
try {
imgData = JsonImgData.decode(request.getInputStream());
path = urlToPath(imgData.getUrl(), request);
name = PathUtil.pathToName(path);
dir = PathUtil.pathToSequenceDir(path);
}
catch (Exception ex) {
getServletContext().log("Cannot get POSTed image parameters", ex);
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
return;
}
// Persist the image description data into database
EntityManager em = ModelFactory.getEntityManager();
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, em);
em.persist(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, em);
em.persist(img);
}
} catch (AuthenticationException ex) {
getServletContext().log("Unauthenticated user" , ex);