r22 - in trunk/echobase-entities/src/main/java/fr/ifremer/echobase/entities: . meta
Author: tchemit Date: 2011-11-08 15:06:34 +0100 (Tue, 08 Nov 2011) New Revision: 22 Url: http://forge.codelutin.com/repositories/revision/echobase/22 Log: add db metas api Added: trunk/echobase-entities/src/main/java/fr/ifremer/echobase/entities/meta/ trunk/echobase-entities/src/main/java/fr/ifremer/echobase/entities/meta/ColumnMeta.java trunk/echobase-entities/src/main/java/fr/ifremer/echobase/entities/meta/DbMeta.java trunk/echobase-entities/src/main/java/fr/ifremer/echobase/entities/meta/TableMeta.java Added: trunk/echobase-entities/src/main/java/fr/ifremer/echobase/entities/meta/ColumnMeta.java =================================================================== --- trunk/echobase-entities/src/main/java/fr/ifremer/echobase/entities/meta/ColumnMeta.java (rev 0) +++ trunk/echobase-entities/src/main/java/fr/ifremer/echobase/entities/meta/ColumnMeta.java 2011-11-08 14:06:34 UTC (rev 22) @@ -0,0 +1,93 @@ +/* + * #%L + * EchoBase :: Entities + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2011 Ifremer, Codelutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * #L% + */ +package fr.ifremer.echobase.entities.meta; + +import org.hibernate.mapping.Column; +import org.nuiton.topia.persistence.TopiaEntity; + +import java.io.Serializable; +import java.util.Date; + +/** +* Define the meta data. +* +* @author tchemit <chemit@codelutin.com> +* @since 0.1 +*/ +public class ColumnMeta implements Serializable { + + protected static ColumnMeta newMeta(String name, String label, Class<?> type) { + return new ColumnMeta(name, label, type); + } + + private static final long serialVersionUID = 1L; + + protected String name; + + protected String i18nKey; + + protected Column hibernateColumn; + + protected Class<?> type; + + public ColumnMeta(String name, Class<?> type) { + this(name, name, type); + } + + public ColumnMeta(String name, String i18nKey, Class<?> type) { + this.name = name; + this.i18nKey = i18nKey; + this.type = type; + } + + public String getName() { + return name; + } + + public String getI18nKey() { + return i18nKey; + } + + public Class<?> getType() { + return type; + } + + public String getColumnType() { + String result = "string"; + if (boolean.class.equals(type)) { + result = "boolean"; + } else if (Date.class.equals(type)) { + result = "date"; + } + return result; + } + + public boolean isFK() { + return TopiaEntity.class.isAssignableFrom(type); + } + + public Column getHibernateColumn() { + return hibernateColumn; + } +} Property changes on: trunk/echobase-entities/src/main/java/fr/ifremer/echobase/entities/meta/ColumnMeta.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Added: trunk/echobase-entities/src/main/java/fr/ifremer/echobase/entities/meta/DbMeta.java =================================================================== --- trunk/echobase-entities/src/main/java/fr/ifremer/echobase/entities/meta/DbMeta.java (rev 0) +++ trunk/echobase-entities/src/main/java/fr/ifremer/echobase/entities/meta/DbMeta.java 2011-11-08 14:06:34 UTC (rev 22) @@ -0,0 +1,85 @@ +/* + * #%L + * EchoBase :: Entities + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2011 Ifremer, Codelutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * #L% + */ +package fr.ifremer.echobase.entities.meta; + + +import com.google.common.base.Preconditions; +import com.google.common.collect.Lists; +import fr.ifremer.echobase.entities.EchoBaseEntityEnum; + +import java.util.Iterator; +import java.util.List; + +/** + * Define metas about a db. + * + * @author tchemit <chemit@codelutin.com> + * @since 0.1 + */ +public class DbMeta implements Iterable<TableMeta> { + + protected List<TableMeta> tables; + + protected final EchoBaseEntityEnum[] entityEnums; + + public DbMeta(EchoBaseEntityEnum[] entityEnums) { + this.entityEnums = entityEnums; + } + + public List<String> getTableNames() { + List<String> result = Lists.newArrayList(); + for (TableMeta tableMeta : getTables()) { + result.add(tableMeta.getName()); + } + return result; + } + + public List<TableMeta> getTables() { + if (tables == null) { + tables = Lists.newArrayList(); + for (EchoBaseEntityEnum entityEnum : entityEnums) { + TableMeta tableMeta = new TableMeta(entityEnum); + tables.add(tableMeta); + } + } + return tables; + } + + public TableMeta getTable(String tableName) { + Preconditions.checkNotNull(tableName); + TableMeta result = null; + for (TableMeta tableMeta : getTables()) { + if (tableName.equals(tableMeta.getName())) { + result = tableMeta; + break; + } + } + return result; + } + + @Override + public Iterator<TableMeta> iterator() { + return getTables().iterator(); + } +} Property changes on: trunk/echobase-entities/src/main/java/fr/ifremer/echobase/entities/meta/DbMeta.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Added: trunk/echobase-entities/src/main/java/fr/ifremer/echobase/entities/meta/TableMeta.java =================================================================== --- trunk/echobase-entities/src/main/java/fr/ifremer/echobase/entities/meta/TableMeta.java (rev 0) +++ trunk/echobase-entities/src/main/java/fr/ifremer/echobase/entities/meta/TableMeta.java 2011-11-08 14:06:34 UTC (rev 22) @@ -0,0 +1,125 @@ +/* + * #%L + * EchoBase :: Entities + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2011 Ifremer, Codelutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * #L% + */ +package fr.ifremer.echobase.entities.meta; + +import com.google.common.base.Preconditions; +import com.google.common.collect.Lists; +import fr.ifremer.echobase.entities.EchoBaseDAOHelper; +import fr.ifremer.echobase.entities.EchoBaseEntityEnum; +import org.nuiton.topia.persistence.TopiaEntity; +import org.nuiton.topia.persistence.util.EntityOperator; + +import java.beans.Introspector; +import java.io.Serializable; +import java.util.Iterator; +import java.util.List; + +/** + * Define metas of a given db table. + * + * @author tchemit <chemit@codelutin.com> + * @since 0.1 + */ +public class TableMeta implements Serializable, Iterable<ColumnMeta> { + + private static final long serialVersionUID = 1L; + + protected final String name; + + protected final String i18nKey; + + protected final EntityOperator<?> operator; + + protected final EchoBaseEntityEnum entityEnum; + + protected List<ColumnMeta> columns; + + public TableMeta(EchoBaseEntityEnum entityEnum) { + Preconditions.checkNotNull(entityEnum); + this.entityEnum = entityEnum; + name = entityEnum.getImplementationFQN(); + Class<? extends TopiaEntity> contract = entityEnum.getContract(); + i18nKey = "echobase.common." + + Introspector.decapitalize(contract.getSimpleName()); + operator = EchoBaseDAOHelper.getOperator(contract); + } + + public String getName() { + return name; + } + + public EchoBaseEntityEnum getEntityEnum() { + return entityEnum; + } + + public String getI18nKey() { + return i18nKey; + } + + public EntityOperator<?> getOperator() { + return operator; + } + + public ColumnMeta getColumns(String columnName) { + Preconditions.checkNotNull(columnName); + ColumnMeta result = null; + for (ColumnMeta columnMeta : getColumns()) { + if (columnName.equals(columnMeta.getName())) { + result = columnMeta; + break; + } + } + return result; + } + + public List<String> getColumnNames() { + List<String> result = Lists.newLinkedList(); + for (ColumnMeta columnMeta : getColumns()) { + result.add(columnMeta.getName()); + } + return result; + } + + public List<ColumnMeta> getColumns() { + if (columns == null) { + columns = Lists.newLinkedList(); + Class<? extends TopiaEntity> contract = entityEnum.getContract(); + EntityOperator<? extends TopiaEntity> operator = + EchoBaseDAOHelper.getOperator(contract); + List<String> properties = operator.getProperties(); + for (String property : properties) { + Class<?> propertyType = operator.getPropertyType(property); + String i18nKey = "echobase.common." + property; + ColumnMeta tableMeta = ColumnMeta.newMeta(property, i18nKey, propertyType); + columns.add(tableMeta); + } + } + return columns; + } + + @Override + public Iterator<ColumnMeta> iterator() { + return getColumns().iterator(); + } +} Property changes on: trunk/echobase-entities/src/main/java/fr/ifremer/echobase/entities/meta/TableMeta.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native
participants (1)
-
tchemit@users.forge.codelutin.com