@Override
 public Account getAccountByKeys(String access, String secretKey) {
   try {
     Credential credential =
         create()
             .selectFrom(CREDENTIAL)
             .where(CREDENTIAL.STATE.eq(CommonStatesConstants.ACTIVE))
             .and(CREDENTIAL.PUBLIC_VALUE.eq(access))
             .and(CREDENTIAL.KIND.in(SUPPORTED_TYPES.get()))
             .fetchOne();
     if (credential == null) {
       return null;
     }
     boolean secretIsCorrect =
         ApiContext.getContext()
             .getTransformationService()
             .compare(secretKey, credential.getSecretValue());
     if (secretIsCorrect) {
       return create()
           .selectFrom(ACCOUNT)
           .where(
               ACCOUNT
                   .ID
                   .eq(credential.getAccountId())
                   .and(ACCOUNT.STATE.eq(CommonStatesConstants.ACTIVE)))
           .fetchOneInto(AccountRecord.class);
     } else {
       return null;
     }
   } catch (InvalidResultException e) {
     throw new ClientVisibleException(ResponseCodes.CONFLICT, "MultipleKeys");
   }
 }
 @Override
 public Account getAccountByLogin(String publicValue, String secretValue) {
   Credential credential =
       create()
           .selectFrom(CREDENTIAL)
           .where(CREDENTIAL.STATE.eq(CommonStatesConstants.ACTIVE))
           .and(
               CREDENTIAL
                   .PUBLIC_VALUE
                   .eq(publicValue)
                   .and(CREDENTIAL.KIND.equalIgnoreCase(CredentialConstants.KIND_PASSWORD)))
           .fetchOne();
   if (credential == null) {
     return null;
   }
   boolean secretIsCorrect =
       ApiContext.getContext()
           .getTransformationService()
           .compare(secretValue, credential.getSecretValue());
   if (secretIsCorrect) {
     return create()
         .selectFrom(ACCOUNT)
         .where(
             ACCOUNT
                 .ID
                 .eq(credential.getAccountId())
                 .and(ACCOUNT.STATE.eq(CommonStatesConstants.ACTIVE)))
         .fetchOneInto(AccountRecord.class);
   } else {
     return null;
   }
 }
 @Override
 public Account getAccountByUuid(String uuid) {
   return create()
       .selectFrom(ACCOUNT)
       .where(ACCOUNT.UUID.eq(uuid).and(ACCOUNT.STATE.eq(CommonStatesConstants.ACTIVE)))
       .orderBy(ACCOUNT.ID.asc())
       .limit(1)
       .fetchOne();
 }
 @Override
 public Account getAccountById(Long id) {
   return create()
       .selectFrom(ACCOUNT)
       .where(
           ACCOUNT
               .ID
               .eq(id)
               .and(ACCOUNT.STATE.ne(CommonStatesConstants.PURGED))
               .and(ACCOUNT.REMOVED.isNull()))
       .fetchOne();
 }
  @Override
  public List<Account> getAccessibleProjects(
      Set<Identity> identities, boolean isAdmin, Long usingAccount) {
    List<Account> projects = new ArrayList<>();
    if (identities == null) {
      return projects;
    }
    if (isAdmin) {
      projects.addAll(
          create()
              .selectFrom(ACCOUNT)
              .where(ACCOUNT.KIND.eq(ProjectConstants.TYPE).and(ACCOUNT.REMOVED.isNull()))
              .orderBy(ACCOUNT.ID.asc())
              .fetch());
      return projects;
    }

    if (usingAccount != null) {
      Account project = getAccountById(usingAccount);
      if (project != null && project.getKind().equalsIgnoreCase(ProjectConstants.TYPE)) {
        projects.add(project);
        return projects;
      }
    }
    // DSL.falseCondition is created so that we can dynamically build a or
    // Condition without caring what the external Ids are and still make one
    // Database call.
    Condition allMembers = DSL.falseCondition();
    for (Identity id : identities) {
      allMembers =
          allMembers.or(
              PROJECT_MEMBER
                  .EXTERNAL_ID
                  .eq(id.getExternalId())
                  .and(PROJECT_MEMBER.EXTERNAL_ID_TYPE.eq(id.getExternalIdType()))
                  .and(PROJECT_MEMBER.REMOVED.isNull())
                  .and(PROJECT_MEMBER.STATE.eq(CommonStatesConstants.ACTIVE)));
    }
    SelectQuery<Record> query = create().selectQuery();
    query.addFrom(ACCOUNT);
    query.addJoin(PROJECT_MEMBER, PROJECT_MEMBER.PROJECT_ID.equal(ACCOUNT.ID));
    query.addConditions(allMembers);
    query.setDistinct(true);
    projects.addAll(query.fetchInto(ACCOUNT));
    Map<Long, Account> returnProjects = new HashMap<>();
    for (Account project : projects) {
      returnProjects.put(project.getId(), project);
    }
    projects = new ArrayList<>();
    projects.addAll(returnProjects.values());
    return projects;
  }
 @Override
 public Account getAdminAccount() {
   return create()
       .selectFrom(ACCOUNT)
       .where(
           ACCOUNT
               .STATE
               .eq(CommonStatesConstants.ACTIVE)
               .and(ACCOUNT.KIND.eq(AccountConstants.ADMIN_KIND)))
       .orderBy(ACCOUNT.ID.asc())
       .limit(1)
       .fetchOne();
 }
 @Override
 public Account getAccountByExternalId(String externalId, String externalType) {
   return create()
       .selectFrom(ACCOUNT)
       .where(
           ACCOUNT
               .EXTERNAL_ID
               .eq(externalId)
               .and(ACCOUNT.EXTERNAL_ID_TYPE.eq(externalType))
               .and(ACCOUNT.STATE.ne("purged")))
       .orderBy(ACCOUNT.ID.asc())
       .fetchOne();
 }
 @Override
 public List<Account> searchUsers(String username) {
   return create()
       .select(ACCOUNT.fields())
       .from(ACCOUNT)
       .join(CREDENTIAL)
       .on(CREDENTIAL.ACCOUNT_ID.eq(ACCOUNT.ID))
       .where(
           ACCOUNT
               .STATE
               .eq(CommonStatesConstants.ACTIVE)
               .and(CREDENTIAL.STATE.eq(CommonStatesConstants.ACTIVE))
               .and(CREDENTIAL.PUBLIC_VALUE.contains(username))
               .and(CREDENTIAL.KIND.eq(CredentialConstants.KIND_PASSWORD)))
       .orderBy(ACCOUNT.ID.asc())
       .fetchInto(Account.class);
 }
  @Override
  public void updateAccount(
      Account account, String name, String kind, String externalId, String externalType) {
    Map<TableField<AccountRecord, String>, String> properties = new HashMap<>();
    if (StringUtils.isNotEmpty(name)) {
      properties.put(ACCOUNT.NAME, name);
    }
    if (StringUtils.isNotEmpty(kind)) {
      properties.put(ACCOUNT.KIND, kind);
    }
    if (StringUtils.isNotEmpty(externalId)) {
      properties.put(ACCOUNT.EXTERNAL_ID, externalId);
    }
    if (StringUtils.isNotEmpty(externalType)) {
      properties.put(ACCOUNT.EXTERNAL_ID_TYPE, externalType);
    }
    int updateCount =
        create().update(ACCOUNT).set(properties).where(ACCOUNT.ID.eq(account.getId())).execute();

    if (1 != updateCount) {
      throw new RuntimeException("UpdateAccount failed.");
    }
  }