/projects/jboss-5.1.0/server/src/main/org/jboss/ejb/plugins/cmp/jdbc/JDBCPostCreateEntityCommand.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus · Java · 109 lines · 70 code · 6 blank · 33 comment · 13 complexity · a3d465a7666e63c3f0917a3211e1c987 MD5 · raw file

  1. /*
  2. * JBoss, Home of Professional Open Source.
  3. * Copyright 2008, Red Hat Middleware LLC, 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.ejb.plugins.cmp.jdbc;
  23. import java.lang.reflect.Method;
  24. import java.util.List;
  25. import java.util.ArrayList;
  26. import org.jboss.ejb.EntityEnterpriseContext;
  27. import org.jboss.ejb.plugins.cmp.jdbc.bridge.JDBCCMRFieldBridge;
  28. import org.jboss.ejb.plugins.cmp.jdbc.bridge.JDBCEntityBridge;
  29. import org.jboss.ejb.plugins.cmp.jdbc.bridge.JDBCFieldBridge;
  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. *
  36. * @version $Revision: 81030 $
  37. */
  38. public final class JDBCPostCreateEntityCommand
  39. {
  40. // Attributes ------------------------------------
  41. private final JDBCEntityBridge entity;
  42. private final JDBCCMRFieldBridge[] cmrWithFKMappedToCMP;
  43. // Constructors ----------------------------------
  44. public JDBCPostCreateEntityCommand(JDBCStoreManager manager)
  45. {
  46. entity = (JDBCEntityBridge) manager.getEntityBridge();
  47. JDBCFieldBridge[] cmrFields = entity.getCMRFields();
  48. List fkToCMPList = new ArrayList(4);
  49. for(int i = 0; i < cmrFields.length; ++i)
  50. {
  51. JDBCCMRFieldBridge cmrField = (JDBCCMRFieldBridge)cmrFields[i];
  52. JDBCCMRFieldBridge relatedCMRField = (JDBCCMRFieldBridge)cmrField.getRelatedCMRField();
  53. if(cmrField.hasFKFieldsMappedToCMPFields()
  54. || relatedCMRField.hasFKFieldsMappedToCMPFields())
  55. {
  56. fkToCMPList.add(cmrField);
  57. }
  58. }
  59. if(fkToCMPList.isEmpty())
  60. cmrWithFKMappedToCMP = null;
  61. else
  62. cmrWithFKMappedToCMP = (JDBCCMRFieldBridge[])fkToCMPList
  63. .toArray(new JDBCCMRFieldBridge[fkToCMPList.size()]);
  64. }
  65. // Public ----------------------------------------
  66. public Object execute(Method m, Object[] args, EntityEnterpriseContext ctx)
  67. {
  68. if(cmrWithFKMappedToCMP == null)
  69. return null;
  70. for(int i = 0; i < cmrWithFKMappedToCMP.length; ++i)
  71. {
  72. JDBCCMRFieldBridge cmrField = cmrWithFKMappedToCMP[i];
  73. JDBCCMRFieldBridge relatedCMRField = (JDBCCMRFieldBridge)cmrField.getRelatedCMRField();
  74. if(cmrField.hasFKFieldsMappedToCMPFields())
  75. {
  76. Object relatedId = cmrField.getRelatedIdFromContext(ctx);
  77. if(relatedId != null)
  78. {
  79. try
  80. {
  81. if(cmrField.isForeignKeyValid(relatedId))
  82. {
  83. cmrField.createRelationLinks(ctx, relatedId);
  84. }
  85. else
  86. {
  87. relatedCMRField.addRelatedPKWaitingForMyPK(relatedId, ctx.getId());
  88. }
  89. }
  90. catch(Exception e)
  91. {
  92. // no such object
  93. }
  94. }
  95. }
  96. else if(relatedCMRField.hasFKFieldsMappedToCMPFields())
  97. {
  98. cmrField.addRelatedPKsWaitedForMe(ctx);
  99. }
  100. }
  101. return null;
  102. }
  103. }