001: /*
002: * TableRelation.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2007, 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.report;
013:
014: import java.util.ArrayList;
015: import java.util.List;
016: import workbench.db.ColumnIdentifier;
017:
018: /**
019: * @author support@sql-workbench.net
020: */
021: public class TableRelation {
022: private int sourceTableId;
023: private int targetTableId;
024:
025: private String fkName;
026: private List<ColumnIdentifier> sourceColumns = new ArrayList<ColumnIdentifier>();
027: private List<ColumnIdentifier> targetColumns = new ArrayList<ColumnIdentifier>();
028: private int relationId;
029: private String deleteAction;
030: private String updateAction;
031:
032: public TableRelation(int id, int sourceId, int destId, String fk) {
033: relationId = id;
034: sourceTableId = sourceId;
035: targetTableId = destId;
036: fkName = fk;
037: }
038:
039: public int getColCount() {
040: return sourceColumns.size();
041: }
042:
043: public void addColumnReference(ColumnIdentifier sourceCol,
044: ColumnIdentifier targetCol) {
045: sourceColumns.add(sourceCol);
046: targetColumns.add(targetCol);
047: }
048:
049: private String wbAction2Designer(String action) {
050: if (action.equalsIgnoreCase("RESTRICT"))
051: return "0";
052: if (action.equalsIgnoreCase("CASCADE"))
053: return "1";
054: if (action.equalsIgnoreCase("SET NULL"))
055: return "2";
056: if (action.equalsIgnoreCase("NO ACTION"))
057: return "3";
058: if (action.equalsIgnoreCase("SET DEFAULT"))
059: return "4";
060: return "0";
061: }
062:
063: public void setDeleteAction(String wbAction) {
064: deleteAction = wbAction2Designer(wbAction);
065: }
066:
067: public void setUpdateAction(String wbAction) {
068: updateAction = wbAction2Designer(wbAction);
069: }
070:
071: public String getDeleteAction() {
072: return deleteAction;
073: }
074:
075: public String getUpdateAction() {
076: return updateAction;
077: }
078:
079: public int getRelationId() {
080: return relationId;
081: }
082:
083: public int getTargetTableId() {
084: return targetTableId;
085: }
086:
087: public int getSourceTableId() {
088: return sourceTableId;
089: }
090:
091: /**
092: * 1 = 1:n (identifying)
093: * 2 = 1:n (non-identifying)
094: * @return
095: */
096: public String getRelationKind() {
097: boolean isPK = true;
098: for (ColumnIdentifier col : targetColumns) {
099: if (!col.isPkColumn()) {
100: isPK = false;
101: break;
102: }
103: }
104: return isPK ? "1" : "2";
105: }
106:
107: /**
108: * Returns the fk columns as a string suitable to be put
109: * into a <tt>relation</tt> tag.
110: * @return
111: */
112: public String getColumns() {
113: StringBuilder result = new StringBuilder();
114: for (int i = 0; i < sourceColumns.size(); i++) {
115: result.append(targetColumns.get(0).getColumnName());
116: result.append('=');
117: result.append(sourceColumns.get(0).getColumnName());
118: result.append("\\n");
119: }
120: return result.toString();
121: }
122:
123: public String getRelationName() {
124: return fkName;
125: }
126:
127: }
|