1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.seasar.tuigwaa.database.function;
17
18 import java.util.ArrayList;
19 import java.util.HashMap;
20 import java.util.HashSet;
21 import java.util.Iterator;
22 import java.util.List;
23 import java.util.Map;
24 import java.util.Set;
25
26 import org.hibernate.ObjectNotFoundException;
27 import org.seasar.framework.log.Logger;
28 import org.seasar.tuigwaa.model.common.EntityUtils;
29 import org.seasar.tuigwaa.model.core.TgwAttribute;
30 import org.seasar.tuigwaa.model.core.TgwEntity;
31 import org.seasar.tuigwaa.model.core.impl.FkAttribute;
32 import org.seasar.tuigwaa.model.core.impl.SelfAttribute;
33 import org.seasar.tuigwaa.model.core.impl.SetAttribute;
34
35 import com.isenshi.util.functor.UnaryFunction;
36 import com.isenshi.util.functor.UnaryProcedure;
37
38 public class UpdateExeFunction extends AbstractExeFunction implements
39 UnaryFunction, UnaryProcedure {
40
41 private Logger logger = Logger.getLogger(getClass());
42
43 private Map updateValueMap = new HashMap();
44
45 private List dtoFieldList = new ArrayList();
46
47 private boolean updateOnly;
48
49 public Map getUpdateValueMap() {
50 return updateValueMap;
51 }
52
53 public boolean isUpdateOnly() {
54 return updateOnly;
55 }
56
57 public void setUpdateOnly(boolean updateOnly) {
58 this.updateOnly = updateOnly;
59 }
60
61 public List getDtoFieldList() {
62 return dtoFieldList;
63 }
64
65 public void putUpdateValue(String field, Object value) {
66 updateValueMap.put(field, value);
67 }
68
69 public void addDtoField(String field) {
70 dtoFieldList.add(field);
71 }
72
73 public Object evaluate(Object obj) {
74 if (dtoFieldList.size() == 0) {
75 executeUpdate((Long) obj);
76 } else {
77 executeCustomUpdate(EntityUtils.getId(obj), obj);
78 }
79 return obj;
80 }
81
82 private Object executeUpdate(Long id) {
83 Object obj = getSession().load(getJavaClass(), id);
84 bindUpdateValue(obj);
85 getSession().update(obj);
86
87 return id;
88 }
89
90 private Object executeCustomUpdate(Long id, Object dto) {
91 if (id == null || id.longValue() <= 0) {
92 if(updateOnly){
93 return null;
94 }
95 bindUpdateValue(dto);
96 EntityUtils.bindSelfObject(dto, getEntity());
97 EntityUtils.setUser(dto);
98 EntityUtils.setTimestamp(dto);
99 save(dto);
100 } else {
101 Object obj = getSession().load(getJavaClass(), id);
102 bindUpdateValue(obj);
103 bindDto(obj, dto);
104 EntityUtils.bindSelfObject(obj, getEntity());
105 EntityUtils.setUser(obj);
106 EntityUtils.setTimestamp(obj);
107 getSession().update(obj);
108 }
109 return null;
110 }
111
112 public void run(Object obj) {
113 evaluate(obj);
114 }
115
116 private void bindUpdateValue(Object obj) {
117 Iterator itr = updateValueMap.keySet().iterator();
118 while (itr.hasNext()) {
119 String field = (String) itr.next();
120 Object value = updateValueMap.get(field);
121 Object convertedValue = convertValue(obj,value,field);
122 EntityUtils.setProperty(obj, field, convertedValue);
123 }
124 }
125
126 private void bindDto(Object obj, Object dto) {
127 Iterator itr = dtoFieldList.iterator();
128 while (itr.hasNext()) {
129 String field = (String) itr.next();
130 EntityUtils.copyPropertyCollectionEscape(dto, obj, field);
131 }
132 }
133
134 private Object convertValue(Object valueObject, Object updateValue, String field){
135
136 TgwEntity entity = getEntity();
137 TgwAttribute attr = entity.getField(field);
138
139 Object ret = updateValue;
140
141 if(attr instanceof FkAttribute){
142 FkAttribute fkAttr = (FkAttribute)attr;
143 try{
144 Class refClass = fkAttr.getRefClass();
145 Long refId = new Long(Long.parseLong(updateValue.toString()));
146 ret = getSession().load(refClass,refId);
147 }catch(ClassNotFoundException cnfe){
148
149 return null;
150 }catch(ObjectNotFoundException onfe){
151 logger.log("ETGW1101",new Object[]{getName()});
152 return null;
153 }
154 }else if(attr instanceof SetAttribute){
155
156 SetAttribute setAttr = (SetAttribute) attr;
157 TgwEntity refEntity = setAttr.getRefEntity();
158
159 Object setObj = EntityUtils.getProperty(valueObject,setAttr);
160 Set refSet = null;
161 if(setObj != null && setObj instanceof Set){
162 refSet = (Set) setObj;
163 }else{
164 refSet = new HashSet();
165 }
166 String strs = updateValue.toString();
167 if(strs != null){
168 String[] refValues = strs.split(",");
169 for(int i=0;i<refValues.length;i++){
170 Object refObj = refEntity.newInstance();
171 EntityUtils.setProperty(refObj,setAttr.getName(),refValues[i]);
172 refSet.add(refObj);
173 }
174 }
175 ret = refSet;
176 }else if(attr instanceof SelfAttribute){
177 Class refClass = entity.getJavaClass();
178 Long refId = new Long(Long.parseLong(updateValue.toString()));
179 try{
180 ret = getSession().load(refClass,refId);
181 }catch(ObjectNotFoundException onfe){
182 logger.log("ETGW1101",new Object[]{getName()});
183 return null;
184 }
185 }
186 return ret;
187 }
188
189
190 }