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

org.usergrid.persistence.EntityFactory Maven / Gradle / Ivy

There is a newer version: 0.0.27.1
Show newest version
/*******************************************************************************
 * Copyright 2012 Apigee Corporation
 * 
 * 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 org.usergrid.persistence;

import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * A factory for creating Entity objects.
 */
public class EntityFactory {

	/** The Constant logger. */
	private static final Logger logger = LoggerFactory.getLogger(EntityFactory.class);

	/**
	 * New entity.
	 * 
	 * @param 
	 *            the generic type
	 * @param id
	 *            the id
	 * @param type
	 *            the type
	 * @param entityClass
	 *            the entity class
	 * @return new entity
	 */
	public static  A newEntity(UUID id, String type,
			Class entityClass) {
		if (type == null) {
			String errorMsg = "Entity type cannot be null";
			logger.error(errorMsg);
			throw new IllegalArgumentException(errorMsg);
		}
		if ("entity".equalsIgnoreCase(type)
				|| "dynamicentity".equalsIgnoreCase(type)) {
			String errorMsg = "Unable to instantiate entity (" + type
					+ ") because that is not a valid type.";
			logger.error(errorMsg);
			throw new IllegalArgumentException(errorMsg);
		}
		Class expectedCls = Schema.getDefaultSchema()
				.getEntityClass(type);
		if ((expectedCls != null)
				&& !DynamicEntity.class.isAssignableFrom(entityClass)
				&& !expectedCls.isAssignableFrom(entityClass)) {
			String errorMsg = "Unable to instantiate entity ("
					+ type
					+ ") because type and entityClass do not match, either use DynamicClass as entityClass or fix mismatch.";
			logger.error(errorMsg);
			throw new IllegalArgumentException(errorMsg);
		} else {
			try {
				A entity = entityClass.newInstance();
				entity.setUuid(id);
				entity.setType(type);
				return entity;
			} catch (IllegalAccessException e) {
				String errorMsg = "Unable to access entity (" + type + "): "
						+ e.getMessage();
				logger.error(errorMsg);
			} catch (InstantiationException e) {
				String errorMsg = "Unable to instantiate entity (" + type
						+ "): " + e.getMessage();
				logger.error(errorMsg);
			}
		}

		return null;
	}

	/**
	 * New entity.
	 * 
	 * @param 
	 *            the generic type
	 * @param id
	 *            the id
	 * @param entityClass
	 *            the entity class
	 * @return new entity
	 */
	public static  A newEntity(UUID id, Class entityClass) {

		if (entityClass == DynamicEntity.class) {
			return null;
		}

		String type = Schema.getDefaultSchema().getEntityType(entityClass);

		return newEntity(id, type, entityClass);
	}

	/**
	 * New entity.
	 * 
	 * @param id
	 *            the id
	 * @param type
	 *            the type
	 * @return new entity
	 */
	public static Entity newEntity(UUID id, String type) {

		Class entityClass = Schema.getDefaultSchema()
				.getEntityClass(type);
		if (entityClass == null) {
			entityClass = DynamicEntity.class;
		}

		return newEntity(id, type, entityClass);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy