Package com.adaptrex.core.ext

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

/*
* 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.ORMModelDefinition;
import com.adaptrex.core.persistence.api.ORMPersistenceManager;
import com.adaptrex.core.AdaptrexRegistry;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

public class ModelDefinition {

  private static Logger log = Logger.getLogger(ModelDefinition.class);
 
  private List<Association> associations = new ArrayList<Association>();
  private String modelName;
  private List<FieldDefinition> fields;
  private String idProperty;
  private RestProxy restProxy;
 
  @JsonInclude
  public final String extend = "Ext.data.Model";
 
  public ModelDefinition(ExtConfig extConfig) {
   
    boolean isRoot = extConfig.getParentConfig() == null;
    Class<?> clazz = extConfig.getEntityClass();
   
    /*
     * Get this model name
     */
    this.modelName = extConfig.getModelName();
   
   
    ORMPersistenceManager orm = AdaptrexRegistry.getPersistenceManager(extConfig.getFactoryName());
   
    /*
     * Get the ORM's part of the model definition
     */
    ORMModelDefinition ormModelDefinition = orm.getModelDefinition(extConfig);

    /*
     * Get info from the orm
     */
    this.fields = ormModelDefinition.getFields();
    this.idProperty = ormModelDefinition.getIdProperty();
   
    /*
     * Add this model's associations
     */
    for (String associationName : extConfig.getAssociations()) {
      if (isRoot && associationName.contains(".")) continue;
      if (!isRoot && !associationName.contains(".")) continue;

      String associationSimpleName = associationName;
      if (associationName.contains(".")) {
        if (!associationName.startsWith(extConfig.getModelName() + ".")) {
          continue;
        } else {
          associationSimpleName = associationName.split("\\.")[1];
        }
      }
     
      if (orm.isAssociated(clazz, associationSimpleName)) {
        Association association = new Association(extConfig, associationName);
        this.associations.add(association);
       
        /*
         * Add return association to associated model
         */
        if (association.getType().equals("hasMany")) {
          String returnAssocModelName = extConfig.getModelName();
          String returnAssocName = extConfig.getModelName();
          try {
            association.getModelDefinition().getAssociations().add(
                new Association("belongsTo", returnAssocModelName, returnAssocName)
              );
          } catch (ClassNotFoundException e) {
            log.debug("Couldn't find return association " + returnAssocModelName);
          }
        }
      }
    }
  }
 
  public void setProxy(RestProxy proxy) {
    this.restProxy = proxy;
  }


  @JsonIgnore
  public String getModelName() {
    return this.modelName;
  }

  /*
   * This is only different than modelName on top level models
   * where there is a fully Ext namespaced model name set
   */
  @JsonInclude
  public List<FieldDefinition> getFields() {
    return this.fields;
  }

  @JsonInclude(JsonInclude.Include.NON_NULL)
  public String getIdProperty() {
    return this.idProperty;
  }

  @JsonInclude(JsonInclude.Include.NON_EMPTY)
  public List<Association> getAssociations() {
    return this.associations;
  }

  @JsonInclude(JsonInclude.Include.NON_NULL)
  public RestProxy getProxy() {
    return this.restProxy;
  }
}
TOP

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

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.