/** This method is used for validating the authtoken. */
  @Transactional
  @BodyParser.Of(BodyParser.Json.class)
  public Result validateAuthToken() {
    EntityManager em = JPA.em();
    String authToken = null;
    ResultStatus resultStatus = null;
    JsonNode resultJson = null;

    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    try {
      RequestBody requestBody = request().body();
      clientId = requestBody.asJson().get("clientid").asText();
      authToken = request().getHeader("AuthorizationBearer");
      Query query =
          em.createQuery("select a from Authtoken a where a.clientId=:clientId and a.token=:token");
      query.setParameter("clientId", clientId);
      query.setParameter("token", authToken);
      Authtoken authTokenObject = (Authtoken) query.getSingleResult();
      String expiryDate = authTokenObject.getExpiryDate();
      Date expDate = formatter.parse(expiryDate);
      if (expDate.compareTo(new Date()) > 0) {
        resultStatus = new ResultStatus();
        resultStatus.setSuccess("true");
        resultStatus.setResult("Authorized! Valid Token");
        resultJson = Json.toJson(resultStatus);
        return ok(resultJson);
      } else {
        resultStatus = new ResultStatus();
        resultStatus.setSuccess("false");
        resultStatus.setResult(
            "UnAuthorized! Token expired. Please send a request for a new token");
        resultJson = Json.toJson(resultStatus);
        return unauthorized(resultJson);
      }
    } catch (NoResultException e) {
      resultStatus = new ResultStatus();
      resultStatus.setSuccess("false");
      resultStatus.setResult("UnAuthorized! Invalid Token");
      resultJson = Json.toJson(resultStatus);
      return unauthorized(resultJson);
    } catch (ParseException e) {
      System.out.println(e.getMessage());
    } catch (Exception exp) {
      return badRequest(exp.getMessage());
    }
    return ok();
  }
  /**
   * This method is to verify if the client is authenticated.
   *
   * @return Result Returns authenticated/unauthenticated status for the client
   */
  @Transactional
  @BodyParser.Of(BodyParser.Json.class)
  public Result verifyClient() {
    ResultStatus resultStatus = null;
    JsonNode resultJson = null;
    Client client = null;
    int expiryHours = 24;
    EntityManager em = JPA.em();

    String authToken = null;
    try {
      RequestBody requestBody = request().body();
      clientId = requestBody.asJson().get("clientid").asText();
      clientSecret = requestBody.asJson().get("clientsecret").asText();
      cacheObject = (CacheObject) Cache.get(clientId);
      UUID uuid = UUID.randomUUID();
      authToken = uuid.toString();
      resultStatus = new ResultStatus();

      if (cacheObject == null) {
        System.out.println("Hitting DB...");
        client = getDBData(clientId, clientSecret);
        cacheObject = new CacheObject();
        cacheObject.setClientId(client.getClientId());
        cacheObject.setClientSecret(client.getClientSecret());
        Cache.set(clientId, cacheObject);
        resultStatus.setSuccess("true");
        resultStatus.setResult(authToken);
        resultJson = Json.toJson(resultStatus);
        updateDBWithToken(client.getClientId(), authToken, expiryHours);
        return ok(resultJson);
      }
      if (cacheObject.getClientId().equals(clientId)
          && cacheObject.getClientSecret().equals(clientSecret)) {
        System.out.println("Getting client details from cache..");
        resultStatus.setSuccess("true");
        resultStatus.setResult(authToken);
        resultJson = Json.toJson(resultStatus);
        updateDBWithToken(cacheObject.getClientId(), authToken, expiryHours);
        return ok(resultJson);
      } else {
        resultStatus.setSuccess("false");
        resultStatus.setResult("Unauthenticated");
        resultJson = Json.toJson(resultStatus);
        return unauthorized(resultJson);
      }

    } catch (NullPointerException e) {
      System.out.println("hitting db..");
      client = getDBData(clientId, clientSecret);
      cacheObject.setClientId(client.getClientId());
      return badRequest();
    } catch (NoResultException exp) {
      resultStatus.setSuccess("false");
      resultStatus.setResult("Unauthenticated");
      resultJson = Json.toJson(resultStatus);
      return unauthorized(resultJson);
    } catch (Exception e) {
      return badRequest(e.getMessage());
    }
  }