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