org.jiemamy.entity.io.meta.EntityMeta.java Source code

Java tutorial

Introduction

Here is the source code for org.jiemamy.entity.io.meta.EntityMeta.java

Source

/*
 * Copyright 2007-2009 Jiemamy Project and the Others.
 * Created on Apr 3, 2009
 *
 * This file is part of Jiemamy.
 *
 * 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.jiemamy.entity.io.meta;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.builder.ToStringBuilder;

import org.jiemamy.utils.ArrayMap;
import org.jiemamy.utils.CollectionsUtil;
import org.jiemamy.utils.PropertyNotFoundException;

/**
 * JPA?
 * 
 * @author j5ik2o
 */
public class EntityMeta {

    /**  */
    private Class<?> entityClass;

    /**  ?? */
    private String name;

    /**  */
    private TableMeta tableMeta;

    /** ID? */
    private List<PropertyMeta> idPropertyMetas = CollectionsUtil.newArrayList();

    /** ????? */
    private ArrayMap<String, PropertyMeta> propertyMetas = new ArrayMap<String, PropertyMeta>();

    /** ?? */
    private PropertyMeta versionPropertyMeta;

    /** ????? */
    private ArrayMap<String, PropertyMeta> columnPropertyMetas = new ArrayMap<String, PropertyMeta>();

    /** mappedBy??? */
    private Map<String, Map<String, PropertyMeta>> mappedByPropertyMetas = CollectionsUtil.newHashMap();

    /**  */
    private Map<String, Object> additionalInfo = CollectionsUtil.newHashMap();

    /**
     * ?
     * 
     * @param <T> ?
     * @param key 
     * @param value 
     */
    public <T> void addAdditionalInfo(String key, T value) {
        additionalInfo.put(key, value);
    }

    /**
     * {@link PropertyMeta}?
     * 
     * @param propertyMeta {@link PropertyMeta}
     * @throws ColumnDuplicatedException ?????????
     * @throws PropertyDuplicatedException ?????????
     */
    public void addPropertyMeta(PropertyMeta propertyMeta)
            throws ColumnDuplicatedException, PropertyDuplicatedException {
        if (propertyMetas.put(propertyMeta.getName(), propertyMeta) != null) {
            throw new PropertyDuplicatedException(name, propertyMeta.getName());
        }
        if (propertyMeta.isId()) {
            idPropertyMetas.add(propertyMeta);
        }
        if (propertyMeta.isVersion()) {
            versionPropertyMeta = propertyMeta;
        }
        if (propertyMeta.getMappedBy() != null) {
            Map<String, PropertyMeta> m = mappedByPropertyMetas.get(propertyMeta.getMappedBy());
            if (m == null) {
                m = CollectionsUtil.newHashMap();
                mappedByPropertyMetas.put(propertyMeta.getMappedBy(), m);
            }
            m.put(propertyMeta.getRelationshipClass().getName(), propertyMeta);
        }
        if (propertyMeta.getColumnMeta() != null) {
            String columnName = propertyMeta.getColumnMeta().getName();
            PropertyMeta pm2 = columnPropertyMetas.put(columnName, propertyMeta);
            if (pm2 != null) {
                throw new ColumnDuplicatedException(name, pm2.getName(), propertyMeta.getName(), columnName);
            }
        }

    }

    /**
     * ??
     * 
     * @param <T> ?
     * @param key 
     * @return 
     */
    @SuppressWarnings("unchecked")
    public <T> T getAdditionalInfo(String key) {
        return (T) additionalInfo.get(key);
    }

    /**
     * ?????????{@link Iterable}??
     * 
     * @return ?????????{@link Iterable}
     */
    public Iterable<PropertyMeta> getAllColumnPropertyMeta() {
        return new Iterable<PropertyMeta>() {

            public Iterator<PropertyMeta> iterator() {
                return new Iterator<PropertyMeta>() {

                    private int i;

                    public boolean hasNext() {
                        return i < columnPropertyMetas.size();
                    }

                    public PropertyMeta next() {
                        return columnPropertyMetas.get(i++);
                    }

                    public void remove() {
                        throw new UnsupportedOperationException();
                    }
                };
            }
        };
    }

    /**
    * ???{@link Iterable}??
    * 
    * @return ???{@link Iterable}
    */
    public Iterable<PropertyMeta> getAllPropertyMeta() {
        return new Iterable<PropertyMeta>() {

            public Iterator<PropertyMeta> iterator() {
                return new Iterator<PropertyMeta>() {

                    private int i;

                    public boolean hasNext() {
                        return i < propertyMetas.size();
                    }

                    public PropertyMeta next() {
                        return propertyMetas.get(i++);
                    }

                    public void remove() {
                        throw new UnsupportedOperationException();
                    }
                };
            }
        };
    }

