Android Open Source - Android-ORM Table Wrap






From Project

Back to project page Android-ORM.

License

The source code is released under:

Apache License

If you think the Android project Android-ORM listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright 2010-2014 Jamling(li.jamling@gmail.com).
 */* w  w  w. ja va2s  .c o  m*/
 * 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 cn.ieclipse.aorm.annotation;

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

import cn.ieclipse.aorm.Aorm;
import cn.ieclipse.aorm.ORMException;

/**
 * @author Jamling
 * 
 */
public class TableWrap {
    private Class<?> clazz;
    private Table table;
    private String pkColName;
    private String pkProName;
    private ArrayList<ColumnWrap> columns = new ArrayList<ColumnWrap>();
    
    public TableWrap(Class<? extends Object> clazz) {
        Table t = clazz.getAnnotation(Table.class);
        if (t != null) {
            table = t;
            this.clazz = clazz;
            List<Field> fields = getClassField(this.clazz);
            if (fields != null) {
                for (Field field : fields) {
                    if (field.isAnnotationPresent(Column.class)) {
                        Column c = field.getAnnotation(Column.class);
                        ColumnWrap wrap = new ColumnWrap(c, field);
                        columns.add(wrap);
                        if (c.id()) {
                            pkColName = c.name();
                            pkProName = field.getName();
                        }
                    }
                }
            }
        }
        else {
            throw new ORMException(
                    "No mapping for "
                            + clazz.getName()
                            + ", did you written Table annotation before class declaration?");
        }
    }
    
    private List<Field> getClassField(Class<?> clazz) {
        List<Field> list = new ArrayList<Field>();
        getParentClassField(clazz, list);
        return list;
    }
    
    private void getParentClassField(Class<?> clazz, List<Field> list) {
        if (clazz != null) {
            Field[] fields = clazz.getDeclaredFields();
            if (fields != null) {
                for (Field field : fields) {
                    list.add(field);
                }
            }
            if (Aorm.allowExtend()) {
                getParentClassField(clazz.getSuperclass(), list);
            }
        }
    }
    
    public String getColumn(String property) {
        if (property == null) {
            throw new NullPointerException();
        }
        String colName = null;
        for (ColumnWrap col : columns) {
            if (col.getPropertyName().equals(property)) {
                colName = col.getColumnName();
                break;
            }
        }
        // when no assign alias to criteria, we should try to find
        // column in criteria tree. so we can't throw ORMException
        // you should check null return value manually.
        return colName;
    }
    
    public Method getGetterByColumn(String column) {
        Method getter = null;
        for (ColumnWrap col : columns) {
            if (col.getColumnName().equals(column)) {
                try {
                    getter = clazz
                            .getMethod(col.getGetter(), (Class<?>[]) null);
                } catch (SecurityException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (NoSuchMethodException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                break;
            }
        }
        return getter;
    }
    
    public Method getSetterByColumn(String column) {
        Method getter = null;
        for (ColumnWrap col : columns) {
            if (col.getColumnName().equals(column)) {
                try {
                    getter = clazz.getMethod(col.getSetter(),
                            col.getFieldType());
                } catch (SecurityException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (NoSuchMethodException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                break;
            }
        }
        return getter;
    }
    
    public String getPK() {
        // String colName = null;
        // for (ColumnWrap col : columns) {
        // if (col.getColumn().id()) {
        // colName = col.getColumnName();
        // break;
        // }
        // }
        //
        // return colName;
        return pkColName;
    }
    
    public String getPKProperty() {
        return pkProName;
    }
    
    public ColumnWrap getColumnWrap(String property) {
        if (property == null) {
            throw new NullPointerException();
        }
        ColumnWrap colName = null;
        for (ColumnWrap col : columns) {
            if (col.getPropertyName().equals(property)) {
                colName = col;
                break;
            }
        }
        return colName;
    }
    
    public List<ColumnWrap> getColumnWraps() {
        return columns;
    }
    
    public String getProperty(String column) {
        String propName = null;
        for (ColumnWrap col : columns) {
            if (col.getColumnName().equals(column)) {
                propName = col.getPropertyName();
                break;
            }
        }
        return propName;
    }
    
    public ArrayList<String> getColumnProjection(String alias) {
        ArrayList<String> cols = new ArrayList<String>(columns.size());
        boolean hasAlias = alias != null && !"".equals(alias.trim());
        
        if (hasAlias) {
            String preffix = alias + ".";
            for (ColumnWrap col : columns) {
                cols.add(preffix + col.getColumnName());
            }
        }
        else {
            for (ColumnWrap col : columns) {
                cols.add(col.getColumnName());
            }
        }
        return cols;
    }
    
    public ArrayList<String> getPropertyProjection(String alias) {
        ArrayList<String> cols = new ArrayList<String>(columns.size());
        boolean hasAlias = alias != null && !"".equals(alias.trim());
        
        if (hasAlias) {
            String preffix = alias + ".";
            for (ColumnWrap col : columns) {
                cols.add(preffix + col.getPropertyName());
            }
        }
        else {
            for (ColumnWrap col : columns) {
                cols.add(col.getPropertyName());
            }
        }
        return cols;
    }
    
    public String getTableName() {
        return table.name();
    }
    
    public Class<?> getTableClass() {
        return clazz;
    }
    
    @Override
    public String toString() {
        return "Table(" + table.name() + ")";
    }
}




Java Source Code List

android.content.ContentResolver.java
android.content.ContentValues.java
android.database.ContentObserver.java
android.database.Cursor.java
android.database.sqlite.SQLiteDatabase.java
android.database.sqlite.SQLiteOpenHelper.java
android.net.Uri.java
android.os.Handler.java
android.util.FastPrintWriter.java
android.util.Log.java
android.util.Printer.java
cn.ieclipse.aorm.Aorm.java
cn.ieclipse.aorm.Criteria.java
cn.ieclipse.aorm.CursorUtils.java
cn.ieclipse.aorm.Mapping.java
cn.ieclipse.aorm.ORMException.java
cn.ieclipse.aorm.Order.java
cn.ieclipse.aorm.Restrictions.java
cn.ieclipse.aorm.SessionObserver.java
cn.ieclipse.aorm.Session.java
cn.ieclipse.aorm.annotation.ColumnWrap.java
cn.ieclipse.aorm.annotation.Column.java
cn.ieclipse.aorm.annotation.TableWrap.java
cn.ieclipse.aorm.annotation.Table.java
cn.ieclipse.aorm.example.AbstractBaseAdapter.java
cn.ieclipse.aorm.example.BaseActivity.java
cn.ieclipse.aorm.example.CourseDetailActivity.java
cn.ieclipse.aorm.example.CourseListActivity.java
cn.ieclipse.aorm.example.DetailActivity.java
cn.ieclipse.aorm.example.ExampleApplication.java
cn.ieclipse.aorm.example.ExampleContentProvider.java
cn.ieclipse.aorm.example.ListActivity.java
cn.ieclipse.aorm.example.MainActivity.java
cn.ieclipse.aorm.example.StudentDetailActivity.java
cn.ieclipse.aorm.example.StudentGradeActivity.java
cn.ieclipse.aorm.example.StudentGradeDetailActivity.java
cn.ieclipse.aorm.example.StudentListActivity.java
cn.ieclipse.aorm.example.bean.Course.java
cn.ieclipse.aorm.example.bean.Grade.java
cn.ieclipse.aorm.example.bean.Student.java