com.adaptrex.core.ext.ModelDefinition 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
The newest version!
/*
* 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.PersistenceTools;
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.AdaptrexFieldType;
import com.adaptrex.core.persistence.api.AdaptrexPersistenceManager;
import com.adaptrex.core.utilities.StringUtilities;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
//import org.apache.log4j.Logger;
public class ModelDefinition {
// private static Logger log = Logger.getLogger(ModelDefinition.class);
private List fields = new ArrayList();
private List associations = new ArrayList();
private String modelName;
private String idProperty = AdaptrexEntityType.ENTITY_ID_NAME;
private RestProxy restProxy;
private List includes;
private List excludes;
private List entityClassIncludes;
private List entityClassExcludes;
@JsonInclude
public final String extend = "Ext.data.Model";
public ModelDefinition(ExtConfig extConfig) throws Exception {
boolean isRoot = extConfig.getParentConfig() == null;
this.modelName = extConfig.getModelName();
AdaptrexPersistenceManager orm = extConfig.getORMPersistenceManager();
/*
* Add the ID field for this model
*/
this.fields.add(new FieldDefinition(AdaptrexEntityType.ENTITY_ID_NAME, ExtTypeFormatter.AUTO));
includes = extConfig.getIncludes();
excludes = extConfig.getExcludes();
entityClassIncludes = PersistenceTools.getIncludes(extConfig, extConfig.getModelName());
entityClassExcludes = PersistenceTools.getExcludes(extConfig, extConfig.getModelName());
AdaptrexEntityType adaptrexEntity = orm.getAdaptrexEntity(extConfig.getEntityClass().getSimpleName());
Map adaptrexFields = adaptrexEntity.getFields();
Map adaptrexCollections = adaptrexEntity.getCollections();
Map adaptrexAssociations = adaptrexEntity.getAssociations();
for (String key : adaptrexFields.keySet()) {
if (doInclude(key))
this.fields.add(adaptrexFields.get(key).getFieldDefinition());
}
for (String key : adaptrexCollections.keySet()) {
if (doInclude(key))
this.fields.add(adaptrexCollections.get(key).getIdsFieldDefinition());
}
for (String key : adaptrexAssociations.keySet()) {
if (doInclude(key))
this.fields.add(adaptrexAssociations.get(key).getIdFieldDefinition());
}
/*
* Add this model's associations
*/
for (String associationName : extConfig.getAssociations()) {
if (isRoot && associationName.contains(".")) continue;
if (!isRoot && !associationName.contains(".")) continue;
String associatedFieldName = associationName;
if (associatedFieldName.contains(".")) {
if (!associatedFieldName.startsWith(extConfig.getModelName() + ".")) {
continue;
} else {
associatedFieldName = associatedFieldName.split("\\.")[1];
}
}
Association association = new Association(extConfig, StringUtilities.uncapitalize(associatedFieldName));
this.associations.add(association);
/*
* Add return association to associated model
* TODO: This is tricky and need is unknown. Pull for now.
*/
// if (association.getType().equals("hasMany")) {
// String returnAssocModelName = extConfig.getModelName();
// String returnAssocName = StringUtilities.uncapitalize(extConfig.getModelName());
// try {
// association.getModelDefinition().getAssociations().add(
// new Association("belongsTo", returnAssocModelName, returnAssocName)
// );
// } catch (ClassNotFoundException e) {
// log.debug("Couldn't find return association " + returnAssocModelName);
// }
// }
}
}
private boolean doInclude(String extFieldName) {
if (includes.size() > 0) {
if (entityClassIncludes.isEmpty()) return false;
if (!entityClassIncludes.contains("*") && !entityClassIncludes.contains(extFieldName)) return false;
}
if (excludes.size() > 0) {
if (entityClassExcludes.contains("*") || entityClassExcludes.contains(extFieldName)) return false;
}
return true;
}
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 getFields() {
return this.fields;
}
@JsonInclude(JsonInclude.Include.NON_NULL)
public String getIdProperty() {
return this.idProperty;
}
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public List getAssociations() {
return this.associations;
}
@JsonInclude(JsonInclude.Include.NON_NULL)
public RestProxy getProxy() {
return this.restProxy;
}
}