All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.yodlee.sdk.api.validators.ProviderAccountsValidator Maven / Gradle / Ivy

There is a newer version: 1.0.31
Show newest version
/**
 * Copyright (c) 2019 Yodlee, Inc. All Rights Reserved.
 *
 * Licensed under the MIT License. See LICENSE file in the project root for license information.
 */
package com.yodlee.sdk.api.validators;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import com.yodlee.api.model.Field;
import com.yodlee.api.model.ProvidersDataset;
import com.yodlee.api.model.provideraccounts.request.ProviderAccountPreferencesRequest;
import com.yodlee.api.model.provideraccounts.request.ProviderAccountRequest;
import com.yodlee.api.model.provideraccounts.request.RefreshProviderAccountRequest;
import com.yodlee.api.model.validator.Problem;
import com.yodlee.sdk.api.ProviderAccountsApi;
import com.yodlee.sdk.api.exception.ApiException;
import com.yodlee.sdk.api.util.ApiUtils;

public class ProviderAccountsValidator {

	private static final String CONFLICTING_DATASET_ERROR_MSG = "providerAccounts.param.dataset.datasetName.invalid";

	public enum ProviderAccountInclude {
		credentials,//
		questions,//
		preferences
	}

	public enum ProviderAccountDetailsInclude {
		preferences
	}

	public static void validateDeleteProviderAccount(ProviderAccountsApi providerAccountsApi, String methodName,
			long providerAccountId) throws ApiException {
		Class[] argTypes = new Class[] {long.class};
		Object[] argValues = new Object[] {providerAccountId};
		List methodProblems = ApiValidator.validate(providerAccountsApi, methodName, argTypes, argValues);
		List contextProblems = ApiValidator.validateUserContext(providerAccountsApi);
		ApiValidator.collectProblems(methodProblems, contextProblems);
	}

	public static void validateGetProviderAccount(ProviderAccountsApi providerAccountsApi, String methodName,
			long providerAccountId, ProviderAccountInclude[] include, String requestId) throws ApiException {
		Class[] argTypes = new Class[] {long.class, ProviderAccountInclude[].class, String.class};
		Object[] argValues = new Object[] {providerAccountId, include, requestId};
		List methodProblems = ApiValidator.validate(providerAccountsApi, methodName, argTypes, argValues);
		List contextProblems = ApiValidator.validateUserContext(providerAccountsApi);
		ApiValidator.collectProblems(methodProblems, contextProblems);
	}

	public static void validateGetAllProviderAccounts(ProviderAccountsApi providerAccountsApi, String methodName,
			ProviderAccountDetailsInclude include, Long[] providerIds) throws ApiException {
		Class[] argTypes = new Class[] {ProviderAccountDetailsInclude.class, Long[].class};
		Object[] argValues = new Object[] {include, providerIds};
		List contextProblems = ApiValidator.validateUserContext(providerAccountsApi);
		List methodProblems = ApiValidator.validate(providerAccountsApi, methodName, argTypes, argValues);
		methodProblems.addAll(ApiValidator.validateId(providerIds, "providerAccounts.param.providerIds.invalid"));
		ApiValidator.collectProblems(methodProblems, contextProblems);
	}

	public static void validateLinkProviderAccount(ProviderAccountsApi providerAccountsApi, String methodName,
			long providerId, ProviderAccountRequest providerAccountRequest) throws ApiException {
		Class[] argTypes = new Class[] {ProviderAccountRequest.class, long.class};
		Object[] argValues = new Object[] {providerAccountRequest, providerId};
		List contextProblems = ApiValidator.validateUserContext(providerAccountsApi);
		List modelproblems = ApiValidator.validate(providerAccountRequest);
		List methodProblems = ApiValidator.validate(providerAccountsApi, methodName, argTypes, argValues);
		if (providerAccountRequest != null) {
			methodProblems.addAll(validateDatasetDuplication(providerAccountRequest.getDatasets()));
			methodProblems.addAll(ApiUtils.isConflictingParams(providerAccountRequest.getDatasets(),
					providerAccountRequest.getDatasetName(), CONFLICTING_DATASET_ERROR_MSG));
			methodProblems.addAll(validateFields(providerAccountRequest.getField()));
		}
		ApiValidator.collectProblems(methodProblems, contextProblems, modelproblems);
	}

	private static List validateFields(List fields) {
		List problems = new ArrayList<>();
		if (fields == null)
			return problems;
		if (fields.size() == 1 && !(fields.get(0).getId().equalsIgnoreCase("authResponse") ||
				fields.get(0).getId().equalsIgnoreCase("authorizationCode"))) {
			problems.add(new Problem(ApiUtils.getErrorMessage("providerAccounts.param.field.id.authorizationCode.required.OR.providerAccounts.param.field.id.authResponse.required"), ""));
			System.out.println("For UK OB we require authorizationCode and for US/AU OB we require authResponse");
		}
		if (fields.size() == 0) {
			problems.add(new Problem(ApiUtils.getErrorMessage("providerAccounts.param.field.id.authorizationCode.required.OR.providerAccounts.param.field.id.authResponse.required"), ""));
			System.out.println("For UK OB we require authorizationCode and for US/AU OB we require authResponse");
		}
		return problems;
	}

