Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 271eddb51095bb3b147d1236f84a0223bf34f244 (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
/*
 * Copyright (c) 2004 - 2011 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
 *    Ibrahim Sallam - code refactoring for CDO 3.0
 */
package org.eclipse.emf.cdo.server.internal.objectivity.mapper;

import org.eclipse.emf.cdo.common.model.EMFUtil;
import org.eclipse.emf.cdo.server.internal.objectivity.bundle.OM;

import org.eclipse.net4j.util.om.trace.ContextTracer;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EAttribute;
import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.EDataType;
import org.eclipse.emf.ecore.EEnum;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.EcorePackage;
import org.eclipse.emf.ecore.util.EcoreUtil;

import java.util.HashMap;

public class ObjyMapper
{
  public static ObjyMapper INSTANCE = new ObjyMapper();

  private static final ContextTracer TRACER_DEBUG = new ContextTracer(OM.DEBUG, ObjyMapper.class);

  protected HashMap<EClassifier, ITypeMapper> typeMap = new HashMap<EClassifier, ITypeMapper>();

  protected HashMap<EClassifier, ITypeMapper> manyTypeMap = new HashMap<EClassifier, ITypeMapper>();

  private ITypeMapper manyRef = ManyReferenceMapper.INSTANCE;

  // private AttributeBridge manyRef = new ManyReferenceMapperTreeListX();
  private ITypeMapper singleRef = new SingleReferenceMapper();

  // private ITypeMapper singleContRef = new SingleContainementReferenceMapper();

  public ObjyMapper()
  {
    // TODO - this is from the old code. Verify if we still need it?!!!!
    // MultipleTypeMapper multipleMapper = new MultipleTypeMapper();
    // multipleMapper.add((IManyTypeMapper)manyRef);
    // multipleMapper.add(new IndexesReferenceMapper());
    // manyRef = multipleMapper;

    initMap();
    initManyMap();
  }

  public ITypeMapper getTypeMapper(EStructuralFeature feature)
  {
    if (feature == null || !EMFUtil.isPersistent(feature))
    {
      if (TRACER_DEBUG.isEnabled())
      {
        TRACER_DEBUG.trace("feature is transient " + feature);
      }
      return null;
    }

    boolean isMany = false;

    if (feature.isMany())
    {
      isMany = true;
    }

    if (feature instanceof EAttribute)
    {
      // PROBLEM
      EObject type = feature.getEType();
      if (type.eIsProxy())
      {
        URI a = EcoreUtil.getURI(type);
        type = EcorePackage.eINSTANCE.eResource().getEObject(a.fragment());
      }
      ITypeMapper attrMapper = null;
      if (isMany)
      {
        attrMapper = manyTypeMap.get(type);
      }
      else
      {
        attrMapper = typeMap.get(type);
      }
      if (attrMapper != null)
      {
        return attrMapper;
      }

      if (type instanceof EEnum)
      {
        return EnumTypeMapper.INSTANCE;
      }

      /*** handle custom types... ***/
      if (type instanceof EDataType)
      {
        if (isMany)
        {
          return CustomDataManyTypeMapper.INSTANCE;
        }

        return CustomDataTypeMapper.INSTANCE;
      }
    }
    else if (feature instanceof EReference)
    {
      if (isMany)
      {
        return manyRef;
      }

      return singleRef;
    }

    TRACER_DEBUG.trace("ERROR: " + feature.getEType() + " not supported for feature "
        + ((EClassifier)feature.eContainer()).getName() + "." + feature.getName());
    throw new RuntimeException(feature.getEType() + " not supported for feature "
        + ((EClassifier)feature.eContainer()).getName() + "." + feature.getName());
  }

  protected void initMap()
  {
    // TODO Do not support these type yet
    typeMap.put(EcorePackage.eINSTANCE.getEBoolean(), NumericTypeMapper.TMBOOLEAN);
    typeMap.put(EcorePackage.eINSTANCE.getEByte(), NumericTypeMapper.TMBYTE);
    typeMap.put(EcorePackage.eINSTANCE.getEChar(), NumericTypeMapper.TMCHAR);
    typeMap.put(EcorePackage.eINSTANCE.getEDate(), NumericTypeMapper.TMDATE);
    typeMap.put(EcorePackage.eINSTANCE.getEDouble(), NumericTypeMapper.TMDOUBLE);
    typeMap.put(EcorePackage.eINSTANCE.getEFloat(), NumericTypeMapper.TMFLOAT);
    typeMap.put(EcorePackage.eINSTANCE.getEInt(), NumericTypeMapper.TMINTEGER);
    typeMap.put(EcorePackage.eINSTANCE.getELong(), NumericTypeMapper.TMLONG);
    typeMap.put(EcorePackage.eINSTANCE.getEShort(), NumericTypeMapper.TMSHORT);
    typeMap.put(EcorePackage.eINSTANCE.getEString(), StringTypeMapper.INSTANCE);

    typeMap.put(EcorePackage.eINSTANCE.getEBooleanObject(), NumericTypeMapper.TMBOOLEAN);
    typeMap.put(EcorePackage.eINSTANCE.getEByteObject(), NumericTypeMapper.TMBYTE);
    typeMap.put(EcorePackage.eINSTANCE.getECharacterObject(), NumericTypeMapper.TMCHAR);
    typeMap.put(EcorePackage.eINSTANCE.getEDoubleObject(), NumericTypeMapper.TMDOUBLE);
    typeMap.put(EcorePackage.eINSTANCE.getEIntegerObject(), NumericTypeMapper.TMINTEGER);
    typeMap.put(EcorePackage.eINSTANCE.getELongObject(), NumericTypeMapper.TMLONG);
    typeMap.put(EcorePackage.eINSTANCE.getEFloatObject(), NumericTypeMapper.TMFLOAT);

    // the EByteArray doesn't show us as isMany()== true?!!!!
    typeMap.put(EcorePackage.eINSTANCE.getEByteArray(), ByteArrayTypeMapper.INSTANCE);
    typeMap.put(EcorePackage.eINSTANCE.getEBigDecimal(), BigDecimalTypeMapper.INSTANCE);
    typeMap.put(EcorePackage.eINSTANCE.getEBigInteger(), BigIntegerTypeMapper.INSTANCE);

  }

  private void initManyMap()
  {
    // TODO Do not support these type yet
    // typeMap.put(EcorePackage.eINSTANCE.getEBigDecimal()
    // typeMap.put(EcorePackage.eINSTANCE.getEBigInteger()

    manyTypeMap.put(EcorePackage.eINSTANCE.getEBoolean(), NumericManyTypeMapper.TMBOOLEAN);
    manyTypeMap.put(EcorePackage.eINSTANCE.getEByte(), NumericManyTypeMapper.TMBYTE);
    manyTypeMap.put(EcorePackage.eINSTANCE.getEChar(), NumericManyTypeMapper.TMCHAR);
    manyTypeMap.put(EcorePackage.eINSTANCE.getEDate(), NumericManyTypeMapper.TMDATE);
    manyTypeMap.put(EcorePackage.eINSTANCE.getEDouble(), NumericManyTypeMapper.TMDOUBLE);
    manyTypeMap.put(EcorePackage.eINSTANCE.getEFloat(), NumericManyTypeMapper.TMFLOAT);
    manyTypeMap.put(EcorePackage.eINSTANCE.getEInt(), NumericManyTypeMapper.TMINTEGER);
    manyTypeMap.put(EcorePackage.eINSTANCE.getELong(), NumericManyTypeMapper.TMLONG);
    manyTypeMap.put(EcorePackage.eINSTANCE.getEShort(), NumericManyTypeMapper.TMSHORT);
    manyTypeMap.put(EcorePackage.eINSTANCE.getEString(), StringManyTypeMapper.INSTANCE);

    manyTypeMap.put(EcorePackage.eINSTANCE.getEBooleanObject(), NumericManyTypeMapper.TMBOOLEAN);
    manyTypeMap.put(EcorePackage.eINSTANCE.getEByteObject(), NumericManyTypeMapper.TMBYTE);
    manyTypeMap.put(EcorePackage.eINSTANCE.getECharacterObject(), NumericManyTypeMapper.TMCHAR);
    manyTypeMap.put(EcorePackage.eINSTANCE.getEDoubleObject(), NumericManyTypeMapper.TMDOUBLE);
    manyTypeMap.put(EcorePackage.eINSTANCE.getEFloatObject(), NumericManyTypeMapper.TMFLOAT);
    manyTypeMap.put(EcorePackage.eINSTANCE.getEIntegerObject(), NumericManyTypeMapper.TMINTEGER);
    manyTypeMap.put(EcorePackage.eINSTANCE.getELongObject(), NumericManyTypeMapper.TMLONG);

    manyTypeMap.put(EcorePackage.eINSTANCE.getEByteArray(), NumericManyTypeMapper.TMBYTE);
    manyTypeMap.put(EcorePackage.eINSTANCE.getEFeatureMapEntry(), FeatureMapTypeMapper.INSTANCE);
  }

}

Back to the top