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  package org.seasar.tuigwaa.database;
17  
18  import java.util.Iterator;
19  
20  import org.dom4j.Document;
21  import org.dom4j.Element;
22  import org.dom4j.dom.DOMDocument;
23  import org.dom4j.dom.DOMElement;
24  import org.seasar.tuigwaa.model.core.TgwAttribute;
25  import org.seasar.tuigwaa.model.core.TgwEntity;
26  import org.seasar.tuigwaa.model.core.impl.DateAttribute;
27  import org.seasar.tuigwaa.model.core.impl.FileAttribute;
28  import org.seasar.tuigwaa.model.core.impl.FkAttribute;
29  import org.seasar.tuigwaa.model.core.impl.SecurityAttribute;
30  import org.seasar.tuigwaa.model.core.impl.SelfAttribute;
31  import org.seasar.tuigwaa.model.core.impl.SetAttribute;
32  import org.seasar.tuigwaa.model.core.impl.StringAttribute;
33  import org.seasar.tuigwaa.system.Constants;
34  import org.seasar.tuigwaa.util.TgwNameUtils;
35  import org.seasar.tuigwaa.util.TgwUtils;
36  
37  public class HbmDocumentFactory {
38  
39  	public static Document create(String schema, TgwEntity entity) {
40  		Document doc = new DOMDocument();
41  		Element rootElem = bindRootElement(doc, schema, entity);
42  		Element classElem = bindClassElement(rootElem, entity);
43  		bindIdElement(classElem, entity);
44  
45  		Iterator itr = entity.getFieldIterator();
46  
47  		while (itr.hasNext()) {
48  			TgwAttribute field = (TgwAttribute) itr.next();
49  			if (field instanceof FkAttribute) {
50  				bindManyToOne(classElem, (FkAttribute) field);
51  			} else if (field instanceof SetAttribute) {
52  				bindOneToMany(classElem, (SetAttribute) field);
53  			} else if (field instanceof SelfAttribute) {
54  				bindManyToOne(classElem, (SelfAttribute) field);
55  			} else {
56  				bindProperty(classElem, field);
57  			}
58  		}
59  
60  		return doc;
61  	}
62  
63  	private static Element bindRootElement(Document doc, String schema,
64  			TgwEntity entity) {
65  		Element rootElem = new DOMElement("hibernate-mapping");
66  		if (schema != null && schema.length() > 0) {
67  			rootElem.addAttribute("schema", schema);
68  		}
69  		rootElem.addAttribute("auto-import", "false");
70  		rootElem.addAttribute("default-lazy", "false");
71  		doc.add(rootElem);
72  		return rootElem;
73  	}
74  
75  	private static Element bindClassElement(Element rootElem, TgwEntity entity) {
76  		Element classElem = new DOMElement("class");
77  		classElem.addAttribute("name", entity.getJavaClassName());
78  		classElem.addAttribute("table", TgwUtils.toTableName(entity));
79  		rootElem.add(classElem);
80  		return classElem;
81  	}
82  
83  	private static void bindIdElement(Element classElem, TgwEntity entity) {
84  		Element idElem = new DOMElement("id");
85  		idElem.addAttribute("name", Constants.ENTITY_BUILTIN_ID);
86  
87  		String pkColumnName = entity.getPrimaryKeyColumnName();
88  
89  		if (pkColumnName == null) {
90  			pkColumnName = Constants.ENTITY_BUILTIN_ID;
91  		}
92  
93  		idElem.addAttribute("column", pkColumnName);
94  
95  		idElem.addAttribute("type", "java.lang.Long");
96  
97  		Element generatorElem = new DOMElement("generator");
98  		if (entity.isRandomId()) {
99  			generatorElem.addAttribute("class", "assigned");
100 		} else {
101 			generatorElem.addAttribute("class", "increment");
102 		}
103 		classElem.add(idElem);
104 		idElem.add(generatorElem);
105 	}
106 
107 	private static void bindProperty(Element classElem, TgwAttribute attr) {
108 		String hibernateType = TgwUtils.toHibernateType(attr);
109 		String columnName = attr.getName();
110 
111 		Element propElem = new DOMElement("property");
112 		propElem.addAttribute("name", columnName);
113 		propElem.addAttribute("type", hibernateType);
114 		propElem.addAttribute("column", columnName);
115 		propElem.addAttribute("not-null", "false");
116 		bindCustomAttribute(propElem, attr);
117 		classElem.add(propElem);
118 
119 		if (attr instanceof DateAttribute) {
120 			String[] parts = TgwNameUtils.toDateParts(columnName);
121 			for (int i = 0; i < parts.length; i++) {
122 				bindExtraProperty(classElem, parts[i], "integer");
123 			}
124 		}
125 	}
126 
127 	private static void bindExtraProperty(Element classElem, String columnName,
128 			String hibernateType) {
129 		Element propElem = new DOMElement("property");
130 		propElem.addAttribute("name", columnName);
131 		propElem.addAttribute("type", hibernateType);
132 		propElem.addAttribute("column", columnName);
133 		propElem.addAttribute("not-null", "false");
134 		classElem.add(propElem);
135 	}
136 
137 	private static void bindCustomAttribute(Element propElem, TgwAttribute attr) {
138 		if (attr.getName().equals(Constants.ENTITY_BUILTIN_CREATIONUSER)) {
139 			SecurityAttribute security = (SecurityAttribute) attr;
140 			if (security.isUnique()) {
141 				propElem.addAttribute("unique", "true");
142 			}
143 			propElem.addAttribute("update", "false");
144 		} else if (attr.getName().equals(Constants.ENTITY_BUILTIN_CREATIONDATE)) {
145 			propElem.addAttribute("update", "false");
146 		} else if (attr instanceof FileAttribute) {
147 			// propElem.addAttribute("update", "false");
148 		} else if (attr instanceof StringAttribute) {
149 			StringAttribute sattr = (StringAttribute) attr;
150 			int length = sattr.getLength();
151 			if (length > 0) {
152 				propElem.addAttribute("length", "" + length);
153 			}
154 		}
155 	}
156 
157 	private static void bindManyToOne(Element classElem, SelfAttribute selfField) {
158 		String refClassName = selfField.getEntity().getJavaClassName();
159 		String columnName = selfField.getName();
160 		bindManyToOne(classElem, refClassName, columnName, false);
161 	}
162 
163 	private static void bindManyToOne(Element classElem, FkAttribute fkField) {
164 		String refClassName = fkField.getRefEntity().getJavaClassName();
165 		String columnName = fkField.getName();
166 		boolean inverseFlag = fkField.getInverseField() != null;
167 		bindManyToOne(classElem, refClassName, columnName, inverseFlag);
168 	}
169 
170 	private static void bindManyToOne(Element classElem, String refClassName,
171 			String columnName, boolean inverseFlag) {
172 		Element manyToOneElem = new DOMElement("many-to-one");
173 		manyToOneElem.addAttribute("class", refClassName);
174 		manyToOneElem.addAttribute("name", columnName);
175 		manyToOneElem.addAttribute("column", columnName);
176 		manyToOneElem.addAttribute("not-found", "ignore" );
177 				
178 		// manyToOneElem.addAttribute("cascade", "all-delete-orphan");
179 
180 		classElem.add(manyToOneElem);
181 	}
182 
183 	private static void bindOneToMany(Element classElem, SetAttribute setField) {
184 
185 		String name = setField.getInverseField().getName();
186 		String refClassName = setField.getRefEntity().getJavaClassName();
187 		String columnName = setField.getName();
188 
189 		Element setElem = new DOMElement("set");
190 		setElem.addAttribute("name", columnName);
191 
192 		// if (EntityUtils.manyToManyFlag(setField)) {
193 		setElem.addAttribute("cascade", "all-delete-orphan");
194 		// }
195 
196 		setElem.addAttribute("inverse", "true");
197 
198 		Element keyElem = new DOMElement("key");
199 		keyElem.addAttribute("column", name);
200 		keyElem.addAttribute("on-delete", "cascade");
201 
202 		Element manyToOneElem = new DOMElement("one-to-many");
203 		manyToOneElem.addAttribute("class", refClassName);
204 
205 		classElem.add(setElem);
206 		setElem.add(keyElem);
207 		setElem.add(manyToOneElem);
208 	}
209 }