@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 = "/edit",
      method = {RequestMethod.POST, RequestMethod.GET})
  public void editor(
      @RequestParam String oper,
      Counterparty counterparty,
      BindingResult result,
      HttpServletResponse response)
      throws IOException {
    if (result.hasErrors()) {
      response.sendError(400, result.toString());
      return;
    }
    switch (oper) {
      case "add":
        counterpartyRepository.save(counterparty);
        response.setStatus(200);
        break;
      case "edit":
        Counterparty cpt = counterpartyRepository.findOne(counterparty.getId());
        if (counterparty != null) {
          if (cpt.getVersion() <= counterparty.getVersion()) {
            cpt.setFullName(counterparty.getFullName());
            cpt.setShortName(counterparty.getShortName());
            counterpartyRepository.save(cpt);
            response.setStatus(200);
          } else {
            response.sendError(400, "ANOTHER TRANSACTION MODIFICATION!");
          }
        } else {
          response.sendError(404, "NO Counterpart WITH ID " + counterparty.getId() + " FOUND");
        }

        break;
      case "del":
        counterpartyRepository.delete(counterparty.getId());
        response.setStatus(200);
        break;
      default:
        response.sendError(406, "UNKNOWN OPERATION");
    }
  }
 @Transactional(readOnly = true)
 @RequestMapping(value = "/showList", method = RequestMethod.GET)
 @ResponseBody
 public Object simpleClientList(
     @RequestParam(required = false) Integer page_num,
     @RequestParam(required = false) Integer per_page,
     @RequestParam(value = "pkey_val[]", required = false) String pkey,
     @RequestParam(value = "q_word[]", required = false) String[] qword) {
   // Sort sort= FormSort.formSortFromSortDescription(orderby);
   Sort sort = new Sort(Sort.Direction.ASC, "shortName");
   PageRequest pager = null;
   if (page_num != null && per_page != null) {
     page_num = page_num < 1 ? 1 : page_num;
     pager = new PageRequest(page_num - 1, per_page, sort);
   }
   if (pager != null) {
     Page<CounterpartProxy> page;
     if (qword != null && qword.length > 0) {
       page = counterpartyRepository.findCounterpart(qword[0], pager);
     } else {
       page = counterpartyRepository.findCounterpart(pager);
     }
     return new JSComboExpenseResp<>(page);
   } else {
     if (pkey != null && !pkey.isEmpty()) {
       Long key = Long.valueOf(pkey);
       Counterparty ft = null;
       if (key != null) {
         ft = counterpartyRepository.findOne(key);
       }
       return ft;
     } else {
       List<CounterpartProxy> page;
       if (qword != null && qword.length > 0) {
         page = counterpartyRepository.findCounterpart("%" + qword[0] + "%", sort);
       } else {
         page = counterpartyRepository.findCounterpart(sort);
       }
       return new JSComboExpenseResp<>(page);
     }
   }
 }
 @Transactional(readOnly = true)
 @RequestMapping(value = "/listing", method = RequestMethod.POST)
 @ResponseBody
 public JGridRowsResponse<Counterparty> getTable(HttpServletRequest request) {
   PageRequest pageRequest = null;
   if (request.getParameter("page") != null) {
     int rows = 10;
     int page;
     try {
       page = Integer.parseInt(request.getParameter("page")) - 1;
       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
     }
   } /**/
   String filterName = request.getParameter("userName");
   if (pageRequest != null) {
     if (filterName != null && !filterName.isEmpty()) {
       return new JGridRowsResponse<>(
           counterpartyRepository.findByShortNameContains(filterName, pageRequest));
     } else return new JGridRowsResponse<>(counterpartyRepository.findAll(pageRequest));
   } else {
     if (filterName != null && !filterName.isEmpty()) {
       return new JGridRowsResponse<>(counterpartyRepository.findByShortNameContains(filterName));
     } else return new JGridRowsResponse<>(counterpartyRepository.findAll());
   }
 }
 @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");
   }
 }
 @Transactional(readOnly = false)
 @RequestMapping(
     value = "/editagreements",
     method = {RequestMethod.POST, RequestMethod.GET})
 public void agrimentsEditor(
     @RequestParam String oper,
     @RequestParam long ID,
     Agreement agreement,
     BindingResult result,
     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.addAgreement(agreement);
           agreementRepository.save(agreement);
           counterpartyRepository.save(counterparty);
           response.setStatus(200);
         } else {
           response.sendError(404, "NO COUNTERPART WITH ID " + ID + " FOUND");
         }
       }
       break;
     case "edit":
       {
         Agreement agr = agreementRepository.findOne(agreement.getId());
         if (agr != null && agr.getVersion() <= agreement.getVersion()) {
           agr.setNumber(agreement.getNumber());
           agr.setName(agreement.getName());
           agr.setStartDate(agreement.getStartDate());
           agr.setEndDate(agreement.getEndDate());
           agr.setPlanEndDate(agreement.getPlanEndDate());
           agreementRepository.save(agr);
           response.setStatus(200);
         } else {
           response.sendError(404, "NO Agreement WITH ID " + agreement.getId() + " FOUND");
         }
       }
       break;
     case "del":
       {
         Counterparty counterparty = counterpartyRepository.findOne(ID);
         if (counterparty != null) {
           Agreement agr = agreementRepository.findOne(agreement.getId());
           if (agr != null) {
             counterparty.removeAgreement(agr);
             agreementRepository.delete(agr);
             counterpartyRepository.save(counterparty);
             response.setStatus(200);
           } else {
             response.sendError(404, "NO Agreement WITH ID " + agreement.getId() + " FOUND");
           }
         } else {
           response.sendError(404, "NO COUNTERPART WITH ID " + ID + " FOUND");
         }
       }
       break;
     default:
       response.sendError(406, "UNKNOWN OPERATION");
   }
 }