Package com.adaptrex.core.persistence.jpa

Source Code of com.adaptrex.core.persistence.jpa.JPAModelDefinition

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

import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;
import com.adaptrex.core.ext.ExtConfig;
import com.adaptrex.core.ext.FieldDefinition;
import com.adaptrex.core.persistence.api.ORMModelDefinition;
import com.adaptrex.core.persistence.api.ORMPersistenceManager;
import com.adaptrex.core.utilities.Inflector;

public class JPAModelDefinition implements ORMModelDefinition {
 
  private List<FieldDefinition>   fields = new ArrayList<FieldDefinition>();
  private String           idProperty;
 
  public JPAModelDefinition(ExtConfig config) {
    boolean isRoot = config.getParentConfig() == null;
    ORMPersistenceManager orm = config.getORMPersistenceManager();
   
    List<String> includes = config.getIncludes();
    List<String> excludes = config.getExcludes();
   
    List<String> entityClassIncludes = new ArrayList<String>();
    List<String> entityClassExcludes = new ArrayList<String>()
   
   
    /*
     * Determine list of included/excluded fields for this model
     */
    if (isRoot) {
      /*
       * Loop each include/exclude/join config and determine if they
       * are set for the root entity.
       */
      for (String incl : includes) {
        if (!incl.contains(".")) entityClassIncludes.add(incl);
      }
   
      for (String excl : excludes) {
        if (!excl.contains(".")) entityClassExcludes.add(excl);
      }
     
    } else {
     
      /*
       * Loop each include/exclude/join config and determine if they are set for
       * the current entity at this position in the tree.  If the full path to a
       * specific field on this entity is set, we want to include it in the entity
       * specific config.  We only need the field portion when testing against
       * the current entity so that gets split out before adding to the list
       */
      for (String incl : includes) {
        if (incl.contains(config.getModelName() + ".")) {
          entityClassIncludes.add(incl.split("\\.")[1]);
        }
      }
   
      for (String excl : excludes) {
        if (excl.contains(config.getModelName() + ".")) {
          entityClassExcludes.add(excl.split("\\.")[1]);
        }
      }
    }
   
   
    /*
     * Get the class for this model
     */
    Class<?> clazz = config.getEntityClass();
   
    /*
     * Create and add the fields for this model
     */
    for (java.lang.reflect.Field field : clazz.getDeclaredFields()) {

      /*
       * Static fields don't get returned
       */
      if (Modifier.isStatic(field.getModifiers())) {
        continue;
      }


      /*
       * Get the field name
       */
      String fieldName = field.getName();
     
      /*
       * Identify id field
       */
      if (orm.isIdField(clazz, fieldName)) {
        this.fields.add(new FieldDefinition(field, config));
        this.idProperty = fieldName;
        continue;
      }
     
     
      /*
       * Create the field names for association id fields
       */
      String extFieldName = field.getName();
      if (orm.isOneToMany(clazz, fieldName) || orm.isManyToMany(clazz, fieldName)) {
        extFieldName = Inflector.getInstance().singularize(fieldName) + "Ids";
      } else if (orm.isManyToOne(clazz, fieldName)) {
        extFieldName = fieldName + "Id";
      }
     
     
      /*
       * Is this field included or excluded?
       */
      if (isRoot) {
        if (includes.size() > 0
            && (!includes.contains("*") && !includes.contains(extFieldName))) {
          continue;
        }
        if (excludes.contains("*") || excludes.contains(extFieldName)) {
          continue;
        }

      } else {
        if (includes.size() > 0) {
          if (entityClassIncludes.isEmpty()
              || (!entityClassIncludes.contains("*") && !entityClassIncludes.contains(extFieldName))) {
            continue;
          }
        }

        if (excludes.size() > 0) {
          if (entityClassExcludes.contains("*") || entityClassExcludes.contains(extFieldName)) {
            continue;
          }
        }
      }
     
     
      /*
       * If we have a one to many association, add a field to hold an array
       * of ID's for that association
       */
      if (orm.isOneToMany(clazz, fieldName) || orm.isManyToMany(clazz, fieldName) || orm.isManyToOne(clazz, fieldName)) {
        this.fields.add(new FieldDefinition(extFieldName, "auto"));
      } else {
        this.fields.add(new FieldDefinition(field, config));
      }
    }
  }
 
  @Override
  public List<FieldDefinition> getFields() {
    return fields;
  }

  @Override
  public String getIdProperty() {
    return idProperty;
  }
}
TOP

Related Classes of com.adaptrex.core.persistence.jpa.JPAModelDefinition

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.