/
XmlPublisherDatabaseWriterFilter.java
122 lines (109 loc) · 4.5 KB
/
XmlPublisherDatabaseWriterFilter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/**
* Licensed to JumpMind Inc under one or more contributor
* license agreements. See the NOTICE file distributed
* with this work for additional information regarding
* copyright ownership. JumpMind Inc licenses this file
* to you under the GNU General Public License, version 3.0 (GPLv3)
* (the "License"); you may not use this file except in compliance
* with the License.
*
* You should have received a copy of the GNU General Public License,
* version 3.0 (GPLv3) along with this library; if not, see
* <http://www.gnu.org/licenses/>.
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jumpmind.symmetric.integrate;
import java.util.List;
import java.util.Set;
import org.jdom.Element;
import org.jumpmind.db.model.Table;
import org.jumpmind.symmetric.ext.INodeGroupExtensionPoint;
import org.jumpmind.symmetric.io.data.CsvData;
import org.jumpmind.symmetric.io.data.DataContext;
import org.jumpmind.symmetric.io.data.DataEventType;
import org.jumpmind.symmetric.io.data.writer.IDatabaseWriterFilter;
/**
* This is an optional {@link IDatabaseWriterFilter} that
* is capable of translating table data to XML and publishing it for consumption
* by the enterprise. It uses JDOM internally to create an XML representation of
* SymmetricDS data.
* <p>
* This filter is typically configured as a Spring bean. The table names that
* should be published are identified by
* {@link #setTableNamesToPublishAsGroup(Set)}. Rows from tables can be grouped
* together (which get synchronized in the same batch) by identifying columns
* that are the same that act as a 'key' by setting
* {@link #setGroupByColumnNames(List)}
* <p>
* The {@link IPublisher} is typically configured and injected onto this bean as
* well. Provided is a {@link SimpleJmsPublisher}.
* <p>
* An example of the XML that is published is as follows:
*
* <pre>
* <batch id="2TEST2" nodeid="00001" time="12345678910">
* <row entity="TABLE_NAME" dml="I">
* <data key="id1">2</data>
* <data key="id2">TEST</data>
* <data key="id3">2</data>
* <data key="data1">Me</data>
* <data key="data2" nil="true"/>
* </row>
* </batch>
* </pre>
*/
public class XmlPublisherDatabaseWriterFilter extends AbstractXmlPublisherExtensionPoint implements
IPublisherFilter, INodeGroupExtensionPoint {
protected boolean loadDataInTargetDatabase = true;
public boolean beforeWrite(
DataContext context, Table table, CsvData data) {
if (tableNamesToPublishAsGroup == null
|| tableNamesToPublishAsGroup.contains(table.getName())) {
String[] rowData = data.getParsedData(CsvData.ROW_DATA);
if (data.getDataEventType() == DataEventType.DELETE) {
rowData = data.getParsedData(CsvData.OLD_DATA);
}
Element xml = getXmlFromCache(context, context.getBatch().getBinaryEncoding(),
table.getColumnNames(),
rowData, table.getPrimaryKeyColumnNames(),
data.getParsedData(CsvData.PK_DATA));
if (xml != null) {
toXmlElement(data.getDataEventType(), xml, table.getCatalog(), table.getSchema(),
table.getName(), table.getColumnNames(),
rowData, table.getPrimaryKeyColumnNames(), data.getParsedData(CsvData.PK_DATA));
}
}
return loadDataInTargetDatabase;
}
public void batchComplete(
DataContext context) {
if (doesXmlExistToPublish(context)) {
finalizeXmlAndPublish(context);
}
}
public void setLoadDataInTargetDatabase(boolean loadDataInTargetDatabase) {
this.loadDataInTargetDatabase = loadDataInTargetDatabase;
}
public void afterWrite(
DataContext context, Table table, CsvData data) {
}
public boolean handlesMissingTable(
DataContext context, Table table) {
return false;
}
public void earlyCommit(
DataContext context) {
}
public void batchCommitted(
DataContext context) {
}
public void batchRolledback(
DataContext context) {
}
}