/*******************************************************************************
 * Copyright 2014 Centre for Development of Advanced Computing(C-DAC), Pune
 *
 * Licensed 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 in.cdac.medinfo.csnotk.csnolib.model;

import in.cdac.medinfo.csnotk.csnolib.commons.Constants;
import in.cdac.medinfo.csnotk.csnolib.commons.EnumComponent;
//Logs
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import in.cdac.medinfo.csnotk.csnolib.util.LoggerUtility;

/**
 * Factory class to generate Components using the
 * <code>getComponent()</code> method.
 */
public class ComponentFactory
{
	/**
	 * Instance of the ComponentFactory class {@link ComponentFactory}
	 */
    private static final Logger logger = LogManager.getLogger(ComponentFactory.class);
	private static ComponentFactory instance=new ComponentFactory();

	/**
	 * Default Constructor
	 */
	private ComponentFactory()
	{

	}
	/**
	 * Function to return the object of the desired Component type.
	 * @param componentType
	 * 						 {@link EnumComponent} value that represents
	 * the type of the Component whose object has to be created.
	 * @param id
	 * 						 represents the Component Id
	 * @return {@link VersionedComponent} It can be an object of {@link Concept}
	 * class or {@link Description} class or {@link Relationship} class.
	 */

	public VersionedComponent getComponent(EnumComponent componentType,String id)
	{
		if(componentType==null)
			return null;
		if((componentType==EnumComponent.CONCEPT)&&(id==null))
			return new Concept();
		else if(componentType==EnumComponent.CONCEPT)
		{
			try
			{
				return new Concept(id);
			}
			catch(Exception e)
			{
				//Get the Stack trace and form the exception message.
				StackTraceElement arrStackTraceElement[];
				arrStackTraceElement = e.getStackTrace();
				String strMessage = e.getClass() + Constants.NEW_LINE + Constants.CLASS_NAME + arrStackTraceElement[0].getClassName() + Constants.NEW_LINE + Constants.METHOD_NAME + arrStackTraceElement[0].getMethodName() + Constants.NEW_LINE +  Constants.LINE_NUMBER + arrStackTraceElement[0].getLineNumber()+ Constants.NEW_LINE +  Constants.MESSAGE_DESCRIPTION + e.getMessage();
				//Log the Exception
				logger.error(LoggerUtility.Exception, strMessage);
				return null;
			}
		}
		else if((componentType==EnumComponent.DESCRIPTION))
			return new Description();
		else if(componentType==EnumComponent.RELATIONSHIP)
			return new Relationship();
		else if(componentType==EnumComponent.CONCRETERELATIONSHIP)
			return new ConcreteRelationship();
		else if(componentType==EnumComponent.SIMPLEMAP)
			return new SimpleMapRefset();
		else
			return null;

	}
	/**
	 * Static function to return the instance of {@link ComponentFactory} class.
	 * @return instance of {@link ComponentFactory} class.
	 */
	public static ComponentFactory getInstance()
	{
		return instance;
	}
}
