01: /*
02: * TableGrantDiff.java
03: *
04: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
05: *
06: * Copyright 2002-2008, Thomas Kellerer
07: * No part of this code maybe reused without the permission of the author
08: *
09: * To contact the author please send an email to: support@sql-workbench.net
10: *
11: */
12: package workbench.db.diff;
13:
14: import java.util.Collection;
15: import java.util.LinkedList;
16: import workbench.db.TableGrant;
17: import workbench.db.report.ReportTableGrants;
18: import workbench.db.report.TagWriter;
19: import workbench.util.StrBuffer;
20:
21: /**
22: * @author support@sql-workbench.net
23: */
24: public class TableGrantDiff {
25: public static final String TAG_ADD_GRANTS = "add-grants";
26: public static final String TAG_REVOKE_GRANTS = "revoke-grants";
27:
28: private Collection<TableGrant> referenceGrants;
29: private Collection<TableGrant> targetGrants;
30:
31: public TableGrantDiff(ReportTableGrants reference,
32: ReportTableGrants target) {
33: if (reference != null) {
34: this .referenceGrants = reference.getGrants();
35: }
36:
37: if (target != null) {
38: this .targetGrants = target.getGrants();
39: }
40: }
41:
42: public StrBuffer getMigrateTargetXml(TagWriter writer,
43: StrBuffer indent) {
44: Collection<TableGrant> grantsToAdd = new LinkedList<TableGrant>();
45: if (this .referenceGrants != null) {
46: grantsToAdd.addAll(this .referenceGrants);
47: }
48: if (this .targetGrants != null) {
49: grantsToAdd.removeAll(targetGrants);
50: }
51:
52: Collection<TableGrant> grantsToRemove = new LinkedList<TableGrant>();
53: if (this .targetGrants != null) {
54: grantsToRemove.addAll(targetGrants);
55: }
56: if (this .referenceGrants != null) {
57: grantsToRemove.removeAll(referenceGrants);
58: }
59:
60: if (grantsToAdd.size() == 0 && grantsToRemove.size() == 0)
61: return null;
62:
63: StrBuffer result = new StrBuffer(grantsToAdd.size() * 50
64: + grantsToRemove.size() * 50);
65: StrBuffer indent2 = new StrBuffer(indent);
66: indent2.append(" ");
67: StrBuffer indent3 = new StrBuffer(indent2);
68: indent3.append(" ");
69: if (grantsToAdd.size() > 0) {
70: ReportTableGrants report = new ReportTableGrants(
71: grantsToAdd);
72: report.setNamespace(writer.getNamespace());
73: writer.appendOpenTag(result, indent2, TAG_ADD_GRANTS);
74: result.append('\n');
75: report.appendXml(result, indent3);
76: writer.appendCloseTag(result, indent2, TAG_ADD_GRANTS);
77: result.append('\n');
78: }
79:
80: if (grantsToRemove.size() > 0) {
81: ReportTableGrants report = new ReportTableGrants(
82: grantsToRemove);
83: report.setNamespace(writer.getNamespace());
84: writer.appendOpenTag(result, indent2, TAG_REVOKE_GRANTS);
85: result.append('\n');
86: report.appendXml(result, indent3);
87: writer.appendCloseTag(result, indent2, TAG_REVOKE_GRANTS);
88: result.append('\n');
89: }
90: return result;
91: }
92:
93: }
|