Package com.adaptrex.core.ext

Source Code of com.adaptrex.core.ext.Association

/*
* 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.ext;

import com.adaptrex.core.persistence.api.AdaptrexAssociationType;
import com.adaptrex.core.persistence.api.AdaptrexCollectionType;
import com.adaptrex.core.persistence.api.AdaptrexEntityType;
import com.adaptrex.core.persistence.api.AdaptrexPersistenceManager;
import com.adaptrex.core.utilities.Inflector;
import com.adaptrex.core.utilities.StringUtilities;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

public class Association {

  private static Logger log = Logger.getLogger(Association.class);

  static String HAS_MANY     = "hasMany";
  static String BELONGS_TO   = "belongsTo";
 
  private ExtConfig extConfig;
  private ModelDefinition modelDefinition;
 
  private String entityClassName;
 
 
  /*
   * Ext options
   */
  private String type;
  private String modelName;
  private String foreignKey;
  private String name;
  private String getterName;
  private String setterName;
 
 
  /*
   * From the "rules" TODO: Reevaluate this
   */
  private String associationKey;

  /*
   * Create a new association
   */
  public Association(ExtConfig parentConfig, String associationName) throws Exception {
    log.debug("Creating association for " + parentConfig.getModelName() + " to " + associationName);
    String associationSimpleName = StringUtilities.capitalize(associationName);
   
    Class<?> parentClass = parentConfig.getEntityClass();
   
    AdaptrexPersistenceManager orm = parentConfig.getORMPersistenceManager();
    AdaptrexEntityType adaptrexEntity = orm.getAdaptrexEntity(parentClass.getSimpleName());
   
    AdaptrexAssociationType adaptrexAssociation = adaptrexEntity.getAssociation(associationName);
    if (adaptrexAssociation != null) {
      this.type = BELONGS_TO;
      this.modelName = parentConfig.getModelName() + associationSimpleName;
      this.entityClassName = adaptrexAssociation.getAssociationType().getSimpleName();
     
      this.name = StringUtilities.uncapitalize(associationName);
      this.associationKey = this.name;
      this.foreignKey = this.name + StringUtils.capitalize(AdaptrexEntityType.ENTITY_ID_NAME);
     
      this.getterName = "get" + associationSimpleName;
      this.setterName = "set" + associationSimpleName;
    }
   
    AdaptrexCollectionType adaptrexCollection = adaptrexEntity.getCollection(associationName);
    if (adaptrexCollection != null) {
      this.type = HAS_MANY;
      this.modelName = parentConfig.getModelName() + Inflector.getInstance().singularize(associationSimpleName);
      this.entityClassName = adaptrexCollection.getItemType().getSimpleName();
      this.associationKey = StringUtilities.uncapitalize(associationName);
      this.name = "get" + associationSimpleName;
    }

    if (adaptrexAssociation == null && adaptrexCollection == null) {
      log.warn("Could not find association from " + parentConfig.getModelName() + " to " + associationName);
      return;
    }
   
    /*
     * The association gets it's own config
     */
    this.extConfig = new ExtConfig(orm, this.entityClassName, parentConfig.getFactoryName());
    this.extConfig.setExcludes(parentConfig.getExcludes());
    this.extConfig.setIncludes(parentConfig.getIncludes());
    this.extConfig.setAssociations(parentConfig.getAssociations());
    this.extConfig.setParentConfig(parentConfig);
    this.extConfig.setModelName(this.modelName);
   
    /*
     * Create the new model definition
     */
    log.debug("Creating a new model definition for association " + this.modelName);
    this.modelDefinition = new ModelDefinition(extConfig);
  }

 
  @JsonInclude(JsonInclude.Include.NON_NULL)
  public String getType() {
    return type;
  }

  @JsonInclude(JsonInclude.Include.NON_NULL)
  public String getGetterName() {
    return getterName;
  }
 
  @JsonInclude(JsonInclude.Include.NON_NULL)
  public String getSetterName() {
    return setterName;
 
 
  @JsonInclude(JsonInclude.Include.NON_NULL)
  public String getPrimaryKey() {
    return AdaptrexEntityType.ENTITY_ID_NAME;
  }
 
  @JsonInclude(JsonInclude.Include.NON_NULL)
  @JsonProperty("model")
  public String getModelName() {
    return modelName;
  }
 
  @JsonInclude(JsonInclude.Include.NON_NULL)
  public String getForeignKey() {
    return foreignKey;
  }

  @JsonInclude(JsonInclude.Include.NON_NULL)
  public String getAssociationKey() {
    return associationKey;
  }

  @JsonInclude(JsonInclude.Include.NON_NULL)
  public String getName() {
    return name;
  }
 
  @JsonIgnore
  public ModelDefinition getModelDefinition() {
    return modelDefinition;
  }
 
  @JsonIgnore
  public String getEntityClassName() {
    return entityClassName;
 
 
 
//  /*
//   * Create Self Association
//   */
//  public Association(String type, String model, String name)
//      throws ClassNotFoundException {
//    this.modelName = model;
//    this.type = type;
////    this.name = name;
//    if (this.type.equals("belongsTo")) {
//      this.foreignKey = name + StringUtils.capitalize(AdaptrexEntityType.ENTITY_ID_NAME);
//    }
//  }
}
TOP

Related Classes of com.adaptrex.core.ext.Association

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.