@RequestMapping(value = "/oauth/addApp/{TOKEN}/{APPID}", method = RequestMethod.GET)
  @Timed
  public AddAppResponseDTO addApp(
      @PathVariable("TOKEN") String ott,
      @PathVariable("APPID") String appId,
      HttpServletRequest request)
      throws IOException, JAXBException, TokenNotFoundException, TokenAlreadyUsedException {
    AddAppResponseDTO response = new AddAppResponseDTO();
    log.debug("REST ADD CUSTOM APPS. token [{}]", ott);

    Token token = tokenRepository.findOneByOtt(ott);
    if (token == null) {
      throw new TokenNotFoundException(ott);
    }

    Person person = token.getPerson();
    Application application = token.getApplication();

    Application customApp = applicationRepository.findOne(new Long(appId));

    log.debug(
        "REST ADD CUSTOM APPS. person [{}], localid [{}], customApp [{}]",
        person,
        person.getLocalID(),
        customApp.getId());

    RelPersonApplication relPersonApplication = null;
    relPersonApplication = new RelPersonApplication();
    relPersonApplication.setApplication(customApp);
    relPersonApplication.setPerson(person);
    relPersonApplication.setToken(token);
    relPersonApplication.setValid(null);
    relPersonApplication.setLast(true);
    relPersonApplication.setCustom(true);
    relPersonApplicationRepository.save(relPersonApplication);

    ApplicationMinDTO appMinAdded = ApplicationMapper.from(customApp, true, null);
    response.setApp(appMinAdded);

    return response;
  }
  @RequestMapping(value = "/oauth/newApps/{TOKEN}", method = RequestMethod.GET)
  @Timed
  public ListAddAppResponseDTO oauthNewAppForUser(
      @PathVariable("TOKEN") String ott, HttpServletRequest request)
      throws IOException, JAXBException, TokenNotFoundException, TokenAlreadyUsedException {
    ListAddAppResponseDTO response = new ListAddAppResponseDTO();
    log.debug("REST GET CUSTOM APPS. token [{}]", ott);

    Token token = tokenRepository.findOneByOtt(ott);
    if (token == null) {
      throw new TokenNotFoundException(ott);
    }

    Person person = token.getPerson();
    Application application = token.getApplication();

    log.debug("REST GET CUSTOM APPS. person [{}], localid [{}]", person, person.getLocalID());

    List<RelPersonApplication> listApplicationAuth =
        relPersonApplicationRepository.findAllByTokenIsAndValidIsNull(token);
    List<Long> listIdsToExclude = new ArrayList<Long>();
    for (int i = 0; i < listApplicationAuth.size(); i++) {
      listIdsToExclude.add(listApplicationAuth.get(i).getApplication().getId());
    }

    log.debug(
        "REST GET CUSTOM APPS. person [{}], localid [{}], listIds [{}]",
        person,
        person.getLocalID(),
        listIdsToExclude);

    List<Application> listCustomApps = applicationRepository.findAllCustomApps(listIdsToExclude);

    response.setListApp(ApplicationMapper.fromListAppCustom(listCustomApps));

    return response;
  }
  @RequestMapping(value = "/oauth/apps/{TOKEN}", method = RequestMethod.GET)
  @Timed
  public GetLandingPageResponseDTO oauthAppForUser(
      @PathVariable("TOKEN") String ott, HttpServletRequest request)
      throws IOException, JAXBException, TokenNotFoundException, TokenAlreadyUsedException {
    GetLandingPageResponseDTO response = new GetLandingPageResponseDTO();
    log.debug("REST GET APPS. token [{}]", ott);

    Token token = tokenRepository.findOneByOtt(ott);
    if (token == null) {
      throw new TokenNotFoundException(ott);
    }

    Person person = token.getPerson();
    Application application = token.getApplication();

    log.debug("REST GET APPS. person [{}], localid [{}]", person, person.getLocalID());

    // qual รจ l'organizzazione dell'utente?
    String orgUnit = null;
    if (application.getAllOrg()) orgUnit = token.getOrgUnit();
    else orgUnit = application.getOrgUnit();

    List<RelPersonApplication> listApplicationAuth =
        relPersonApplicationRepository.findAllByTokenIsAndValidIsNull(token);
    String urlRegisterOrcid = null;
    String urlLoginOrcid = null;
    if (listApplicationAuth.size() > 0) {
      Application applicationAuthorize = listApplicationAuth.get(0).getApplication();

      String callBackUrl = getCallbackOrcidURL(request);
      OrcidOAuthClient clientOrcid =
          new OrcidOAuthClient(
              applicationAuthorize.getApplicationID(),
              applicationAuthorize.getApplicationSecret(),
              callBackUrl,
              orcidApiType);

      List<OrcidAuthScope> orcidScopes = clientOrcid.getListAllScope();
      urlRegisterOrcid =
          clientOrcid.getAuthzCodeRegisterRequest(
              listApplicationAuth.get(0).getId().toString(),
              orcidScopes,
              person.getFirstName(),
              person.getLastName(),
              person.getEmail());
      urlLoginOrcid =
          clientOrcid.getAuthzCodeLoginRequest(
              listApplicationAuth.get(0).getId().toString(),
              orcidScopes,
              person.getFirstName(),
              person.getLastName(),
              person.getEmail());
      ;
    }
    String urlHelp = null;
    if ((application.getHelpURL() != null) && (!application.getHelpURL().isEmpty())) {
      urlHelp = application.getHelpURL();
    } else if ((application.getHelpMail() != null) && (!application.getHelpMail().isEmpty())) {
      urlHelp =
          "mailto:" + application.getHelpMail() + "?Subject=ORCID Support " + person.getLocalID();
    }

    // List<Application> applicationForUser =
    // applicationRepository.findAllByOrgUnitOrAllOrgIsTrue(orgUnit);
    response.setFirstname(person.getFirstName());
    response.setLastname(person.getLastName());
    response.setUrlRegisterOrcid(urlRegisterOrcid);
    response.setUrlLoginOrcid(urlLoginOrcid);
    response.setUrlHelp(urlHelp);
    response.setListApp(ApplicationMapper.fromListRelApp(listApplicationAuth));

    return response;
  }