一个实体类要被增加包括几个部分:
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
本文地址:http://com.8s8s.com/it/it14462.htm