/**
*    Reisang Risom
**/
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.log4j.Logger;
import com.mgeretail.SpringApplicationContext;
public class ObjectCreationServices {
private Logger _log=Logger.getLogger(ObjectCreationServices.class);
private String className=null;
public ObjectCreationServices(Object _beanObject)
{
className=_beanObject.getClass().getName();
}
public ObjectCreationServices()
{
}
public ArrayList<Object> createObjects(HttpServletRequest request,String[] parameters,int fromIndex,int toIndex)
{
ArrayList<Object> objectArray=new ArrayList<Object>();
int nullCounter=0;
Object boolFlag;
try {
for(int h=fromIndex;h<toIndex+1;h++)
{
//Object object= SpringApplicationContext.getBean(className);
Object object=Class.forName(className).newInstance();
BeanInfo beaninfo=Introspector.getBeanInfo(object.getClass());
PropertyDescriptor[] descriptorObj=beaninfo.getPropertyDescriptors();
for(int j=0;j<parameters.length;j++)
{
PropertyDescriptor propertyDesc1=(PropertyDescriptor)descriptorObj[j];
for(int i=0;i<descriptorObj.length;++i)
{
PropertyDescriptor propertyDesc=(PropertyDescriptor)descriptorObj[i];
if(propertyDesc1.getShortDescription().equalsIgnoreCase(propertyDesc.getShortDescription()))
{
if(!propertyDesc1.getShortDescription().equalsIgnoreCase(“barNos”))
{
if(request.getParameter(propertyDesc1.getShortDescription()+String.valueOf(h))!=null && !request.getParameter(propertyDesc1.getShortDescription()+String.valueOf(h)).equalsIgnoreCase(“”))
{
_log.info(“param “+propertyDesc1.getPropertyType()+”:” +”value”+request.getParameter(propertyDesc1.getShortDescription()+String.valueOf(h)));
if(propertyDesc1.getPropertyType().toString().contains(“String”))
{
propertyDesc.getWriteMethod().invoke(object,request.getParameter(propertyDesc1.getShortDescription()+String.valueOf(h)));
}else if(propertyDesc1.getPropertyType().toString().equals(“double”))
{
propertyDesc.getWriteMethod().invoke(object,Double.parseDouble(request.getParameter(propertyDesc1.getShortDescription()+String.valueOf(h))));
}else if(propertyDesc1.getPropertyType().toString().equals(“boolean”))
{
boolFlag=request.getParameter(propertyDesc1.getShortDescription()+String.valueOf(h));
boolFlag=boolFlag.equals(“true”)?true:boolFlag.equals(“false”)?false:boolFlag.equals(“on”)?true:false;
propertyDesc.getWriteMethod().invoke(object,boolFlag);
}else if(propertyDesc1.getPropertyType().toString().equals(“int”))
{
propertyDesc.getWriteMethod().invoke(object,Integer.parseInt(request.getParameter(propertyDesc1.getShortDescription()+String.valueOf(h))));
}
nullCounter++;
break;
}
}
}
}
}
if(nullCounter>0)
{
objectArray.add(object);
}
nullCounter=0;
}
} catch (Exception e) {
_log.error(e.getMessage(),e);
}
return objectArray;
}
public ArrayList<Object> createObjects(HttpServletRequest request,int fromIndex,int toIndex)
{
ArrayList<Object> objectArray=new ArrayList<Object>();
int nullCounter=0;
Object boolFlag;
try {
for(int h=fromIndex;h<toIndex+1;h++)
{
//Object object= SpringApplicationContext.getBean(className);
Object object=Class.forName(className).newInstance();
BeanInfo beaninfo=Introspector.getBeanInfo(object.getClass());
PropertyDescriptor[] descriptorObj=beaninfo.getPropertyDescriptors();
for(int j=0;j<descriptorObj.length;j++)
{
PropertyDescriptor propertyDesc1=(PropertyDescriptor)descriptorObj[j];
for(int i=0;i<descriptorObj.length;++i)
{
PropertyDescriptor propertyDesc=(PropertyDescriptor)descriptorObj[i];
if(propertyDesc1.getShortDescription().equalsIgnoreCase(propertyDesc.getShortDescription()))
{
if(!propertyDesc1.getShortDescription().equalsIgnoreCase(“barNos”))
{
if(request.getParameter(propertyDesc1.getShortDescription()+String.valueOf(h))!=null && !request.getParameter(propertyDesc1.getShortDescription()+String.valueOf(h)).equalsIgnoreCase(“”))
{
if(propertyDesc1.getPropertyType().toString().contains(“String”))
{
propertyDesc.getWriteMethod().invoke(object,request.getParameter(propertyDesc1.getShortDescription()+String.valueOf(h)));
}else if(propertyDesc1.getPropertyType().toString().equals(“double”))
{
propertyDesc.getWriteMethod().invoke(object,Double.parseDouble(request.getParameter(propertyDesc1.getShortDescription()+String.valueOf(h))));
}else if(propertyDesc1.getPropertyType().toString().equals(“boolean”))
{
boolFlag=request.getParameter(propertyDesc1.getShortDescription()+String.valueOf(h));
boolFlag=boolFlag.equals(“true”)?true:boolFlag.equals(“false”)?false:boolFlag.equals(“on”)?true:false;
propertyDesc.getWriteMethod().invoke(object,boolFlag);
}else if(propertyDesc1.getPropertyType().toString().equals(“int”))
{
propertyDesc.getWriteMethod().invoke(object,Integer.parseInt(request.getParameter(propertyDesc1.getShortDescription()+String.valueOf(h))));
}
nullCounter++;
break;
}
}
}
}
}
if(nullCounter>0)
{
objectArray.add(object);
}
nullCounter=0;
}
} catch (Exception e) {
_log.error(e.getMessage(),e);
}
return objectArray;
}
public String createValuePairedString(Object beanObject)
{
StringBuilder strBuilder=new StringBuilder();
try {
//Object bean = Class.forName(“com.mgeretail.modules.beans.”+beanName).newInstance();
Object bean=SpringApplicationContext.getBean(className);
String tempValHolder=””;
BeanInfo beaninfo=Introspector.getBeanInfo(bean.getClass());
PropertyDescriptor[] objectsDetails=beaninfo.getPropertyDescriptors();
for(int i=0;i<objectsDetails.length;++i)
{
PropertyDescriptor propertyDesc=(PropertyDescriptor)objectsDetails[i];
tempValHolder=propertyDesc.getReadMethod().invoke(beanObject)==null?””:String.valueOf(propertyDesc.getReadMethod().invoke(beanObject));
strBuilder.append(“[“+propertyDesc.getShortDescription()+”::”+tempValHolder+”]”);
}
} catch (Exception e) {
_log.error(e.getMessage(),e);
}
return strBuilder.toString();
}
public Object createObject(String pairedVals)
{
HashMap<String,String> map=new HashMap<String, String>();
Object bean = null;
try
{
String splitedVals[]=pairedVals.split(“]”);
String splitedVals1[];
for(int i=0;i<splitedVals.length;i++)
{
splitedVals1=splitedVals[i].split(“::”);
if(splitedVals1.length==2)
{
map.put(splitedVals1[0].substring(1),splitedVals1[1]);
}
else
{
map.put(splitedVals1[0].substring(1),””);
}
}
bean=SpringApplicationContext.getBean(className);
//bean=Class.forName(“com.mgeretail.modules.beans.”+beanName).newInstance();
BeanInfo beaninfo=Introspector.getBeanInfo(bean.getClass());
PropertyDescriptor[] objectsDetails=beaninfo.getPropertyDescriptors();
for(int i=0;i<objectsDetails.length;++i)
{
PropertyDescriptor propertyDesc=(PropertyDescriptor)objectsDetails[i];
if(!map.get(propertyDesc.getShortDescription()).equalsIgnoreCase(“”) &&
map.get(propertyDesc.getShortDescription())!=null &&
!propertyDesc.getShortDescription().equalsIgnoreCase(“class”))
{
propertyDesc.getWriteMethod().invoke(bean,map.get(propertyDesc.getShortDescription()));
}
}
}
catch(Exception e)
{
_log.error(e.getMessage(),e);
}
return bean;
}
}
Advertisements