Java tutorial
/* Copyright c 2005-2012. * Licensed under GNU LESSER General Public License, Version 3. * http://www.gnu.org/licenses */ package org.beangle.model.entity; import java.io.Serializable; import java.util.Map; import org.apache.commons.beanutils.PropertyUtils; import org.beangle.model.entity.context.DefaultModelBuilder; import org.beangle.model.entity.types.EntityType; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public final class Model { public static final String NULL = "null"; private static final Logger logger = LoggerFactory.getLogger(Model.class); private static EntityContext context; private static Populator populator; private static Model instance = new Model(); static { new DefaultModelBuilder().build(); } private Model() { } @SuppressWarnings("unchecked") public static <T> T newInstance(final Class<T> clazz) { return (T) getEntityType(clazz).newInstance(); } public static <T> T newInstance(final Class<T> clazz, final Serializable id) { @SuppressWarnings("unchecked") T entity = (T) getEntityType(clazz).newInstance(); try { PropertyUtils.setProperty(entity, "id", id); } catch (Exception e) { logger.error("initialize {} with id {} error", clazz, id); } return entity; } public static EntityType getEntityType(String entityName) { return context.getEntityType(entityName); } public static Type getType(String entityName) { return context.getType(entityName); } public static String getEntityName(Object obj) { return context.getEntityName(obj); } public static EntityType getEntityType(Class<?> clazz) { EntityType type = context.getEntityType(clazz); if (null == type) { type = new EntityType(clazz); } return type; } /** * params([attr(string)->value(object)]<br> * <br> * paramsidnullnull.<br> * ??idparams null? * * @param params * @param entity */ public static void populate(Map<String, Object> params, Object entity) { populator.populate(entity, params); } public static EntityContext getContext() { return context; } public static void setContext(EntityContext context) { Model.context = context; } public static Populator getPopulator() { return populator; } public static void setPopulator(Populator populator) { Model.populator = populator; } public static Model getInstance() { return instance; } }