Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
<copyright> Copyright (c) 2012 Springsite BV (The Netherlands) 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 </copyright> $Id: EntityManagerProvider.java,v 1.7 2011/09/26 19:48:10 mtaal Exp $
 
 package org.eclipse.emf.texo.store;
 
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
 import  org.eclipse.emf.common.notify.Adapter;
 import  org.eclipse.emf.common.notify.Notification;
 import  org.eclipse.emf.common.notify.impl.AdapterImpl;
 import  org.eclipse.emf.common.util.URI;
 import  org.eclipse.emf.ecore.EObject;
 import  org.eclipse.emf.ecore.InternalEObject;
 import  org.eclipse.emf.ecore.resource.impl.ResourceImpl;
The base class of the Texo resource implementations.

Author(s):
Martin Taal
 
 public abstract class TexoResource extends ResourceImpl implements TexoComponent {
 
   public static final String TYPE_PARAMETER = "type"//$NON-NLS-1$
   public static final String TYPES_PARAMETER = "types"//$NON-NLS-1$
 
   private EObjectStore eObjectStore;
 
   private Set<EObject> modifiedObjects = new LinkedHashSet<EObject>();
   private Set<EObject> newObjects = new LinkedHashSet<EObject>();
   private Set<EObject> deletedObjects = new LinkedHashSet<EObject>();
 
   private boolean loaded = false;
 
   private boolean isSaving = false;
 
 
   public TexoResource() {
     setLoaded(true);
   }
 
   public TexoResource(URI uri) {
     super(uri);
     uriSet(uri);
     setLoaded(true);
   }
 
   @Override
   protected boolean isAttachedDetachedHelperRequired() {
     return true;
   }
 
   // is called for contained objects, these should not be stored
   // in the newObjects list.
   @Override
   protected void attachedHelper(EObject eObject) {
     if (.contains(eObject)) {
       .remove(eObject);
     } else if (!isLoading() && !.contains(eObject)) {
       .add(eObject);
     }
     eObject.eAdapters().add();
   }
 
   @Override
   protected void detachedHelper(EObject eObject) {
     if (! && !isUnloading() && !.contains(eObject)) {
       .add(eObject);
     }
     eObject.eAdapters().remove();
     super.detachedHelper(eObject);
   }
 
   public boolean isUnloading() {
     // unloadingContents is a protected member of the super class
     return unloadingContents != null;
   }
 
  public void setURI(URI uri) {
    super.setURI(uri);
    uriSet(uri);
  }
  protected void uriSet(URI theUri) {
    getEObjectStore().setUri(theUri);
  }
  public String getURIFragment(EObject eObject) {
    return getEObjectStore().getQualifiedIdString(eObject);
  }
  public EObject getEObject(String uriFragment) {
    final EObject eObject = getEObjectStore().getFromQualifiedIdString(uriFragment);
    if (eObject != null && eObject.eResource() == null) {
      addLoadedObject(eObject);
    }
    return eObject;
  }
  public synchronized void save(Map<?, ?> optionsthrows IOException {
    try {
       = true;
      final List<EObject> toInsert = new ArrayList<EObject>();
      toInsert.addAll();
      final List<EObject> toUpdate = new ArrayList<EObject>();
      toUpdate.addAll();
      getEObjectStore().persist(toInserttoUpdatenew ArrayList<EObject>());
      .clear();
      .clear();
      .clear();
    } finally {
       = false;
    }
  }
  public void load(Map<?, ?> optionsthrows IOException {
    final List<Stringtypes = getTypes();
    for (String type : types) {
      query("select e from " + type + " e", Collections.<StringObjectemptyMap(), 0, -1); //$NON-NLS-1$//$NON-NLS-2$
    }
     = true;
  }
  public boolean isLoaded() {
    if () {
      return true;
    }
    // no types to load, we are loaded
    if (getTypes().isEmpty()) {
      return true;
    }
    return ;
  }
  protected void doUnload() {
    super.doUnload();
    .clear();
    .clear();
    .clear();
    if ( != null) {
    }
     = false;
  }
  // some super class methods which should not be called in this implementation
  protected EObject getEObject(List<StringuriFragmentPath) {
    throw new UnsupportedOperationException();
  }
  protected EObject getEObjectForURIFragmentRootSegment(String uriFragmentRootSegment) {
    throw new UnsupportedOperationException();
  }
  protected EObject getEObjectByID(String id) {
    return getEObject(id);
  }
  public void setIntrinsicIDToEObjectMap(Map<String, EObject> intrinsicIDToEObjectMap) {
    throw new UnsupportedOperationException();
  }

  
Will query the EObjectStore and add the returned objects to this resource. If you don't want to add returned objects to this resource then get the object store (getEObjectStore() and call the query method on the object store.
  public List<EObject> query(String qryStrMap<StringObjectnamedParametersint firstResultint maxResults) {
    final List<EObject> eObjects = getEObjectStore().query(qryStrnamedParametersfirstResultmaxResults);
    for (EObject eObject : eObjects) {
      if (eObject.eResource() == null) {
        addLoadedObject(eObject);
      }
    }
    return eObjects;
  }
  protected void addLoadedObject(EObject eObject) {
    try {
      // protected member from superclass
      isLoading = true;
      if (eObject.eResource() == null) {
        if (eObject.eContainer() != null) {
          addLoadedObject(eObject.eContainer());
        } else {
          getContents().add(eObject);
        }
      }
    } finally {
      // protected member from superclass
      isLoading = false;
    }
  }
  public void refresh(EObject eObject) {
    getEObjectStore().refresh(eObject);
  }

  
Calls EObjectStore.getReferingObjects(EObject, int, boolean) and adds the returned objects to this resource. If you don't want objects to be added to this resource then get the object store ( getEObjectStore() and call the same method on the EObjectStore.
  public List<EObject> getReferingObjects(EObject targetint maxResultboolean includeContainmentReferences) {
    final List<EObject> eObjects = getEObjectStore()
        .getReferingObjects(targetmaxResultincludeContainmentReferences);
    for (EObject eObject : eObjects) {
      if (eObject.eResource() == null) {
        addLoadedObject(eObject);
      }
    }
    return eObjects;
  }
  public synchronized EObjectStore getEObjectStore() {
    if ( == null) {
    }
    return ;
  }

  
Is called when this resource does not have an objectstore. Sub class is expected to create a new one.
  protected EObjectStore createEObjectStore() {
    throw new UnsupportedOperationException("Override this method or set an object store"); //$NON-NLS-1$
  }
  public void setEObjectStore(EObjectStore eObjectStore) {
    this. = eObjectStore;
  }
  private List<StringgetTypes() {
    final List<Stringvalues = new ArrayList<String>();
    if (getURI() == null) {
      return values;
    }
    final String queryString = getURI().query();
    if (queryString == null) {
      return values;
    }
    try {
      for (String param : queryString.split("&")) { //$NON-NLS-1$
        String keyValue[] = param.split("="); //$NON-NLS-1$
        String key = URLDecoder.decode(keyValue[0], "UTF-8"); //$NON-NLS-1$
        if (.equals(key) || .equals(key)) {
          if (keyValue.length > 1) {
            final String value = URLDecoder.decode(keyValue[1], "UTF-8"); //$NON-NLS-1$
            if (value.contains(",")) { //$NON-NLS-1$
              for (String val : value.split(",")) { //$NON-NLS-1$
                values.add(val.trim());
              }
            } else {
              values.add(value);
            }
            return values;
          }
        }
      }
    } catch (Exception e) {
      throw new IllegalStateException(e);
    }
    return values;
  }
  protected Adapter createModificationTrackingAdapter() {
  }
    return new ObjectModificationTrackingAdapter();
  }

  
Overridden/replace ModificationTrackingAdapter to prevent modification during save/load.
  private class ResourceModificationTrackingAdapter extends ModificationTrackingAdapter {
    @Override
    public void notifyChanged(Notification notification) {
      // don't track changes during load or save
      if (isLoading() ||  || isUnloading()) {
        return;
      }
      super.notifyChanged(notification);
    }
  }

  
An adapter which keeps track of which objects changed for modification
  protected class ObjectModificationTrackingAdapter extends AdapterImpl {
    @Override
    public void notifyChanged(Notification notification) {
      // don't track changes during load or save
      if (isLoading() ||  || isUnloading()) {
        return;
      }
      if (!notification.isTouch() && notification.getNotifier() instanceof EObject) {
        final EObject notifier = (EObject) notification.getNotifier();
        if (notifier.eResource() == TexoResource.this && !.contains(notifier)
            && !.contains(notifier)) {
          .add(notifier);
        }
      }
    }
  }
  protected void unloaded(InternalEObject internalEObject) {
    super.unloaded(internalEObject);
    final URI objectUri = getEObjectStore().toUri(internalEObject);
    getEObjectStore().removeFromCache(objectUri);
  }

  
Set the number of levels of children to retrieve when requesting an object from the server through a request. The default for the child levels is 2 (one level).

Parameters:
childLevels a number larger than 0.
  public void setChildLevels(int childLevels) {
    getEObjectStore().setChildLevels(childLevels);
  }

  
Return the current child levels setting. If -1 is returned then the system will use the default system setting (which is 2, meaning one child level).
  public int getChildLevels() {
    return getEObjectStore().getChildLevels();
  }
New to GrepCode? Check out our FAQ X