Skip to main content
summaryrefslogtreecommitdiffstats
blob: 639b070d4a53af267cafaac91a4adde9ab69d321 (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
199
200
201
202
203
204
205
206
207
208
/*
 * Copyright (c) 2004 - 2012 Eike Stepper (Berlin, Germany) and others.
 * 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:
 *    Eike Stepper - initial API and implementation
 */
package org.eclipse.net4j.internal.db.ddl.delta;

import org.eclipse.net4j.db.ddl.IDBSchemaElement;
import org.eclipse.net4j.db.ddl.delta.IDBDelta;
import org.eclipse.net4j.db.ddl.delta.IDBDeltaVisitor;
import org.eclipse.net4j.db.ddl.delta.IDBDeltaWithPosition;
import org.eclipse.net4j.spi.db.DBNamedElement;
import org.eclipse.net4j.spi.db.DBSchemaElement;
import org.eclipse.net4j.util.StringUtil;

import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author Eike Stepper
 */
public abstract class DBDelta extends DBNamedElement implements IDBDelta
{
  private static final IDBDelta[] NO_ELEMENTS = {};

  private static final long serialVersionUID = 1L;

  private DBDelta parent;

  private ChangeKind changeKind;

  private transient IDBDelta[] elements;

  public DBDelta(DBDelta parent, String name, ChangeKind changeKind)
  {
    super(name);
    this.parent = parent;
    this.changeKind = changeKind;
  }

  /**
   * Constructor for deserialization.
   */
  protected DBDelta()
  {
  }

  public DBDelta getParent()
  {
    return parent;
  }

  public final ChangeKind getChangeKind()
  {
    return changeKind;
  }

  public final int compareTo(IDBDelta delta2)
  {
    int result = getDeltaType().compareTo(delta2.getDeltaType());
    if (result == 0)
    {
      if (this instanceof IDBDeltaWithPosition && delta2 instanceof IDBDeltaWithPosition)
      {
        IDBDeltaWithPosition withPosition1 = (IDBDeltaWithPosition)this;
        IDBDeltaWithPosition withPosition2 = (IDBDeltaWithPosition)delta2;
        return withPosition1.getPosition() - withPosition2.getPosition();
      }

      result = getName().compareTo(delta2.getName());
    }

    return result;
  }

  public final void accept(IDBDeltaVisitor visitor)
  {
    doAccept(visitor);

    IDBDelta[] deltas = getElements();
    for (IDBDelta delta : deltas)
    {
      delta.accept(visitor);
    }
  }

  protected abstract void doAccept(IDBDeltaVisitor visitor);

  public final boolean isEmpty()
  {
    return getElements().length == 0;
  }

  public final IDBDelta[] getElements()
  {
    if (elements == null)
    {
      List<IDBDelta> deltas = new ArrayList<IDBDelta>();
      collectElements(deltas);

      if (deltas.isEmpty())
      {
        elements = NO_ELEMENTS;
      }
      else
      {
        elements = deltas.toArray(new IDBDelta[deltas.size()]);
        Arrays.sort(elements);
      }
    }

    return elements;
  }

  protected final void resetElements()
  {
    elements = null;
  }

  protected abstract void collectElements(List<IDBDelta> elements);

  @Override
  public void dump(Writer writer) throws IOException
  {
    int level = getLevel();
    for (int i = 0; i < level; i++)
    {
      writer.append("  ");
    }

    writer.append(getChangeKind().toString());
    writer.append(" ");
    writer.append(getDeltaType().toString());
    writer.append(" ");
    writer.append(getName());
    dumpAdditionalProperties(writer);
    writer.append(StringUtil.NL);

    for (IDBDelta delta : getElements())
    {
      ((DBDelta)delta).dump(writer);
    }
  }

  protected void dumpAdditionalProperties(Writer writer) throws IOException
  {
  }

  private int getLevel()
  {
    if (parent == null)
    {
      return 0;
    }

    return parent.getLevel() + 1;
  }

  public static String getName(IDBSchemaElement element, IDBSchemaElement oldElement)
  {
    return oldElement == null ? element.getName() : oldElement.getName();
  }

  public static ChangeKind getChangeKind(Object object, Object oldObject)
  {
    return object == null ? ChangeKind.REMOVED : oldObject == null ? ChangeKind.ADDED : ChangeKind.CHANGED;
  }

  protected static <T extends IDBSchemaElement> void compare(T[] elements, T[] oldElements,
      SchemaElementComparator<T> comparator)
  {
    for (int i = 0; i < elements.length; i++)
    {
      T element = elements[i];
      String name = element.getName();

      T oldElement = DBSchemaElement.findElement(oldElements, name);
      comparator.compare(element, oldElement);
    }

    for (int i = 0; i < oldElements.length; i++)
    {
      T oldElement = oldElements[i];
      String name = oldElement.getName();

      if (DBSchemaElement.findElement(elements, name) == null)
      {
        comparator.compare(null, oldElement);
      }
    }
  }

  /**
   * @author Eike Stepper
   */
  public interface SchemaElementComparator<T extends IDBSchemaElement>
  {
    public void compare(T element, T oldElement);
  }
}

Back to the top