Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 4e7a32808fdf11744215e5adf7627bd919c6f8b8 (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
/*
 * Copyright (c) 2009-2013, 2016 Eike Stepper (Loehne, 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:
 *    Martin Taal - initial api
 *    Eike Stepper - maintenance
 */
package org.eclipse.emf.cdo.server.internal.hibernate.tuplizer;

import org.eclipse.emf.cdo.common.id.CDOID;
import org.eclipse.emf.cdo.common.id.CDOIDExternal;
import org.eclipse.emf.cdo.common.id.CDOIDUtil;
import org.eclipse.emf.cdo.common.model.CDOClassifierRef;
import org.eclipse.emf.cdo.common.revision.CDORevision;
import org.eclipse.emf.cdo.server.internal.hibernate.HibernateStoreAccessor;
import org.eclipse.emf.cdo.server.internal.hibernate.HibernateThreadContext;
import org.eclipse.emf.cdo.server.internal.hibernate.HibernateUtil;

import org.eclipse.net4j.util.WrappedException;

import org.eclipse.emf.ecore.EClass;

import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.type.StandardBasicTypes;
import org.hibernate.usertype.UserType;

import java.io.Serializable;
import java.lang.reflect.Constructor;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;

/**
 * Persists a CDOID in the DB in the contents of a resource, a many-to-any mapping.
 */
public class CDOIDAnyUserType implements UserType
{
  private static final int[] SQL_TYPES = { Types.VARCHAR, Types.VARCHAR };

  private static final String SEPARATOR = "__;__"; //$NON-NLS-1$

  private static final String EXTERNAL = "EXTERNAL"; //$NON-NLS-1$

  /** Constructor by id */
  private final HashMap<String, Constructor<?>> constructors = new HashMap<String, Constructor<?>>();

  public CDOIDAnyUserType()
  {
  }

  public int[] sqlTypes()
  {
    return SQL_TYPES;
  }

  public Class<?> returnedClass()
  {
    return CDOID.class;
  }

  public boolean isMutable()
  {
    return false;
  }

  public Object deepCopy(Object value)
  {
    return value;
  }

  public boolean equals(Object x, Object y)
  {
    if (x == y)
    {
      return true;
    }

    if (x == null || y == null)
    {
      return false;
    }

    return x.equals(y);
  }

  public Object nullSafeGet(ResultSet rs, String[] names, SessionImplementor sessionImplementor, Object owner) throws SQLException
  {
    final String value = StandardBasicTypes.STRING.nullSafeGet(rs, names[1], sessionImplementor);
    if (rs.wasNull())
    {
      return null;
    }
    final String entityName = StandardBasicTypes.STRING.nullSafeGet(rs, names[0], sessionImplementor);
    return deserializeId(entityName, value);
  }

  public void nullSafeSet(PreparedStatement statement, Object value, int index, SessionImplementor sessionImplementor) throws SQLException
  {
    final String entityName;
    final CDOID localValue;
    if (value instanceof CDORevision)
    {
      final CDORevision cdoRevision = (CDORevision)value;
      localValue = cdoRevision.getID();
      // cast to object to use correct method from hibernate util
      entityName = HibernateUtil.getInstance().getEntityName((Object)cdoRevision);
    }
    else if (value instanceof CDOID)
    {
      localValue = (CDOID)value;
      entityName = HibernateUtil.getInstance().getEntityName(localValue);
    }
    else
    {
      throw new IllegalArgumentException("Type " + value + " not supported here");
    }

    // the first column is there for backward compatibility, fill it with nulls..
    final String strValue = serializeId(localValue);
    if (strValue == null)
    {
      statement.setNull(index, Types.VARCHAR);
      statement.setNull(index + 1, Types.VARCHAR);
    }
    else
    {
      statement.setString(index, entityName);
      statement.setString(index + 1, strValue);
    }
  }

  protected String serializeId(CDOID id)
  {
    final CDOID cdoID = HibernateUtil.getInstance().resolvePossibleTempId(id);
    if (cdoID == null || cdoID.isNull())
    {
      return null;
    }
    if (cdoID.getType() == CDOID.Type.EXTERNAL_OBJECT)
    {
      return EXTERNAL + SEPARATOR + ((CDOIDExternal)cdoID).getURI();
    }
    final Serializable idValue = HibernateUtil.getInstance().getIdValue(cdoID);
    return idValue + SEPARATOR + idValue.getClass().getName();
  }

  protected CDOID deserializeId(String entityName, String value)
  {
    final int end1 = value.indexOf(SEPARATOR);
    final int start2 = end1 + SEPARATOR.length();

    final String idStr = value.substring(0, end1);
    final String idClassName = value.substring(start2);

    if (EXTERNAL.equals(entityName))
    {
      return CDOIDUtil.createExternal(idStr);
    }

    final Serializable idValue = getId(idStr, idClassName);
    final HibernateStoreAccessor accessor = HibernateThreadContext.getCurrentStoreAccessor();
    final EClass eClass = accessor.getStore().getEClass(entityName);
    return HibernateUtil.getInstance().createCDOID(new CDOClassifierRef(eClass), idValue);
  }

  public Serializable disassemble(Object value)
  {
    return (Serializable)value;
  }

  public Object assemble(Serializable cachedValue, Object owner)
  {
    return cachedValue;
  }

  public Object replace(Object original, Object target, Object owner)
  {
    return original;
  }

  public int hashCode(Object x)
  {
    return x.hashCode();
  }

  /** Creates an id object of the correct type */
  private Serializable getId(String idStr, String idType)
  {
    try
    {
      Constructor<?> constructor = constructors.get(idType);
      if (constructor == null)
      {
        final Class<?> idClass = Thread.currentThread().getContextClassLoader().loadClass(idType);
        constructor = idClass.getConstructor(new Class[] { String.class });
        constructors.put(idType, constructor);
      }

      return (Serializable)constructor.newInstance(new Object[] { idStr });
    }
    catch (Exception e)
    {
      throw WrappedException.wrap(e);
    }
  }
}

Back to the top