PageRenderTime 47ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/jboss-as-7.1.1.Final/cmp/src/main/java/org/jboss/as/cmp/jdbc2/schema/Schema.java

#
Java | 219 lines | 167 code | 26 blank | 26 comment | 44 complexity | f7445dc0fd0ee0a67ccb8ea775e6e25e MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  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.as.cmp.jdbc2.schema;
  23. import java.sql.SQLException;
  24. import javax.transaction.Status;
  25. import javax.transaction.Synchronization;
  26. import javax.transaction.Transaction;
  27. import static org.jboss.as.cmp.CmpMessages.MESSAGES;
  28. import org.jboss.as.cmp.jdbc.metadata.JDBCEntityMetaData;
  29. import org.jboss.as.cmp.jdbc2.bridge.JDBCCMRFieldBridge2;
  30. import org.jboss.as.cmp.jdbc2.bridge.JDBCEntityBridge2;
  31. import org.jboss.tm.TransactionLocal;
  32. /**
  33. * @author <a href="mailto:alex@jboss.org">Alexey Loubyansky</a>
  34. * @version <tt>$Revision: 94413 $</tt>
  35. */
  36. public class Schema {
  37. private final String viewsTxLocalKey;
  38. private EntityTable[] entityTables;
  39. private RelationTable[] relationTables;
  40. private TransactionLocal txLocal = null; // TODO: jeb - get this
  41. public Schema(String ejbModuleName) {
  42. this.viewsTxLocalKey = ejbModuleName + ".schema.views";
  43. }
  44. public EntityTable createEntityTable(JDBCEntityMetaData metadata, JDBCEntityBridge2 entity) {
  45. if (entityTables == null) {
  46. entityTables = new EntityTable[1];
  47. } else {
  48. EntityTable[] tmp = entityTables;
  49. entityTables = new EntityTable[tmp.length + 1];
  50. System.arraycopy(tmp, 0, entityTables, 0, tmp.length);
  51. }
  52. EntityTable table = new EntityTable(metadata, entity, this, entityTables.length - 1);
  53. entityTables[entityTables.length - 1] = table;
  54. return table;
  55. }
  56. public RelationTable createRelationTable(JDBCCMRFieldBridge2 leftField, JDBCCMRFieldBridge2 rightField) {
  57. if (relationTables == null) {
  58. relationTables = new RelationTable[1];
  59. } else {
  60. RelationTable[] tmp = relationTables;
  61. relationTables = new RelationTable[tmp.length + 1];
  62. System.arraycopy(tmp, 0, relationTables, 0, tmp.length);
  63. }
  64. RelationTable table = new RelationTable(leftField, rightField, this, relationTables.length - 1);
  65. relationTables[relationTables.length - 1] = table;
  66. return table;
  67. }
  68. public Table.View getView(EntityTable table) {
  69. Views views = getViews();
  70. Table.View view = views.entityViews[table.getTableId()];
  71. if (view == null) {
  72. view = table.createView(views.tx);
  73. views.entityViews[table.getTableId()] = view;
  74. }
  75. return view;
  76. }
  77. public Table.View getView(RelationTable table) {
  78. Views views = getViews();
  79. Table.View view = views.relationViews[table.getTableId()];
  80. if (view == null) {
  81. view = table.createView(views.tx);
  82. views.relationViews[table.getTableId()] = view;
  83. }
  84. return view;
  85. }
  86. public void flush() {
  87. Views views = getViews();
  88. Table.View[] relationViews = views.relationViews;
  89. if (relationViews != null) {
  90. for (int i = 0; i < relationViews.length; ++i) {
  91. final Table.View view = relationViews[i];
  92. if (view != null) {
  93. try {
  94. view.flushDeleted(views);
  95. } catch (SQLException e) {
  96. throw MESSAGES.failedToDeleteManyToMany(e);
  97. }
  98. }
  99. }
  100. }
  101. final Table.View[] entityViews = views.entityViews;
  102. for (int i = 0; i < entityViews.length; ++i) {
  103. Table.View view = entityViews[i];
  104. if (view != null) {
  105. try {
  106. view.flushDeleted(views);
  107. } catch (SQLException e) {
  108. throw MESSAGES.failedToDeleteInstance(e);
  109. }
  110. }
  111. }
  112. for (int i = 0; i < entityViews.length; ++i) {
  113. Table.View view = entityViews[i];
  114. if (view != null) {
  115. try {
  116. view.flushCreated(views);
  117. } catch (SQLException e) {
  118. throw MESSAGES.failedToCreateInstance(e);
  119. }
  120. }
  121. }
  122. for (int i = 0; i < entityViews.length; ++i) {
  123. Table.View view = entityViews[i];
  124. if (view != null) {
  125. try {
  126. view.flushUpdated();
  127. } catch (SQLException e) {
  128. throw MESSAGES.failedToUpdateInstance(e);
  129. }
  130. }
  131. }
  132. if (relationViews != null) {
  133. for (int i = 0; i < relationViews.length; ++i) {
  134. final Table.View view = relationViews[i];
  135. if (view != null) {
  136. try {
  137. view.flushCreated(views);
  138. } catch (SQLException e) {
  139. throw MESSAGES.failedToCreateManyToMany(e);
  140. }
  141. }
  142. }
  143. }
  144. }
  145. private Views getViews() {
  146. return null;
  147. }
  148. // Inner
  149. public class Views {
  150. public final Transaction tx;
  151. public final Table.View[] entityViews;
  152. public final Table.View[] relationViews;
  153. public Views(Transaction tx) {
  154. this.tx = tx;
  155. this.entityViews = new Table.View[entityTables.length];
  156. this.relationViews = relationTables == null ? null : new Table.View[relationTables.length];
  157. }
  158. }
  159. private class SchemaSynchronization implements Synchronization {
  160. private final Views views;
  161. public SchemaSynchronization(Views views) {
  162. this.views = views;
  163. }
  164. public void beforeCompletion() {
  165. flush();
  166. for (int i = 0; i < views.entityViews.length; ++i) {
  167. Table.View view = views.entityViews[i];
  168. if (view != null) {
  169. view.beforeCompletion();
  170. }
  171. }
  172. }
  173. public void afterCompletion(int status) {
  174. if (status == Status.STATUS_MARKED_ROLLBACK ||
  175. status == Status.STATUS_ROLLEDBACK ||
  176. status == Status.STATUS_ROLLING_BACK) {
  177. for (int i = 0; i < views.entityViews.length; ++i) {
  178. Table.View view = views.entityViews[i];
  179. if (view != null) {
  180. view.rolledback();
  181. }
  182. }
  183. } else {
  184. for (int i = 0; i < views.entityViews.length; ++i) {
  185. Table.View view = views.entityViews[i];
  186. if (view != null) {
  187. view.committed();
  188. }
  189. }
  190. }
  191. }
  192. }
  193. }