Package com.adaptrex.core.persistence.cayenne

Source Code of com.adaptrex.core.persistence.cayenne.CayenneEntityType

/*
* Copyright 2012 Adaptrex, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*    http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.adaptrex.core.persistence.cayenne;

import java.util.Collection;

import org.apache.cayenne.Cayenne;
import org.apache.cayenne.CayenneDataObject;
import org.apache.cayenne.lifecycle.id.EntityIdCoder;
import org.apache.cayenne.map.DataMap;
import org.apache.cayenne.map.ObjAttribute;
import org.apache.cayenne.map.ObjEntity;
import org.apache.cayenne.map.ObjRelationship;
import org.apache.commons.lang3.StringUtils;

import com.adaptrex.core.persistence.api.AdaptrexEntityType;
import com.adaptrex.core.security.SecurityModel;
import com.adaptrex.core.utilities.StringUtilities;

public class CayenneEntityType extends AdaptrexEntityType {

  public CayenneEntityType(ObjEntity objEntity, SecurityModel securityModel) throws ClassNotFoundException {
    super(objEntity.getName(), objEntity.getJavaClass(), securityModel);
   
    DataMap dataMap = objEntity.getDataMap();
    String entityName = objEntity.getName();
   
    /*
     * Field Attributes
     */
    for (ObjAttribute objAttribute : objEntity.getAttributes()) {
      this.addField(new CayenneFieldType(objEntity, objAttribute, this.secureEntity));
    }
   
    for (ObjRelationship objRelationship : objEntity.getRelationships()) {
      Class<?> itemType = dataMap.getObjEntity(objRelationship.getTargetEntityName()).getJavaClass();
      String relName = objRelationship.getName();
     
      /*
       * Join Tables (for non-flattened associations)
       */
      String targetName = objRelationship.getTargetEntityName();
      if (targetName.startsWith(entityName + "Has") || targetName.endsWith("Has" + entityName)) {
        CayenneCollectionType cayenneCollection = new CayenneCollectionType(
            objEntity, objRelationship, Collection.class, itemType, this.secureEntity 
          );
        String actualRelationName = cayenneCollection.getName();
        this.collections.put(actualRelationName, cayenneCollection);
        this.collectionIdsFieldMap.put(
            StringUtilities.singularize(actualRelationName) +
              StringUtils.capitalize(AdaptrexEntityType.COLLECTION_IDS_NAME),
            cayenneCollection);
       
      }
     
     
      /*
       * Don't add entity for actual join table
       */
      else if (entityName.startsWith(targetName + "Has") || entityName.startsWith("Has" + targetName)) {
        continue;
      }
     
     
     
      /*
       * Collections
       */
      else if (objRelationship.isToMany()) {
        Class<?> collectionType = Class.forName(objRelationship.getCollectionType());
        CayenneCollectionType cayenneCollection = new CayenneCollectionType(
            objEntity, objRelationship, collectionType, itemType, this.secureEntity
          );
       
        this.collections.put(relName, cayenneCollection);
        this.collectionIdsFieldMap.put(
            StringUtilities.singularize(relName) +
              StringUtils.capitalize(AdaptrexEntityType.COLLECTION_IDS_NAME),
            cayenneCollection);
      }
     
      /*
       * Associations
       */
      else {
        CayenneAssociationType cayenneAssociation = new CayenneAssociationType(
            objEntity, objRelationship, itemType, secureEntity
          );
        this.associations.put(relName, cayenneAssociation);
        this.associationIdFieldMap.put(
            relName + StringUtils.capitalize(AdaptrexEntityType.ENTITY_ID_NAME),
            cayenneAssociation
          );
      }
    }
  }
 
  public Object getEntityIdFrom(Object entity) {
    if (entity == null) return null;
    CayenneDataObject dataObject = (CayenneDataObject) entity;
    ObjEntity objEntity = Cayenne.getObjEntity(dataObject);
    EntityIdCoder entityIdCoder = new EntityIdCoder(objEntity);
    return entityIdCoder.toStringId(dataObject.getObjectId());     
  }
 
  /*
   * Expose the cayenne fields to allow for
   */
  public CayenneFieldType getCayenneField(String key) {
    return (CayenneFieldType) getField(key);
  }
 
  public CayenneAssociationType getCayenneAssociation(String key) {
    return (CayenneAssociationType) getAssociation(key);
  }
 
  public CayenneCollectionType getCayenneCollection(String key) {
    return (CayenneCollectionType) getCollection(key);
 
}
TOP

Related Classes of com.adaptrex.core.persistence.cayenne.CayenneEntityType

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.