/**
 * 
 */
package com.cim.idm.mwmsextend.userprofile.service;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jdom.Element;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import com.cim.idm.wmspackage.user.UserServiceProxy;
import com.cim.idm.wmspackage.user.management.data.UserInfo;
import com.cim.idm.wmspackage.user.management.data.UserInfoKey;
import com.cim.idm.framework.exception.ext.FrameworkErrorSignal;
import com.cim.idm.framework.exception.ext.NotFoundSignal;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;


/**
 * @author sjlee
 *
 */
public class UserProfileServiceUtil implements ApplicationContextAware {

	private ApplicationContext     	applicationContext;
	private static Log 				log = LogFactory.getLog(UserProfileServiceImpl.class);
	
	
	/*
	* Name : setApplicationContext
	* Desc : This function is setApplicationContext
	* Author : AIM Systems, Inc
	* Date : 2011.02.17
	*/
	@Override
	public void setApplicationContext(ApplicationContext arg0)
			throws BeansException {
		// TODO Auto-generated method stub
		applicationContext = arg0;
	}
	
	/*
	* Name : createUserProfileElement
	* Desc : This function is createUserProfileElement
	* Author : AIM Systems, Inc
	* Date : 2011.02.17
	*/
	public Element createUserProfileElement(String userId) throws FrameworkErrorSignal, NotFoundSignal
	{	
		UserInfoKey userInfoKey = new UserInfoKey();
		userInfoKey.setUserId( userId );

		UserInfo userInfoData = null;
		userInfoData  = UserServiceProxy.getUserInfoService().selectByKey( userInfoKey );
		
		Element element = null;
		String node = null;

		node = "USERPROFILE";
		element = new Element(node);		

		Element elmUserId = new Element("USERID");
		elmUserId.setText(userInfoData.getKey().getUserId());
		element.addContent(elmUserId);
		
		Element elmSiteName = new Element("SITENAME");
		elmSiteName.setText(userInfoData.getSiteName());
		element.addContent(elmSiteName);
		
		Element elmUserName = new Element("USERNAME");
		elmUserName.setText(userInfoData.getUserName());
		element.addContent(elmUserName);

		Element elmUserGroupName = new Element("USERGROUPNAME");
		elmUserGroupName.setText(userInfoData.getUserGroupName());
		element.addContent(elmUserGroupName);

		return element;
	}
	public String getUserInfo(String userId) throws FrameworkErrorSignal, NotFoundSignal
	{
		String jsonStr="";
		try 
		{
			UserInfoKey userInfoKey = new UserInfoKey();
			userInfoKey.setUserId( userId );
	
			UserInfo userInfoData = null;
			userInfoData  = UserServiceProxy.getUserInfoService().selectByKey( userInfoKey );
			ObjectMapper mapper=new ObjectMapper();
			jsonStr=mapper.writeValueAsString(userInfoData);
				
			} catch (JsonProcessingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return jsonStr;
	}

	public String getMenuList(String userId) throws FrameworkErrorSignal, NotFoundSignal
	{
		String jsonStr="";
		try
		{
			UserInfoKey userInfoKey = new UserInfoKey();
			userInfoKey.setUserId( userId );

			UserInfo userInfoData = null;
			userInfoData  = UserServiceProxy.getUserInfoService().selectByKey( userInfoKey );
			ObjectMapper mapper=new ObjectMapper();
			jsonStr=mapper.writeValueAsString(userInfoData);

		} catch (JsonProcessingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return jsonStr;
	}
	
}