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.Date;
19 import java.util.Random;
20
21 import org.hibernate.ObjectNotFoundException;
22 import org.hibernate.Session;
23 import org.seasar.framework.container.S2Container;
24 import org.seasar.framework.container.factory.SingletonS2ContainerFactory;
25 import org.seasar.tuigwaa.database.DynaSessionFactory;
26 import org.seasar.tuigwaa.model.DAOService;
27 import org.seasar.tuigwaa.model.ModelService;
28 import org.seasar.tuigwaa.model.common.EntityUtils;
29 import org.seasar.tuigwaa.model.core.TgwEntity;
30 import org.seasar.tuigwaa.util.TgwContext;
31
32 public abstract class AbstractExeFunction implements DaoMethod {
33
34 private Random random = new Random(new Date().getTime());
35
36 private String name_;
37
38 private String schema_;
39
40 private String entityName_;
41
42 private String domainName;
43
44 public final String getName() {
45 return name_;
46 }
47
48 public final void setName(String name) {
49 this.name_ = name;
50 }
51
52 public final String getEntityName() {
53 return entityName_;
54 }
55
56 public final void setEntityName(String entityName) {
57 this.entityName_ = entityName;
58 }
59
60 /***
61 * @deprecated
62 */
63 public final String getSchema() {
64 return TgwContext.getDomainName();
65
66 }
67
68 /***
69 * @deprecated
70 */
71 public final void setSchema(String schema) {
72 this.schema_ = schema;
73 }
74
75 protected final Session getSession() {
76
77 return getSessionFromFactory(domainName);
78 }
79
80 public void setDomainName(String domainName) {
81 this.domainName = domainName;
82 }
83
84 private final Session getSessionFromFactory(String schema) {
85 S2Container container = SingletonS2ContainerFactory.getContainer();
86 DynaSessionFactory sessionFactory = (DynaSessionFactory) container
87 .getComponent(DynaSessionFactory.class);
88 return sessionFactory.getSession(schema);
89 }
90
91 public final TgwEntity getEntity() {
92 S2Container container = SingletonS2ContainerFactory.getContainer();
93 ModelService service = (ModelService) container
94 .getComponent(ModelService.class);
95
96 String schema = domainName;
97 String entityName = getEntityName();
98 TgwEntity entity = service.getEntity(schema, entityName);
99 return entity;
100 }
101
102 protected final TgwEntity getAggregationEntity() {
103 S2Container container = SingletonS2ContainerFactory.getContainer();
104 DAOService daoProvider = (DAOService) container
105 .getComponent(DAOService.class);
106 TgwEntity entity = getEntity();
107 return daoProvider.createProjectionEntity(entity, getName());
108 }
109
110 protected final Class getJavaClass() {
111 return getEntity().getJavaClass();
112 }
113
114 protected final void save(Object obj) {
115 if (getEntity().isRandomId()) {
116 long id = getNextId(nextLong());
117 EntityUtils.setId(obj, new Long(id));
118 getSession().save(obj);
119 } else {
120 getSession().save(obj);
121 }
122 }
123
124 protected long getNextId(long l) {
125 Object obj = null;
126 try {
127 obj = getSession().load(getJavaClass(), new Long(l));
128 } catch (ObjectNotFoundException e) {
129 }
130 if (obj != null) {
131 return getNextId(nextLong());
132 }
133 return l;
134 }
135
136 protected long nextLong() {
137 long l = random.nextLong();
138 if (l == 0) {
139 return 1;
140 } else if (l < 0) {
141 return -l;
142 } else {
143 return l;
144 }
145 }
146
147 public Object evaluate() {
148 return null;
149 }
150
151 public Object evaluate(Object obj) {
152 return null;
153 }
154
155 public Object evaluate(Object left, Object right) {
156 return null;
157 }
158 }