com.adaptrex.core.ext.Association Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of adaptrex-core Show documentation
Show all versions of adaptrex-core Show documentation
The Core Adaptrex Framework
/*
* 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.Adaptrex;
import com.adaptrex.core.persistence.api.ORMPersistenceManager;
import com.adaptrex.core.services.AdaptrexServices;
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 java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Association {
private Logger log = LoggerFactory.getLogger(Association.class);
private ExtConfig extConfig;
private ModelDefinition modelDefinition;
/*
* Ext options
*/
private String type;
private String modelName;
private String foreignKey;
private String name;
/*
* From the "rules" TODO: Reevaluate this
*/
private String associationKey;
/*
* Create a new association
*/
public Association(ExtConfig parentConfig, String associationName) {
log.debug("Creating association for " + associationName);
Class> parentClass = parentConfig.getEntityClass();
/*
* First, make sure an association exists
*/
ORMPersistenceManager orm = parentConfig.getORMPersistenceManager();
String associationSimpleName = associationName;
if (associationName.contains(".")) {
associationSimpleName = associationName.split("\\.")[1];
}
boolean isManyToOne = orm.isManyToOne(parentClass, associationSimpleName);
boolean isOneToMany = orm.isOneToMany(parentClass, associationSimpleName) || orm.isManyToMany(parentClass, associationSimpleName);
if (!isManyToOne && !isOneToMany) {
throw new RuntimeException("Could not find association from "
+ parentConfig.getModelName() + " to "
+ associationName);
}
/*
* If we've got a :n relationship, get the type of the collection elements
*/
String entityClassName = associationName;
if (isOneToMany || isManyToOne) {
entityClassName = orm.getFieldEntityClass(parentClass, StringUtilities.uncapitalize(associationSimpleName)).getSimpleName();
}
/*
* Set the association type
*/
this.type = isManyToOne ? "belongsTo" : "hasMany";
/*
* Set the model name and association name
*/
if (isOneToMany) {
this.modelName = parentConfig.getModelName() + Inflector.getInstance().singularize(associationSimpleName);
} else {
this.modelName = parentConfig.getModelName() + associationSimpleName;
}
this.name = StringUtilities.uncapitalize(associationSimpleName);
/*
* Next, create a new config for this association's model
*/
this.extConfig = new ExtConfig(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);
/*
* Set foreignKey
*/
if (this.type.equals("belongsTo")) {
this.foreignKey = this.name + "Id";
this.associationKey = this.name;
}
/*
* 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)
@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;
}
/*
* Create Return 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 + "Id";
}
}
}