    /**
     * ????????
     * 
     * @param index 
     * @return 
     */
    public PropertyMeta getColumnPropertyMeta(int index) {
        return columnPropertyMetas.get(index);
    }

    /**
     * ????????
     * 
     * @param columnName ??
     * @return 
     * @throws EntityColumnNotFoundException ???????
     * 
     */
    public PropertyMeta getColumnPropertyMeta(String columnName) throws EntityColumnNotFoundException {
        PropertyMeta meta = columnPropertyMetas.get(columnName);
        if (meta == null) {
            throw new EntityColumnNotFoundException(name, columnName);
        }
        return meta;
    }

    /**
     * ??
     * 
     * @return 
     */
    public Class<?> getEntityClass() {
        return entityClass;
    }

    /**
     * ID???
     * 
     * @return ID?
     */
    public List<PropertyMeta> getIdPropertyMetas() {
        return idPropertyMetas;
    }

    /**
     * MappedBy??????
     * 
     * @param mappedBy ?????
     * @param relationshipClass 
     * @return MappedBy????
     */
    public PropertyMeta getMappedByPropertyMeta(String mappedBy, Class<?> relationshipClass) {
        Map<String, PropertyMeta> m = mappedByPropertyMetas.get(mappedBy);
        if (m == null) {
            return null;
        }
        return m.get(relationshipClass.getName());
    }

    /**
     * ????
     * 
     * @return ??
     */
    public String getName() {
        return name;
    }

    /**
     * ??
     * 
     * @param index 
     * @return 
     */
    public PropertyMeta getPropertyMeta(int index) {
        return propertyMetas.get(index);
    }

    /**
    * ??
    * 
    * @param propertyName ??
    * @return {@link PropertyMeta}
    * @throws PropertyNotFoundException ?????????
    */
    public PropertyMeta getPropertyMeta(String propertyName) throws PropertyNotFoundException {
        PropertyMeta meta = propertyMetas.get(propertyName);
        if (meta == null) {
            throw new PropertyNotFoundException(name, propertyName);
        }
        return meta;
    }

    /**
     * ???
     * 
     * @return ?
     */
    public int getPropertyMetaSize() {
        return propertyMetas.size();
    }

    /**
     * ??
     * 
     * @return {@link TableMeta}
     */
    public TableMeta getTableMeta() {
        return tableMeta;
    }

    /**
     * ???
     * 
     * @return ?
     */
    public PropertyMeta getVersionPropertyMeta() {
        return versionPropertyMeta;
    }

    /**
     * ??????????????
     * 
     * @param columnName ??
     * @return ??????
     */
    public boolean hasColumnPropertyMeta(String columnName) {
        return columnPropertyMetas.containsKey(columnName);
    }

    /**
     * ???
     * 
     * @param propertyName ??
     * @return true????
     */
    public boolean hasPropertyMeta(String propertyName) {
        return propertyMetas.containsKey(propertyName);
    }

    /**
     * ?????<code>true</code>??
     * 
     * @return ?????<code>true</code>
     */
    public boolean hasVersionPropertyMeta() {
        return versionPropertyMeta != null;
    }

    /**
     * ?
     * 
     * @param entityClass 
     */
    public void setEntityClass(Class<?> entityClass) {
        this.entityClass = entityClass;
    }

    /**
     * ID??
     * 
     * @param idPropertyMetas ID?
     */
    public void setIdPropertyMetas(List<PropertyMeta> idPropertyMetas) {
        this.idPropertyMetas = idPropertyMetas;
    }

    /**
     * ???
     * 
     * @param name ??
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * ?
     * 
     * @param propertyMetas 
     */
    public void setPropertyMetaMap(ArrayMap<String, PropertyMeta> propertyMetas) {
        this.propertyMetas = propertyMetas;
    }

    /**
     * ?
     * 
     * @param tableMeta {@link TableMeta}
     */
    public void setTableMeta(TableMeta tableMeta) {
        this.tableMeta = tableMeta;
    }

    /**
     * ??
     * 
     * @param versionPropertyMeta ?
     */
    public void setVersionPropertyMeta(PropertyMeta versionPropertyMeta) {
        this.versionPropertyMeta = versionPropertyMeta;
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }

}