Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b2542dd07a43391fcd6507bc481fc41cf9a8ebe9 (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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*
 * Copyright (c) 2012, 2013 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.branch.CDOBranchPoint;
import org.eclipse.emf.cdo.common.id.CDOID;
import org.eclipse.emf.cdo.common.id.CDOIDUtil;
import org.eclipse.emf.cdo.common.model.CDOClassifierRef;
import org.eclipse.emf.cdo.common.revision.CDORevision;
import org.eclipse.emf.cdo.common.revision.CDORevisionData;
import org.eclipse.emf.cdo.common.revision.CDORevisionHandler;
import org.eclipse.emf.cdo.eresource.EresourcePackage;
import org.eclipse.emf.cdo.spi.common.revision.DetachedCDORevision;
import org.eclipse.emf.cdo.spi.common.revision.InternalCDORevision;

import org.eclipse.emf.common.util.Enumerator;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EEnum;
import org.eclipse.emf.ecore.EEnumLiteral;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.teneo.hibernate.HbDataStore;
import org.eclipse.emf.teneo.hibernate.auditing.AuditHandler;
import org.eclipse.emf.teneo.hibernate.auditing.AuditVersionProvider;
import org.eclipse.emf.teneo.hibernate.auditing.model.teneoauditing.TeneoAuditEntry;
import org.eclipse.emf.teneo.hibernate.auditing.model.teneoauditing.TeneoAuditKind;

import org.hibernate.Query;
import org.hibernate.Session;

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

/**
 * Utility methods to support auditing in the hibernate store.
 * An internal class.
 *
 * @author Martin Taal
 */
public class HibernateAuditHandler
{
  private HibernateStore hibernateStore;

  private HbDataStore cdoDataStore;

  public CDOAuditHandler getCDOAuditHandler()
  {
    return (CDOAuditHandler)cdoDataStore.getAuditHandler();
  }

  public InternalCDORevision readRevisionByVersion(Session session, CDOID id, int version)
  {
    final CDOClassifierRef classifierRef = CDOIDUtil.getClassifierRef(id);
    if (classifierRef == null)
    {
      throw new IllegalArgumentException("This CDOID type of " + id + " is not supported by this store."); //$NON-NLS-1$ //$NON-NLS-2$
    }
    final EClass eClass = HibernateUtil.getInstance().getEClass(classifierRef);
    AuditVersionProvider auditVersionProvider = cdoDataStore.getAuditVersionProvider();
    auditVersionProvider.setSession(session);
    final TeneoAuditEntry auditEntry = auditVersionProvider.getAuditEntryForVersion(eClass, id, version);
    return (InternalCDORevision)getCDORevision(session, auditEntry);
  }

  public InternalCDORevision readRevision(Session session, CDOID id, long timeStamp)
  {
    final CDOClassifierRef classifierRef = CDOIDUtil.getClassifierRef(id);
    if (classifierRef == null)
    {
      throw new IllegalArgumentException("This CDOID type of " + id + " is not supported by this store."); //$NON-NLS-1$ //$NON-NLS-2$
    }
    final EClass eClass = HibernateUtil.getInstance().getEClass(classifierRef);
    final CDORevision revision = getCDORevision(session, eClass, id, timeStamp);
    return (InternalCDORevision)revision;
  }

  public CDORevision getCDORevision(Session session, EClass eClass, Object id, long timeStamp)
  {
    AuditVersionProvider auditVersionProvider = cdoDataStore.getAuditVersionProvider();
    auditVersionProvider.setSession(session);
    long useTimeStamp = timeStamp;
    if (useTimeStamp == 0)
    {
      // use the last revision
      useTimeStamp = Long.MAX_VALUE;
    }
    final TeneoAuditEntry auditEntry = auditVersionProvider.getAuditEntry(eClass, id, useTimeStamp);
    return getCDORevision(session, auditEntry);
  }

  public CDORevision getCDORevision(Session session, TeneoAuditEntry teneoAuditEntry)
  {
    final CDORevision revision = convertAuditEntryToCDORevision(teneoAuditEntry);
    return revision;
  }

  protected CDORevision convertAuditEntryToCDORevision(TeneoAuditEntry teneoAuditEntry)
  {
    if (teneoAuditEntry == null)
    {
      return null;
    }
    final HibernateStoreAccessor storeAccessor = HibernateThreadContext.getCurrentStoreAccessor();
    final EClass domainEClass = getCDOAuditHandler().getModelElement(teneoAuditEntry.eClass());
    final CDOID cdoID = HibernateUtil.getInstance().convertStringToCDOID(teneoAuditEntry.getTeneo_object_id());
    final InternalCDORevision revision;
    if (teneoAuditEntry.getTeneo_audit_kind() == TeneoAuditKind.DELETE)
    {
      revision = new DetachedCDORevision(domainEClass, cdoID,
          hibernateStore.getRepository().getBranchManager().getMainBranch(),
          new Long(teneoAuditEntry.getTeneo_object_version()).intValue(), teneoAuditEntry.getTeneo_start(),
          CDOBranchPoint.UNSPECIFIED_DATE);
      revision.setRevised(CDOBranchPoint.UNSPECIFIED_DATE);
    }
    else
    {
      revision = hibernateStore.createRevision(domainEClass, cdoID);
      revision.setVersion(new Long(teneoAuditEntry.getTeneo_object_version()).intValue());
      revision.setBranchPoint(
          storeAccessor.getStore().getMainBranchHead().getBranch().getPoint(teneoAuditEntry.getTeneo_start()));
      if (teneoAuditEntry.getTeneo_end() > 0)
      {
        revision.setRevised(teneoAuditEntry.getTeneo_end());
      }
      else
      {
        revision.setRevised(CDOBranchPoint.UNSPECIFIED_DATE);
      }
      convertContent(teneoAuditEntry, revision);
    }
    return revision;
  }

  protected void convertContent(TeneoAuditEntry auditEntry, InternalCDORevision cdoRevision)
  {
    final EClass auditingEClass = auditEntry.eClass();
    for (EStructuralFeature targetEFeature : cdoRevision.getEClass().getEAllStructuralFeatures())
    {
      final EStructuralFeature sourceEFeature = auditingEClass.getEStructuralFeature(targetEFeature.getName());
      if (sourceEFeature == null)
      {
        continue;
      }
      if (!auditEntry.eIsSet(sourceEFeature) && !sourceEFeature.isMany())
      {
        continue;
      }
      if (sourceEFeature.isMany())
      {
        for (Object value : (Collection<?>)auditEntry.eGet(sourceEFeature))
        {
          cdoRevision.getList(targetEFeature).add(convertValue(targetEFeature, value));
        }
      }
      else
      {
        final Object value = auditEntry.eGet(sourceEFeature);
        cdoRevision.setValue(targetEFeature, convertValue(targetEFeature, value));
      }
    }

    if (auditEntry.getTeneo_container_id() != null)
    {
      cdoRevision.setContainerID(HibernateUtil.getInstance().convertStringToCDOID(auditEntry.getTeneo_container_id()));
      cdoRevision.setContainingFeatureID(auditEntry.getTeneo_container_feature_id());
    }

    final String resourceID = auditEntry.getTeneo_resourceid();
    if (resourceID != null)
    {
      cdoRevision.setResourceID(HibernateUtil.getInstance().convertStringToCDOID(resourceID));
    }
  }

  private Object convertValue(EStructuralFeature targetEFeature, Object value)
  {
    if (value == null)
    {
      final Object defaultValue = targetEFeature.getDefaultValue();
      if (defaultValue == null)
      {
        return null;
      }
      final boolean handleUnsetAsNull = getCdoDataStore().getPersistenceOptions().getHandleUnsetAsNull();
      if (!handleUnsetAsNull && targetEFeature.isUnsettable())
      {
        // there was a default value so was explicitly set to null
        // otherwise the default value would be in the db
        return CDORevisionData.NIL;
      }
      return null;
    }
    if (targetEFeature instanceof EReference)
    {
      final CDOID cdoID = value instanceof CDOID ? (CDOID)value
          : HibernateUtil.getInstance().convertStringToCDOID((String)value);
      return cdoID;
    }
    if (value instanceof Enumerator)
    {
      return ((Enumerator)value).getValue();
    }
    if (value instanceof EEnumLiteral)
    {
      return ((EEnumLiteral)value).getValue();
    }
    if (value instanceof String && targetEFeature.getEType() instanceof EEnum)
    {
      return ((EEnum)targetEFeature.getEType()).getEEnumLiteralByLiteral((String)value).getValue();
    }
    return value;
  }

  public List<?> getCDOResources(Session session, CDOID folderId, long timeStamp)
  {
    final AuditHandler auditHandler = getCDOAuditHandler();
    final EClass auditEClass = auditHandler.getAuditingModelElement(EresourcePackage.eINSTANCE.getCDOResourceNode());
    final String entityName = cdoDataStore.toEntityName(auditEClass);
    return getCDOResources(session, folderId, timeStamp, entityName);
  }

  public List<?> getCDOResources(Session session, CDOID folderId, long timeStamp, String entityName)
  {
    final AuditHandler auditHandler = getCDOAuditHandler();
    String idStr = null;
    if (folderId != null)
    {
      idStr = auditHandler.idToString(EresourcePackage.eINSTANCE.getCDOResourceFolder(), CDOIDUtil.getLong(folderId));
    }

    final String qryStr = "select e from " + entityName + " e where e.folder"
        + (idStr == null ? " is null " : "=:folder")
        + " and (e.teneo_end=-1 or e.teneo_end>:end) and e.teneo_start<=:start";
    final Query qry = session.createQuery(qryStr);
    if (idStr != null)
    {
      qry.setParameter("folder", idStr);
    }
    qry.setParameter("start", timeStamp);
    qry.setParameter("end", timeStamp);
    return qry.list();
  }

  public void handleRevisionsByEClass(Session session, EClass eClass, CDORevisionHandler handler, long timeStamp)
  {
    AuditVersionProvider auditVersionProvider = cdoDataStore.getAuditVersionProvider();
    auditVersionProvider.setSession(session);
    final List<TeneoAuditEntry> teneoAuditEntries = auditVersionProvider.getSpecificAuditEntries(eClass, timeStamp);
    for (TeneoAuditEntry teneoAuditEntry : teneoAuditEntries)
    {
      final CDORevision cdoRevision = getCDORevision(session, teneoAuditEntry);

      // if a subclass ignore
      if (cdoRevision.getEClass() != eClass)
      {
        continue;
      }

      if (!handler.handleRevision(cdoRevision))
      {
        return;
      }
    }
  }

  public HbDataStore getCdoDataStore()
  {
    return cdoDataStore;
  }

  public void setCdoDataStore(HbDataStore cdoDataStore)
  {
    this.cdoDataStore = cdoDataStore;
  }

  public HibernateStore getHibernateStore()
  {
    return hibernateStore;
  }

  public void setHibernateStore(HibernateStore hibernateStore)
  {
    this.hibernateStore = hibernateStore;
  }
}

Back to the top