分享
 
 
 

DSWF自动生成代码之 Add设计

王朝java/jsp·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

一个实体类要被增加包括几个部分:

1、many-to-one的部分。

这里有一个逻辑就是如果要单独增加一个“人”那么他所属的“部门”必须是已经添加好的。

要么这个“人”也可以是多重记录操作,同时也增加一个新“部门”。对于单独添加的“人”的“部门”采用什么方式表现,用户可以选择“下拉框”、“弹出窗口”、“树形结构”。

这里前台会提交一个parent的id过来,后台用这个id对parent进行查询。然后放到这个对象里面去。

2、one-to-one的部分。

one2one类似于many2one,这里会加判断如果没有提交id过来那么就先save一个。

one2one有一个属性可以判断外键加在哪一边,有一个保存的先后顺序的问题。

3、one-to-many的部分。

相当于给人增加“通讯录”,一个人有多个通讯录。等人增加之后再加这个。

4、many-to-many的部分。

这里要求many部分必须是已经添加好的。

(如果有不是添加好的需求再说吧)

4、其他部分。

暂时不支持。

#*

1 get the data from the from

2 check the relationship between the objects

the typical example is :

a class with the children, with the one-to-one class, with the parent class (means a many-to-one parent)

with the children

children are in an array

with the one-to-one class

a one-to-one class has one direct fk, if the fk is in this class then this class is a child

else is a parent.

with the parent

a many-to-one parent must exits in the database not need to save here. just search from db.

1,the 'parent' with be submit in this object, need to search it out first.

2,set the parent back to the object.

3,if this is a 'one2one' parent:

set the child to null,save the object, reset the object to one2one child. save child.

when reset the object the setter method should specify the id property.

4,if this is a 'one2one' child:

search the parent,reset the parent to the object,save this.

5,give the children the object,save every child.

3 redirect to the toadd action, prepare a new add.

4 a file submit with the data, rules:

user.logoImg

must have a form-property "_logoImgFile"

if the user is multi recodes

must have a form-property "_logoImgFile[]"

the user must override the deal with file methods.

but i don't set the value to the ref field, it shoud be done at the beforeSaveXXX() method.

*#

#set ($form = $action.getForm())

#* create multi records (multi record) *#

#macro(createMultiRecords $field)

protected void createMultiRecords${util.getMethodByProperty($field.getProperty())}(${field.getClassType()}[] ${field.getProperty()}s){

#* a multi records with no parent *#

for (int i=0;i<${field.getProperty()}s.length;i++) {

$field.getClassType() ${field.getProperty()} = ${field.getProperty()}s[i];

#foreach ($parent in $util.getManyToOne($field.getClassType()))

$parent.getClassType() $parent.getProperty() = ${field.getProperty()}.${util.getGetterMethodByProperty($parent.getColumnProperty())}();

if ($parent.getProperty()!=null && ${parent.getProperty()}.getId()!=null) {

$parent.getProperty() = (${parent.getClassType()})${parent.getProperty()}.searchByKey();

${field.getProperty()}.${util.getSetterMethodByProperty($parent.getProperty())}(${parent.getProperty()});

}

#end

${field.getProperty()}.save();

}

}

#end

#* create children marco *#

#macro(createChildren $field)

protected void createChildren${util.getMethodByProperty($field.getColumnProperty())}(${field.getClassType()}[] ${field.getColumnProperty()}s){

## a multi records with parent ##

#set($parent = $field.getParent())

$parent.getClassType() $parent.getProperty() = this.getEntity${util.getMethodByProperty($parent.getProperty())}();

#if ($util.isManyToMany($field))

## the many2many type should be records that already persist ##

List ${field.getColumnProperty()}list = new ArrayList();

for (int i=0;i<${field.getColumnProperty()}s.length;i++) {

$field.getClassType() ${field.getColumnProperty()} = ${field.getColumnProperty()}s[i];

${field.getColumnProperty()} = (${field.getClassType()})${field.getColumnProperty()}.searchByKey();

${field.getColumnProperty()}list.add(${field.getColumnProperty()});

}

${parent.getProperty()}.${util.getSetterMethodByProperty(${field.getColumnProperty()})}(${field.getColumnProperty()}list);

${parent.getProperty()}.update();

#else

## the many2one type should be a blank object not persist ##

for (int i=0;i<${field.getColumnProperty()}s.length;i++) {

$field.getClassType() ${field.getColumnProperty()} = ${field.getColumnProperty()}s[i];

${field.getColumnProperty()}.${util.getSetterMethodByProperty(${parent.getProperty()})}($parent.getProperty());

${field.getColumnProperty()}.save();

}

#end

// create children

}

#end

#* create entity marco *#

#macro (createEntity $field)

// in create entity

