KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > revolt > support > LogTable


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2006
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.revolt.support;
25
26 import java.sql.ResultSet JavaDoc;
27 import java.sql.SQLException JavaDoc;
28 import java.util.Collection JavaDoc;
29 import java.util.Collections JavaDoc;
30
31 import javax.sql.DataSource JavaDoc;
32
33 import org.riotfamily.revolt.ChangeSet;
34 import org.riotfamily.revolt.DatabaseOutOfSyncException;
35 import org.riotfamily.revolt.Dialect;
36 import org.riotfamily.revolt.Script;
37 import org.riotfamily.revolt.definition.Column;
38 import org.riotfamily.revolt.definition.Table;
39 import org.riotfamily.revolt.refactor.InsertData;
40 import org.springframework.jdbc.core.JdbcTemplate;
41 import org.springframework.jdbc.core.RowMapper;
42
43 /**
44  * @author Felix Gnass [fgnass at neteye dot de]
45  *
46  */

47 public class LogTable {
48
49     private static final String JavaDoc TABLE_NAME = "revolt_change_log";
50     
51     private DataSource JavaDoc dataSource;
52     
53     private Dialect dialect;
54     
55     private Table table;
56     
57     private boolean exists;
58     
59     public LogTable(DataSource JavaDoc dataSource, Dialect dialect) {
60         this.dataSource = dataSource;
61         this.dialect = dialect;
62         
63         table = new Table(TABLE_NAME);
64         table.addColumn(new Column("change_set_id", TypeMap.VARCHAR, 255));
65         table.addColumn(new Column("module", TypeMap.VARCHAR, 255));
66         table.addColumn(new Column("seq_nr", TypeMap.INTEGER));
67         
68         exists = DatabaseUtils.tableExists(dataSource, table);
69     }
70     
71     public boolean exists() {
72         return exists;
73     }
74     
75     public Collection JavaDoc getAppliedChangeSetIds(final String JavaDoc moduleName) {
76         if (!exists) {
77             return Collections.EMPTY_LIST;
78         }
79         JdbcTemplate template = new JdbcTemplate(dataSource);
80         return template.query("select change_set_id, seq_nr from "
81                 + TABLE_NAME + " where module = ? order by seq_nr asc",
82                 new Object JavaDoc[] { moduleName }, new RowMapper() {
83                     public Object JavaDoc mapRow(ResultSet JavaDoc rs, int rowNumber)
84                             throws SQLException JavaDoc {
85                         
86                         String JavaDoc changeSetId = rs.getString(1);
87                         int sequenceNumber = rs.getInt(2);
88                         
89                         if (sequenceNumber != rowNumber) {
90                             throw new DatabaseOutOfSyncException(
91                                     "Expected a ChangeSet with sequence number "
92                                     + rowNumber + " for module '" + moduleName
93                                     + "' but found ChangeSet [" + changeSetId
94                                     + "] which has the number "
95                                     + sequenceNumber);
96                         }
97                         return changeSetId;
98                     }
99                 }
100         );
101     }
102     
103     public Script getCreateTableScript() {
104         Script script = dialect.createTable(table);
105         if (DatabaseUtils.anyTablesExist(dataSource)) {
106             script.forceManualExecution();
107         }
108         return script;
109     }
110         
111     public Script getInsertScript(ChangeSet changeSet) {
112         InsertData insert = new InsertData(TABLE_NAME);
113         insert.addEntry("module", changeSet.getModuleName());
114         insert.addEntry("change_set_id", changeSet.getId());
115         insert.addEntry("seq_nr", new Integer JavaDoc(changeSet.getSequenceNumber()));
116         return insert.getScript(dialect);
117     }
118
119 }
120
Popular Tags