/flexodesktop/model/flexofoundation/src/main/java/org/openflexo/foundation/dm/eo/model/EOJoin.java

https://github.com/bluepimento/openflexo · Java · 305 lines · 224 code · 27 blank · 54 comment · 106 complexity · 6f00aae6974f6326ff7c0f19b4c3eae6 MD5 · raw file

  1. /*
  2. * (c) Copyright 2010-2011 AgileBirds
  3. *
  4. * This file is part of OpenFlexo.
  5. *
  6. * OpenFlexo is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * OpenFlexo is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with OpenFlexo. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. package org.openflexo.foundation.dm.eo.model;
  21. import java.util.HashMap;
  22. import java.util.Map;
  23. import java.util.logging.Level;
  24. import java.util.logging.Logger;
  25. import org.openflexo.logging.FlexoLogger;
  26. /**
  27. * @author gpolet
  28. *
  29. */
  30. public class EOJoin extends EOObject {
  31. private static final Logger logger = FlexoLogger.getLogger(EOJoin.class.getPackage().getName());
  32. private static final String SOURCE_ATTRIBUTE_KEY = "sourceAttribute";
  33. private static final String DESTINATION_ATTRIBUTE_KEY = "destinationAttribute";
  34. private EOAttribute sourceAttribute;
  35. private EOAttribute destinationAttribute;
  36. private EORelationship relationship;
  37. /**
  38. * @param m
  39. * @return
  40. */
  41. public static EOJoin createJoinFromMap(Map<Object, Object> map, EORelationship relationship) {
  42. EOJoin join = new EOJoin(relationship);
  43. join.setOriginalMap(map);
  44. return join;
  45. }
  46. private EOJoin(EORelationship relationship) {
  47. this.relationship = relationship;
  48. }
  49. public EOJoin(EORelationship relationship, EOAttribute source, EOAttribute destination) throws IllegalArgumentException,
  50. InvalidJoinException {
  51. this(relationship);
  52. setOriginalMap(new HashMap<Object, Object>());
  53. setSourceAttribute(source);
  54. setDestinationAttribute(destination);
  55. }
  56. public EOAttribute getDestinationAttribute() {
  57. return destinationAttribute;
  58. }
  59. public void setDestinationAttribute(EOAttribute destinationAttribute) throws InvalidJoinException {
  60. if (relationship != null) {
  61. if (destinationAttribute != null && destinationAttribute.getEntity() != relationship.getDestinationEntity()) {
  62. throw new IllegalArgumentException(
  63. "The destination attribute that has been set is not from the destination entity of the relationship");
  64. }
  65. checkJoinValidity(sourceAttribute, destinationAttribute);
  66. }
  67. if (this.destinationAttribute != destinationAttribute) {
  68. if (relationship != null && this.destinationAttribute != null) {
  69. relationship.removeFromDestinationAttributes(this.destinationAttribute);
  70. }
  71. this.destinationAttribute = destinationAttribute;
  72. if (relationship != null && destinationAttribute != null) {
  73. relationship.addToDestinationAttributes(destinationAttribute);
  74. }
  75. if (destinationAttribute != null) {
  76. getOriginalMap().put(DESTINATION_ATTRIBUTE_KEY, destinationAttribute.getName());
  77. } else {
  78. getOriginalMap().remove(DESTINATION_ATTRIBUTE_KEY);
  79. }
  80. }
  81. }
  82. public EOAttribute getSourceAttribute() {
  83. return sourceAttribute;
  84. }
  85. private void checkJoinValidity(EOAttribute src, EOAttribute dest) throws InvalidJoinException {
  86. if (src != null && dest != null) {
  87. String srcExternalType = src.getExternalType();
  88. String destExternalType = dest.getExternalType();
  89. if (srcExternalType == null && src.getPrototype() != null) {
  90. srcExternalType = src.getPrototype().getExternalType();
  91. }
  92. if (destExternalType == null && dest.getPrototype() != null) {
  93. destExternalType = dest.getPrototype().getExternalType();
  94. }
  95. if (srcExternalType != null && destExternalType != null) {
  96. if (!srcExternalType.equals(destExternalType)) {
  97. throw new InvalidJoinException(srcExternalType + " cannot be join with " + destExternalType);
  98. }
  99. }
  100. }
  101. }
  102. public void setSourceAttribute(EOAttribute sourceAttribute) throws IllegalArgumentException, InvalidJoinException {
  103. if (relationship != null) {
  104. if (sourceAttribute != null && sourceAttribute.getEntity() != relationship.getEntity()) {
  105. throw new IllegalArgumentException(
  106. "The source attribute that has been set is not from the source entity of the relationship");
  107. }
  108. checkJoinValidity(sourceAttribute, destinationAttribute);
  109. }
  110. if (this.sourceAttribute != sourceAttribute) {
  111. if (relationship != null && this.sourceAttribute != null) {
  112. relationship.removeFromSourceAttributes(this.sourceAttribute);
  113. }
  114. this.sourceAttribute = sourceAttribute;
  115. if (relationship != null && sourceAttribute != null) {
  116. relationship.addToSourceAttributes(sourceAttribute);
  117. }
  118. if (sourceAttribute != null) {
  119. getOriginalMap().put(SOURCE_ATTRIBUTE_KEY, sourceAttribute.getName());
  120. } else {
  121. getOriginalMap().remove(SOURCE_ATTRIBUTE_KEY);
  122. }
  123. }
  124. }
  125. /**
  126. * Overrides resolveObjects
  127. *
  128. * @throws InvalidJoinException
  129. * @throws IllegalArgumentException
  130. *
  131. * @see org.openflexo.foundation.dm.eo.model.EOObject#resolveObjects()
  132. */
  133. @Override
  134. protected void resolveObjects() {
  135. String src = (String) getOriginalMap().get(SOURCE_ATTRIBUTE_KEY);
  136. if (src != null) {
  137. EOAttribute att = getRelationship().getEntity().attributeNamed(src);
  138. if (att != null) {
  139. try {
  140. setSourceAttribute(att);
  141. } catch (Exception e) {
  142. throw new RuntimeException(e);
  143. }
  144. } else {
  145. if (logger.isLoggable(Level.WARNING)) {
  146. logger.warning("Could not resolve source attribute " + src + " in entity named "
  147. + getRelationship().getEntity().getName());
  148. }
  149. }
  150. }
  151. String dest = (String) getOriginalMap().get(DESTINATION_ATTRIBUTE_KEY);
  152. if (dest != null) {
  153. if (getRelationship().getDestinationEntity() != null) {
  154. EOAttribute att = getRelationship().getDestinationEntity().attributeNamed(dest);
  155. if (att != null) {
  156. try {
  157. setDestinationAttribute(att);
  158. } catch (Exception e) {
  159. throw new RuntimeException(e);
  160. }
  161. } else {
  162. if (logger.isLoggable(Level.WARNING)) {
  163. logger.warning("Could not resolve destination attribute " + src + " in entity named "
  164. + getRelationship().getDestinationEntity().getName());
  165. }
  166. }
  167. } else if (logger.isLoggable(Level.WARNING)) {
  168. logger.warning("Destination attribute is not null but the destination entity cannot be resolved");
  169. }
  170. }
  171. }
  172. public EORelationship getRelationship() {
  173. return relationship;
  174. }
  175. public void setRelationship(EORelationship relationship) {
  176. if (getRelationship() != relationship) {
  177. if (getRelationship() != null) {
  178. if (logger.isLoggable(Level.WARNING)) {
  179. logger.warning("This should not happen: trying to change join from relationship");
  180. }
  181. if (getSourceAttribute() != null) {
  182. getRelationship().removeFromSourceAttributes(getSourceAttribute());
  183. }
  184. if (getDestinationAttribute() != null) {
  185. getRelationship().removeFromDestinationAttributes(getDestinationAttribute());
  186. }
  187. }
  188. this.relationship = relationship;
  189. if (relationship != null) {
  190. if (getSourceAttribute() != null) {
  191. if (getSourceAttribute().getEntity() == relationship.getEntity()) {
  192. relationship.addToSourceAttributes(getSourceAttribute());
  193. } else {
  194. try {
  195. setSourceAttribute(null);
  196. } catch (InvalidJoinException e) {
  197. // NEVER APPEND because arg is null
  198. }
  199. }
  200. }
  201. if (getDestinationAttribute() != null) {
  202. if (getDestinationAttribute().getEntity() == relationship.getDestinationEntity()) {
  203. relationship.addToDestinationAttributes(getDestinationAttribute());
  204. } else {
  205. try {
  206. setDestinationAttribute(null);
  207. } catch (InvalidJoinException e) {
  208. // NEVER APPEND because arg is null
  209. }
  210. }
  211. }
  212. }
  213. }
  214. }
  215. /**
  216. *
  217. */
  218. public void synchronizeObjectWithOriginalMap() {
  219. Map<Object, Object> map = getOriginalMap();
  220. if (getSourceAttribute() != null) {
  221. map.put(SOURCE_ATTRIBUTE_KEY, getSourceAttribute().getName());
  222. } else {
  223. map.remove(SOURCE_ATTRIBUTE_KEY);
  224. }
  225. if (getDestinationAttribute() != null) {
  226. map.put(DESTINATION_ATTRIBUTE_KEY, getDestinationAttribute().getName());
  227. } else {
  228. map.remove(DESTINATION_ATTRIBUTE_KEY);
  229. }
  230. }
  231. /**
  232. * Overrides delete
  233. *
  234. * @see org.openflexo.foundation.dm.eo.model.EOObject#delete()
  235. */
  236. @Override
  237. public void delete() {
  238. try {
  239. setSourceAttribute(null);
  240. setDestinationAttribute(null);
  241. } catch (InvalidJoinException e) {
  242. // NEVER APPEND because arg is null
  243. }
  244. if (relationship != null) {
  245. relationship.removeJoin(this);
  246. }
  247. }
  248. /**
  249. * Overrides clearObjects
  250. *
  251. * @see org.openflexo.foundation.dm.eo.model.EOObject#clearObjects()
  252. */
  253. @Override
  254. protected void clearObjects() {
  255. sourceAttribute = null;
  256. destinationAttribute = null;
  257. }
  258. public String getPListRepresentation() {
  259. return FlexoPropertyListSerialization.getPListRepresentation(getMapRepresentation());
  260. }
  261. /**
  262. * @return
  263. */
  264. public Map<Object, Object> getMapRepresentation() {
  265. Map<Object, Object> map = new HashMap<Object, Object>();
  266. if (getSourceAttribute() != null) {
  267. map.put(SOURCE_ATTRIBUTE_KEY, getSourceAttribute().getName());
  268. } else {
  269. map.remove(SOURCE_ATTRIBUTE_KEY);
  270. }
  271. if (getDestinationAttribute() != null) {
  272. map.put(DESTINATION_ATTRIBUTE_KEY, getDestinationAttribute().getName());
  273. } else {
  274. map.remove(DESTINATION_ATTRIBUTE_KEY);
  275. }
  276. return map;
  277. }
  278. }