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/13
18   */
19  package org.seasar.tuigwaa.model;
20  
21  import java.beans.PropertyDescriptor;
22  import java.util.ArrayList;
23  import java.util.Collection;
24  import java.util.HashMap;
25  import java.util.HashSet;
26  import java.util.Iterator;
27  import java.util.List;
28  import java.util.Map;
29  import java.util.Set;
30  
31  import javassist.CannotCompileException;
32  
33  import org.apache.commons.beanutils.PropertyUtils;
34  import org.seasar.tuigwaa.database.DynaDatabaseService;
35  import org.seasar.tuigwaa.model.DAOServiceImpl.FunctionSet;
36  import org.seasar.tuigwaa.model.common.DomainUtils;
37  import org.seasar.tuigwaa.model.common.EntityInfo;
38  import org.seasar.tuigwaa.model.common.EntityResource;
39  import org.seasar.tuigwaa.model.common.EntityUtils;
40  import org.seasar.tuigwaa.model.core.TgwAttribute;
41  import org.seasar.tuigwaa.model.core.TgwDomain;
42  import org.seasar.tuigwaa.model.core.TgwEntity;
43  import org.seasar.tuigwaa.model.core.impl.SetAttribute;
44  import org.seasar.tuigwaa.model.core.impl.TgwDomainImpl;
45  import org.seasar.tuigwaa.model.core.impl.TgwEntityImpl;
46  import org.seasar.tuigwaa.system.Constants;
47  import org.seasar.tuigwaa.system.TgwServiceException;
48  import org.seasar.tuigwaa.system.TgwRuntimeException;
49  
50  import com.isenshi.util.UniqueNameGenerator;
51  
52  /***
53   * @author nishioka
54   */
55  public class ModelServiceImpl implements ModelService {
56  
57  	private Map generatorMap = new HashMap();
58  
59  	private Map domainMap = new HashMap();
60  
61  	private JavaClassService javaService_;
62  
63  	private DynaDatabaseService dbService_;
64  
65  	private DAOService daoService_;
66  
67  	private EntityResource resource_;
68  
69  	public ModelServiceImpl(JavaClassService javaService,
70  			DynaDatabaseService dbService, DAOService daoService,
71  			EntityResource resource) {
72  		this.javaService_ = javaService;
73  		this.dbService_ = dbService;
74  		this.daoService_ = daoService;
75  		this.resource_ = resource;
76  	}
77  
78  	public void createDomain(String domainName) {
79  		if (getDomain(domainName) != null) {
80  			return;
81  		}
82  		TgwDomain domain = new TgwDomainImpl(domainName);
83  		putDomain(domainName, domain);
84  		try {
85  			dbService_.createSchema(domainName);
86  		} catch (Exception e) {
87  			e.printStackTrace();
88  		}
89  	}
90  
91  	public void deleteDomain(String domainName) {
92  		TgwDomain domain = (TgwDomain) domainMap.remove(domainName);
93  		if (domain == null) {
94  			return;
95  		}
96  		List list = DomainUtils.sortByNoReference(domain);
97  		String[] entityNames = new String[list.size()];
98  
99  		Iterator itr = list.iterator();
100 		for (int i = 0; itr.hasNext(); i++) {
101 			entityNames[i] = ((TgwEntity) itr.next()).getName();
102 		}
103 		try {
104 			// dbService_.dropTables(domainName, entityNames);
105 			dbService_.dropSchema(domainName);
106 		} catch (Exception e) {
107 			e.printStackTrace();
108 		}
109 	}
110 
111 	public void copyDomain(String srcDomainName, String destDomainName) {
112 		resource_.copy(srcDomainName, destDomainName);
113 	}
114 
115 	public List readDatabase(String domainName) {
116 		List list = dbService_.readTables(domainName);
117 		createEntities(domainName, list);
118 		return list;
119 	}
120 
121 	public TgwEntity readJavaClass(String domainName, String entityName,
122 			Class clazz) {
123 		TgwEntity entity = new TgwEntityImpl(domainName, entityName);
124 		entity.setDisplayName(entityName);
125 		entity.setImportedClassName(clazz.getName());
126 
127 		PropertyDescriptor[] descs = PropertyUtils
128 				.getPropertyDescriptors(clazz);
129 		for (int i = 0; i < descs.length; i++) {
130 			PropertyDescriptor desc = descs[i];
131 			if (desc.getReadMethod() != null && desc.getWriteMethod() != null) {
132 				Class propClazz = desc.getPropertyType();
133 				String propName = desc.getName();
134 
135 				if (Constants.ENTITY_BUILTIN_ID.equals(propName)) {
136 					continue;
137 				} else if (Constants.ENTITY_BUILTIN_CREATIONUSER
138 						.equals(propName)) {
139 					EntityUtils.addCreationUserAttribute(entity, false);
140 					continue;
141 				} else if (Constants.ENTITY_BUILTIN_MODIFICATIONUSER
142 						.equals(propName)) {
143 					EntityUtils.addModificationUserAttribute(entity);
144 					continue;
145 				} else if (Constants.ENTITY_BUILTIN_CREATIONDATE
146 						.equals(propName)) {
147 					EntityUtils.addCreationDateAttribute(entity);
148 					continue;
149 				} else if (Constants.ENTITY_BUILTIN_MODIFICATIONDATE
150 						.equals(propName)) {
151 					EntityUtils.addModificationDateAttribute(entity);
152 					continue;
153 				}
154 
155 				TgwAttribute attr = EntityUtils.toTgwAttribute(propClazz);
156 
157 				if (attr == null) {
158 					continue;
159 				}
160 
161 				attr.setName(propName);
162 				attr.setDisplayName(propName);
163 				entity.addField(attr);
164 
165 			}
166 		}
167 
168 		return entity;
169 	}
170 
171 	public void dropEntity(String domainName, String entityName)
172 			throws Exception {
173 
174 		List crossRefList = new ArrayList();
175 
176 		TgwEntity entity = getEntity(domainName, entityName);
177 		Collection entitySet = DomainUtils.getReferencedEntities(entity);
178 		entitySet.add(entity);
179 		List sortedList = DomainUtils.sortByReference(entitySet);
180 		String[] entityNames = new String[sortedList.size()];
181 		Iterator itr = sortedList.iterator();
182 		for (int i = 0; i < entityNames.length; i++) {
183 			TgwEntity anEntity = (TgwEntity) itr.next();
184 			String aDomainName = anEntity.getDomainName();
185 			String anEntityName = anEntity.getName();
186 			removeEntity(aDomainName, anEntityName);
187 			entityNames[i] = anEntityName;
188 			crossRefList.addAll(EntityUtils.getCrossRefList(anEntity));
189 		}
190 
191 		removeCrossRefList(crossRefList);
192 
193 		dbService_.dropTables(domainName, entityNames);
194 		doSave(domainName);
195 	}
196 
197 	public void createEntity(TgwEntity entity) throws ClassNotFoundException,
198 			CannotCompileException {
199 		javaService_.createClass(entity);
200 		dbService_.createTable(entity);
201 		putEntity(entity);
202 		doSave(entity.getDomainName());
203 	}
204 
205 	public Collection alterEntity(TgwEntity entity,
206 			boolean isChangedEntityStructure) throws Exception {
207 		Collection refEntitySet = DomainUtils.getReferencedEntities(entity);
208 		// refEntitySet.addAll(DomainUtils.getCrossReferenceEntities(entity));
209 		refEntitySet.add(entity);
210 		if (isChangedEntityStructure) {
211 			javaService_.alterClass(refEntitySet);
212 			dbService_.alterTable(entity); // don't need setting refEntitySet
213 		}
214 		doSave(entity.getDomainName());
215 		return refEntitySet;
216 	}
217 
218 	public TgwEntity getEntity(String domainName, String entityName) {
219 		TgwDomain domain = getDomain(domainName);
220 		if (domain == null) {
221 			throw new TgwRuntimeException("ETGW2001",
222 					new Object[] { domainName });
223 		}
224 		return domain.getEntity(entityName);
225 	}
226 
227 	public TgwEntity getEntityByDisplayName(String domainName,
228 			String displayName) {
229 		TgwDomain domain = getDomain(domainName);
230 		if (domain == null) {
231 			throw new TgwRuntimeException("ETGW2002",
232 					new Object[] { domainName });
233 		}
234 		return domain.getEntityByDisplayName(displayName);
235 	}
236 
237 	public Iterator getEntityNameInterator(String domainName) {
238 		TgwDomain domain = getDomain(domainName);
239 		return domain.getEntityNameIterator();
240 	}
241 
242 	public Iterator getEntityIterator(String domainName) {
243 		TgwDomain domain = getDomain(domainName);
244 		if (domain != null) {
245 			return domain.getEntityList().iterator();
246 		}
247 		return null;
248 	}
249 
250 	public List getEntityList(String domainName) {
251 		TgwDomain domain = getDomain(domainName);
252 		if (domain != null) {
253 			return domain.getEntityList();
254 		}
255 		return null;
256 	}
257 
258 	public EntityInfo createEntityInfo(String domainName, String entityName) {
259 		TgwEntity entity = getEntity(domainName, entityName);
260 		return doCreateEntityInfo(entity);
261 	}
262 
263 	public List createEntityInfoList(String domainName) {
264 		List entityInfoList = new ArrayList();
265 		Iterator itr = getEntityIterator(domainName);
266 		if (itr != null) {
267 			while (itr.hasNext()) {
268 				TgwEntity entity = (TgwEntity) itr.next();
269 				EntityInfo info = doCreateEntityInfo(entity);
270 				entityInfoList.add(info);
271 			}
272 		}
273 		return entityInfoList;
274 	}
275 
276 	public void saveDomain(String domainName) {
277 		TgwDomain domain = getDomain(domainName);
278 		doSave(domain);
279 	}
280 
281 	public void loadDomain(String domainName) {
282 		if (getDomain(domainName) != null
283 				&& getDomain(domainName).getEntityList().size() != 0) {
284 			return;
285 		}
286 		TgwDomain domain = resource_.load(domainName);
287 		if (domain == null) {
288 			domain = new TgwDomainImpl(domainName);
289 		} else {
290 			doCreateEntityAll(domain);
291 		}
292 		putDomain(domainName, domain);
293 	}
294 
295 	public TgwDomain getDomain(String domainName) {
296 		return (TgwDomain) domainMap.get(domainName);
297 	}
298 
299 	public List getNewRefList(TgwEntity fromEntity) {
300 		String domainName = fromEntity.getDomainName();
301 		TgwDomain domain = getDomain(domainName);
302 		if (domain != null) {
303 			return domain.getNewRefList(fromEntity);
304 		} else {
305 			return new ArrayList();
306 		}
307 	}
308 
309 	public void updateEntity(TgwEntity entity) throws TgwServiceException {
310 
311 		doSave(entity.getDomainName());
312 	}
313 
314 	// [Start] ----- Private Method -----
315 
316 	private void doSave(String domainName) {
317 		doSave(getDomain(domainName));
318 	}
319 
320 	private void doSave(TgwDomain domain) {
321 		resource_.save(domain);
322 	}
323 
324 	private void putEntity(TgwEntity entity) {
325 		String domainName = entity.getDomainName();
326 		TgwDomainImpl domain = (TgwDomainImpl) getDomain(domainName);
327 		domain.addEntity(entity);
328 
329 	}
330 
331 	private TgwEntity removeEntity(String schema, String name) {
332 		TgwEntity entity = null;
333 		synchronized (domainMap) {
334 			TgwDomainImpl domain = (TgwDomainImpl) getDomain(schema);
335 			if (domain != null) {
336 				entity = (TgwEntity) domain.removeEntity(name);
337 			}
338 		}
339 		return entity;
340 	}
341 
342 	public void createEntities(String domainName, List entityList) {
343 		javaService_.alterClass(entityList);
344 		dbService_.createTables(domainName, entityList);
345 		Iterator itr = entityList.iterator();
346 		while (itr.hasNext()) {
347 			TgwEntity entity = (TgwEntity) itr.next();
348 			putEntity(entity);
349 		}
350 		doSave(domainName);
351 	}
352 
353 	private void doCreateEntityAll(TgwDomain domain) {
354 		if (domain == null) {
355 			return;
356 		}
357 		javaService_.createPackage(domain);
358 		dbService_.createTables(domain.getName(), DomainUtils
359 				.sortByNoReference(domain));
360 	}
361 
362 	private EntityInfo doCreateEntityInfo(TgwEntity entity) {
363 		FunctionSet functionSet = daoService_.getFunctionSet(entity);
364 		return new EntityInfo(entity, functionSet);
365 	}
366 
367 	private void removeCrossRefList(List list) {
368 		Set entities = new HashSet();
369 		Iterator itr = list.iterator();
370 		while (itr.hasNext()) {
371 			SetAttribute attr = (SetAttribute) itr.next();
372 			TgwEntity entity = attr.getEntity();
373 			entity.removeAttribute(attr.getName());
374 			entities.add(attr.getEntity());
375 		}
376 		javaService_.alterClass(entities);
377 		// dbService_.alterTable(entity); // don't need dyna db
378 	}
379 
380 	public String nextEntityName(String domainName) {
381 		String nextName = null;
382 		synchronized (generatorMap) {
383 			UniqueNameGenerator entityNameGenerator = (UniqueNameGenerator) generatorMap
384 					.get(domainName);
385 			if (entityNameGenerator == null) {
386 				entityNameGenerator = new UniqueNameGenerator();
387 				generatorMap.put(domainName, entityNameGenerator);
388 			}
389 			Iterator entityNameItr = getDomain(domainName)
390 					.getEntityNameIterator();
391 			while (entityNameItr.hasNext()) {
392 				entityNameGenerator.putName((String) entityNameItr.next());
393 			}
394 			nextName = entityNameGenerator.nextUniqueName("tgw_entity");
395 		}
396 		return nextName;
397 	}
398 
399 	private void putDomain(String domainName, TgwDomain domain) {
400 		domainMap.put(domainName, domain);
401 	}
402 }