Skip to main content
summaryrefslogtreecommitdiffstats
blob: 0893ccd09203af200f18441ecf861f5c8ff519cd (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
/*
 * Copyright (c) 2004 - 2012 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:
 *    Ibrahim Sallam - initial API and implementation
 */
package org.eclipse.emf.cdo.server.internal.objectivity.schema;

import org.eclipse.emf.cdo.common.branch.CDOBranch;
import org.eclipse.emf.cdo.common.id.CDOID;
import org.eclipse.emf.cdo.eresource.EresourcePackage;
import org.eclipse.emf.cdo.server.internal.objectivity.ObjectivityStoreAccessor;
import org.eclipse.emf.cdo.server.internal.objectivity.bundle.OM;
import org.eclipse.emf.cdo.server.internal.objectivity.db.ObjyObject;
import org.eclipse.emf.cdo.server.internal.objectivity.db.ObjyObjectManager;
import org.eclipse.emf.cdo.server.internal.objectivity.db.ObjySchema;
import org.eclipse.emf.cdo.server.internal.objectivity.db.ObjySession;
import org.eclipse.emf.cdo.spi.common.revision.InternalCDORevision;

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

import org.eclipse.emf.ecore.EStructuralFeature;

import com.objy.as.app.Class_Object;
import com.objy.as.app.Proposed_Class;
import com.objy.as.app.d_Access_Kind;
import com.objy.as.app.d_Module;
import com.objy.db.ObjyRuntimeException;
import com.objy.db.app.ooId;
import com.objy.db.app.ooObj;
import com.objy.db.util.ooTreeListX;

/***
 * OoResouceList is a specialized ooArrayListId, where all elements are of type Resourc(Node|Folder). The class will
 * allow adding, validating and removing resources From the list.
 * 
 * @author ibrahim
 */
public class ObjyResourceList
{

  private static final ContextTracer TRACER_DEBUG = new ContextTracer(OM.DEBUG, ObjyProxy.class);

  static public String className = "ooResourceList";

  static public String Attribute_arrayName = "oo_array";

  private ObjySession objySession;

  protected Class_Object classObject;

  protected ObjyArrayListId list = null;

  protected ooId objectId;

  private static final EStructuralFeature featureName = EresourcePackage.Literals.CDO_RESOURCE_NODE__NAME;

  // we could use this "EresourcePackage.eINSTANCE.getCDOResourceNode_Name()" instead of above.

  public static void buildSchema()
  {
    d_Module top_mod = ObjySchema.getTopModule();
    if (top_mod.resolve_class(ObjyResourceList.className) == null
        && top_mod.resolve_proposed_class(ObjyResourceList.className) == null)
    {

      if (TRACER_DEBUG.isEnabled())
      {
        TRACER_DEBUG.trace("Schema not found for ooArrayListId. Adding ooArrayListId");
      }

      boolean inProcess = top_mod.proposed_classes().hasNext();

      // Proposed_Class A = new Proposed_Class(ooArrayListId.ClassName);
      Proposed_Class propClass = top_mod.propose_new_class(ObjyResourceList.className);

      propClass.add_base_class(com.objy.as.app.d_Module.LAST, com.objy.as.app.d_Access_Kind.d_PUBLIC,
          ObjyBase.CLASS_NAME /* "ooObj" */);

      propClass.add_ref_attribute(com.objy.as.app.d_Module.LAST, // Access kind
          d_Access_Kind.d_PUBLIC, // Access kind
          ObjyResourceList.Attribute_arrayName, // Attribute name
          1, // # elements in fixed-size array
          ObjyArrayListId.className, false); // Default value // Default value

      // top_mod.propose_new_class(A);
      if (!inProcess)
      {
        top_mod.activate_proposals(true, true);
      }

      if (TRACER_DEBUG.isEnabled())
      {
        TRACER_DEBUG.trace("SCHEMA changed : ooArrayListId added");
      }
    }

  }

  public ObjyResourceList(ObjySession objySession, ObjyObject objyObject)
  {
    classObject = objyObject.ooClassObject();
    this.objySession = objySession;
    objectId = objyObject.ooId();
  }

  // public OoResourceList(ObjySession objySession, Class_Object classObject)
  // {
  // this.classObject = classObject;
  // this.objySession = objySession;
  // }

  private ObjyArrayListId getList()
  {
    if (list != null)
    {
      return list;
    }

    try
    {
      // Class_Position position = classObject.position_in_class(ObjyResourceList.Attribute_arrayName);
      ooId oid = classObject.nget_ooId(ObjyResourceList.Attribute_arrayName);
      if (!oid.isNull())
      {
        list = new ObjyArrayListId(Class_Object.class_object_from_oid(oid));
      }
    }
    catch (ObjyRuntimeException e)
    {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    return list;
  }

  public void remove(ObjyObject objyObject)
  {
    int size = (int)getList().size();
    for (int i = 0; i < size; i++)
    {
      if (objyObject.ooId().equals(getList().get(i)))
      {
        getList().remove(i);
        break;
      }
    }
  }

  public void add(ObjyObject objyObject)
  {
    // TODO - we need to make sure that objyObject is a resource!!!
    getList().add(objyObject.ooId());
  }

  // public void checkDuplicateResources(ObjectivityStoreAccessor storeAccessor, CDORevision revision)
  // throws IllegalStateException
  // {
  // CDOID folderID = (CDOID)revision.data().getContainerID();
  // String name = (String)revision.data().get(EresourcePackage.eINSTANCE.getCDOResourceNode_Name(), 0);
  // CDOID existingID = storeAccessor.readResourceID(folderID, name, revision.getBranch().getHead());
  // if (existingID != CDOID.NULL && !existingID.equals(revision.getID()))
  // {
  //      throw new IllegalStateException("Duplicate resource or folder: " + name + " in folder " + folderID); //$NON-NLS-1$ //$NON-NLS-2$
  // }
  // }

  public void checkDuplicateResources(ObjectivityStoreAccessor storeAccessor, InternalCDORevision revision)
      throws IllegalStateException
  {
    // CDOID folderID = (CDOID)revision.data().getContainerID();
    CDOID folderId = (CDOID)revision.data().getContainerID();
    String name = (String)revision.data().get(EresourcePackage.eINSTANCE.getCDOResourceNode_Name(), 0);

    // ooId folderId = objyObject.getEContainerAsOid();
    // String name = OoResourceList.getResourceName(objyObject);

    // iterate over all resource in the list, and verify if we have both name and folderID.
    int size = (int)getList().size();
    ObjyObjectManager objyObjectManager = storeAccessor.getObjySession().getObjectManager();
    for (int i = 0; i < size; i++)
    {
      ObjyObject resource = getResource(i);
      ObjyObject resourceRevision = resource;
      // get the proper revision of the resource (might need to refactor this code, see readRevision())
      if (storeAccessor.getStore().isRequiredToSupportBranches())
      {
        try
        {
          resourceRevision = resource.getRevision(revision.getTimeStamp(), revision.getBranch().getID(),
              objyObjectManager);
        }
        catch (RuntimeException ex)
        {
          ex.printStackTrace();
        }
      }
      else if (storeAccessor.getStore().isRequiredToSupportAudits())
      {
        try
        {
          resourceRevision = resource.getRevision(revision.getTimeStamp(), CDOBranch.MAIN_BRANCH_ID, objyObjectManager);
        }
        catch (RuntimeException ex)
        {
          ex.printStackTrace();
        }
      }

      if (resourceRevision == null || resourceRevision.getVersion() < 0)
      {
        continue;
      }

      // int v = resource.getVersion();
      CDOID resourceFolderId = (CDOID)resourceRevision.getEContainer();
      String resourceName = ObjyResourceList.getResourceName(resourceRevision);
      if (resourceFolderId != null && resourceFolderId.equals(folderId) && resourceName != null
          && resourceName.equals(name))
      {
        throw new IllegalStateException("Duplicate resource or folder: " + name + " in folder: " + folderId); //$NON-NLS-1$
      }
    }
  }

  public ObjyObject getResource(int index)
  {
    return objySession.getObjectManager().getObject(getList().get(index));
  }

  public static String getResourceName(ObjyObject objyObject)
  {
    String name = (String)objyObject.get(featureName);
    return name;
  }

  public static ObjyObject create(ooId nearOid)
  {
    Class_Object classObject = Class_Object.new_persistent_object(ObjySchema.getObjyClass(ObjyResourceList.className)
        .getASClass(), nearOid, false);
    // ObjyObjectManager.newInternalObjCount++;
    // Class_Position position = classObject.position_in_class(ObjyResourceList.Attribute_arrayName);
    // Class_Object arrayClassObject = Class_Object.new_persistent_object(
    // ObjySchema.getTopModule().resolve_class(ObjyArrayListId.className), classObject.objectID(), false);
    // // ooId arrayOid = arrayClassObject.objectID();
    ooTreeListX list = new ooTreeListX(10, false);
    // ObjyObjectManager.newInternalObjCount++;
    ooObj anObj = ooObj.create_ooObj(classObject.objectID());
    anObj.cluster(list);
    // System.out.println("initObject: " + anObj.getOid().getStoreString() + " treeListX: "
    // // + list.getOid().getStoreString());
    classObject.nset_ooId(ObjyResourceList.Attribute_arrayName, list.getOid());

    // classObject.set_ooId(position, arrayClassObject.objectID());
    // ObjyArrayListId.initObject(arrayClassObject);
    ObjyObject objyObject = null;
    try
    {
      objyObject = new ObjyObject(classObject);
    }
    catch (RuntimeException ex)
    {
      ex.printStackTrace();
    }

    return objyObject;
  }

  public ooId ooId()
  {
    return objectId;
  }

  public int size()
  {
    return (int)getList().size();
  }

}

Back to the top