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.model.common;
17  
18  import java.io.IOException;
19  import java.io.InputStream;
20  import java.io.OutputStream;
21  import java.util.HashMap;
22  import java.util.Iterator;
23  import java.util.Map;
24  
25  import org.apache.commons.lang.StringUtils;
26  import org.eclipse.emf.common.util.EList;
27  import org.eclipse.emf.common.util.EMap;
28  import org.eclipse.emf.ecore.EAnnotation;
29  import org.eclipse.emf.ecore.EAttribute;
30  import org.eclipse.emf.ecore.EClass;
31  import org.eclipse.emf.ecore.EPackage;
32  import org.eclipse.emf.ecore.EReference;
33  import org.eclipse.emf.ecore.EStructuralFeature;
34  import org.eclipse.emf.ecore.EcoreFactory;
35  import org.eclipse.emf.ecore.EcorePackage;
36  import org.eclipse.emf.ecore.resource.Resource;
37  import org.eclipse.emf.ecore.xmi.impl.XMLResourceImpl;
38  import org.seasar.tuigwaa.model.core.TgwAttribute;
39  import org.seasar.tuigwaa.model.core.TgwDomain;
40  import org.seasar.tuigwaa.model.core.TgwEntity;
41  import org.seasar.tuigwaa.model.core.impl.BooleanAttribute;
42  import org.seasar.tuigwaa.model.core.impl.DateAttribute;
43  import org.seasar.tuigwaa.model.core.impl.DoubleAttribute;
44  import org.seasar.tuigwaa.model.core.impl.FileAttribute;
45  import org.seasar.tuigwaa.model.core.impl.FkAttribute;
46  import org.seasar.tuigwaa.model.core.impl.FloatAttribute;
47  import org.seasar.tuigwaa.model.core.impl.IntegerAttribute;
48  import org.seasar.tuigwaa.model.core.impl.LongAttribute;
49  import org.seasar.tuigwaa.model.core.impl.SecurityAttribute;
50  import org.seasar.tuigwaa.model.core.impl.SelfAttribute;
51  import org.seasar.tuigwaa.model.core.impl.SetAttribute;
52  import org.seasar.tuigwaa.model.core.impl.StringAttribute;
53  import org.seasar.tuigwaa.model.core.impl.TgwDomainImpl;
54  import org.seasar.tuigwaa.model.core.impl.TgwEntityImpl;
55  import org.seasar.tuigwaa.model.core.impl.TimestampAttribute;
56  import org.seasar.tuigwaa.plugin.TgwPluginUtils;
57  import org.seasar.tuigwaa.system.Constants;
58  import org.seasar.tuigwaa.util.TgwNameUtils;
59  import org.seasar.tuigwaa.util.TgwResource;
60  
61  import com.isenshi.util.CharUtil;
62  import com.isenshi.util.ResourceUtils;
63  
64  /***
65   * Tuigwaaのモデル情報(テーブル情報)と EcoreのXMLファイルとの入出力を担当するクラス
66   * 
67   * @author nishioka
68   */
69  public class EntityResourceEcoreImpl implements EntityResource {
70  
71  	/***
72  	 * Tuigwaaのバージョン
73  	 */
74  	private static final String A_VERSION = "version";
75  
76  	/***
77  	 * テーブルで削除されたカラム
78  	 */
79  	private static final String A_DELETED_CHILDREN = "deletedChildren";
80  
81  	private static final String A_PKCOLUMN = "pkcolumn";
82  
83  	/***
84  	 * 主キーの表示名
85  	 */
86  	private static final String A_PKDISPLAYNAME = "pkDisplayName";
87  
88  	/***
89  	 * 主キーにランダムな整数を割り当てるか
90  	 */
91  	private static final String A_RANDOMID = "randomId";
92  
93  	/***
94  	 * 文字列の最長値
95  	 */
96  	private static final String A_LENGTH = "length";
97  
98  	private static final String A_IMPORTEDENTITY = "importedEntity";
99  
100 	private static final String A_IMPORTEDCLASSNAME = "importedClassName";
101 
102 	private static final String A_REPRESENTATIVE = "representative";
103 
104 	private static final String A_DISPLAYNAME = "displayName";
105 
106 	private static final String A_REQUIRED = "required";
107 
108 	private static final String A_UNIQUE = "unique";
109 
110 	private static final String A_DESCRIPTION = "description";
111 
112 	private static final String A_STRINGFIELDTYPE = "stringType";
113 
114 	private static final String A_BOOLEANVALUE = "booleanValue";
115 
116 	private static final String A_ARRAYVALUE = "arrayValue";
117 
118 	private static final String A_STARTYEAR = "startYear";
119 
120 	private static final String A_STARTTYPE = "startType";
121 
122 	private static final String A_ENDYEAR = "endYear";
123 
124 	private static final String A_ENDTYPE = "endType";
125 
126 	private static final String A_LISTTYPE = "listType";
127 
128 	private static final String A_BUILTIN = "buildInField";
129 
130 	private static final String A_MIN = "min";
131 
132 	private static final String A_MAX = "max";
133 
134 	private static final String A_INTERVAL = "interval";
135 
136 	private static final String A_FORMTYPE = "formType";
137 
138 	private static final String A_REFFILTER = "refFilter";
139 
140 	private static final EcorePackage ECORE_PACKAGE = EcorePackage.eINSTANCE;
141 
142 	private static final EcoreFactory ECORE_FACTORY = EcoreFactory.eINSTANCE;
143 
144 	private> EPackageFactory epackageFactory = new EPackageFactory();
145 
146 	private TgwDomainFactory domainFactory = new TgwDomainFactory();
147 
148 	public EntityResourceEcoreImpl() {
149 	}
150 
151 	public void save(TgwDomain domain) {
152 		if (domain == null) {
153 			return;
154 		}
155 		EPackage epackage = epackageFactory.create(domain);
156 		ifg> (epackage == null) {
157 			return;
158 		}
159 
160 		// add version annotaion
161 		EAnnotation ann = ECORE_FACTORY.createEAnnotation();
162 		String version = TgwResource.getProperty("system.version");
163 		putValue(ann, A_VERSION, version);
164 		epackage.getEAnnotations().add(ann);
165 
166 		// save
167 		doSave(domain.getName(), epackage);
168 	}
169 
170 	public TgwDomain load(String domainName) {
171 		EPackage ePackage = doLoad(domainName);
172 		if (ePackage == null) {
173 			return null;
174 		}
175 
176 		String version = getVersion(ePackage);
177 		TgwDomainFactory factory = getDomainFactory(version);
178 		TgwDomain domain = factory.create(ePackage);
179 
180 		return domain;
181 	}
182 	
183 	public TgwDomain copy(String srcDomain, String destDomain){		
184 		TgwDomain src = load(srcDomain);
185 		if(src == null){
186 			return null;
187 		}		
188 		src.setName(destDomain);
189 		save(src);		
190 		TgwDomain dest = load(destDomain);		
191 		return dest;
192 	}
193 	
194 
195 	private> String getVersion(EPackage epackage) {
196 		EList list = epackage.getEAnnotations();
197 		if (list.size() > 0) {
198 			EAnnotation ann = (EAnnotation) list.get(0);
199 			return getValue(ann, A_VERSION);
200 		}
201 		return null;
202 	}
203 
204 	private TgwDomainFactory getDomainFactory(String version) {
205 		return domainFactory;
206 	}
207 
208 	private void doSave(String domain, EPackage ePackage) {
209 		Resource resource = new XMLResourceImpl();
210 		resource.getContents().add(ePackage);
211 		OutputStream stream = ResourceUtils.getOutputStream(getPath(domain));
212 
213 		Map options = new HashMap();
214 		options.put(XMLResourceImpl.OPTION_ENCODING, "Windows-31J");
215 		try {
216 			resource.save(stream, options);
217 		} catch (IOException e) {
218 			e.printStackTrace();
219 			throw new RuntimeException(e);
220 		}
221 	}
222 
223 	private EPackage doLoad(String domain) {
224 		String path = getPath(domain);
225 		if (!ResourceUtils.isExist(path)) {
226 			return null;
227 		}
228 
229 		InputStream stream = ResourceUtils.getInputStream(path);
230 		Resource resource = new XMLResourceImpl();
231 		try {
232 			resource.load(stream, null);
233 		} catch (IOException e) {
234 			e.printStackTrace();
235 			throw new RuntimeException(e);
236 		}
237 		return (EPackage) resource.getContents().get(0);
238 	}
239 
240 	class EPackageFactory {
241 
242 		EPackage create(TgwDomain domain) {
243 			EPackage epackage = ECORE_FACTORY.createEPackage();
244 			String domainName = domain.getName();
245 			epackage.setName(domainName);
246 			epackage.setNsPrefix(TgwResource.getProperty("system.name"));
247 			epackage.setNsURI(TgwResource.getProperty("system.url"));
248 
249 			Map eclassMap = new HashMap();
250 			Iterator itr = domain.getEntityList().iterator();
251 			if (itr == null) {
252 				return null;
253 			}
254 			while (itr.hasNext()) {
255 				TgwEntity entity = (TgwEntity) itr.next();
256 				if(!TgwPluginUtils.isPluginEntity(entity.getName())){
257 					EClass eclass = toEClass(entity, eclassMap);
258 					epackage.getEClassifiers().add(eclass);
259 				}
260 			}
261 			returnng> epackage;
262 		}
263 
264 		/***
265 		 * モデル(テーブル)の情報を、EMFのクラスに変換
266 		 */
267 		private EClass toEClass(TgwEntity entity, Map eclassMap) {
268 			EClass entityClass = createEClass(entity, eclassMap);
269 			EAnnotation ann = ECORE_FACTORY.createEAnnotation();
270 
271 			// add class annotation
272 			putValue(ann, A_DISPLAYNAME, entity.getDisplayName());
273 			putValue(ann, A_DESCRIPTION, entity.getDescription());
274 			putValue(ann, A_REPRESENTATIVE, entity.getRepresentativeField());
275 			if (entity.isRandomId()) {
276 				putValue(ann, A_RANDOMID, "true");
277 			}
278 			if (entity.isImportedEntity()) {
279 				putValue(ann, A_IMPORTEDENTITY, "true");
280 			}
281 			if (entity.getImportedClassName() != null) {
282 				putValue(ann, A_IMPORTEDCLASSNAME, entity
283 						.getImportedClassName());
284 			}
285 			if (entity.getPrimaryKeyColumnName() != null) {
286 				putValue(ann, A_PKCOLUMN, entity.getPrimaryKeyColumnName());
287 			}
288 			putValue(ann, A_PKDISPLAYNAME, entity.getPrimaryKeyDisplayName());
289 
290 			if (entity.getRemovedAttrNames() != null
291 					&& entity.getRemovedAttrNames().size() > 0) {
292 				String attrNames = StringUtils.join(entity
293 						.getRemovedAttrNames().iterator(), ",");
294 				putValue(ann, A_DELETED_CHILDREN, attrNames);
295 			}
296 
297 			ann.setSource("tuigwaa class");
298 			entityClass.getEAnnotations().add(ann);
299 
300 			Iterator itr = entity.getFieldIterator();
301 			while (itr.hasNext()) {
302 				TgwAttribute field = (TgwAttribute) itr.next();
303 				EStructuralFeature feature = toEStructuralFeature(field,
304 						eclassMap);
305 				entityClass.getEStructuralFeatures().add(feature);
306 			}
307 			return entityClass;
308 		}
309 
310 		/***
311 		 * モデルの属性(テーブルのカラム)の情報を、EMFのクラスに変換
312 		 */
313 		private EStructuralFeature toEStructuralFeature(TgwAttribute attr,
314 				Map eclassMap) {
315 
316 			EStructuralFeature feature = (EStructuralFeature) attr.accept(
317 					TGWATTR_TO_EATTR_VISITOR, eclassMap);
318 
319 			EAnnotation ann = (EAnnotation) feature.getEAnnotations().get(0);
320 
321 			feature.getEAnnotations().add(ann);
322 			feature.setName(attr.getName());
323 
324 			// add annotaion
325 			putValue(ann, A_DESCRIPTION, attr.getDescription());
326 			putValue(ann, A_DISPLAYNAME, attr.getDisplayName());
327 			putValue(ann, A_REQUIRED, "" + attr.isRequired());
328 
329 			return feature;
330 		}
331 
332 		private EClass createEClass(TgwEntity entity, Map eclassMap) {
333 			EClass entityClass = (EClass) eclassMap.get(entity.getName());
334 			if (entityClass == null) {
335 				entityClass = ECORE_FACTORY.createEClass();
336 				entityClass.setName(CharUtil.toUpperCaseAtFisrtChar(entity
337 						.getName()));
338 				eclassMap.put(entity.getName(), entityClass);
339 			}
340 			return entityClass;
341 		}
342 
343 		/***
344 		 * TgwAttributeをEstructuralFeatureに変換するクラス
345 		 */
346 		private TgwAttributeVisitor TGWATTR_TO_EATTR_VISITOR = new TgwAttributeVisitor() {
347 
348 			public Object visit(LongAttribute attr, Object data) {
349 				return null;
350 			}
351 
352 			public Object visit(IntegerAttribute attr, Object data) {
353 				EStructuralFeature feature = createEAttribute();
354 				feature.setEType(ECORE_PACKAGE.getEInt());
355 				if ("restriction".equals(attr.getOption())) {
356 					EAnnotation anno = (EAnnotation) feature.getEAnnotations()
357 							.get(0);
358 					putValue(anno, A_MIN, "" + attr.getMin());
359 					putValue(anno, A_MAX, "" + attr.getMax());
360 					putValue(anno, A_INTERVAL, "" + attr.getInterval());
361 				}
362 				return feature;
363 			}
364 
365 			public Object visit(FloatAttribute field, Object data) {
366 				EStructuralFeature feature = createEAttribute();
367 				feature.setEType(ECORE_PACKAGE.getEFloat());
368 				return feature;
369 			}
370 
371 			public Object visit(BooleanAttribute field, Object data) {
372 				EStructuralFeature feature = createEAttribute();
373 				EAnnotation anno = (EAnnotation) feature.getEAnnotations().get(
374 						0);
375 
376 				feature.setEType(ECORE_PACKAGE.getEBoolean());
377 				String trueStr = ((BooleanAttribute) field).getTrueStr();
378 				String falseStr = ((BooleanAttribute) field).getFalseStr();
379 
380 				putValue(anno, A_BOOLEANVALUE, trueStr + "," + falseStr);
381 				putValue(anno, A_FORMTYPE, field.getFormType());
382 				return feature;
383 			}
384 
385 			public Object visit(StringAttribute attr, Object data) {
386 				EStructuralFeature feature = createEAttribute();
387 				EAnnotation ann = (EAnnotation) feature.getEAnnotations()
388 						.get(0);
389 
390 				feature.setEType(ECORE_PACKAGE.getEString());
391 				String mask = attr.getMask();
392 				if (mask != null && !mask.equals("")) {
393 					putValue(ann, A_STRINGFIELDTYPE, mask);
394 				}
395 				String[] array = attr.getRestrictions();
396 				if (array != null && array.length > 0) {
397 					String arrayStr = CharUtil.arrayToString(array);
398 					putValue(ann, A_ARRAYVALUE, arrayStr);
399 					putValue(ann, A_FORMTYPE, attr.getFormType());
400 				}
401 				int length = attr.getLength();
402 				if (length > 0) {
403 					putValue(ann, A_LENGTH, "" + length);
404 				}
405 				return feature;
406 			}
407 
408 			public Object visit(DateAttribute field, Object data) {
409 				EStructuralFeature feature = createEAttribute();
410 				EAnnotation ann = (EAnnotation) feature.getEAnnotations()
411 						.get(0);
412 				DateAttribute dField = (DateAttribute) field;
413 				feature.setEType(ECORE_PACKAGE.getEDate());
414 
415 				String startYear = String.valueOf(dField.getStartYear());
416 				String endYear = String.valueOf(dField.getEndYear());
417 
418 				putValue(ann, A_STARTYEAR, startYear);
419 				putValue(ann, A_STARTTYPE, dField.getStartSuffix());
420 				putValue(ann, A_ENDYEAR, endYear);
421 				putValue(ann, A_ENDTYPE, dField.getEndSuffix());
422 				return feature;
423 			}
424 
425 			public Object visit(FkAttribute attr, Object data) {
426 				EStructuralFeature feature = createEReference();
427 				EAnnotation ann = (EAnnotation) feature.getEAnnotations()
428 						.get(0);
429 				feature.setEType(createEClass(attr.getRefEntity(), (Map) data));
430 				putValue(ann, A_REFFILTER, attr.getRefEntityFilter());
431 				return feature;
432 				// departmentEmployee.setUpperBound(EStructuralFeature.UNBOUNDED_MULTIPLICITY);
433 				// departmentEmployee.setContainment(true);
434 			}
435 
436 			public Object visit(FileAttribute field, Object data) {
437 				EStructuralFeature feature = createEAttribute();
438 				feature.setEType(ECORE_PACKAGE.getEByteArray());
439 				return feature;
440 			}
441 
442 			public Object visit(TimestampAttribute field, Object data) {
443 				EStructuralFeature feature = createEAttribute();
444 				EAnnotation annotation = (EAnnotation) feature
445 						.getEAnnotations().get(0);
446 				feature.setEType(ECORE_PACKAGE.getEDate());
447 				putValue(annotation, A_BUILTIN, field.getName());
448 				return feature;
449 			}
450 
451 			public Object visit(SecurityAttribute field, Object data) {
452 				EStructuralFeature feature = createEAttribute();
453 				EAnnotation annotation = (EAnnotation) feature
454 						.getEAnnotations().get(0);
455 				feature.setEType(ECORE_PACKAGE.getEString());
456 				putValue(annotation, A_BUILTIN, field.getName());
457 				if (field.isUnique()) {
458 					putValue(annotation, A_UNIQUE, "true");
459 				}
460 				return feature;
461 			}
462 
463 			public Object visit(SetAttribute field, Object data) {
464 				EStructuralFeature feature = createEAttribute();
465 				EAnnotation anno = (EAnnotation) feature.getEAnnotations().get(
466 						0);
467 				feature.setEType(ECORE_PACKAGE.getEEList());
468 				String refEntityName = ((SetAttribute) field)
469 						.getRefEntityName();
470 				putValue(anno, A_LISTTYPE, refEntityName);
471 				return feature;
472 			}
473 
474 			public Object visit(SelfAttribute field, Object data) {
475 				EStructuralFeature feature = createEAttribute();
476 				EAnnotation ann = (EAnnotation) feature.getEAnnotations()
477 						.get(0);
478 				feature.setEType(ECORE_PACKAGE.getELong());
479 				putValue(ann, A_REFFILTER, field.getRefEntityFilter());
480 				return feature;
481 			}
482 
483 			public Object visit(DoubleAttribute field, Object data) {
484 				return null;
485 			}
486 
487 			private EStructuralFeature createEAttribute() {
488 				EStructuralFeature feature = ECORE_FACTORY.createEAttribute();
489 				EAnnotation anno = ECORE_FACTORY.createEAnnotation();
490 				feature.getEAnnotations().add(anno);
491 				return feature;
492 			}
493 
494 			private EStructuralFeature createEReference() {
495 				EStructuralFeature feature = ECORE_FACTORY.createEReference();
496 				EAnnotation anno = ECORE_FACTORY.createEAnnotation();
497 				feature.getEAnnotations().add(anno);
498 				return feature;
499 			}
500 		};
501 	}
502 
503 	class TgwDomainFactory {
504 
505 		TgwDomain create(EPackage ePackage) {
506 			Iterator itr = ePackage.getEClassifiers().iterator();
507 			String domainName = ePackage.getName();
508 			TgwDomain domain = new TgwDomainImpl(domainName);
509 			while (itr.hasNext()) {
510 				Object tmp = itr.next();
511 				if (tmp instanceof EClass) {
512 					toEntity(domainName, (EClass) tmp, domain);
513 				}
514 			}
515 			secondPass(domain);
516 			return domain;
517 		}
518 
519 		private void secondPass(TgwDomain domain) {
520 			Iterator itr = domain.getEntityList().iterator();
521 			while (itr.hasNext()) {
522 				TgwEntity entity = (TgwEntity) itr.next();
523 				Iterator itr2 = entity.getFieldIterator();
524 				while (itr2.hasNext()) {
525 					TgwAttribute field = (TgwAttribute) itr2.next();
526 					if (field instanceof SetAttribute) {
527 						((SetAttribute) field).connectFkField();
528 					}
529 				}
530 			}
531 		}
532 
533 		private TgwEntity toEntity(String domainName, EClass eclass,
534 				TgwDomain domain) {
535 			TgwEntity entity = createEntity(domainName, eclass.getName()
536 					.toLowerCase(), domain);
537 
538 			EAnnotation ann = (EAnnotation) eclass.getEAnnotations().get(0);
539 			String description = getValue(ann, A_DESCRIPTION);
540 			String dispalyName = getValue(ann, A_DISPLAYNAME);
541 			String representative = getValue(ann, A_REPRESENTATIVE);
542 
543 			boolean randomId = "true".equals(getValue(ann, A_RANDOMID));
544 			boolean importedEntity = "true".equals(getValue(ann,
545 					A_IMPORTEDENTITY));
546 
547 			entity.setDescription(description);
548 			entity.setDisplayName(dispalyName);
549 			entity.setRepresentativeField(representative);
550 			entity.setRandomId(randomId);
551 			entity.setImportedEntity(importedEntity);
552 
553 			String importedClassName = getValue(ann, A_IMPORTEDCLASSNAME);
554 			if (importedClassName != null && importedClassName.length() > 0) {
555 				entity.setImportedClassName(importedClassName);
556 			}
557 			String removedAttrNames = getValue(ann, A_DELETED_CHILDREN);
558 			if (removedAttrNames != null) {
559 				String[] names = removedAttrNames.split(",");
560 				for (int i = 0; i < names.length; i++) {
561 					entity.addRemovedAttrName(names[i]);
562 				}
563 			}
564 
565 			String pkColumn = getValue(ann, A_PKCOLUMN);
566 			if (pkColumn != null) {
567 				entity.setPrimaryKeyColumnName(pkColumn);
568 			}
569 
570 			String pkDisplayName = getValue(ann, A_PKDISPLAYNAME);
571 			if (pkDisplayName != null) {
572 				entity.setPrimaryKeyDisplayName(pkDisplayName);
573 			}
574 
575 			Iterator itr = eclass.getEStructuralFeatures().iterator();
576 			while (itr.hasNext()) {
577 				EStructuralFeature feature = (EStructuralFeature) itr.next();
578 				TgwAttribute field = toField(domainName, feature, domain,
579 						entity);
580 				entity.addField(field);
581 			}
582 			return entity;
583 		}
584 
585 		private TgwAttribute toField(String domainName,
586 				EStructuralFeature feature, TgwDomain domain, TgwEntity entity) {
587 			EAnnotation annotaion = (EAnnotation) feature.getEAnnotations()
588 					.get(0);
589 			EMap eMap = annotaion.getDetails();
590 			TgwAttribute field = toTgwAttribute(domainName, feature, domain,
591 					eMap, entity);
592 			field.setName(feature.getName());
593 
594 			// General Attribute Annotaion
595 			String fieldDispalyName = (String) eMap.get(A_DISPLAYNAME);
596 			String fieldDescription = (String) eMap.get(A_DESCRIPTION);
597 			String required = (String) eMap.get(A_REQUIRED);
598 			field.setDisplayName(fieldDispalyName);
599 			field.setDescription(fieldDescription);
600 			if (required != null && required.equals("true")) {
601 				field.setRequired(true);
602 			}
603 
604 			return field;
605 		}
606 
607 		private TgwAttribute toTgwAttribute(String domainName,
608 				EStructuralFeature feature, TgwDomain domain, EMap eMap,
609 				TgwEntity entity) {
610 			if (feature instanceof EReference) {
611 				String entityName = feature.getEType().getName().toLowerCase();
612 				TgwEntity refEntity = createEntity(domainName, entityName,
613 						domain);
614 				FkAttribute field = new FkAttribute();
615 				field.setRefEntity(refEntity);
616 				String refFilter = (String) eMap.get(A_REFFILTER);
617 				field.setRefEntityFilter(refFilter);
618 				return field;
619 			} else if (feature instanceof EAttribute) {
620 				if (feature.getEType().getName().equals("EString")) {
621 					String builtin = (String) eMap.get(A_BUILTIN);
622 					if (builtin != null) {
623 						SecurityAttribute attr = new SecurityAttribute();
624 						String unique = (String) eMap.get(A_UNIQUE);
625 						if ("true".equals(unique)) {
626 							attr.setUnique(true);
627 						}
628 						return attr;
629 					} else {
630 						StringAttribute field = new StringAttribute();
631 						String mask = (String) eMap.get(A_STRINGFIELDTYPE);
632 						if (mask != null && !"".equals(mask)) {
633 							field.setMask(mask);
634 						}
635 						String arrayStr = (String) eMap.get(A_ARRAYVALUE);
636 						String formType = (String) eMap.get(A_FORMTYPE);
637 						if (arrayStr != null && !"".equals(arrayStr)) {
638 							String[] array = arrayStr.split(",");
639 							field.setRestrictions(array);
640 							field.setOption("restriction");
641 							field.setFormType(formType);
642 						}
643 						String length = (String) eMap.get(A_LENGTH);
644 						if (length != null) {
645 							field.setLength(Integer.parseInt(length));
646 						}
647 						return field;
648 					}
649 				} else if (feature.getEType().getName().equals("EDate")) {
650 					String builtin = (String) eMap.get(A_BUILTIN);
651 					if (builtin == null) {
652 						DateAttribute field = new DateAttribute();
653 
654 						String startYearStr = (String) eMap.get(A_STARTYEAR);
655 						if (startYearStr != null) {
656 							int startYear = Integer.parseInt(startYearStr);
657 							field.setStartYear(startYear);
658 						}
659 						String endYearStr = (String) eMap.get(A_ENDYEAR);
660 						if (endYearStr != null) {
661 							int endYear = Integer.parseInt(endYearStr);
662 							field.setEndYear(endYear);
663 						}
664 						String startType = (String) eMap.get(A_STARTTYPE);
665 						String endType = (String) eMap.get(A_ENDTYPE);
666 						field.setStartSuffix(startType);
667 						field.setEndSuffix(endType);
668 						return field;
669 					} else {
670 						return new TimestampAttribute();
671 					}
672 				} else if (feature.getEType().getName().equals("EInt")) {
673 					IntegerAttribute attr = new IntegerAttribute();
674 					String intervalStr = (String) eMap.get(A_INTERVAL);
675 					if (intervalStr != null && !"".equals(intervalStr)
676 							&& Integer.parseInt(intervalStr) > 0) {
677 						String minStr = (String) eMap.get(A_MIN);
678 						String maxStr = (String) eMap.get(A_MAX);
679 						attr.setMin(Integer.parseInt(minStr));
680 						attr.setMax(Integer.parseInt(maxStr));
681 						attr.setInterval(Integer.parseInt(intervalStr));
682 						attr.setOption("restriction");
683 					}
684 					return attr;
685 				} else if (feature.getEType().getName().equals("ELong")) {
686 					 SelfAttribute selfAttr = new SelfAttribute();
687 					 String refFilter = (String) eMap.get(A_REFFILTER);
688 					 selfAttr.setRefEntityFilter(refFilter);
689 					 return selfAttr;
690 				} else if (feature.getEType().getName().equals("EFloat")) {
691 					return new FloatAttribute();
692 				} else if (feature.getEType().getName().equals("EEList")) {
693 					SetAttribute field = new SetAttribute();
694 					String refEntityName = (String) eMap.get(A_LISTTYPE);
695 					TgwEntity refEntity = createEntity(domainName,
696 							refEntityName, domain);
697 					// if
698 					// (TgwNameUtils.toManyToManyName(entity.getName()).equals(
699 					// refEntityName)) {
700 					if (refEntityName != null
701 							&& refEntityName.startsWith(TgwNameUtils
702 									.toManyToManyName(entity.getName()))) {
703 						field.setOption(SetAttribute.OPTION_M2M);
704 					}
705 					field.setRefEntity(refEntity);
706 					return field;
707 				} else if (feature.getEType().getName().equals("EByteArray")) {
708 					return new FileAttribute();
709 				} else if (feature.getEType().getName().equals("EBoolean")) {
710 					BooleanAttribute field = new BooleanAttribute();
711 					String value = (String) eMap.get(A_BOOLEANVALUE);
712 					String[] values = value.split(",");
713 					field.setTrueStr(values[0]);
714 					field.setFalseStr(values[1]);
715 
716 					String formType = (String) eMap.get(A_FORMTYPE);
717 					if (formType != null && formType.length() > 0) {
718 						field.setFormType(formType);
719 					}
720 					return field;
721 				}
722 			}
723 			return null;
724 		}
725 
726 		private TgwEntity createEntity(String domainName, String entityName,
727 				TgwDomain domain) {
728 			TgwEntity entity = domain.getEntity(entityName);
729 			if (entity == null) {
730 				entity = new TgwEntityImpl(domainName, entityName);
731 				domain.addEntity(entity);
732 			}
733 			return entity;
734 		}
735 	}
736 
737 	private static void putValue(EAnnotation ann, String label, String value) {
738 		ann.getDetails().put(label, value);
739 	}
740 
741 	private static String getValue(EAnnotation ann, String label) {
742 		return (String) ann.getDetails().get(label);
743 	}
744 
745 	private String getPath(String domain) {
746 		return Constants.DIRECTORY_APP + domain + "/model.ecore";
747 	}
748 }