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

import org.eclipse.emf.cdo.common.id.CDOID;
import org.eclipse.emf.cdo.common.model.CDOClassRef;
import org.eclipse.emf.cdo.common.model.CDOFeature;
import org.eclipse.emf.cdo.common.model.CDOPackage;
import org.eclipse.emf.cdo.common.model.CDOPackageInfo;
import org.eclipse.emf.cdo.common.model.resource.CDOResourceClass;
import org.eclipse.emf.cdo.common.revision.CDORevision;
import org.eclipse.emf.cdo.server.ISession;
import org.eclipse.emf.cdo.server.IView;
import org.eclipse.emf.cdo.server.hibernate.IHibernateStoreReader;
import org.eclipse.emf.cdo.server.hibernate.id.CDOIDHibernate;
import org.eclipse.emf.cdo.server.internal.hibernate.bundle.OM;

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

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Expression;

import java.util.Collection;
import java.util.List;

/**
 * @author Eike Stepper
 * @author Martin Taal
 */
public class HibernateStoreReader extends HibernateStoreAccessor implements IHibernateStoreReader
{
  private static final ContextTracer TRACER = new ContextTracer(OM.DEBUG, HibernateStoreReader.class);

  public HibernateStoreReader(HibernateStore store, ISession session)
  {
    super(store, session);
  }

  protected HibernateStoreReader(HibernateStore store, IView view)
  {
    super(store, view);
  }

  public HibernateStoreChunkReader createChunkReader(CDORevision revision, CDOFeature feature)
  {
    return new HibernateStoreChunkReader(this, revision, feature);
  }

  public CloseableIterator<CDOID> readObjectIDs(boolean withTypes)
  {
    throw new UnsupportedOperationException();
  }

  public CDOClassRef readObjectType(CDOID id)
  {
    CDORevision cdoRevision = readRevision(id, -1);
    return cdoRevision.getCDOClass().createClassRef();
  }

  public void readPackage(CDOPackage cdoPackage)
  {
    getStore().getPackageHandler().readPackage(cdoPackage);
  }

  public Collection<CDOPackageInfo> readPackageInfos()
  {
    return getStore().getPackageHandler().getCDOPackageInfos();
  }

  public CDOID readResourceID(String path)
  {
    if (TRACER.isEnabled())
    {
      TRACER.trace("Finding resourceid using path " + path);
    }

    Session session = getHibernateSession();
    Criteria criteria = session.createCriteria(CDOResourceClass.NAME);
    criteria.add(Expression.eq("path", path));
    List<?> result = criteria.list();
    if (result.size() == 0)
    {
      if (TRACER.isEnabled())
      {
        TRACER.trace("Resource not found");
      }

      // TODO: throw exception?
      return null;
    }

    // TODO: throw exception if list.size() > 1?
    CDORevision cdoRevision = (CDORevision)result.get(0);
    return cdoRevision.getID();
  }

  public String readResourcePath(CDOID id)
  {
    if (id == null)
    {
      throw new IllegalArgumentException("ID must be not null");
    }

    if (!(id instanceof CDOIDHibernate))
    {
      throw new IllegalArgumentException("ID type " + id.getClass().getName() + " not supported by hibernate reader");
    }

    if (TRACER.isEnabled())
    {
      TRACER.trace("Finding resource using id " + id);
    }

    Session session = getHibernateSession();
    Query qry = session.createQuery("select path from " + CDOResourceClass.NAME + " where id=:id");
    CDOIDHibernate idHibernate = (CDOIDHibernate)id;
    qry.setParameter("id", idHibernate.getId());
    final List<?> result = qry.list();
    if (result.size() == 0)
    {
      if (TRACER.isEnabled())
      {
        TRACER.trace("Resource not found");
      }

      // TODO: throw exception?
      return null;
    }

    return (String)result.get(0);
  }

  public CDORevision readRevision(CDOID id, int referenceChunk)
  {
    return HibernateUtil.getInstance().getCDORevision(id);
  }

  public CDORevision readRevisionByTime(CDOID id, int referenceChunk, long timeStamp)
  {
    throw new UnsupportedOperationException();
  }

  public CDORevision readRevisionByVersion(CDOID id, int referenceChunk, int version)
  {
    // TODO Could be necessary to implement
    throw new UnsupportedOperationException();
  }
}

Back to the top