protected void createEntity${util.getMethodByProperty($field.getProperty())}($field.getClassType() $field.getProperty()) {

## set every mant2one parent to the object ##

#foreach ($parent in $util.getManyToOne($field.getClassType()))

$parent.getClassType() $parent.getProperty() = ${field.getProperty()}.${util.getGetterMethodByProperty(${parent.getProperty()})}();

if ($parent.getProperty()!=null && ${parent.getProperty()}.getId()!=null) {

$parent.getProperty() = (${parent.getClassType()})${parent.getProperty()}.searchByKey();

${field.getProperty()}.${util.getSetterMethodByProperty($parent.getProperty())}(${parent.getProperty()});

}

#end

## deal with one2one ##

#foreach (child in $util.getOneToOne($field.getClassType()))

$child.getClassType() $child.getProperty() = ${field.getProperty()}.${util.getGetterMethodByProperty($child.getProperty())}();

if ($child.getProperty().getId()!=null) {

## set every one2one child to the object ##

$child.getProperty() = (${child.getClassType()})${field.getProperty()}.${util.getGetterMethodByProperty($child.getProperty())}().searchByKey();

${field.getProperty()}.${util.getSetterMethodByProperty($child.getProperty())}(${child.getProperty()});

}else{

## clear the one2one children ##

${field.getProperty()}.${util.getSetterMethodByProperty($child.getProperty())}(null);

}

#end

this.beforeSave${util.getMethodByProperty($field.getProperty())}(${field.getProperty()});

${field.getProperty()}.save();

## save the one2one children ##

#foreach ($child in $util.getOneToOne($field.getClassType()))

${child.getProperty()}.${util.getSetterMethodByProperty($child.getInverseProperty())}($child.getInverseProperty());

this.beforeSave${util.getMethodByProperty($child.getProperty())}($child.getProperty());

${child.getProperty()}.save();

#end

this.httpServletRequest.setAttribute("$field.getProperty()",$field.getProperty());

//create entity

}

protected void beforeSave${util.getMethodByProperty($field.getProperty())}($field.getClassType() $field.getProperty()) {

## may deal with the file upload operation and etc. ##

//before save entity

}

#foreach ($child in $util.getOneToOne($field.getClassType()))

protected void beforeSave${util.getMethodByProperty($child.getProperty())}($child.getClassType() $child.getProperty()){

## may deal with the file upload operation and etc. ##

//before save child

}

#end

private final $field.getClassType() getEntity${util.getMethodByProperty($field.getProperty())}() {

return ($field.getClassType())this.httpServletRequest.getAttribute("$field.getProperty()");

}

#end

public ActionForward doAction() throws Exception {

#* top level field contains hbm class and array type multi-records *#

#foreach ($field in $util.getTopLevelFields($form.getRefFields()))

#set ($formGetter = ${util.getGetterMethodByProperty($field.getProperty())})

$field.getClassType() $field.getProperty() = this.${formGetter}();

#if ($hbmclass == $field.getType())#* the hbm type of field *#

this.createEntity${util.getMethodByProperty($field.getProperty())}($field.getProperty());

#elseif ($hbmarray == $field.getType())

this.createMultiRecords${util.getMethodByProperty($field.getProperty())}($field.getProperty());

#end

#end

#* second level field contains sub array *#

#foreach ($field in $util.getArrayLevelFields($form.getRefFields()))

${field.getClassType()}[] $field.getColumnProperty()= this.${util.getGetterMethodByProperty($field.getColumnProperty())}();

this.createChildren${util.getMethodByProperty($field.getColumnProperty())}($field.getColumnProperty());

#end

return this.getForward();

}

protected ActionForward getForward(){

return this.actionMapping.findForward("toadd");

}

#* get data from the form *#

#foreach ($field in $util.getTopLevelFields($form.getRefFields()))

#if ($hbmarray == $field.getType())

protected ${field.getClassType()}[] ${util.getGetterMethodByProperty($field.getProperty())}(){

DynaValidatorForm _form = (DynaValidatorForm) actionForm;

return (${field.getClassType()}[])_form.get("${field.getProperty()}");

}// array type getter

#elseif ($hbmclass == $field.getType())

protected $field.getClassType() ${util.getGetterMethodByProperty($field.getProperty())}(){

DynaValidatorForm _form = (DynaValidatorForm) actionForm;

return (${field.getClassType()})_form.get("${field.getProperty()}");

}// class type getter

#end

#end

#* get data from the form array *#

#foreach ($field in $util.getArrayLevelFields($form.getRefFields()))

protected ${field.getClassType()}[] ${util.getGetterMethodByProperty($field.getColumnProperty())}(){

DynaValidatorForm _form = (DynaValidatorForm) actionForm;

return (${field.getClassType()}[])_form.get("${field.getColumnProperty()}");

}

#end

## create the top level methods ##

#foreach ($field in $util.getTopLevelFields($form.getRefFields()))

#if ($hbmclass == $field.getType())

// call create entity

#createEntity($field)

#elseif ($hbmarray == $field.getType())

// call create children

#createMultiRecords($field)

#end

#end

## create the sub level methods ##

#foreach ($field in $util.getArrayLevelFields($form.getRefFields()))

#createChildren($field)

#end

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有