package ibase.webitm.utility;

import javax.servlet.http.HttpServletRequest;

import ibase.utility.BaseLogger;
import ibase.utility.UserInfoBean;
import ibase.webitm.ejb.DocumentHandlerWrapperEJB;
import org.json.JSONException;
import org.json.JSONObject;

public class DoumentHandlerUtility extends RestAPIServiceUtility {
	
	private UserInfoBean userInfo = null;

//change the method name
	    public byte[] getAttachConfigData(String objName, String refser, String refId, String checkObjName, HttpServletRequest request,String tokenIDfromHeader) 
	    {
	    	
			BaseLogger.log( "3", null, null, "Insidet the getAttachConfigData() :" );

	        String returnData = null;
	        RestAPIServiceUtility utility = new RestAPIServiceUtility();
	        
	        if (userInfo == null) {
				APIUtility apiUtility = new APIUtility();
				userInfo = apiUtility.createUserInfoFromJWTToken(tokenIDfromHeader);
				
				BaseLogger.log( "3", null, null, "USER_INFO created from TOKEN: [" + userInfo + "]" );
			}

			BaseLogger.log( "3", null, null, "objName ::  : ["+ objName +"]" );

	        try {
	            if (!"".equals(utility.checkNull(objName))) {
	                DocumentHandlerWrapperEJB docWrapperHandler = new DocumentHandlerWrapperEJB();
	                docWrapperHandler.setUserInfo(userInfo);
	                returnData = docWrapperHandler.getAttachmentsMetaData(objName, refId, userInfo, checkObjName);
	    			BaseLogger.log( "3", null, null, "returnData ::  : ["+ returnData +"]" );
	            } else {
	                JSONObject jsonObject = new JSONObject();
	                jsonObject.put("EXCEPTION", "ObjName is invalid [" + objName + "].");
	                returnData = jsonObject.toString();
	            }
	        } catch (Exception ex) {
	            BaseLogger.log("3", userInfo, null, "Exception in DocumentHandlerServlet :: handleDocument() : " + ex);
	            JSONObject jsonObject = new JSONObject();
	            try {
					jsonObject.put("EXCEPTION", ex);
				} catch (JSONException e) {
					e.printStackTrace();
				}
	            returnData = jsonObject.toString();
	        }

	        return returnData.getBytes();
	    }
	    
	    //for getFileConfigurationData
	    public String getFileConfigurationData(String objName,HttpServletRequest request,String tokenIDfromHeader) {
	        String returnData = null;
	        
	        if (userInfo == null) {
				APIUtility apiUtility = new APIUtility();
				userInfo = apiUtility.createUserInfoFromJWTToken(tokenIDfromHeader);
				
				BaseLogger.log( "3", null, null, "USER_INFO created from TOKEN: [" + userInfo + "]" );
			}
	        try {
	            if (!"".equals(checkNull(objName))) {
	                DocumentHandlerWrapperEJB docWrapperHandler = new DocumentHandlerWrapperEJB();
	                docWrapperHandler.setUserInfo(userInfo);
	                returnData = docWrapperHandler.getFileConfigurationsMetadata(objName, userInfo.getTransDB());
	            } else {
	                BaseLogger.log("3", userInfo, null, "DocumentHandlerServlet :: handleDocument() : Invalid objName[" + objName + "]");
	                JSONObject jsonObject = new JSONObject();
	                jsonObject.put("EXCEPTION", "ObjName is invalid [" + objName + "].");
	                returnData = jsonObject.toString();
	            }
	        } catch (Exception ex) {
	            BaseLogger.log("3", userInfo, null, "Exception in DocumentHandlerServlet :: handleDocument() : " + ex);
	            JSONObject jsonObject = new JSONObject();
	            try {
					jsonObject.put("EXCEPTION", ex.toString());
				} catch (JSONException e) {
					e.printStackTrace();
				}
	            returnData = jsonObject.toString();
	        }
	        return returnData;
	    }

   

}
	
