Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 782d87dae03c513fbdcf1eeee04e1a08e3b86b30 (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
/**
 * Copyright (c) 2004 - 2010 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:
 *    Martin Taal - initial API and implementation
 *    Eike Stepper - maintenance
 */
package org.eclipse.emf.cdo.server.internal.hibernate;

import org.eclipse.emf.cdo.common.id.CDOID;
import org.eclipse.emf.cdo.server.IStoreAccessor;
import org.eclipse.emf.cdo.server.IStoreAccessor.CommitContext;
import org.eclipse.emf.cdo.spi.common.revision.InternalCDORevision;

import java.util.HashMap;

/**
 * A HibernateCommitContext contains the commitcontext as well as support for direct (hashmap) based search for a new or
 * changed object using the id.
 * 
 * @author Martin Taal
 */
public class HibernateCommitContext
{
  private CommitContext commitContext;

  private HashMap<CDOID, InternalCDORevision> dirtyObjects = null;

  private HashMap<CDOID, InternalCDORevision> newObjects = null;

  public IStoreAccessor.CommitContext getCommitContext()
  {
    return commitContext;
  }

  public void setCommitContext(IStoreAccessor.CommitContext commitContext)
  {
    this.commitContext = commitContext;
  }

  // initialize is not done when the commitContext is set because it appeared
  // that at that moment the temp id's are not repaired. The initialize method
  // is called on demand.
  protected void initialize()
  {
    if (dirtyObjects != null)
    {
      return;
    }

    dirtyObjects = new HashMap<CDOID, InternalCDORevision>();
    for (InternalCDORevision cdoRevision : commitContext.getDirtyObjects())
    {
      dirtyObjects.put(cdoRevision.getID(), cdoRevision);
    }

    newObjects = new HashMap<CDOID, InternalCDORevision>();
    for (InternalCDORevision cdoRevision : commitContext.getNewObjects())
    {
      newObjects.put(cdoRevision.getID(), cdoRevision);
    }
  }

  public InternalCDORevision getDirtyObject(CDOID id)
  {
    initialize();
    return dirtyObjects.get(id);
  }

  public InternalCDORevision getNewObject(CDOID id)
  {
    initialize();
    return newObjects.get(id);
  }

  public void setNewID(CDOID oldId, CDOID newId)
  {
    initialize();
    InternalCDORevision cdoRevision;
    if ((cdoRevision = dirtyObjects.get(oldId)) != null)
    {
      dirtyObjects.remove(oldId);
      dirtyObjects.put(newId, cdoRevision);
      return;
    }

    if ((cdoRevision = newObjects.get(oldId)) != null)
    {
      newObjects.remove(oldId);
      newObjects.put(newId, cdoRevision);
      return;
    }
  }
}

Back to the top