Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 6e3e8b847f3fd326074f41a2eb29feede123293f (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
/***************************************************************************
 * 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.internal.protocol.id.CDOIDNullImpl;
import org.eclipse.emf.cdo.internal.protocol.revision.InternalCDORevision;
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.IStore;
import org.eclipse.emf.cdo.server.IStoreWriter.CommitContext;
import org.eclipse.emf.cdo.server.hibernate.id.CDOIDHibernate;

import org.hibernate.Session;

import java.util.Map;
import java.util.Properties;

/**
 * @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;
  }

  /** Converts from a Map<String, String> to a Properties */
  public Properties getPropertiesFromStore(IStore store)
  {
    Properties props = new Properties();
    Map<String, String> storeProps = store.getRepository().getProperties();
    for (String key : storeProps.keySet())
    {
      props.setProperty(key, storeProps.get(key));
    }

    return props;
  }

  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)
  {
    CDORevision cdoRevision = getCDORevision(cdoID);
    if (cdoRevision.getID() instanceof CDOIDHibernate)
    {
      return (CDOIDHibernate)cdoRevision.getID();
    }

    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();
  }

  public InternalCDORevision getCDORevision(Object target)
  {
    // if (target instanceof CDOObject)
    // {
    // return (InternalCDORevision)((CDOObject)target).cdoRevision();
    // }
    // else
    // {
    return (InternalCDORevision)target;
    // }
  }

  /**
   * 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)
  {
    if (id instanceof CDOIDNullImpl)
    {
      return null;
    }

    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)
    {
      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);
    }

    CDOIDHibernate cdoIDHibernate = (CDOIDHibernate)id;
    Session session = HibernateThreadContext.getSession();
    return (CDORevision)session.get(cdoIDHibernate.getEntityName(), cdoIDHibernate.getId());
  }
}

Back to the top