	private static List validateDatasetDuplication(List datasets) {
		List problems = new ArrayList<>();
		if (datasets == null || datasets.isEmpty())
			return problems;
		Set providerSet = new HashSet<>();
		for (ProvidersDataset providersDataset : datasets) {
			if (!providerSet.add(providersDataset)) {
				problems.add(new Problem(ApiUtils.getErrorMessage("providerAccounts.param.dataset.invalid"), ""));
				return problems;
			}
		}
		return problems;
	}

	public static void validateGetProviderAccountProfiles(ProviderAccountsApi providerAccountsApi, String methodName,
			Long[] providerAccountId) throws ApiException {
		Class[] argTypes = new Class[] {Long[].class};
		Object[] argValues = new Object[] {providerAccountId};
		List contextProblems = ApiValidator.validateUserContext(providerAccountsApi);
		List methodProblems = ApiValidator.validate(providerAccountsApi, methodName, argTypes, argValues);
		methodProblems.addAll(
				ApiValidator.validateId(providerAccountId, "providerAccounts.param.providerAccountIds.invalid"));
		ApiValidator.collectProblems(methodProblems, contextProblems);
	}

	public static void validateUpdatePreferences(ProviderAccountsApi providerAccountsApi, String methodName,
			ProviderAccountPreferencesRequest preferences, Long providerAccountId) throws ApiException {
		Class[] argTypes = new Class[] {ProviderAccountPreferencesRequest.class, Long.class};
		Object[] argValues = new Object[] {preferences, providerAccountId};
		List methodProblems = ApiValidator.validate(providerAccountsApi, methodName, argTypes, argValues);
		List contextProblems = ApiValidator.validateUserContext(providerAccountsApi);
		List modelproblems = ApiValidator.validate(preferences);
		ApiValidator.collectProblems(methodProblems, contextProblems, modelproblems);
	}

	public static void validateMFAProviderAccount(ProviderAccountsApi providerAccountsApi, String methodName,
			long providerAccountId, Field[] mfaFields) throws ApiException {
		Class[] argTypes = new Class[] {long.class, Field[].class};
		Object[] argValues = new Object[] {providerAccountId, mfaFields};
		List methodProblems = ApiValidator.validate(providerAccountsApi, methodName, argTypes, argValues);
		List modelProblems = new ArrayList<>();
		if (mfaFields != null) {
			for (Field field : mfaFields) {
				if (field != null)
					modelProblems.addAll(field.validate());
			}
		}
		List contextProblems = ApiValidator.validateUserContext(providerAccountsApi);
		ApiValidator.collectProblems(methodProblems, modelProblems, contextProblems);
	}

	public static void validateUpdateProviderAccount(ProviderAccountsApi providerAccountsApi, String methodName,
			long providerAccountId, ProviderAccountRequest providerAccountRequest) throws ApiException {
		Class[] argTypes = new Class[] {long.class, ProviderAccountRequest.class};
		Object[] argValues = new Object[] {providerAccountId, providerAccountRequest};
		List modelProblems = ApiValidator.validate(providerAccountRequest);
		List contextProblems = ApiValidator.validateUserContext(providerAccountsApi);
		List methodProblems = ApiValidator.validate(providerAccountsApi, methodName, argTypes, argValues);
		if (providerAccountRequest != null) {
			methodProblems.addAll(validateDatasetDuplication(providerAccountRequest.getDatasets()));
			methodProblems.addAll(ApiUtils.isConflictingParams(providerAccountRequest.getDatasets(),
					providerAccountRequest.getDatasetName(), CONFLICTING_DATASET_ERROR_MSG));
			methodProblems.addAll(validateFields(providerAccountRequest.getField()));
		}
		ApiValidator.collectProblems(methodProblems, modelProblems, contextProblems);
	}

	public static void validateRefreshAllProviderAccounts(ProviderAccountsApi providerAccountsApi, String methodName)
			throws ApiException {
		Class[] argTypes = new Class[] {};
		Object[] argValues = new Object[] {};
		List methodProblems = ApiValidator.validate(providerAccountsApi, methodName, argTypes, argValues);
		List contextProblems = ApiValidator.validateUserContext(providerAccountsApi);
		ApiValidator.collectProblems(methodProblems, contextProblems);
	}

	public static void validateRefreshProviderAccount(ProviderAccountsApi providerAccountsApi, String methodName,
			long providerAccountId, RefreshProviderAccountRequest refreshProviderAccountRequest) throws ApiException {
		Class[] argTypes = new Class[] {long.class, RefreshProviderAccountRequest.class};
		Object[] argValues = new Object[] {providerAccountId, refreshProviderAccountRequest};
		List methodProblems = ApiValidator.validate(providerAccountsApi, methodName, argTypes, argValues);
		List contextProblems = ApiValidator.validateUserContext(providerAccountsApi);
		if (refreshProviderAccountRequest != null) {
			methodProblems.addAll(validateDatasetDuplication(refreshProviderAccountRequest.getDatasets()));
			methodProblems.addAll(ApiUtils.isConflictingParams(refreshProviderAccountRequest.getDatasets(),
					refreshProviderAccountRequest.getDatasetName(), CONFLICTING_DATASET_ERROR_MSG));
		}
		ApiValidator.collectProblems(methodProblems, contextProblems);
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy