/*******************************************************************************
 * Copyright  (c) 2015-2016, WSO2.Telco Inc. (http://www.wso2telco.com) All Rights Reserved.
 *
 *  WSO2.Telco Inc. licences this file to you under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package com.wso2telco.dep.subscriptionvalidator.util;

import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.synapse.MessageContext;
import org.apache.synapse.core.axis2.Axis2MessageContext;
import org.wso2.carbon.apimgt.api.APIManagementException;

import com.wso2telco.dep.subscriptionvalidator.exceptions.ValidatorException;
import com.wso2telco.dep.subscriptionvalidator.services.MifeValidator;

// TODO: Auto-generated Javadoc
/**
 * The Class ValidatorUtils.
 */
public class ValidatorUtils {

    /** The Constant log. */
    private static final Log log = LogFactory.getLog(ValidatorUtils.class);

    private static List<ValidatorClassDTO> validatorClassDTOs = null;

    /**
     * Gets the validator for subscription.
     *
     * @param applicationId the application id
     * @param apiId the api id
     * @return the validator for subscription
     * @throws ValidatorException the validator exception
     */
    /*public static MifeValidator getValidatorForSubscription(int applicationId, int apiId) throws ValidatorException {

        String className = ValidatorDBUtils.getValidatorClassForSubscription(applicationId, apiId);
        log.debug("Validator Class Name: " + className);

        if (className == null) {
            throw new ValidatorException("No Validator class defined for the subscription with appID: " +
                    applicationId + " apiID: " + apiId);
        }
        try {
            MifeValidator validator = (MifeValidator) Class.forName(className).newInstance();
            return validator;
        } catch (Exception e) {
            log.error("Error occured while loading Validator class: " + className, e);
            throw new ValidatorException("Error occured while loading Validator class: " + className, e);
        }
    }*/
    private static String getValidatorClassName(int applicationId, int apiId){
    	String className = null;
    	for(ValidatorClassDTO classDTO : validatorClassDTOs){
    		if(classDTO.getApp() == applicationId && classDTO.getApi() == apiId){
    			className = classDTO.getClassName();
    			log.debug("Validator Class Name: " + className);
    			break;
    		}
    	}
    	
    	return className;
    }
    
    public static MifeValidator getValidatorForSubscription(int applicationId, int apiId) throws ValidatorException {
    	String className = null;
    	if(validatorClassDTOs == null || validatorClassDTOs.isEmpty()){
    		validatorClassDTOs = ValidatorDBUtils.getValidatorClassForSMSSubscription();
    	}
    	className = getValidatorClassName(applicationId, apiId);
    	if(className == null){
    		validatorClassDTOs = ValidatorDBUtils.getValidatorClassForSMSSubscription();
    		className = getValidatorClassName(applicationId, apiId);
    	}

        if (className == null) {
            throw new ValidatorException("No Validator class defined for the subscription with appID: " +
                    applicationId + " apiID: " + apiId);
        }
        try {
            MifeValidator validator = (MifeValidator) Class.forName(className).newInstance();
            return validator;
        } catch (Exception e) {
            log.error("Error occured while loading Validator class: " + className, e);
            throw new ValidatorException("Error occured while loading Validator class: " + className, e);
        }
    }

    /**
     * Gets the validator for subscription.
     *
     * @param mc the mc
     * @return the validator for subscription
     * @throws APIManagementException the API management exception
     * @throws ValidatorException the validator exception
     */
    public static MifeValidator getValidatorForSubscription(MessageContext mc) throws APIManagementException,
            ValidatorException {

     //   AuthenticationContext authContext = APISecurityUtils.getAuthenticationContext(mc);
      //  int applicationId = Integer.parseInt(authContext.getApplicationId());
        int applicationId = Integer.parseInt((String)mc.getProperty("APPLICATION_ID"));
        //String api_version = (String) mc.getProperty(RESTConstants.SYNAPSE_REST_API);
        //api_version = api_version.replace("--", "_").replace(":v", "_");
        //APIIdentifier apiIdentifier = new APIIdentifier(api_version);
        //int apiId = ValidatorDBUtils.getApiId(apiIdentifier);
        int apiId = Integer.parseInt((String) mc.getProperty("API_ID"));

        return getValidatorForSubscription(applicationId, apiId);
    }


    /**
     * Gets the validator for subscription using properties in the message context.
     *
     * @param mc the mc
     * @return the validator for subscription
     * @throws ValidatorException the validator exception
     */
    public static MifeValidator getValidatorForSubscriptionFromMessageContext(MessageContext mc) throws
            ValidatorException {

        String apiIdString = null;
        int applicationId = Integer.parseInt((String)mc.getProperty("APPLICATION_ID"));
        Object headers = ((Axis2MessageContext) mc).getAxis2MessageContext()
                .getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);
        if (headers != null && headers instanceof Map) {
            Map headersMap = (Map) headers;
            apiIdString = (String) headersMap.get("API_ID");

        }
        int apiId = Integer.parseInt(apiIdString);

        return getValidatorForSubscription(applicationId, apiId);
    }

}

@@ -897,31 +897,8 @@
 l;%0A%0A
-import java.util.List;%0A
 impo
@@ -1520,79 +1520,8 @@
 );%0A%0A
-    private static List%3CValidatorClassDTO%3E validatorClassDTOs = null;%0A%0A
     
@@ -1769,18 +1769,16 @@
  */%0A    
-/*
 public s
@@ -2046,1559 +2046,8 @@
 );%0A%0A
-        if (className == null) %7B%0A            throw new ValidatorException(%22No Validator class defined for the subscription with appID: %22 +%0A                    applicationId + %22 apiID: %22 + apiId);%0A        %7D%0A        try %7B%0A            MifeValidator validator = (MifeValidator) Class.forName(className).newInstance();%0A            return validator;%0A        %7D catch (Exception e) %7B%0A            log.error(%22Error occured while loading Validator class: %22 + className, e);%0A            throw new ValidatorException(%22Error occured while loading Validator class: %22 + className, e);%0A        %7D%0A    %7D*/%0A    private static String getValidatorClassName(int applicationId, int apiId)%7B%0A    %09String className = null;%0A    %09for(ValidatorClassDTO classDTO : validatorClassDTOs)%7B%0A    %09%09if(classDTO.getApp() == applicationId && classDTO.getApi() == apiId)%7B%0A    %09%09%09className = classDTO.getClassName();%0A    %09%09%09log.debug(%22Validator Class Name: %22 + className);%0A    %09%09%09break;%0A    %09%09%7D%0A    %09%7D%0A    %09%0A    %09return className;%0A    %7D%0A    %0A    public static MifeValidator getValidatorForSubscription(int applicationId, int apiId) throws ValidatorException %7B%0A    %09String className = null;%0A    %09if(validatorClassDTOs == null %7C%7C validatorClassDTOs.isEmpty())%7B%0A    %09%09validatorClassDTOs = ValidatorDBUtils.getValidatorClassForSMSSubscription();%0A    %09%7D%0A    %09className = getValidatorClassName(applicationId, apiId);%0A    %09if(className == null)%7B%0A    %09%09validatorClassDTOs = ValidatorDBUtils.getValidatorClassForSMSSubscription();%0A    %09%09className = getValidatorClassName(applicationId, apiId);%0A    %09%7D%0A%0A
     

Revert "TPS improvement"