1 /*
2 * PROJECT : DAR Runtime and Tools
3 * COPYRIGHT : Copyright (C) 1999-2004 tim.stephenson@enableit.org
4 * LICENSE : GNU LESSER GENERAL PUBLIC LICENSE
5 * Version 2.1, February 1999
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program 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
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21 package org.enableit.db.darrt.resolvers;
22
23 import java.sql.Connection;
24 import java.util.HashMap;
25
26 import org.apache.log4j.Logger;
27 import org.enableit.db.ConnFactory;
28 import org.enableit.db.DBException;
29 import org.enableit.db.DBFilter;
30 import org.enableit.db.DBUpdate;
31 import org.enableit.db.DatabaseProxy;
32 import org.enableit.db.beans.Col;
33 import org.enableit.db.beans.Column;
34 import org.enableit.db.beans.Provider;
35 import org.enableit.db.beans.Row;
36 import org.enableit.db.beans.RowSet;
37 import org.enableit.db.beans.Table;
38 import org.enableit.db.darrt.ConflictResolver;
39 import org.enableit.db.darrt.DataHandler;
40 import org.enableit.db.darrt.MetaDataFactory;
41
42
43 /***
44 * Conflict Resolver to replace existing rows with new ones in all cases.
45 * @author Tim Stephenson
46 */
47 public class DeleteAndInsertConflictResolver implements ConflictResolver {
48 /*
49 * Properties
50 */
51
52 /***
53 * The Log4J <code>Logger</code> doing the logging.
54 */
55 private static Logger logger = Logger.getLogger(DeleteAndInsertConflictResolver.class);
56
57 /***
58 * CVS info ABOUT this class and its current version
59 */
60 public static final String about = "$Revision $";
61
62 /*
63 * Constructors
64 */
65
66 /***
67 * Default Constructor
68 */
69 public DeleteAndInsertConflictResolver() {
70 }
71
72 /*
73 * Methods
74 */
75
76 /***
77 * Remove the existing row that shares the same primary key,
78 * replacing it with the one received.
79 */
80 public RowSet resolve(Provider target, RowSet rows) {
81 Connection conn = null;
82
83 try {
84 Table table = MetaDataFactory.getInstance().getMetaData(target,
85 rows.getTable());
86 HashMap pksTemplate = new HashMap();
87 DBUpdate delete = new DBUpdate("DELETE FROM " + rows.getTable());
88 boolean foundPk = false;
89
90 for (int i = 0; i < table.getColumnCount(); i++) {
91 Column column = table.getColumn(i);
92
93 if ("TRUE".equalsIgnoreCase(column.getPrimaryKey())) {
94 pksTemplate.put(column.getColName(), null);
95 foundPk = true;
96 }
97 }
98
99 if (!foundPk) {
100 throw new DBException(
101 "Cannot use DeleteAndInsertConflictResolver with table that has no primary key!");
102 }
103
104 conn = ConnFactory.getConnection(target);
105
106 for (int i = 0; i < rows.getRowCount(); i++) {
107 Row row = rows.getRow(i);
108
109 for (int j = 0; j < row.getColCount(); j++) {
110 Col col = row.getCol(j);
111
112 if (pksTemplate.keySet().contains(col.getName())) {
113 delete.addFilter(new DBFilter(col.getName(),
114 col.getValue(), col.getType(), DBFilter.EQ));
115 }
116 }
117
118 int r = DatabaseProxy.executeUpdate(conn, delete.getSql(),
119 delete.getParameters());
120 }
121
122 // Now use DataHandler to resubmit and should have no errors.
123 DataHandler dh = new DataHandler();
124
125 rows = dh.importData(target, rows);
126 } catch (Exception e) {
127 // returned row set will include the unfixed rows
128 logger.error(e.getMessage(), e);
129 } finally {
130 try {
131 conn.close();
132 } catch (Exception e) {
133 ;
134 }
135
136 conn = null;
137 }
138
139 return rows;
140 }
141 }
This page was automatically generated by Maven