PageRenderTime 35ms CodeModel.GetById 28ms app.highlight 6ms RepoModel.GetById 0ms app.codeStats 0ms

/ModelibraSwing/src/org/modelibra/swing/domain/model/concept/entity/EntityBridge.java

https://github.com/DarioGT/Modelibra-Family
Java | 128 lines | 112 code | 15 blank | 1 comment | 39 complexity | 1ccc2e8f422dfc75b047c8d8743a8769 MD5 | raw file
  1package org.modelibra.swing.domain.model.concept.entity;
  2
  3import java.util.Date;
  4import java.util.List;
  5
  6import org.modelibra.DomainModel;
  7import org.modelibra.IEntities;
  8import org.modelibra.IEntity;
  9import org.modelibra.ModelMeta;
 10import org.modelibra.config.ConceptConfig;
 11import org.modelibra.config.NeighborConfig;
 12import org.modelibra.config.PropertyConfig;
 13import org.modelibra.swing.domain.model.concept.entity.property.PropertyBridge;
 14import org.modelibra.type.PropertyClass;
 15import org.modelibra.util.Transformer;
 16
 17public class EntityBridge {
 18
 19	public static final String DEFAULT_URL = "http://www.modelibra.swing.org/";
 20
 21	public static final String DEFAULT_EMAIL = "modelibra.swing@email.org";
 22
 23	private IEntities<?> entities;
 24
 25	private List<PropertyConfig> propertyConfigList;
 26
 27	private List<NeighborConfig> parentNeighborConfigList;
 28
 29	public EntityBridge(IEntities<?> entities,
 30			List<PropertyConfig> propertyConfigList,
 31			List<NeighborConfig> parentConfigList) {
 32		this.entities = entities;
 33		this.propertyConfigList = propertyConfigList;
 34		this.parentNeighborConfigList = parentConfigList;
 35	}
 36
 37	public IEntity<?> createDefaultEntity() {
 38		ModelMeta modelMeta = new ModelMeta(entities.getModel());
 39		IEntity<?> newEntity = modelMeta.createEntity(entities);
 40		for (PropertyConfig propertyConfig : propertyConfigList) {
 41			if (!propertyConfig.isDerived()) {
 42				String defaultString = propertyConfig.getDefaultValue();
 43				if (defaultString != null) {
 44					PropertyBridge propertyBridge = new PropertyBridge(newEntity,
 45							propertyConfig);
 46					propertyBridge.setProperty(defaultString);
 47				} else if (!propertyConfig.isRequired()) {
 48					newEntity.setProperty(propertyConfig.getCode(), null);
 49				} else if (propertyConfig.getPropertyClass().equals(
 50						PropertyClass.getString())) {
 51					if (propertyConfig.isValidateClassType()) {
 52						if (propertyConfig.getValidationType().equals(
 53								PropertyClass.getUrl())) {
 54							newEntity.setProperty(propertyConfig.getCode(),
 55									DEFAULT_URL);
 56						} else if (propertyConfig.getValidationType().equals(
 57								PropertyClass.getEmail())) {
 58							newEntity.setProperty(propertyConfig.getCode(),
 59									DEFAULT_EMAIL);
 60						}
 61					} else {
 62						newEntity.setProperty(propertyConfig.getCode(), "");
 63					}
 64				} else if (propertyConfig.getPropertyClass().equals(
 65						PropertyClass.getInteger())
 66						|| propertyConfig.getPropertyClass().equals(
 67								PropertyClass.getLong())) {
 68					newEntity.setProperty(propertyConfig.getCode(), 0);
 69				} else if (propertyConfig.getPropertyClass().equals(
 70						PropertyClass.getBoolean())) {
 71					newEntity.setProperty(propertyConfig.getCode(), Boolean.FALSE);
 72				} else if (propertyConfig.getPropertyClass().equals(
 73						PropertyClass.getFloat())
 74						|| propertyConfig.getPropertyClass().equals(
 75								PropertyClass.getDouble())) {
 76					newEntity.setProperty(propertyConfig.getCode(), 0);
 77				} else if (propertyConfig.getPropertyClass().equals(
 78						PropertyClass.getDate())) {
 79					newEntity.setProperty(propertyConfig.getCode(), new Date());
 80				} else if (propertyConfig.getPropertyClass().equals(
 81						PropertyClass.getUrl())) {
 82					newEntity.setProperty(propertyConfig.getCode(), Transformer
 83							.url(DEFAULT_URL));
 84				} else if (propertyConfig.getPropertyClass().equals(
 85						PropertyClass.getEmail())) {
 86					newEntity.setProperty(propertyConfig.getCode(), Transformer
 87							.email(DEFAULT_EMAIL));
 88				}
 89			}
 90		}
 91
 92		if (parentNeighborConfigList != null) {
 93			for (NeighborConfig parentNeighborConfig : parentNeighborConfigList) {
 94				if (parentNeighborConfig.isParent() && parentNeighborConfig.isMandatory()) {
 95					if (newEntity.getParentNeighbor(parentNeighborConfig
 96							.getCode()) == null) {
 97						IEntity<?> firstParent = null;
 98						IEntities<?> lookupEntities = getLookupEntities(
 99								newEntity, parentNeighborConfig);
100						if (lookupEntities != null) {
101							if (lookupEntities.size() > 0) {
102								firstParent = lookupEntities.first();
103							}
104						}
105						if (firstParent != null) {
106							newEntity.setParentNeighbor(parentNeighborConfig
107									.getCode(), firstParent);
108						}
109					}
110				}
111			}
112		}
113
114		return newEntity;
115	}
116
117	private IEntities<?> getLookupEntities(IEntity<?> newEntity,
118			NeighborConfig parentNeighborConfig) {
119		ConceptConfig parentConceptConfig = parentNeighborConfig
120				.getDestinationConceptConfig();
121
122		// to support lookup of non-entry concepts
123		IEntities<?> lookupEntities = ((DomainModel) newEntity.getModel())
124				.getSpecificModelEntities(parentConceptConfig.getEntitiesCode());
125		return lookupEntities;
126	}
127
128}