Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 536d41222240329db28cbaa82af3fc07cff9f3c4 (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
/***************************************************************************
 * Copyright (c) 2004 - 2008 Martin Taal 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 and implementation
 **************************************************************************/
package org.eclipse.emf.cdo.server.internal.hibernate;

import org.eclipse.emf.cdo.protocol.id.CDOID;
import org.eclipse.emf.cdo.protocol.id.CDOIDTemp;
import org.eclipse.emf.cdo.protocol.revision.CDORevision;
import org.eclipse.emf.cdo.server.IStoreWriter.CommitContext;
import org.eclipse.emf.cdo.server.hibernate.CDOIDHibernate;

import org.hibernate.Session;

/**
 * @author Martin Taal
 */
public class HibernateUtil
{
  private static HibernateUtil instance = new HibernateUtil();

  /**
   * @return the instance
   */
  public static HibernateUtil getInstance()
  {
    return instance;
  }

  /**
   * @param instance
   *          the instance to set
   */
  public static void setInstance(HibernateUtil instance)
  {
    HibernateUtil.instance = instance;
  }

  public String getEntityName(CDORevision cdoRevision)
  {
    return cdoRevision.getCDOClass().getName();
  }

  /**
   * Translates a temporary cdoID into a hibernate ID, by finding the object it refers to in the CommitContext and then
   * returning or by persisting the object. Note assumes that the hibernate session and CommitContext are set in
   * HibernateThreadContext.
   */
  public CDOIDHibernate getCDOIDHibernate(CDOID cdoID)
  {
    final CDORevision cdoRevision = getCDORevision(cdoID);
    if (cdoRevision.getID() instanceof CDOIDHibernate)
    {
      return (CDOIDHibernate)cdoRevision.getID();
    }
    final Session session = HibernateThreadContext.getSession();
    session.saveOrUpdate(cdoRevision);
    if (!(cdoRevision.getID() instanceof CDOIDHibernate))
    {
      throw new IllegalStateException("CDORevision " + cdoRevision.getCDOClass().getName() + " " + cdoRevision.getID()
          + " does not have a hibernate cdoid after saving/updating it");
    }
    return (CDOIDHibernate)cdoRevision.getID();
  }

  /**
   * Gets a current object, first checks the new and dirty objects from the commitcontent. Otherwise reads it from the
   * session.
   */
  public CDORevision getCDORevision(CDOID id)
  {
    final CommitContext commitContext = HibernateThreadContext.getCommitContext();
    for (CDORevision revision : commitContext.getNewObjects())
    {
      if (revision.getID().equals(id))
      {
        return revision;
      }
    }
    for (CDORevision revision : commitContext.getDirtyObjects())
    {
      if (revision.getID().equals(id))
      {
        return revision;
      }
    }

    // maybe the temp was already translated
    if (id instanceof CDOIDTemp)
    {
      final CDOID newID = commitContext.getIDMappings().get(id);
      if (newID != null)
      {
        return getCDORevision(newID);
      }
    }

    if (!(id instanceof CDOIDHibernate))
    {
      throw new IllegalArgumentException("Passed cdoid is not an instance of CDOIDHibernate but a "
          + id.getClass().getName() + ": " + id);
    }
    final CDOIDHibernate cdoIDHibernate = (CDOIDHibernate)id;
    final Session session = HibernateThreadContext.getSession();
    return (CDORevision)session.get(cdoIDHibernate.getEntityName(), cdoIDHibernate.getId());
  }
}

Back to the top