Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c992b8f9addc7e32b3562197db32b7635408e59f (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
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
 * Copyright (c) 2011-2013, 2015, 2016 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:
 *    Simon McDuff - initial API and implementation
 *    Eike Stepper - maintenance
 */
package org.eclipse.emf.internal.cdo.object;

import org.eclipse.emf.cdo.CDODeltaNotification;
import org.eclipse.emf.cdo.CDOObject;
import org.eclipse.emf.cdo.common.id.CDOID;
import org.eclipse.emf.cdo.common.id.CDOIDExternal;
import org.eclipse.emf.cdo.common.revision.delta.CDORevisionDelta;
import org.eclipse.emf.cdo.util.CDOUtil;
import org.eclipse.emf.cdo.util.ObjectNotFoundException;

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.util.ECollections;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EAttribute;
import org.eclipse.emf.ecore.EDataType;
import org.eclipse.emf.ecore.EEnum;
import org.eclipse.emf.ecore.EEnumLiteral;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.InternalEObject;
import org.eclipse.emf.ecore.impl.ENotificationImpl;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.spi.cdo.InternalCDOObject;
import org.eclipse.emf.spi.cdo.InternalCDOView;

/**
 * @author Simon McDuff
 * @since 2.0
 */
public class CDODeltaNotificationImpl extends ENotificationImpl implements CDODeltaNotification
{
  private CDORevisionDelta revisionDelta;

  public CDODeltaNotificationImpl(InternalEObject notifier, int eventType, EStructuralFeature feature, Object oldValue, Object newValue)
  {
    super(getEObject(notifier), eventType, feature, oldValue, newValue);
  }

  public CDODeltaNotificationImpl(InternalEObject notifier, int eventType, EStructuralFeature feature, Object oldValue, Object newValue, int position)
  {
    super(getEObject(notifier), eventType, feature, oldValue, newValue, position);
  }

  public CDODeltaNotificationImpl(InternalEObject notifier, int eventType, EStructuralFeature feature, boolean oldBooleanValue, boolean newBooleanValue)
  {
    super(getEObject(notifier), eventType, feature.getFeatureID(), oldBooleanValue, newBooleanValue);
    this.feature = feature;
  }

  public CDODeltaNotificationImpl(InternalEObject notifier, int eventType, EStructuralFeature feature, byte oldByteValue, byte newByteValue)
  {
    super(getEObject(notifier), eventType, feature.getFeatureID(), oldByteValue, newByteValue);
    this.feature = feature;
  }

  public CDODeltaNotificationImpl(InternalEObject notifier, int eventType, EStructuralFeature feature, char oldCharValue, char newCharValue)
  {
    super(getEObject(notifier), eventType, feature.getFeatureID(), oldCharValue, newCharValue);
    this.feature = feature;
  }

  public CDODeltaNotificationImpl(InternalEObject notifier, int eventType, EStructuralFeature feature, double oldDoubleValue, double newDoubleValue)
  {
    super(getEObject(notifier), eventType, feature.getFeatureID(), oldDoubleValue, newDoubleValue);
    this.feature = feature;
  }

  public CDODeltaNotificationImpl(InternalEObject notifier, int eventType, EStructuralFeature feature, float oldFloatValue, float newFloatValue)
  {
    super(getEObject(notifier), eventType, feature.getFeatureID(), oldFloatValue, newFloatValue);
    this.feature = feature;
  }

  public CDODeltaNotificationImpl(InternalEObject notifier, int eventType, EStructuralFeature feature, int oldIntValue, int newIntValue)
  {
    super(getEObject(notifier), eventType, feature.getFeatureID(), oldIntValue, newIntValue);
    this.feature = feature;
  }

  public CDODeltaNotificationImpl(InternalEObject notifier, int eventType, EStructuralFeature feature, long oldLongValue, long newLongValue)
  {
    super(getEObject(notifier), eventType, feature.getFeatureID(), oldLongValue, newLongValue);
    this.feature = feature;
  }

  public CDODeltaNotificationImpl(InternalEObject notifier, int eventType, EStructuralFeature feature, short oldShortValue, short newShortValue)
  {
    super(getEObject(notifier), eventType, feature.getFeatureID(), oldShortValue, newShortValue);
    this.feature = feature;
  }

  @Override
  public Object getNewValue()
  {
    Object object = super.getNewValue();
    return adapt(object);
  }

  @Override
  public Object getOldValue()
  {
    Object oldValue = super.getOldValue();
    if (oldValue == null && getEventType() == Notification.REMOVE_MANY)
    {
      Object feature = getFeature();
      if (feature instanceof EStructuralFeature)
      {
        EStructuralFeature structuralFeature = (EStructuralFeature)feature;
        if (structuralFeature.isMany())
        {
          return ECollections.emptyEList();
        }
      }
    }

    return adapt(oldValue);
  }

  public Object adapt(Object object)
  {
    if (object instanceof CDOID)
    {
      CDOID id = (CDOID)object;

      if (id instanceof CDOIDExternal)
      {
        CDOIDExternal idExternal = (CDOIDExternal)id;
        Resource resource = notifier.eResource();
        if (resource != null)
        {
          String uriString = idExternal.getURI();
          URI uri = URI.createURI(uriString);
          object = resource.getResourceSet().getEObject(uri, false);
        }
      }
      else
      {
        try
        {
          InternalCDOView view = getCDOObject().cdoView();
          object = view.getObject(id, true);
        }
        catch (ObjectNotFoundException ex)
        {
          object = null;
        }
      }
    }

    if (object instanceof CDOObject)
    {
      object = CDOUtil.getEObject((EObject)object);
    }

    if (feature instanceof EAttribute)
    {
      if (object == null)
      {
        object = feature.getDefaultValue();
      }
      else
      {
        EDataType eAttributeType = ((EAttribute)feature).getEAttributeType();
        if (eAttributeType != null)
        {
          if (eAttributeType instanceof EEnum && object instanceof Integer)
          {
            EEnumLiteral literal = ((EEnum)eAttributeType).getEEnumLiteral((Integer)object);
            String stringValue = literal.getLiteral();
            object = eAttributeType.getEPackage().getEFactoryInstance().createFromString(eAttributeType, stringValue);
          }
        }
      }
    }

    return object;
  }

  public boolean hasNext()
  {
    return next != null;
  }

  public CDORevisionDelta getRevisionDelta()
  {
    return revisionDelta;
  }

  public void setRevisionDelta(CDORevisionDelta revisionDelta)
  {
    this.revisionDelta = revisionDelta;
  }

  @Override
  public boolean merge(Notification notification)
  {
    // Do not merge at all. See bug 317144.
    return false;
  }

  private InternalCDOObject getCDOObject()
  {
    return (InternalCDOObject)CDOUtil.getCDOObject((EObject)getNotifier());
  }

  private static InternalEObject getEObject(InternalEObject notifier)
  {
    return (InternalEObject)CDOUtil.getEObject(notifier);
  }
}

Back to the top