All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.adaptrex.core.ext.ModelDefinition Maven / Gradle / Ivy

There is a newer version: 1.0-Alpha3
Show 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.api.ORMModelDefinition;
import com.adaptrex.core.persistence.api.ORMPersistenceManager;
import com.adaptrex.core.services.AdaptrexServices;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import java.util.ArrayList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ModelDefinition {

	private static Logger log = LoggerFactory.getLogger(ModelDefinition.class);
	
	private List associations = new ArrayList();
	private String modelName;
	private List 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 = AdaptrexServices.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 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;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy