Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: eed31d109ef2643af366de009f18355d06d77289 (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
/**
 * 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:
 *    Victor Roldan Betancort - initial API and implementation
 */
package org.eclipse.emf.cdo.internal.server.db4o;

import org.eclipse.emf.cdo.common.id.CDOID;
import org.eclipse.emf.cdo.common.id.CDOIDUtil;

import com.db4o.ObjectContainer;
import com.db4o.ObjectSet;
import com.db4o.query.Query;

/**
 * @author Victor Roldan Betancort
 */
public final class QueryUtil
{

  public static PrimitiveRevision getLastPrimitiveRevision(ObjectContainer container, int branchId, CDOID id)
  {
    ObjectSet<?> set = getRevisionById(container, branchId, id);

    PrimitiveRevision lastRevision = null;
    for (Object value : set)
    {
      PrimitiveRevision rev = (PrimitiveRevision)value;
      if (lastRevision == null)
      {
        lastRevision = rev;
      }
      if (rev.getVersion() > lastRevision.getVersion())
      {
        lastRevision = rev;
      }
    }
    return lastRevision;
  }

  public static PrimitiveRevision getPrimitiveRevisionByVersion(ObjectContainer container, CDOID id, int branchId,
      int version)
  {
    ObjectSet<?> set = getRevisionById(container, branchId, id);
    for (Object value : set)
    {
      PrimitiveRevision rev = (PrimitiveRevision)value;
      if (rev.getVersion() == version)
      {
        return rev;
      }
    }
    return null;
  }

  private static ObjectSet<?> getRevisionById(ObjectContainer container, int branchId, CDOID id)
  {
    Query query = container.query();
    query.constrain(PrimitiveRevision.class);
    query.descend("id").constrain(CDOIDUtil.getLong(id));
    query.descend("branchID").constrain(branchId);
    ObjectSet<?> set = query.execute();
    return set;
  }

  public static void removeRevisionFromContainer(ObjectContainer container, int branchId, CDOID id)
  {
    PrimitiveRevision primitiveRevision = QueryUtil.getLastPrimitiveRevision(container, branchId, id);
    if (primitiveRevision != null)
    {
      container.delete(primitiveRevision);
    }
    else
    {
      throw new IllegalArgumentException("Revision with ID " + id + " not found, cannot detach");
    }
  }

}

Back to the top