通过定义一个bean层来实现某些通用方法的公用,减少代码量。
根据需要定义一个bean:
public class WTObjectViewBean {
/**
*
*/
private static final long serialVersionUID = 1L;
public String objectNumber;//部件编码
public String objectName;//部件名称
public String creator;//创建者
public String creatTime;//创建时间
public String objectUrl;//部件url
public long objectId;//部件id
public String cadnumber;//cad编码
public String cadname;//cad名称
public long cadid;//cad id
public String cadurl;//cad图纸的链接
public String viewurl;//cad productview视图
private String String1;
public String string2;
public String string3;
public String string4;
public String string5;
public String string6;
public String string7;
public String string8;
public String string9;
public String string10;
public String string11;
public Long long1;
public Long long2;
public Long long3;
public Long long4;
public Long long5;
public Long long6;
public Long long7;
public Long long8;
public Long long9;
public Long long10;
public Float float1;
public Float float2;
public Float float3;
public Float float4;
public Float float5;
public Float float6;
public Float float7;
public Float float8;
public Float float9;
public Float float10;
public String thumbnailUrl;//缩略图的url
public String objectType;//部件类型
public String getCreator() {
return creator;
}
public void setCreator(String creator) {
this.creator = creator;
}
public String getCreatTime() {
return creatTime;
}
public void setCreatTime(String creatTime) {
this.creatTime = creatTime;
}
public static long getSerialversionuid() {
return serialVersionUID;
}
public String getObjectType() {
return objectType;
}
public void setObjectType(String objectType) {
this.objectType = objectType;
}
public String getThumbnailUrl() {
return thumbnailUrl;
}
public void setThumbnailUrl(String thumbnailUrl) {
this.thumbnailUrl = thumbnailUrl;
}
public String getViewurl() {
return viewurl;
}
public void setViewurl(String viewurl) {
this.viewurl = viewurl;
}
public String getObjectNumber() {
return objectNumber;
}
public void setObjectNumber(String objectNumber) {
this.objectNumber = objectNumber;
}
public String getObjectName() {
return objectName;
}
public void setObjectName(String partname) {
this.objectName = partname;
}
public long getObjectId() {
return objectId;
}
public void setObjectId(long partid) {
this.objectId = partid;
}
public String getObjectUrl() {
return objectUrl;
}
public void setObjectUrl(String parturl) {
this.objectUrl = parturl;
}
public String getCadnumber() {
return cadnumber;
}
public void setCadnumber(String cadnumber) {
this.cadnumber = cadnumber;
}
public String getCadname() {
return cadname;
}
public void setCadname(String cadname) {
this.cadname = cadname;
}
public long getCadid() {
return cadid;
}
public void setCadid(long cadid) {
this.cadid = cadid;
}
public String getCadurl() {
return cadurl;
}
public void setCadurl(String cadurl) {
this.cadurl = cadurl;
}
public String getString1() {
return String1;
}
public void setString1(String string1) {
this.String1 = string1;
}
public String getString2() {
return string2;
}
public void setString2(String string2) {
this.string2 = string2;
}
public String getString3() {
return string3;
}
public void setString3(String string3) {
this.string3 = string3;
}
public String getString4() {
return string4;
}
public void setString4(String string4) {
this.string4 = string4;
}
public String getString5() {
return string5;
}
public void setString5(String string5) {
this.string5 = string5;
}
public String getString6() {
return string6;
}
public void setString6(String string6) {
this.string6 = string6;
}
public String getString7() {
return string7;
}
public void setString7(String string7) {
this.string7 = string7;
}
public String getString8() {
return string8;
}
public void setString8(String string8) {
this.string8 = string8;
}
public String getString9() {
return string9;
}
public void setString9(String string9) {
this.string9 = string9;
}
public String getString10() {
return string10;
}
public void setString10(String string10) {
this.string10 = string10;
}
public String getString11() {
return string11;
}
public void setString11(String string11) {
this.string11 = string11;
}
public Long getLong1() {
return long1;
}
public void setLong1(Long long1) {
this.long1 = long1;
}
public Long getLong2() {
return long2;
}
public void setLong2(Long long2) {
this.long2 = long2;
}
public Long getLong3() {
return long3;
}
public void setLong3(Long long3) {
this.long3 = long3;
}
public Long getLong4() {
return long4;
}
public void setLong4(Long long4) {
this.long4 = long4;
}
public Long getLong5() {
return long5;
}
public void setLong5(Long long5) {
this.long5 = long5;
}
public Long getLong6() {
return long6;
}
public void setLong6(Long long6) {
this.long6 = long6;
}
public Long getLong7() {
return long7;
}
public void setLong7(Long long7) {
this.long7 = long7;
}
public Long getLong8() {
return long8;
}
public void setLong8(Long long8) {
this.long8 = long8;
}
public Long getLong9() {
return long9;
}
public void setLong9(Long long9) {
this.long9 = long9;
}
public Long getLong10() {
return long10;
}
public void setLong10(Long long10) {
this.long10 = long10;
}
public Float getFloat1() {
return float1;
}
public void setFloat1(Float float1) {
this.float1 = float1;
}
public Float getFloat2() {
return float2;
}
public void setFloat2(Float float2) {
this.float2 = float2;
}
public Float getFloat3() {
return float3;
}
public void setFloat3(Float float3) {
this.float3 = float3;
}
public Float getFloat4() {
return float4;
}
public void setFloat4(Float float4) {
this.float4 = float4;
}
public Float getFloat5() {
return float5;
}
public void setFloat5(Float float5) {
this.float5 = float5;
}
public Float getFloat6() {
return float6;
}
public void setFloat6(Float float6) {
this.float6 = float6;
}
public Float getFloat7() {
return float7;
}
public void setFloat7(Float float7) {
this.float7 = float7;
}
public Float getFloat8() {
return float8;
}
public void setFloat8(Float float8) {
this.float8 = float8;
}
public Float getFloat9() {
return float9;
}
public void setFloat9(Float float9) {
this.float9 = float9;
}
public Float getFloat10() {
return float10;
}
public void setFloat10(Float float10) {
this.float10 = float10;
}
}
再定义转json的设置,这里是转成json格式时忽略空值:
/**
* 获取json转化的默认配置,value值为空不输出
* @return
**/
public static JsonConfig getJsonConfig()
{
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.registerDefaultValueProcessor(String.class, new DefaultDefaultValueProcessor() {
public Object getDefaultValue(Class type) {
return null;
}
});
jsonConfig.registerDefaultValueProcessor(Long.class, new DefaultDefaultValueProcessor() {
public Object getDefaultValue(Class type) {
return null;
}
});
jsonConfig.registerDefaultValueProcessor(Float.class, new DefaultDefaultValueProcessor() {
public Object getDefaultValue(Class type) {
return null;
}
});
PropertyFilter filter = new PropertyFilter() {
public boolean apply(Object object, String fieldName, Object fieldValue) {
return null == fieldValue;
}
};
jsonConfig.setJsonPropertyFilter(filter);//value值为空不输出
return jsonConfig;
}
/**
*将WTObjectViewBean转化为bean
* @param view
* @return
*/
public static WTObjectViewBean WTObjectViewToBean(WTObject obj) throws IOException
{
WTObjectViewBean bean=new WTObjectViewBean();
WTProperties prop=WTProperties.getLocalProperties();
String webport = prop.getProperty("wt.webserver.port");
String urlbase = prop.getProperty("wt.rmi.server.hostname");
urlbase = urlbase + ":" + webport;
String url = "http://" +urlbase +"/Windchill/";
if(obj!=null) {
if(obj instanceof LYHPart) {
LYHPart view = (LYHPart)obj;
if(view.getCadid()!=null)
{
bean.setCadid(view.getCadid());
}
if(view.getCadname()!=null)
{
bean.setCadname(view.getCadname());
}
if(view.getCadnum()!=null)
{
bean.setCadnumber(view.getCadnum());
}
if(view.getCadid()!=null)
{
String strcadurl=url+"/app/#ptc1/tcomp/infoPage?oid=OR:wt.epm.EPMDocument:"+view.getCadid();
bean.setCadurl(strcadurl);
}
if(view.getFloat1()!=null)
{
bean.setFloat1(view.getFloat1());
}
if(view.getFloat2()!=null)
{
bean.setFloat2(view.getFloat2());
}
if(view.getFloat3()!=null)
{
bean.setFloat3(view.getFloat3());
}
if(view.getFloat4()!=null)
{
bean.setFloat4(view.getFloat4());
}
if(view.getFloat5()!=null)
{
bean.setFloat5(view.getFloat5());
}
if(view.getFloat6()!=null)
{
bean.setFloat6(view.getFloat6());
}
if(view.getFloat7()!=null)
{
bean.setFloat7(view.getFloat7());
}
if(view.getFloat8()!=null)
{
bean.setFloat8(view.getFloat8());
}
if(view.getFloat9()!=null)
{
bean.setFloat9(view.getFloat9());
}
if(view.getFloat10()!=null)
{
bean.setFloat10(view.getFloat10());
}
if(view.getLong1()!=null)
{
bean.setLong1(view.getLong1());
}
if(view.getLong2()!=null)
{
bean.setLong2(view.getLong2());
}
if(view.getLong3()!=null)
{
bean.setLong3(view.getLong3());
}
if(view.getLong4()!=null)
{
bean.setLong4(view.getLong4());
}
if(view.getLong5()!=null)
{
bean.setLong5(view.getLong5());
}
if(view.getLong6()!=null)
{
bean.setLong6(view.getLong6());
}
if(view.getLong7()!=null)
{
bean.setLong7(view.getLong7());
}
if(view.getLong8()!=null)
{
bean.setLong8(view.getLong8());
}
if(view.getLong9()!=null)
{
bean.setLong9(view.getLong9());
}
if(view.getLong10()!=null)
{
bean.setLong10(view.getLong10());
}
if(view.getPartname()!=null)
{
bean.setObjectName(view.getPartname());
}
if(view.getPartnum()!=null)
{
bean.setObjectNumber(view.getPartnum());
}
if(view.getPartid()!=null)
{
String strparturl=url+"/app/#ptc1/tcomp/infoPage?oid=OR:wt.part.WTPart:"+view.getPartid();
bean.setObjectUrl(strparturl);
}
if(view.getPartid()!=null)
{
String thumbnailUrl=getThumbnailUrl(view);
bean.setThumbnailUrl(thumbnailUrl);
}
if(view.getPartid()!=null)
{
String objectType=view.getPersistInfo().getObjectIdentifier().getClassname();
bean.setObjectType(objectType);
}
if(view.getPartid()!=null)
{
long id =view.getPersistInfo().getObjectIdentifier().getId();
bean.setObjectId(id);;
}
if(view.getString1()!=null)
{
bean.setString1(view.getString1());
}
if(view.getString2()!=null)
{
bean.setString2(view.getString2());
}
if(view.getString3()!=null)
{
bean.setString3(view.getString3());
}
if(view.getString4()!=null)
{
bean.setString4(view.getString4());
}
if(view.getString5()!=null)
{
bean.setString5(view.getString5());
}
if(view.getString6()!=null)
{
bean.setString6(view.getString6());
}
if(view.getString7()!=null)
{
bean.setString7(view.getString7());
}
if(view.getString8()!=null)
{
bean.setString8(view.getString8());
}
if(view.getString9()!=null)
{
bean.setString9(view.getString9());
}
if(view.getString10()!=null)
{
bean.setString10(view.getString10());
}
}
if(obj instanceof WTPart) {
WTPart view = (WTPart)obj;
if(view.getName()!=null)
{
bean.setObjectName(view.getName());
}
if(view.getNumber()!=null)
{
bean.setObjectNumber(view.getNumber());
}
if(view.getCreatorName()!=null)
{
bean.setCreator(view.getCreatorName());
}
if(view.getCreateTimestamp()!=null)
{
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Timestamp createtime=view.getCreateTimestamp();
bean.setCreatTime(sdf.format(createtime));
}
if(view.getPersistInfo().getObjectIdentifier().getId()!=0)
{
String strparturl=url+"/app/#ptc1/tcomp/infoPage?oid=OR:wt.part.WTPart:"+view.getPersistInfo().getObjectIdentifier().getId();
bean.setObjectUrl(strparturl);
}
}
}
return bean;
}
之后是调用bean转json的方法:
public static JSONObject getLYHMyFavoriteJson(LYHPart part) throws IOException, WTException, PropertyVetoException
{
JSONObject partjson=new JSONObject();
JsonConfig config=getJsonConfig();
if(part!=null)
{
WTObjectViewBean bean=WTObjectViewToBean(part);
partjson=JSONObject.fromObject(bean,config);//将基本属性转化为json
}
return partjson;
}