Package org.qdao.model

Source Code of org.qdao.model.Project

package org.qdao.model;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import org.qdao.TableDescriptor;
import org.qdao.annotation.Column;
import org.qdao.annotation.Column.DBColumnType;
import org.qdao.annotation.PrimaryKey;
import org.qdao.annotation.Table;

@Table(name = "T_PROJECT", description = "FIRST_PROJECT")
public class Project {

  @Column(name = "PHY_NAME", decimal = 1, length = 64, type = DBColumnType.NVARCHAR2, nullable = false)
  @PrimaryKey()
  public String physcialName;

  @Column(name = "LGC_NAME", decimal = 1, length = 64, type = DBColumnType.NVARCHAR2, nullable = false)
  public String logicalName;

  @Column(name = "DESCRIPTION", length = 1024, type = DBColumnType.NVARCHAR2)
  public String description;

  public static void main(String args[]) {
    Program p1 = new Program();
    p1.identical = "WAMNET_PROGRAM";
    p1.description = "WAMNET_PROGRAM_one";

    Project p = new Project();
    p.physcialName = "WAMNET";
    p.logicalName = "wamnet";
    p.description = "The WAMNET Project";

    Class<?> tableClazz = p.getClass();

    TableDescriptor tableDescriptor = getTableDescriptor(tableClazz);

    // output the table's descriptor
    print("TABLE'S NAME " + tableDescriptor.name +
        " \t TABLE'S DESCRIPTION " + tableDescriptor.description);

    for (String key : tableDescriptor.primaryKeys) {
      print("primary key --> " + key);
    }
    for (TableDescriptor.ColumnDescription column : tableDescriptor.columns) {
      print(column.decimal + '\t' +
          column.description + '\t' +
          column.length + '\t' +
          column.name + '\t' +
          column.nullable + '\t' +
          column.type + '\t');
    }
  }

  public static void print(Object o) {
    System.out.println(o);
  }

  public static TableDescriptor getTableDescriptor(Class<?> tableClazz) {
    TableDescriptor tableDescriptor = new TableDescriptor();

    Table table = (Table) tableClazz.getAnnotation(Table.class);
    if (table != null) {
      tableDescriptor.name = table.name();
      tableDescriptor.description = table.description();
    }

    Field[] fields = tableClazz.getDeclaredFields();
    List<TableDescriptor.ColumnDescription> columnDescriptors =
        new ArrayList<TableDescriptor.ColumnDescription>();

    List<String> primaryKeys = new ArrayList<String>();

    for (int i = 0; i < fields.length; i++) {
      Column column = fields[i].getAnnotation(Column.class);
      if (column != null) {
        TableDescriptor.ColumnDescription columnDescription = new TableDescriptor.ColumnDescription();
        columnDescription.decimal = column.decimal();
        columnDescription.description = column.description();
        columnDescription.name = column.name();
        columnDescription.length = column.length();
        columnDescription.nullable = column.nullable();
        columnDescription.type = column.type();
        columnDescriptors.add(columnDescription);
      }
      PrimaryKey primaryKey = fields[i].getAnnotation(PrimaryKey.class);
      if (primaryKey != null) {
        primaryKeys.add(column.name());
      }
    }

    // set the columns
    tableDescriptor.columns =
        columnDescriptors.toArray(
        new TableDescriptor.ColumnDescription[columnDescriptors.size()]);
    tableDescriptor.primaryKeys = primaryKeys.toArray(new String[primaryKeys.size()]);
    return tableDescriptor;
  }
}
TOP

Related Classes of org.qdao.model.Project

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.