private void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
    String id = null;
    Integer idIndex = 0;
    final AccessLevelManager layerManager =
        new AccessLevelManager(HibernateUtil.getCurrentSession());
    boolean rollback = false;
    while ((id = request.getParameter("ids[" + idIndex + "][id]")) != null) {
      try {
        layerManager.delete(id);
      } catch (ObjectNotFoundException e) {
        response.getWriter().print(NOTFOUND_MESSAGE);
        rollback = true;
        break;
      } catch (RuntimeException e) {
        rollback = true;
        response.getWriter().print(ERROR_MESSAGE);
        break;
      } finally {
        idIndex++;
      }
    }

    if (rollback == true) {
      HibernateUtil.getCurrentSession().getTransaction().rollback();
    } else {
      response.getWriter().print(OK_MESSAGE);
    }

    response.getWriter().flush();
  }
 public AccessLevel retrieveObjectFromRequest(HttpServletRequest request) {
   String name = request.getParameter("name");
   if (name == null || name.equals("")) {
     throw new RuntimeException("No name attribute specified");
   }
   final AccessLevel accessLevel = AccessLevel.makeAccessLevel(name);
   String layerWmsId;
   int layerIndex = 0;
   final LayerManager layerManager = new LayerManager(HibernateUtil.getCurrentSession());
   while ((layerWmsId = request.getParameter("layers[" + layerIndex + "][wmsId]")) != null) {
     if (layerWmsId.equals("")) {
       throw new RuntimeException("");
     }
     try {
       final Layer layer = layerManager.getById(layerWmsId);
       accessLevel.addLayer(Layer.makeLayer(layerWmsId, layerWmsId));
     } catch (HibernateException e) {
       throw new RuntimeException("No layer with the specified id");
     } catch (LayerRepetitionException e) {
       throw new RuntimeException("Repeated layer to add");
     }
     layerIndex += 1;
   }
   return accessLevel;
 }
 private void update(HttpServletRequest request, HttpServletResponse response) throws IOException {
   final AccessLevelManager accessLevelManager =
       new AccessLevelManager(HibernateUtil.getCurrentSession());
   try {
     AccessLevel accessLevel = this.retrieveObjectFromRequest(request);
     accessLevelManager.update(accessLevel);
     response.getWriter().print(OK_MESSAGE);
   } catch (ObjectNotFoundException e) {
     response.getWriter().print(NOTFOUND_MESSAGE);
   } catch (RuntimeException e) {
     response.getWriter().print(ERROR_MESSAGE);
   } finally {
     response.getWriter().flush();
   }
 }
  private void readAll(HttpServletRequest request, HttpServletResponse response)
      throws IOException {
    final AccessLevelManager accessLevelManager =
        new AccessLevelManager(HibernateUtil.getCurrentSession());
    final List<AccessLevel> accessLevels = accessLevelManager.readAll();
    JSONSerializer serializer = new JSONSerializer();
    final StringBuilder jsonLayersBuilder = new StringBuilder();

    serializer
        .rootName("objects")
        .include("layers")
        .exclude("*.class")
        .serialize(accessLevels, jsonLayersBuilder);

    response.setContentType("application/json");
    response.getWriter().println(jsonLayersBuilder.toString());
    response.getWriter().flush();
  }
 private void create(HttpServletRequest request, HttpServletResponse response) throws IOException {
   final String name = request.getParameter("name");
   if (name != null) {
     final AccessLevelManager accessLevelManager =
         new AccessLevelManager(HibernateUtil.getCurrentSession());
     try {
       AccessLevel accessLevel = this.retrieveObjectFromRequest(request);
       accessLevelManager.create(accessLevel);
       response.getWriter().print(OK_MESSAGE);
     } catch (ObjectDuplicateException e) {
       response.getWriter().print(DUPLICATE_MESSAGE);
     } catch (RollbackException e) {
       response.getWriter().print(ERROR_MESSAGE);
     } finally {
       response.getWriter().flush();
     }
   }
 }