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