Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
<copyright> Copyright (c) 2011-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.provider;
 
 import java.util.Map;
 import java.util.Set;
 
 import  org.eclipse.emf.ecore.EAnnotation;
 import  org.eclipse.emf.ecore.EAttribute;
 import  org.eclipse.emf.ecore.EClass;
 import  org.eclipse.emf.ecore.EDataType;
 import  org.eclipse.emf.ecore.EObject;
 import  org.eclipse.emf.ecore.EReference;
 import  org.eclipse.emf.ecore.EStructuralFeature;
Provides the EAttribute which models the id of an object. The EAttribute is identified by an EAnnotation with the source set to ModelConstants.ID_ANNOTATION_SOURCE.

Author(s):
Martin Taal
 
 public class IdProvider implements TexoComponentTexoStaticSingleton {
 
   private static final String EMPTY_ID_STRING = ""//$NON-NLS-1$
 
   private static IdProvider instance = ComponentProvider.getInstance().newInstance(IdProvider.class);
 
   public static IdProvider getInstance() {
     return ;
   }
 
   public static void setInstance(IdProvider instance) {
     . = instance;
   }
 
   private final Map<EClass, EAttribute> idEAttributes = new ConcurrentHashMap<EClass, EAttribute>();
 
   private final Set<EClass> noIdEAttributes = new HashSet<EClass>();

  
Returns the id value of an object. Works for EObject as well as ModelObjects.

Parameters:
object the object to determine an id for, must be an object which can be converted to a ModelObject using the ModelResolver.getModelObject(Object).
Returns:
the id value of the object
 
   public Object getId(Object object) {
     if (object instanceof EObject) {
       final EObject eObject = (EObject) object;
       final EAttribute idEAttribute = getCreateIdEAttribute(eObject.eClass());
       return eObject.eGet(idEAttribute);
     }
 
     final ModelObject<?> modelObject = ModelResolver.getInstance().getModelObject(object);
     return modelObject.eGet(getCreateIdEAttribute(modelObject.eClass()));
   }

  
Returns the id value of an object as a String. Internally calls the getIdAsString(ModelObject).

Parameters:
object the object to determine an id for, must be an object which can be converted to a ModelObject using the ModelResolver.getModelObject(Object).
Returns:
the id value of the object as a String
See also:
ModelFactory.convertToString(EDataType, Object)
 
   public String getIdAsString(Object object) {
     if (object instanceof EObject) {
       final EObject eObject = (EObject) object;
       final EAttribute idEAttribute = getCreateIdEAttribute(eObject.eClass());
       return getIdAsString(eObject.eClass(), eObject.eGet(idEAttribute));
     }
     return getIdAsString(ModelResolver.getInstance().getModelObject(object));
  }

  

See also:
getIdAsString(Object)
  public String getIdAsString(ModelObject<?> modelObject) {
    final EAttribute idEAttribute = getCreateIdEAttribute(modelObject.eClass());
    return getIdAsString(modelObject.eClass(), modelObject.eGet(idEAttribute));
  }

  

See also:
getIdAsString(Object)
ModelFactory.convertToString(EDataType, Object)
  public String getIdAsString(EClass eClassObject idValue) {
    final EAttribute idEAttribute = getCreateIdEAttribute(eClass);
    final EDataType eDataType = idEAttribute.getEAttributeType();
    if (idValue == null) {
      // NOTE: or should null be returned....
      return ;
    }
    final String eDataTypePackageUri = eDataType.getEPackage().getNsURI();
    final ModelPackage modelPackage = ModelResolver.getInstance().getModelPackage(eDataTypePackageUri);
    if (modelPackage != null) {
      final ModelFactory modelFactory = modelPackage.getModelFactory();
      return modelFactory.convertToString(eDataTypeidValue);
    }
    return eDataType.getEPackage().getEFactoryInstance().convertToString(eDataTypeidValue);
  }

  
Convert the id String to an object of the correct type.

Parameters:
eClass
idString
Returns:
the id of the correct java class.
See also:
ModelFactory.createFromString(EDataType, String)
  public Object convertIdStringToId(EClass eClassString idString) {
    if (idString == null || idString.trim().length() == 0) {
      return null;
    }
    final EAttribute idEAttribute = getCreateIdEAttribute(eClass);
    final EDataType eDataType = idEAttribute.getEAttributeType();
    final String eDataTypePackageUri = eDataType.getEPackage().getNsURI();
    final ModelPackage modelPackage = ModelResolver.getInstance().getModelPackage(eDataTypePackageUri);
    if (modelPackage != null) {
      final ModelFactory modelFactory = modelPackage.getModelFactory();
      return modelFactory.createFromString(eDataTypeidString);
    }
    return eDataType.getEPackage().getEFactoryInstance().createFromString(eDataTypeidString);
  }

  
Find the EAttribute that represents the id of instances of the EClass.
  public EAttribute getIdEAttribute(EClass eClass) {
    return getCreateIdEAttribute(eClass);
  }

  
Return true if the object has a modeled id eattribute, false otherwise.
  public boolean hasIdEAttribute(Object object) {
    final ModelObject<?> modelObject = ModelResolver.getInstance().getModelObject(object);
    return hasIdEAttribute(modelObject.eClass());
  }

  
Return true if the eClass has a modeled id eattribute, false otherwise.
  public boolean hasIdEAttribute(EClass eClass) {
    EAttribute idEAttribute = .get(eClass);
    if (idEAttribute != null) {
      return true;
    }
    if (.contains(eClass)) {
      return false;
    }
    try {
      getCreateIdEAttribute(eClass);
      return true;
    } catch (IllegalStateException e) {
      .add(eClass);
      return false;
    }
  }
  protected EAttribute getCreateIdEAttribute(EClass eClass) {
    EAttribute idEAttribute = .get(eClass);
    if (idEAttribute != null) {
      return idEAttribute;
    }
    for (EStructuralFeature eFeature : eClass.getEAllStructuralFeatures()) {
      if (ModelUtils.hasEAnnotation(eFeature.)) {
        if (eFeature instanceof EReference) {
          throw new UnsupportedOperationException("EReference id's not yet supported, eclass " //$NON-NLS-1$
              + eClass.getName());
        }
        idEAttribute = (EAttribute) eFeature;
        break;
      }
    }
    if (idEAttribute == null) {
      throw new IllegalStateException("Not possible to determine id eattribute for eClass " + eClass); //$NON-NLS-1$
    }
    .put(eClassidEAttribute);
    return idEAttribute;
  }
New to GrepCode? Check out our FAQ X