1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.seasar.tuigwaa.model;
23
24 import java.util.Collection;
25 import java.util.Iterator;
26
27 import javassist.CannotCompileException;
28 import javassist.ClassPool;
29
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32 import org.seasar.framework.util.ClassPoolUtil;
33 import org.seasar.tuigwaa.model.common.DomainUtils;
34 import org.seasar.tuigwaa.model.common.EntityUtils;
35 import org.seasar.tuigwaa.model.common.PojoClassGenerator;
36 import org.seasar.tuigwaa.model.common.TgwJavaClassUtils;
37 import org.seasar.tuigwaa.model.core.TgwAttribute;
38 import org.seasar.tuigwaa.model.core.TgwDomain;
39 import org.seasar.tuigwaa.model.core.TgwEntity;
40 import org.seasar.tuigwaa.model.core.impl.DateAttribute;
41 import org.seasar.tuigwaa.system.Constants;
42 import org.seasar.tuigwaa.util.TgwNameUtils;
43
44 import com.isenshi.util.UniqueNameGenerator;
45
46 /***
47 * @author nishioka
48 */
49 public class JavaClassServiceImpl implements JavaClassService {
50
51 private UniqueNameGenerator nameGenerator = new UniqueNameGenerator();
52
53 private Log log = LogFactory.getLog(getClass());
54
55 public void createPackage(TgwDomain domain) {
56 alterClass(domain.getEntityList());
57 }
58
59 public void createClass(TgwEntity entity) {
60 String resolvedClassName = resolveJavaClassName(entity);
61 entity.setJavaClassName(resolvedClassName);
62 doCreateClass(entity);
63 }
64
65 public void alterClass(TgwEntity entity) {
66 Collection refEntitySet = DomainUtils.getReferencedEntities(entity);
67 refEntitySet.add(entity);
68 alterClass(refEntitySet);
69 }
70
71 public void alterClass(Collection entitySet) {
72 Iterator itr = entitySet.iterator();
73
74
75 while (itr.hasNext()) {
76 TgwEntity anEntity = (TgwEntity) itr.next();
77 String resolvedClassName = resolveJavaClassName(anEntity);
78 anEntity.setJavaClassName(resolvedClassName);
79 }
80
81
82 itr = entitySet.iterator();
83 while (itr.hasNext()) {
84 doCreateClass((TgwEntity) itr.next());
85 }
86 }
87
88
89
90 private void doCreateClass(TgwEntity entity) {
91 String importedClassName = entity.getImportedClassName();
92 if (importedClassName != null) {
93 log.info("Import ..." + entity.getImportedClassName());
94
95 try {
96 Class checkedClass = TgwJavaClassUtils
97 .checkClass(importedClassName);
98 entity.setJavaClass(checkedClass);
99 entity.setJavaClassName(checkedClass.getName());
100
101
102
103 } catch (ClassNotFoundException e) {
104 e.printStackTrace();
105 }
106 return;
107 }
108
109 log.info("Creating ..." + entity.getJavaClassName());
110
111 ClassPool cp = ClassPoolUtil.getClassPool(null);
112
113 PojoClassGenerator generator = new PojoClassGenerator(cp, entity
114 .getJavaClassName());
115
116 Iterator itr = entity.getFieldIterator();
117
118 try {
119 generator
120 .addProperty(Constants.ENTITY_BUILTIN_ID, "java.lang.Long");
121 while (itr.hasNext()) {
122 TgwAttribute attr = (TgwAttribute) itr.next();
123 String name = attr.getName();
124 String javaType = EntityUtils.toJavaType(attr);
125 generator.addProperty(name, javaType);
126
127 if (attr instanceof DateAttribute) {
128 String[] parts = TgwNameUtils.toDateParts(name);
129 for (int i = 0; i < parts.length; i++) {
130 generator.addProperty(parts[i], "java.lang.Integer");
131 }
132 }
133 }
134 } catch (CannotCompileException e) {
135 e.printStackTrace();
136 throw new RuntimeException(e);
137 } catch (ClassNotFoundException e) {
138 e.printStackTrace();
139 throw new RuntimeException(e);
140 }
141 Class generatedClass = generator.toClass();
142 entity.setJavaClass(generatedClass);
143 }
144
145 private String resolveJavaClassName(TgwEntity entity) {
146 String pureClassName = TgwNameUtils.toPureClassName(entity);
147 String versionedClassName = nameGenerator.nextUniqueName(pureClassName);
148
149 return versionedClassName;
150 }
151 }