/** {@inheritDoc} */
  public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
      throws IOException {

    ThreadCategory.setPrefix(MapsConstants.LOG4J_CATEGORY);
    log = ThreadCategory.getInstance(this.getClass());
    String action = request.getParameter("action");
    String elems = request.getParameter("elems");
    log.debug("Adding Nodes action:" + action + ", elems=" + elems);

    BufferedWriter bw =
        new BufferedWriter(new OutputStreamWriter(response.getOutputStream(), "UTF-8"));
    try {
      Integer[] nodeids = null;

      boolean actionfound = false;

      if (action.equals(MapsConstants.ADDNODES_ACTION)) {
        log.debug("Adding nodes by id: " + elems);
        actionfound = true;
        String[] snodeids = elems.split(",");
        nodeids = new Integer[snodeids.length];
        for (int i = 0; i < snodeids.length; i++) {
          nodeids[i] = new Integer(snodeids[i]);
        }
      }

      if (action.equals(MapsConstants.ADDNODES_BY_CATEGORY_ACTION)) {
        log.debug("Adding nodes by category: " + elems);
        actionfound = true;
        String categoryName = elems;
        CategoryFactory.init();
        CatFactory cf = CategoryFactory.getInstance();
        cf.getReadLock().lock();
        try {
          final String rule = cf.getEffectiveRule(categoryName);
          final List<InetAddress> nodeIPs = FilterDaoFactory.getInstance().getIPAddressList(rule);
          LogUtils.debugf(this, "ips found: %s", nodeIPs.toString());
          nodeids = new Integer[nodeIPs.size()];
          for (int i = 0; i < nodeIPs.size(); i++) {
            final InetAddress nodeIp = nodeIPs.get(i);
            final List<Integer> ids =
                NetworkElementFactory.getInstance(getServletContext())
                    .getNodeIdsWithIpLike(InetAddressUtils.str(nodeIp));
            LogUtils.debugf(this, "Ids by ipaddress %s: %s", nodeIp, ids.toString());
            nodeids[i] = ids.get(0);
          }
        } finally {
          cf.getReadLock().unlock();
        }
      }

      if (action.equals(MapsConstants.ADDNODES_BY_LABEL_ACTION)) {
        log.debug("Adding nodes by label: " + elems);
        actionfound = true;
        List<OnmsNode> nodes = NetworkElementFactory.getInstance(getServletContext()).getAllNodes();
        nodeids = new Integer[nodes.size()];
        for (int i = 0; i < nodes.size(); i++) {
          nodeids[i] = nodes.get(i).getId();
        }
      }

      if (action.equals(MapsConstants.ADDRANGE_ACTION)) {
        log.debug("Adding nodes by range: " + elems);
        actionfound = true;
        nodeids =
            (Integer[])
                NetworkElementFactory.getInstance(getServletContext())
                    .getNodeIdsWithIpLike(elems)
                    .toArray(new Integer[0]);
      }

      if (action.equals(MapsConstants.ADDNODES_NEIG_ACTION)) {
        log.debug("Adding nodes neighbor of:" + elems);
        actionfound = true;
        nodeids =
            (Integer[])
                NetworkElementFactory.getInstance(getServletContext())
                    .getLinkedNodeIdOnNode(WebSecurityUtils.safeParseInt(elems))
                    .toArray(new Integer[0]);
      }

      if (action.equals(MapsConstants.ADDNODES_WITH_NEIG_ACTION)) {
        log.debug("Adding nodes with neighbor of:" + elems);
        actionfound = true;
        Set<Integer> linkednodeids =
            NetworkElementFactory.getInstance(getServletContext())
                .getLinkedNodeIdOnNode(WebSecurityUtils.safeParseInt(elems));
        linkednodeids.add(new Integer(elems));
        nodeids = linkednodeids.toArray(new Integer[linkednodeids.size()]);
      }

      VMap map = manager.openMap();
      if (log.isDebugEnabled()) log.debug("Got map from manager " + map);

      List<VElement> velems = new ArrayList<VElement>();
      // response for addElement
      if (actionfound) {
        log.debug("Before Checking map contains elems");

        for (int i = 0; i < nodeids.length; i++) {
          int elemId = nodeids[i].intValue();
          if (map.containsElement(elemId, MapsConstants.NODE_TYPE)) {
            log.debug(
                "Action: "
                    + action
                    + " . Map Contains Element: "
                    + elemId
                    + MapsConstants.NODE_TYPE);
            continue;
          }

          velems.add(manager.newElement(map.getId(), elemId, MapsConstants.NODE_TYPE));
        } // end for

        // get links and add elements to map
        map = manager.addElements(map, velems);
        log.debug("After getting/adding links");

        bw.write(ResponseAssembler.getAddElementResponse(null, velems, map.getLinks()));
      }
    } catch (Throwable e) {
      log.error("Error while adding nodes for action: " + action, e);
      bw.write(ResponseAssembler.getMapErrorResponse(action));
    } finally {
      bw.close();
    }

    return null;
  }
  /** {@inheritDoc} */
  @Override
  public void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    String nodeIdString = request.getParameter("node");
    String labelType = request.getParameter("labeltype");
    String userLabel = request.getParameter("userlabel");

    if (nodeIdString == null) {
      throw new MissingParameterException("node", new String[] {"node", "labeltype", "userlabel"});
    }
    if (labelType == null) {
      throw new MissingParameterException(
          "labeltype", new String[] {"node", "labeltype", "userlabel"});
    }
    if (userLabel == null) {
      throw new MissingParameterException(
          "userlabel", new String[] {"node", "labeltype", "userlabel"});
    }

    try {
      final int nodeId = WebSecurityUtils.safeParseInt(nodeIdString);
      final OnmsNode node = NetworkElementFactory.getInstance(getServletContext()).getNode(nodeId);
      NodeLabelJDBCImpl oldLabel = new NodeLabelJDBCImpl(node.getLabel(), node.getLabelSource());
      NodeLabelJDBCImpl newLabel = null;

      if (labelType.equals("auto")) {
        newLabel = NodeLabelJDBCImpl.getInstance().computeLabel(nodeId);
      } else if (labelType.equals("user")) {
        newLabel = new NodeLabelJDBCImpl(userLabel, NodeLabelSource.USER);
      } else {
        throw new ServletException("Unexpected labeltype value: " + labelType);
      }

      final String newNodeLabel = newLabel.getLabel();
      boolean managedByProvisiond = node.getForeignSource() != null && node.getForeignId() != null;
      if (managedByProvisiond) {
        WebApplicationContext beanFactory =
            WebApplicationContextUtils.getWebApplicationContext(getServletContext());
        final TransactionTemplate transactionTemplate =
            beanFactory.getBean(TransactionTemplate.class);
        final RequisitionAccessService requisitionService =
            beanFactory.getBean(RequisitionAccessService.class);
        transactionTemplate.execute(
            new TransactionCallback<RequisitionNode>() {
              @Override
              public RequisitionNode doInTransaction(TransactionStatus status) {
                MultivaluedMapImpl params = new MultivaluedMapImpl();
                params.putSingle("node-label", newNodeLabel);
                requisitionService.updateNode(node.getForeignSource(), node.getForeignId(), params);
                return requisitionService.getNode(node.getForeignSource(), node.getForeignId());
              }
            });
      }

      this.sendLabelChangeEvent(nodeId, oldLabel, newLabel);

      if (managedByProvisiond) {
        response.sendRedirect(
            Util.calculateUrlBase(
                request,
                "admin/nodelabelProvisioned.jsp?node="
                    + nodeIdString
                    + "&foreignSource="
                    + node.getForeignSource()));
      } else {
        NodeLabelJDBCImpl.getInstance().assignLabel(nodeId, newLabel);
        response.sendRedirect(
            Util.calculateUrlBase(request, "element/node.jsp?node=" + nodeIdString));
      }
    } catch (SQLException e) {
      throw new ServletException("Database exception", e);
    } catch (Throwable e) {
      throw new ServletException("Exception sending node label change event", e);
    }
  }