Package org.myphotodiary.json

Examples of org.myphotodiary.json.JsonAttributeMove


      HttpServletResponse response) throws ServletException, IOException {
    getServletContext().log(
        "-> AttributeTreeSvr.doPost()\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 move from: " + data.getKey1() + " to " + data.getKey2());

    // Persist the new attribute into database
    EntityManager em = ModelFactory.getEntityManager();
    EntityTransaction tx = null;
    try {
      tx = em.getTransaction();
      tx.begin();
      Attribute attr1 = null;
      TypedQuery<Attribute> query;
      try {
        query = em.createQuery("select attribute from Attribute attribute where attribute.name = ?1", Attribute.class);
        query.setParameter(1, data.getKey1());
        attr1 = query.getSingleResult();
      }
      catch (NoResultException ex) {
        //
      }
     
      query = em.createQuery("select attribute from Attribute attribute where attribute.name = ?1", Attribute.class);
      query.setParameter(1, data.getKey2());
      Attribute attr2 = query.getSingleResult();
     
      // Link child attribute (key2) with its new parent (key1)
      // if parent is null, child is a root element
      attr2.setParent(attr1);
View Full Code Here


      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();
View Full Code Here

TOP

Related Classes of org.myphotodiary.json.JsonAttributeMove

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.