capint
6/22/2015 - 2:22 PM

CDO >> Example of overriding CDOResource

CDO >> Example of overriding CDOResource

public class CDOResourceOverridingImpl extends CDOResourceImpl {

  public CDOResourceOverridingImpl(URI initialURI) {
    super(initialURI);
  }

  /**
   * E.g. Override getEObject to populate a map of <id, object> and attach a content adapter
   */
  @Override
  public EObject getEObject(String uriFragment_p) {
    if (!id2EObjectPopulated) {
      // we populate the map with ids
      // and add a content adapter to keep it in sync
      addedRemovedObjectResourceContentAdapter = new AddedRemovedObjectResourceContentAdapter(this);
      eAdapters().add(addedRemovedObjectResourceContentAdapter);

      TreeIterator<EObject> allContents = getAllContents();
      while (allContents.hasNext()) {
        EObject eObject = allContents.next();

        String identifier = getURIFragment(eObject);
        if (identifier != null) {
          id2EObject.put(identifier, eObject);
        }
      }
      id2EObjectPopulated = true;
    }

    if (id2EObject.containsKey(uriFragment_p)) {
      return id2EObject.get(uriFragment_p);
    }

    return super.getEObject(uriFragment_p);
  }

  /**
   * E.g. Overrding getURIFragment to provide another ID selection mechanism
   */
  @Override
  public String getURIFragment(EObject object_p) {
    String identifier = IdManager.getInstance().getId(object_p);
    if ((identifier != null) && !identifier.isEmpty()) {
      return identifier;
    }
    return super.getURIFragment(object_p);
  }

  /**
   * E.g. Overriding attached to update the map <id, object> when an object is attached to the resource
   */
  @Override
  public void attached(EObject object_p) {
    super.attached(object_p);

    if (id2EObjectPopulated) {
      // if the map was initialized, we keep it updated
      addedObject(object_p);

      // and also for contained objects
      TreeIterator<Object> allContents = EcoreUtil.getAllProperContents(object_p, false);
      while (allContents.hasNext()) {
        Object next = allContents.next();
        if (next instanceof EObject) {
          EObject eObject = (EObject) next;
          addedObject(eObject);
        }
      }
    }
  }

  /**
   * E.g. Overriding detached to update the map <id, object> when an object is detached to the resource
   */
  @Override
  public void detached(EObject object_p) {
    super.detached(object_p);

    if (id2EObjectPopulated) {
      // if the map was initialized, we keep it updated
      removedObject(object_p);

      // and also for contained objects
      TreeIterator<Object> allContents = EcoreUtil.getAllProperContents(object_p, false);
      while (allContents.hasNext()) {
        Object next = allContents.next();
        if (next instanceof EObject) {
          EObject eObject = (EObject) next;
          removedObject(eObject);
        }
      }
    }
  }

  /**
   * E.g. Overriding unload() to clear the map <id, object> when the resource is unloaded
   */
  @Override
  public void unload() {
    if (addedRemovedObjectResourceContentAdapter != null) {
      eAdapters().remove(addedRemovedObjectResourceContentAdapter);
      addedRemovedObjectResourceContentAdapter = null;
    }
    id2EObject.clear();
    super.unload();
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public void addedObject(Object object_p) {
    if (object_p instanceof EObject) {
      EObject eObject = (EObject) object_p;
      String identifier = getURIFragment(eObject);
      if (identifier != null) {
        id2EObject.put(identifier, eObject);
      }
    }
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public void removedObject(Object object_p) {
    if (object_p instanceof EObject) {
      EObject eObject = (EObject) object_p;
      String identifier = getURIFragment(eObject);
      if (identifier != null) {
        id2EObject.remove(identifier);
      }
    }
  }
}