Skip to main content
summaryrefslogtreecommitdiffstats
blob: f1a55cd85d7855dd218b874b30e63278b8d7f805 (plain) (blame)
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*******************************************************************************
 * Copyright (c) 2004, 2007 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/

package org.eclipse.osee.database.schema.internal.data;

import java.util.ArrayList;
import java.util.List;
import org.eclipse.osee.framework.jdk.core.persistence.Xmlizable;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

/**
 * @author Roberto E. Escobar
 */
public class ReferenceClause implements Xmlizable {
   public static final String REFERENCES_TAG = "References";

   public enum ReferencesFields {
      schema,
      table,
      column,
      onUpdate,
      onDelete;
   }

   public enum OnDeleteEnum {
      NO_ACTION,
      RESTRICT,
      CASCADE,
      SET_NULL,
      UNSPECIFIED;

      @Override
      public String toString() {
         String toReturn = super.toString();
         toReturn = toReturn.replaceAll("_", " ");
         return toReturn;
      }
   }

   public enum OnUpdateEnum {
      NO_ACTION,
      RESTRICT,
      UNSPECIFIED;

      @Override
      public String toString() {
         String toReturn = super.toString();
         toReturn = toReturn.replaceAll("_", " ");
         return toReturn;
      }
   }

   private String schema;
   private String table;
   List<String> columns;
   OnDeleteEnum onDeleteAction;
   OnUpdateEnum onUpdateAction;

   public ReferenceClause(String schema, String table) {
      this.schema = schema.toUpperCase();
      this.schema = this.schema.trim();
      this.table = table.toUpperCase();
      this.table = this.table.trim();
      this.columns = new ArrayList<String>();
      this.onDeleteAction = OnDeleteEnum.UNSPECIFIED;
      this.onUpdateAction = OnUpdateEnum.UNSPECIFIED;
   }

   public void setSchema(String schema) {
      this.schema = schema;
   }

   public OnDeleteEnum getOnDeleteAction() {
      return onDeleteAction;
   }

   public OnUpdateEnum getOnUpdateAction() {
      return onUpdateAction;
   }

   public void setOnDeleteAction(OnDeleteEnum onDeleteAction) {
      this.onDeleteAction = onDeleteAction;
   }

   public void setOnUpdateAction(OnUpdateEnum onUpdateAction) {
      this.onUpdateAction = onUpdateAction;
   }

   public void addColumn(String column) {
      column = column.toUpperCase();
      column = column.trim();
      columns.add(column);
   }

   public String getFullyQualifiedTableName() {
      return schema + "." + table;
   }

   public List<String> getColumns() {
      return columns;
   }

   public String getCommaSeparatedColumnsList() {
      return org.eclipse.osee.framework.jdk.core.util.Collections.toString(",", columns);
   }

   @Override
   public String toString() {
      String toReturn = REFERENCES_TAG + ": " + getFullyQualifiedTableName();
      toReturn += "\t[";
      for (String column : columns) {
         toReturn += column + " ";
      }
      toReturn += "]";
      return toReturn;
   }

   @Override
   public Element toXml(Document doc) {
      Element refElement = doc.createElement(REFERENCES_TAG);
      refElement.setAttribute(ReferencesFields.schema.name(), schema);
      refElement.setAttribute(ReferencesFields.table.name(), table);
      refElement.setAttribute(ReferencesFields.column.name(), getCommaSeparatedColumnsList());

      if (!onDeleteAction.equals(OnDeleteEnum.UNSPECIFIED)) {
         refElement.setAttribute(ReferencesFields.onDelete.name(), onDeleteAction.toString());
      }

      if (!onUpdateAction.equals(OnUpdateEnum.UNSPECIFIED)) {
         refElement.setAttribute(ReferencesFields.onUpdate.name(), onUpdateAction.toString());
      }
      return refElement;
   }

   @Override
   public int hashCode() {
      final int prime = 31;
      int result = 1;
      result = prime * result + ((columns == null) ? 0 : columns.hashCode());
      result = prime * result + ((onDeleteAction == null) ? 0 : onDeleteAction.hashCode());
      result = prime * result + ((onUpdateAction == null) ? 0 : onUpdateAction.hashCode());
      result = prime * result + ((schema == null) ? 0 : schema.hashCode());
      result = prime * result + ((table == null) ? 0 : table.hashCode());
      return result;
   }

   @Override
   public boolean equals(Object obj) {
      if (this == obj) {
         return true;
      }
      if (obj == null) {
         return false;
      }
      if (getClass() != obj.getClass()) {
         return false;
      }
      ReferenceClause other = (ReferenceClause) obj;
      if (columns == null) {
         if (other.columns != null) {
            return false;
         }
      } else if (!columns.equals(other.columns)) {
         return false;
      }
      if (onDeleteAction != other.onDeleteAction) {
         return false;
      }
      if (onUpdateAction != other.onUpdateAction) {
         return false;
      }
      if (schema == null) {
         if (other.schema != null) {
            return false;
         }
      } else if (!schema.equals(other.schema)) {
         return false;
      }
      if (table == null) {
         if (other.table != null) {
            return false;
         }
      } else if (!table.equals(other.table)) {
         return false;
      }
      return true;
   }

}

Back to the top