001: /*
002: * ViewDiff.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.db.diff;
013:
014: import java.util.ArrayList;
015: import java.util.Collection;
016: import java.util.List;
017: import workbench.db.IndexDefinition;
018: import workbench.db.report.ReportView;
019: import workbench.db.report.TagAttribute;
020: import workbench.db.report.TagWriter;
021: import workbench.util.StrBuffer;
022:
023: /**
024: * Compares two database views for differences in their definition.
025: * The generating source of the views is compared using String.equals(),
026: * so any difference in Upper/Lowercase writing (even if not important
027: * for the functionality of the view) qualify the two views as
028: * beeing different.
029: *
030: * @author support@sql-workbench.net
031: */
032: public class ViewDiff {
033: public static final String TAG_CREATE_VIEW = "create-view";
034: public static final String TAG_UPDATE_VIEW = "update-view";
035:
036: private ReportView reference;
037: private ReportView target;
038: private TagWriter writer;
039: private StrBuffer indent;
040:
041: public ViewDiff(ReportView ref, ReportView tar) {
042: reference = ref;
043: target = tar;
044: }
045:
046: public StrBuffer getMigrateTargetXml() {
047: StrBuffer result = new StrBuffer(500);
048: if (this .writer == null)
049: this .writer = new TagWriter();
050:
051: StrBuffer myindent = new StrBuffer(indent);
052: myindent.append(" ");
053: boolean sourceDifferent = false;
054: boolean indexDifferent = false;
055: boolean createView = (target == null);
056:
057: CharSequence s = null;
058:
059: s = reference.getViewSource();
060: String refSource = (s == null ? null : s.toString());
061: s = (target == null ? null : target.getViewSource());
062: String targetSource = (s == null ? null : s.toString());
063:
064: if (targetSource != null) {
065: sourceDifferent = !refSource.trim().equals(
066: targetSource.trim());
067: }
068:
069: StrBuffer indexDiff = getIndexDiff();
070: if (indexDiff != null && indexDiff.length() > 0) {
071: indexDifferent = true;
072: }
073:
074: if (!sourceDifferent && !createView && !indexDifferent)
075: return result;
076:
077: List<TagAttribute> att = new ArrayList<TagAttribute>();
078:
079: String type = reference.getView().getType();
080: if (!"VIEW".equals(type)) {
081: att.add(new TagAttribute("type", type));
082: }
083:
084: if (indexDifferent && !sourceDifferent) {
085: att.add(new TagAttribute("name", target.getView()
086: .getTableName()));
087: }
088:
089: writer.appendOpenTag(result, this .indent,
090: (createView ? TAG_CREATE_VIEW : TAG_UPDATE_VIEW), att,
091: true);
092:
093: result.append('\n');
094: if (createView) {
095: result.append(reference.getXml(myindent, true));
096: } else if (sourceDifferent) {
097: result.append(reference.getXml(myindent, indexDifferent));
098: } else {
099: result.append(indexDiff);
100: }
101: writer.appendCloseTag(result, this .indent,
102: (createView ? TAG_CREATE_VIEW : TAG_UPDATE_VIEW));
103:
104: return result;
105: }
106:
107: private StrBuffer getIndexDiff() {
108: if (this .target == null)
109: return null;
110:
111: Collection<IndexDefinition> ref = this .reference.getIndexList();
112: Collection<IndexDefinition> targ = this .target.getIndexList();
113: if (ref == null && targ == null)
114: return null;
115: IndexDiff id = new IndexDiff(ref, targ);
116: id.setTagWriter(this .writer);
117: id.setIndent(indent);
118: StrBuffer diff = id.getMigrateTargetXml();
119: return diff;
120: }
121:
122: /**
123: * Set the {@link workbench.db.report.TagWriter} to
124: * be used for writing the XML tags
125: */
126: public void setTagWriter(TagWriter tagWriter) {
127: this .writer = tagWriter;
128: }
129:
130: /**
131: * Set an indent for generating the XML
132: */
133: public void setIndent(String ind) {
134: if (ind == null)
135: this .indent = null;
136: this .indent = new StrBuffer(ind);
137: }
138:
139: /**
140: * Set an indent for generating the XML
141: */
142: public void setIndent(StrBuffer ind) {
143: this.indent = ind;
144: }
145:
146: }
|