View Javadoc

1   /*
2    * Copyright 2004-2006 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  /*
17   * Created on 2005/08/10
18   *
19   * TODO To change the template for this generated file go to
20   * Window - Preferences - Java - Code Style - Code Templates
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  		// update class name
75  		while (itr.hasNext()) {
76  			TgwEntity anEntity = (TgwEntity) itr.next();
77  			String resolvedClassName = resolveJavaClassName(anEntity);
78  			anEntity.setJavaClassName(resolvedClassName);
79  		}
80  
81  		// create each class
82  		itr = entitySet.iterator();
83  		while (itr.hasNext()) {
84  			doCreateClass((TgwEntity) itr.next());
85  		}
86  	}
87  
88  	// [Start] ----- Private Method ----
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 				// entity.setJavaClassName(importedClassName);
102 				// entity.setJavaClass(Class.forName(importedClassName));
103 			} catch (ClassNotFoundException e) {
104 				e.printStackTrace();
105 			}
106 			return;
107 		}
108 
109 		log.info("Creating ..." + entity.getJavaClassName());
110 		// ClassPool cp = ClassPoolUtil.getClassPool();
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 		// entity.setJavaClassName(versionedClassName);
149 		return versionedClassName;
150 	}
151 }