@Override
  public void removeClaimFromApplication(Application application, RequestClaim claim) {
    ApplicationEntity applicationEntity = null;
    if (application.getId() != 0) {
      applicationEntity = em.find(ApplicationEntity.class, application.getId());
    } else {
      Query query = null;
      query = em.createQuery("select a from Application a where a.realm=:realm");
      query.setParameter("realm", application.getRealm());

      applicationEntity = (ApplicationEntity) query.getSingleResult();
    }

    ApplicationClaimEntity foundEntity = null;
    for (ApplicationClaimEntity acm : applicationEntity.getRequestedClaims()) {
      if (claim.getClaimType().toString().equals(acm.getClaim().getClaimType())) {
        foundEntity = acm;
        break;
      }
    }
    if (foundEntity == null) {
      throw new EntityNotFoundException("ApplicationClaimEntity not found");
    }

    applicationEntity.getRequestedClaims().remove(foundEntity);
  }
  @Override
  public Application addApplication(Application application) {
    ApplicationEntity entity = new ApplicationEntity();

    domain2entity(application, entity);
    em.persist(entity);

    LOG.debug("Application '{}' added", application.getRealm());
    return entity2domain(entity, Arrays.asList("all"));
  }
  @Override
  public void addClaimToApplication(Application application, RequestClaim claim) {
    ApplicationEntity applicationEntity = null;
    if (application.getId() != 0) {
      applicationEntity = em.find(ApplicationEntity.class, application.getId());
    } else {
      Query query = null;
      query = em.createQuery("select a from Application a where a.realm=:realm");
      query.setParameter("realm", application.getRealm());

      applicationEntity = (ApplicationEntity) query.getSingleResult();
    }

    Claim c = claimDAO.getClaim(claim.getClaimType().toString());
    ClaimEntity claimEntity = em.find(ClaimEntity.class, c.getId());

    ApplicationClaimEntity appClaimEntity = new ApplicationClaimEntity();
    appClaimEntity.setClaim(claimEntity);
    appClaimEntity.setApplication(applicationEntity);
    appClaimEntity.setOptional(claim.isOptional());

    applicationEntity.getRequestedClaims().add(appClaimEntity);
  }
  public static Application entity2domain(ApplicationEntity entity, List<String> expandList) {
    Application application = new Application();
    application.setId(entity.getId());
    application.setEncryptionCertificate(entity.getEncryptionCertificate());
    application.setLifeTime(entity.getLifeTime());
    application.setProtocol(entity.getProtocol());
    application.setRealm(entity.getRealm());
    application.setRole(entity.getRole());
    application.setServiceDescription(entity.getServiceDescription());
    application.setServiceDisplayName(entity.getServiceDisplayName());
    application.setTokenType(entity.getTokenType());
    application.setPolicyNamespace(entity.getPolicyNamespace());
    application.setPassiveRequestorEndpoint(entity.getPassiveRequestorEndpoint());

    if (expandList != null && (expandList.contains("all") || expandList.contains("claims"))) {
      for (ApplicationClaimEntity item : entity.getRequestedClaims()) {
        RequestClaim claim = entity2domain(item);
        application.getRequestedClaims().add(claim);
      }
    }
    return application;
  }
  public static void domain2entity(Application application, ApplicationEntity entity) {
    // The ID must not be updated if the entity has got an id already (update case)
    if (application.getId() > 0) {
      entity.setId(application.getId());
    }

    entity.setEncryptionCertificate(application.getEncryptionCertificate());
    entity.setLifeTime(application.getLifeTime());
    entity.setProtocol(application.getProtocol());
    entity.setRealm(application.getRealm());
    entity.setRole(application.getRole());
    entity.setServiceDescription(application.getServiceDescription());
    entity.setServiceDisplayName(application.getServiceDisplayName());
    entity.setTokenType(application.getTokenType());
    entity.setPolicyNamespace(application.getPolicyNamespace());
    entity.setPassiveRequestorEndpoint(application.getPassiveRequestorEndpoint());
  }