Пример #1
0
  /**
   * Returns an ObjEntity stripped of any server-side information, such as DbEntity mapping.
   * "clientClassName" property of this entity is used to initialize "className" property of
   * returned entity.
   *
   * @since 1.2
   */
  public ObjEntity getClientEntity() {

    ClientObjEntity entity = new ClientObjEntity(getName());
    entity.setClassName(getClientClassName());
    entity.setSuperClassName(getClientSuperClassName());
    entity.setSuperEntityName(getSuperEntityName());
    entity.setDeclaredQualifier(getDeclaredQualifier());

    // TODO: should we also copy lock type?

    Collection<ObjAttribute> primaryKeys = getMutablePrimaryKeys();
    Collection<ObjAttribute> clientPK = new ArrayList<ObjAttribute>(primaryKeys.size());

    for (ObjAttribute attribute : getDeclaredAttributes()) {
      ObjAttribute clientAttribute = attribute.getClientAttribute();
      entity.addAttribute(clientAttribute);

      if (primaryKeys.remove(attribute)) {
        clientPK.add(clientAttribute);
      }
    }

    // after all meaningful pks got removed, here we only have synthetic pks
    // left...
    for (ObjAttribute attribute : primaryKeys) {
      ObjAttribute clientAttribute = attribute.getClientAttribute();
      clientPK.add(clientAttribute);
    }

    entity.setPrimaryKeys(clientPK);

    // copy relationships; skip runtime generated relationships
    for (ObjRelationship relationship : getDeclaredRelationships()) {
      if (relationship.isRuntime()) {
        continue;
      }

      ObjEntity targetEntity = (ObjEntity) relationship.getTargetEntity();
      // note that 'isClientAllowed' also checks parent DataMap client
      // policy
      // that can be handy in case of cross-map relationships
      if (targetEntity == null || !targetEntity.isClientAllowed()) {
        continue;
      }

      entity.addRelationship(relationship.getClientRelationship());
    }

    // TODO: andrus 2/5/2007 - copy embeddables
    // TODO: andrus 2/5/2007 - copy callback methods

    return entity;
  }
Пример #2
0
  /**
   * Clears all the mapping between this obj entity and its current db entity. Clears mapping
   * between entities, attributes and relationships.
   */
  public void clearDbMapping() {
    if (dbEntityName == null) {
      return;
    }

    for (ObjAttribute attribute : getAttributeMap().values()) {
      DbAttribute dbAttr = attribute.getDbAttribute();
      if (dbAttr != null) {
        attribute.setDbAttributePath(null);
      }
    }

    for (ObjRelationship relationship : getRelationships()) {
      relationship.clearDbRelationships();
    }

    dbEntityName = null;
  }
Пример #3
0
  /**
   * Returns ObjAttribute of this entity that maps to <code>dbAttribute</code> parameter. Returns
   * null if no such attribute is found.
   */
  public ObjAttribute getAttributeForDbAttribute(DbAttribute dbAttribute) {

    for (ObjAttribute next : getAttributeMap().values()) {

      if (next instanceof EmbeddedAttribute) {
        ObjAttribute embeddedAttribute =
            ((EmbeddedAttribute) next).getAttributeForDbPath(dbAttribute.getName());
        if (embeddedAttribute != null) {
          return embeddedAttribute;
        }
      } else {
        if (next.getDbAttribute() == dbAttribute) {
          return next;
        }
      }
    }

    return null;
  }
Пример #4
0
  /**
   * Returns a named attribute that is either declared in this ObjEntity or is inherited. In any
   * case returned attribute 'getEntity' method will return this entity. Returns null if no matching
   * attribute is found.
   */
  @Override
  public ObjAttribute getAttribute(String name) {
    ObjAttribute attribute = (ObjAttribute) super.getAttribute(name);
    if (attribute != null) {
      return attribute;
    }

    // check embedded attribute
    int dot = name.indexOf('.');
    if (dot > 0 && dot < name.length() - 1) {
      ObjAttribute embedded = getAttribute(name.substring(0, dot));
      if (embedded instanceof EmbeddedAttribute) {
        return ((EmbeddedAttribute) embedded).getAttribute(name.substring(dot + 1));
      }
    }

    // check super attribute
    ObjEntity superEntity = getSuperEntity();
    if (superEntity != null) {

      ObjAttribute superAttribute = superEntity.getAttribute(name);
      if (superAttribute == null) {
        return null;
      }

      // decorate returned attribute to make it appear as if it belongs to
      // this
      // entity

      ObjAttribute decoratedAttribute = new ObjAttribute(superAttribute);
      decoratedAttribute.setEntity(this);

      String pathOverride = attributeOverrides.get(name);
      if (pathOverride != null) {
        decoratedAttribute.setDbAttributePath(pathOverride);
      }

      return decoratedAttribute;
    }

    return null;
  }
Пример #5
0
  /** Recursively appends all attributes in the entity inheritance hierarchy. */
  final void appendAttributes(Map<String, ObjAttribute> map) {
    map.putAll((Map<String, ObjAttribute>) super.getAttributeMap());

    ObjEntity superEntity = getSuperEntity();
    if (superEntity != null) {
      SortedMap<String, ObjAttribute> attributeMap = new TreeMap<String, ObjAttribute>();
      superEntity.appendAttributes(attributeMap);
      for (String attributeName : attributeMap.keySet()) {

        String overridedDbPath = attributeOverrides.get(attributeName);

        ObjAttribute attribute = new ObjAttribute(attributeMap.get(attributeName));
        attribute.setEntity(this);
        if (overridedDbPath != null) {
          attribute.setDbAttributePath(overridedDbPath);
        }
        map.put(attributeName, attribute);
      }
    }
  }
Пример #6
0
  private Collection<ObjAttribute> getMutablePrimaryKeys() {
    if (getDbEntity() == null) {
      throw new CayenneRuntimeException("No DbEntity for ObjEntity: " + getName());
    }

    Collection<DbAttribute> pkAttributes = getDbEntity().getPrimaryKeys();
    Collection<ObjAttribute> attributes = new ArrayList<ObjAttribute>(pkAttributes.size());

    for (DbAttribute pk : pkAttributes) {
      ObjAttribute attribute = getAttributeForDbAttribute(pk);

      // create synthetic attribute
      if (attribute == null) {
        attribute =
            new SyntheticPKObjAttribute(NameConverter.underscoredToJava(pk.getName(), false));
        attribute.setDbAttributePath(pk.getName());
        attribute.setType(TypesMapping.getJavaBySqlType(pk.getType()));
      }

      attributes.add(attribute);
    }

    return attributes;
  }