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

cn.enilu.common.code.TableDescriptor Maven / Gradle / Ivy

package cn.enilu.common.code;


import org.atteo.evo.inflector.English;
import org.nutz.lang.Strings;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
 * 实体(表)基本信息描述类
* 作者: zhangtao
* 创建日期: 16-7-10
*/ public class TableDescriptor { private final String basePackageName; private final String baseUri; private final List columns = new ArrayList(); public final String name;//表名称 private String entityName;//实体类名称 private String pkType;//主键类型 private String comment;//注释 private String label="项"; private String serPackageName;//service包名 private String modPackageName;//models包名 public TableDescriptor(String name, String entityName,String basePackageName, String baseUri,String serPackageName,String modPackageName) { this.name = name; this.entityName = entityName; this.basePackageName = basePackageName; this.serPackageName = serPackageName; this.modPackageName = modPackageName; if (!baseUri.endsWith("/")) { baseUri = baseUri + "/"; } this.baseUri = baseUri; } public String getBaseUri() { return baseUri; } public String getName() { return name; } public String getPlural() { return English.plural(name); } public String getPkType() { return pkType; } public void setPkType(String pkType) { this.pkType = pkType; } public String getBasePackageName() { return basePackageName; } public List getColumns() { return columns; } public void addColumn(ColumnDescriptor column) { columns.add(column); } public String getClassName() { return getEntityClassName(); } public String getUriPrefix() { if(getName().contains("_")){ return baseUri+getName().replace("_","/"); } return baseUri + getName(); } public String getViewBasePath() { return baseUri.replaceFirst("/", "") + getName().replace("_","/"); } public String getModPackageName() { return modPackageName; } public void setModPackageName(String modPackageName) { this.modPackageName = modPackageName; } public String getSerPackageName() { return serPackageName; } public void setSerPackageName(String serPackageName) { this.serPackageName = serPackageName; } public String getEntityClassName() { if(Strings.isBlank(entityName)){ return Utils.UPPER_CAMEL(name); } return entityName; } public String getEntityFullClassName() { return basePackageName + "."+getModPackageName()+"." + getEntityClassName(); } public String getServiceFullClassName() { return basePackageName + "."+getSerPackageName()+"." + getServiceClassName(); } public String getEntityInstanceName() { return Utils.LOWER_CAMEL( name); } public String getEntityInstancesName() { return getEntityInstanceName() + "s"; } public String getServiceInstanceName() { return getEntityInstanceName() + "Service"; } public String getServiceClassName() { return Utils.UPPER_CAMEL(getEntityClassName()) + "Service"; } public String getControllerClassName() { return Utils.UPPER_CAMEL(getEntityClassName()) + "Controller"; } public void addPrimaryKeyColumn(String columnName) { for (ColumnDescriptor column : columns) { if (column.columnName.equals(columnName)) { column.primary = true; break; } } } public ColumnDescriptor getPrimaryColumn() { for (ColumnDescriptor column : columns) { if (column.primary) { return column; } } return null; } public String getPrimaryType() { ColumnDescriptor columnDescriptor = getPrimaryColumn(); if (columnDescriptor == null) { return null; } return columnDescriptor.getSimpleJavaTypeName(); } public String getTableAnnotation() { return "@Table"; } public List getImports() { Set klasses = new LinkedHashSet(); for (ColumnDescriptor column : columns) { String klass = column.getJavaType(); if (klass.startsWith("java.lang") || klass.indexOf('.') == -1) { continue; } klasses.add(column.getJavaType()); } List imports = new ArrayList(); imports.addAll(klasses); imports.add(null); imports.add(Serializable.class.getName()); imports.add(null); klasses.clear(); if (klasses.size() > 0) { imports.addAll(klasses); imports.add(null); } return imports; } public List getEnumColumns() { List result = new ArrayList(); for (ColumnDescriptor column : columns) { if (column.isEnum()) { result.add(column); } } return result; } //todo public String getQueryColumns(String op) { List result = new ArrayList(); for (ColumnDescriptor column : columns) { if (op.equals(column.getQueryOperator())) { result.add("\"" + column.columnName + "\""); } } if (result.isEmpty()) { return null; } return ""; } public List getSearchableColumns() { List result = new ArrayList(); for (ColumnDescriptor column : columns) { if (!Strings.isBlank(column.getQueryOperator())) { result.add(column); } } return result; } public List getLabeledColumns() { List result = new ArrayList(); for (ColumnDescriptor column : columns) { if (column.getComment()!=null&&!column.primary) { result.add(column); } } return result; } public String[] getLabeledColumnNames() { List columnDescriptors = getLabeledColumns(); List result = new ArrayList(); for(ColumnDescriptor descriptor:columnDescriptors){ result.add(descriptor.getFieldName()); } return result.toArray(new String[0]); } public String getLabeledColumnNamesString() { StringBuilder buf = new StringBuilder(); for (String name : getLabeledColumnNames()) { buf.append("\"").append(name).append("\"").append(", "); } if (buf.length() > 2) { buf.setLength(buf.length() - 2); } return buf.toString(); } public List getIndexColumns() { List result = new ArrayList(); for (ColumnDescriptor column : columns) { if (!Strings.isBlank(column.getLabel()) && !"id".equals(column.columnName)) { result.add(column); } } return result; } public void setLabel(String label) { this.label = label; } public String getLabel() { return label; } public String getComment() { return comment; } public void setComment(String comment) { this.comment = comment; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy