@Transactional(readOnly = true)
  @RequestMapping(value = "/persons", method = RequestMethod.POST)
  @ResponseBody
  public JGridRowsResponse<Person> getPersons(HttpServletRequest request, @RequestParam Long ID) {
    PageRequest pageRequest = null;
    if (request.getParameter("nodeid") != null && !request.getParameter("nodeid").isEmpty()) {
      try {
        long nodeid = Integer.parseInt(request.getParameter("nodeid"));
        Person p = personRepository.getOne(nodeid);
        if (p != null) {
          return new JGridRowsResponse<>(p.getSubordinates());
        }
      } catch (NumberFormatException e) {
        // do nohting
      }

      return new JGridRowsResponse<>();
    } else {
      if (request.getParameter("page") != null) {
        int rows = 10;
        int page;
        try {
          page = Integer.parseInt(request.getParameter("page")) - 1;
          if (page < 0) page = 0;
          rows =
              request.getParameter("rows") == null
                  ? 10
                  : Integer.parseInt(request.getParameter("rows"));
          if (request.getParameter("sidx") != null && !request.getParameter("sidx").isEmpty()) {
            String direction = request.getParameter("sord");
            pageRequest =
                new PageRequest(
                    page,
                    rows,
                    "asc".equals(direction) ? Sort.Direction.ASC : Sort.Direction.DESC,
                    request.getParameter("sidx"));
          } else {
            pageRequest = new PageRequest(page, rows);
          }
        } catch (NumberFormatException ex) {
          // do nothing
        }
      } /**/
      if (pageRequest != null) {
        return new JGridRowsResponse<>(counterpartyRepository.getPersonsHead(ID, pageRequest));
      } else {
        return new JGridRowsResponse<>(counterpartyRepository.getPersonsHead(ID));
      }
    }
  }
 @Transactional(readOnly = false)
 @RequestMapping(
     value = "/editperson",
     method = {RequestMethod.POST, RequestMethod.GET})
 public void personsEditor(
     @RequestParam String oper,
     @RequestParam long ID,
     Person person,
     BindingResult result,
     HttpServletRequest request,
     HttpServletResponse response)
     throws IOException {
   if (result.hasErrors()) {
     response.sendError(400, result.toString());
     return;
   }
   switch (oper) {
     case "add":
       {
         Counterparty counterparty = counterpartyRepository.findOne(ID);
         if (counterparty != null) {
           counterparty.addPerson(person);
           String headIDS = request.getParameter("headID_primary_key");
           if (headIDS != null && !headIDS.isEmpty()) {
             try {
               Long headid = Long.getLong(headIDS);
               Person headPerson = personRepository.findOne(headid);
               person.setHead(headPerson);
             } catch (NumberFormatException e) {
               // do nothing
             }
           }
           personRepository.save(person);
           counterpartyRepository.save(counterparty);
           response.setStatus(200);
         } else {
           response.sendError(404, "NO COUNTERPART WITH ID " + ID + " FOUND");
         }
       }
       break;
     case "edit":
       {
         Person prsn = personRepository.findOne(person.getId());
         if (prsn != null && prsn.getVersion() <= person.getVersion()) {
           prsn.setSurname(person.getSurname());
           prsn.setFirstName(person.getFirstName());
           prsn.setMiddleName(person.getMiddleName());
           prsn.setComments(person.getComments());
           prsn.setDate_of_birth(person.getDate_of_birth());
           String headIDS = request.getParameter("headID_primary_key");
           if (headIDS != null && !headIDS.isEmpty()) {
             try {
               Long headid = Long.getLong(headIDS);
               Person headPerson = personRepository.findOne(headid);
               person.setHead(headPerson);
             } catch (NumberFormatException e) {
               // do nothing
             }
           }
           personRepository.save(prsn);
           response.setStatus(200);
         } else {
           response.sendError(404, "NO Agreement WITH ID " + person.getId() + " FOUND");
         }
       }
       break;
     case "del":
       {
         Counterparty counterparty = counterpartyRepository.findOne(ID);
         if (counterparty != null) {
           Person prsn = personRepository.findOne(person.getId());
           if (prsn != null) {
             counterparty.removePerson(prsn);
             personRepository.delete(prsn);
             counterpartyRepository.save(counterparty);
             response.setStatus(200);
           } else {
             response.sendError(404, "NO Agreement WITH ID " + person.getId() + " FOUND");
           }
         } else {
           response.sendError(404, "NO COUNTERPART WITH ID " + ID + " FOUND");
         }
       }
       break;
     default:
       response.sendError(406, "UNKNOWN OPERATION");
   }
 }