/**
*
*/
package com.humana.bpic.et.common.util;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.io.*;
import com.ibm.websphere.bo.*;
import com.ibm.websphere.sca.*;
import commonj.sdo.*;
/**
* @author sxd5347
*
*/
public class BOUtil {
private static BOXMLSerializer xmlSerializer = (BOXMLSerializer) ServiceManager.INSTANCE
.locateService("com/ibm/websphere/bo/BOXMLSerializer");
private static BOFactory boFactory = (BOFactory) ServiceManager.INSTANCE
.locateService("com/ibm/websphere/bo/BOFactory");
private static BOInstanceValidator instanceValidator=(BOInstanceValidator) ServiceManager.INSTANCE.locateService("com/ibm/websphere/bo/BOInstanceValidator");
public static BOFactory getBOFactory() {
return boFactory;
}
public static DataObject createBO(String uri, String boType) {
return boFactory.create(uri, boType);
}
public static DataObject createByElement(String uri, String elementName) {
return boFactory.createByElement(uri, elementName);
}
public static String boToString(DataObject bo) {
String result = "";
if (bo == null)
return result;
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
xmlSerializer.writeDataObject(bo, bo.getType().getURI(),
getBOName(bo), bos);
result = new String(bos.toByteArray());
} catch (Exception ex) {
ex.printStackTrace();
}
return result;
}
public static String formatDate(Date date){
String formattedDate1 = "";
String DATE_FORMAT_NOW = "yyyy-MM-dd";
if(date == null)
return formattedDate1;
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
String formattedDate = sdf.format(date );
StringTokenizer st = new StringTokenizer(formattedDate,"-");
while(st.hasMoreTokens()){
formattedDate1 = formattedDate1.concat(st.nextToken());
}
return formattedDate1;
}
public static String toString(DataObject bo) {
String result = "";
if (bo == null)
return result;
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
xmlSerializer.writeDataObject(bo, bo.getType().getURI(),
getBOName(bo), bos);
result = new String(bos.toByteArray());
} catch (Exception ex) {
ex.printStackTrace();
}
return result;
}
public static String getBOName(DataObject bo) {
String boName = bo.getType().getName();
if (boName.indexOf("BaseType") > 0)
boName = boName.substring(0, boName.indexOf("BaseType"));
else if (boName.indexOf("Type") > 0)
boName = boName.substring(0, boName.indexOf("Type"));
return boName;
}
public static String exceptionToString(Throwable t) {
String str = null;
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
t.printStackTrace(pw);
str = sw.toString();
try {
pw.close();
sw.close();
} catch (IOException e) {
}
if (str == null)
str = t.getMessage();
return str;
}
public static BOInstanceValidator getBOValidator()
{
return instanceValidator;
}
public static Map validateData(DataObject input)
{
List validationErrorList=new ArrayList();
Map resultMap=new HashMap();
//System.out.println("****validation starts for "+input);
boolean isValid=com.humana.bpic.et.common.util.BOUtil.getBOValidator().validate(input,validationErrorList);
//System.out.println("*** isValid "+isValid);
if (!isValid) {
java.util.Iterator errorList = validationErrorList.iterator();
while(errorList.hasNext()){
DataObject error = (DataObject)errorList.next();
String prop=(String)error.get("property");
String val=(String)error.get("message");
//System.out.println("Error property: " + prop);
//System.out.println("Error message: " + val);
if(val.indexOf("The type definition cannot be abstract for element")>=0)
{
//System.out.println("***ignoring type definition error messages ");
}
else
resultMap.put(prop,val );
}
}
return resultMap;
}
public static Date dtTimeFormatt(Date input) throws ParseException{
SimpleDateFormat sdt=new SimpleDateFormat("yyyy-MM-dd");
String inputDtString=sdt.format(input);
System.out.println("inputdtString ="+inputDtString);
inputDtString+=" 00:00:00";
SimpleDateFormat sdt2=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date dtTimeStamp=sdt2.parse(inputDtString);
System.out.println("***dateTime "+dtTimeStamp);
return dtTimeStamp;
}
}