/cmp/src/main/java/org/jboss/as/cmp/jdbc/JDBCPostCreateEntityCommand.java

http://github.com/jbossas/jboss-as · Java · 91 lines · 54 code · 5 blank · 32 comment · 14 complexity · e86d92f3faefc6ebb1a433d2f5a3076c MD5 · raw file

  1. /*
  2. * JBoss, Home of Professional Open Source.
  3. * Copyright 2011, Red Hat, Inc., and individual contributors
  4. * as indicated by the @author tags. See the copyright.txt file in the
  5. * distribution for a full listing of individual contributors.
  6. *
  7. * This is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation; either version 2.1 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This software is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this software; if not, write to the Free
  19. * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  21. */
  22. package org.jboss.as.cmp.jdbc;
  23. import java.lang.reflect.Method;
  24. import java.util.ArrayList;
  25. import java.util.List;
  26. import org.jboss.as.cmp.jdbc.bridge.JDBCCMRFieldBridge;
  27. import org.jboss.as.cmp.jdbc.bridge.JDBCEntityBridge;
  28. import org.jboss.as.cmp.jdbc.bridge.JDBCFieldBridge;
  29. import org.jboss.as.cmp.context.CmpEntityBeanContext;
  30. /**
  31. * This command establishes relationships for CMR fields that have
  32. * foreign keys mapped to primary keys.
  33. *
  34. * @author <a href="mailto:aloubyansky@hotmail.com">Alex Loubyansky</a>
  35. * @version $Revision: 81030 $
  36. */
  37. public final class JDBCPostCreateEntityCommand {
  38. // Attributes ------------------------------------
  39. private final JDBCEntityBridge entity;
  40. private final JDBCCMRFieldBridge[] cmrWithFKMappedToCMP;
  41. // Constructors ----------------------------------
  42. public JDBCPostCreateEntityCommand(JDBCStoreManager manager) {
  43. entity = (JDBCEntityBridge) manager.getEntityBridge();
  44. JDBCFieldBridge[] cmrFields = entity.getCMRFields();
  45. List fkToCMPList = new ArrayList(4);
  46. for (int i = 0; i < cmrFields.length; ++i) {
  47. JDBCCMRFieldBridge cmrField = (JDBCCMRFieldBridge) cmrFields[i];
  48. JDBCCMRFieldBridge relatedCMRField = (JDBCCMRFieldBridge) cmrField.getRelatedCMRField();
  49. if (cmrField.hasFKFieldsMappedToCMPFields()
  50. || relatedCMRField.hasFKFieldsMappedToCMPFields()) {
  51. fkToCMPList.add(cmrField);
  52. }
  53. }
  54. if (fkToCMPList.isEmpty())
  55. cmrWithFKMappedToCMP = null;
  56. else
  57. cmrWithFKMappedToCMP = (JDBCCMRFieldBridge[]) fkToCMPList
  58. .toArray(new JDBCCMRFieldBridge[fkToCMPList.size()]);
  59. }
  60. // Public ----------------------------------------
  61. public Object execute(Method m, Object[] args, CmpEntityBeanContext ctx) {
  62. if (cmrWithFKMappedToCMP == null)
  63. return null;
  64. for (int i = 0; i < cmrWithFKMappedToCMP.length; ++i) {
  65. JDBCCMRFieldBridge cmrField = cmrWithFKMappedToCMP[i];
  66. JDBCCMRFieldBridge relatedCMRField = (JDBCCMRFieldBridge) cmrField.getRelatedCMRField();
  67. if (cmrField.hasFKFieldsMappedToCMPFields()) {
  68. Object relatedId = cmrField.getRelatedIdFromContext(ctx);
  69. if (relatedId != null) {
  70. try {
  71. if (cmrField.isForeignKeyValid(relatedId)) {
  72. cmrField.createRelationLinks(ctx, relatedId);
  73. } else {
  74. relatedCMRField.addRelatedPKWaitingForMyPK(relatedId, ctx.getPrimaryKeyUnchecked());
  75. }
  76. } catch (Exception e) {
  77. // no such object
  78. }
  79. }
  80. } else if (relatedCMRField.hasFKFieldsMappedToCMPFields()) {
  81. cmrField.addRelatedPKsWaitedForMe(ctx);
  82. }
  83. }
  84. return null;
  85. }
  86. }