private void checkGetTicketInput(String localID, GetTicketRequestDTO jsonGetTicket)
      throws LocalIDDifferentException, LocalIdMissingException, ApplicationIdMissingException,
          ApplicationlIDDifferentException {
    String currentLogin = SecurityUtils.getCurrentLogin();
    if (!currentLogin.equals(jsonGetTicket.getAppId())) {
      throw new ApplicationlIDDifferentException(currentLogin, jsonGetTicket.getAppId());
    }

    if (!localID.equals(jsonGetTicket.getLocalID()))
      throw new LocalIDDifferentException(jsonGetTicket.getLocalID(), localID);

    if (jsonGetTicket.getLocalID() == null || jsonGetTicket.getLocalID().isEmpty())
      throw new LocalIdMissingException();

    if (jsonGetTicket.getAppId() == null || jsonGetTicket.getAppId().isEmpty())
      throw new ApplicationIdMissingException();
  }
  @RequestMapping(
      value = "/user/{LOCALID}/ticket",
      method = RequestMethod.POST,
      produces = MediaType.APPLICATION_JSON_VALUE)
  @Timed
  public ResponseEntity<GetTicketResponseDTO> getTicket(
      @RequestBody GetTicketRequestDTO jsonGetTicket, @PathVariable("LOCALID") String localID)
      throws ApplicationNotFoundException, LocalIDDifferentException, LocalIdMissingException,
          ApplicationIdMissingException, ApplicationlIDDifferentException,
          OrgIdIsOnlyForPublicAppException, OrgIdIsMissingException {
    log.debug("REST GETTICKET START. localid [{}], appid [{}]", localID, jsonGetTicket.getAppId());

    checkGetTicketInput(localID, jsonGetTicket);

    Application application =
        applicationRepository.findOneByApplicationID(jsonGetTicket.getAppId());
    if (application == null) {
      throw new ApplicationNotFoundException(jsonGetTicket.getAppId());
    }

    Person person = personRepository.findOneByLocalID(localID);
    if (person == null) {
      // create new person
      person = new Person();
      person.setLocalID(localID);
    }
    // update persona name surname mail
    if (jsonGetTicket.getFirstname() != null && !jsonGetTicket.getFirstname().isEmpty())
      person.setFirstName(jsonGetTicket.getFirstname());
    if (jsonGetTicket.getLastname() != null && !jsonGetTicket.getLastname().isEmpty())
      person.setLastName(jsonGetTicket.getLastname());
    if (jsonGetTicket.getMail() != null && !jsonGetTicket.getMail().isEmpty())
      person.setEmail(jsonGetTicket.getMail());
    personRepository.save(person);

    if ((jsonGetTicket.getOrgId() != null)
        && (!jsonGetTicket.getOrgId().isEmpty())
        && (!application.getAllOrg())) {
      // if app isn't for all org must not specify org-id
      throw new OrgIdIsOnlyForPublicAppException(application.getApplicationID());
    } else if (application.getAllOrg()
        && ((jsonGetTicket.getOrgId() == null) || (jsonGetTicket.getOrgId().isEmpty()))) {
      // if app is for all org must specify org-id
      throw new OrgIdIsMissingException(application.getApplicationID());
    }

    // create token
    Token token = new Token();
    token.setApplication(application);
    token.setPerson(person);
    token.setOrgUnit(jsonGetTicket.getOrgId());
    token.setUrlCallback(jsonGetTicket.getUrlCallback());
    token.setDateReleased(DateTime.now());
    tokenRepository.save(token);
    token.setOtt(generateTokenData(token.getId().toString()));
    tokenRepository.save(token);

    // search if person-app have an access-token
    RelPersonApplication relPersonApplication =
        relPersonApplicationRepository.findOneByPersonIsAndApplicationIsAndLastIsTrue(
            person, application);
    String orcid = person.getOrcid();
    String apiKey = null;

    GetTicketResponseDTO response = new GetTicketResponseDTO();
    response.setToken(token.getOtt());
    if ((relPersonApplication != null)
        && ((relPersonApplication.getDenied() == null)
            || (relPersonApplication.getDenied() == false))) {
      apiKey = relPersonApplication.getOauthAccessToken();
      response.setOrcidAccessToken(apiKey);
    }
    response.setOrcid(person.getOrcid());
    if ((orcid != null) && (apiKey != null)) response.setResultCode(ResultCode.SUCCESS.getCode());
    else response.setResultCode(ResultCode.SUCCESS_ALREADY_EXISTS.getCode());

    log.info(
        "REST GETTICKET NEW TICKET.appid [{}], localid [{}], token [{}], resultCode [{}]",
        jsonGetTicket.getAppId(),
        localID,
        token.getOtt(),
        response.getResultCode());
    log.debug(
        "REST GETTICKET END. localid [{}], token [{}], resultCode [{}]",
        localID,
        token.getOtt(),
        response.getResultCode());
    return new ResponseEntity<GetTicketResponseDTO>(response, HttpStatus.OK);
  }