Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f33c9dad056f40d766d76c47ae64d692c86238aa (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
/**
 * 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.model.CDOModelUtil;
import org.eclipse.emf.cdo.common.model.CDOPackageRegistry;
import org.eclipse.emf.cdo.common.model.CDOPackageUnit;
import org.eclipse.emf.cdo.common.model.EMFUtil;
import org.eclipse.emf.cdo.server.IStore;
import org.eclipse.emf.cdo.spi.common.model.InternalCDOPackageRegistry;
import org.eclipse.emf.cdo.spi.common.model.InternalCDOPackageUnit;

import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Victor Roldan Betancort
 */
public class PrimitivePackageUnit
{
  private String id;

  private Integer ordinalType;

  private Long timeStamp;

  private List<Byte> ePackageBytes;

  public PrimitivePackageUnit(String id, Integer ordinalType, Long timeStamp, List<Byte> ePackageBytes)
  {
    setId(id);
    setOrdinalType(ordinalType);
    setTimeStamp(timeStamp);
    setePackageBytes(ePackageBytes);
  }

  public void setId(String id)
  {
    this.id = id;
  }

  public String getId()
  {
    return id;
  }

  public void setOrdinalType(Integer ordinalType)
  {
    this.ordinalType = ordinalType;
  }

  public Integer getOrdinalType()
  {
    return ordinalType;
  }

  public void setTimeStamp(Long timeStamp)
  {
    this.timeStamp = timeStamp;
  }

  public Long getTimeStamp()
  {
    return timeStamp;
  }

  public void setePackageBytes(List<Byte> ePackageBytes)
  {
    this.ePackageBytes = ePackageBytes;
  }

  public List<Byte> getePackageBytes()
  {
    return ePackageBytes;
  }

  public static PrimitivePackageUnit getPrimitivePackageUnit(IStore store, InternalCDOPackageUnit packageUnit)
  {
    return new PrimitivePackageUnit(new String(packageUnit.getID()), new Integer(packageUnit.getOriginalType()
        .ordinal()), new Long(packageUnit.getTimeStamp()), getEPackageBytes(store, packageUnit));
  }

  public static InternalCDOPackageUnit getPackageUnit(InternalCDOPackageRegistry packageRegistry,
      PrimitivePackageUnit packageUnit)
  {
    InternalCDOPackageUnit cdoPackageUnit = (InternalCDOPackageUnit)CDOModelUtil.createPackageUnit();
    CDOPackageUnit.Type type = CDOPackageUnit.Type.values()[packageUnit.getOrdinalType()];
    cdoPackageUnit.setOriginalType(type);
    cdoPackageUnit.setTimeStamp(packageUnit.getTimeStamp());
    EPackage ePackage = getEPackageFromBytes(packageUnit.getePackageBytes());
    cdoPackageUnit.setPackageRegistry(packageRegistry);
    cdoPackageUnit.init(ePackage);
    return cdoPackageUnit;
  }

  private static List<Byte> getEPackageBytes(IStore store, InternalCDOPackageUnit packageUnit)
  {
    EPackage ePackage = packageUnit.getTopLevelPackageInfo().getEPackage();
    CDOPackageRegistry packageRegistry = store.getRepository().getPackageRegistry();
    byte[] bytes = EMFUtil.getEPackageBytes(ePackage, true, packageRegistry);
    List<Byte> bytesObject = new ArrayList<Byte>();
    for (byte bt : bytes)
    {
      bytesObject.add(new Byte(bt));
    }

    return bytesObject;
  }

  private static EPackage getEPackageFromBytes(List<Byte> ePackageBytesList)
  {
    ResourceSet rSet = new ResourceSetImpl();
    byte[] packageBytes = new byte[ePackageBytesList.size()];
    for (int i = 0; i < packageBytes.length; i++)
    {
      packageBytes[i] = ePackageBytesList.get(i);
    }
    EPackage ePackage = EMFUtil.createEPackage("", packageBytes, true, rSet, false);
    return ePackage;
  }
}

Back to the top