package com.adins.mss.dao;

import java.util.List;
import java.util.ArrayList;
import android.database.Cursor;

import de.greenrobot.dao.AbstractDao;
import de.greenrobot.dao.Property;
import de.greenrobot.dao.internal.SqlUtils;
import de.greenrobot.dao.internal.DaoConfig;
import de.greenrobot.dao.database.Database;
import de.greenrobot.dao.database.DatabaseStatement;
import de.greenrobot.dao.query.Query;
import de.greenrobot.dao.query.QueryBuilder;

import com.adins.mss.dao.PlanTask;

// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
/** 
 * DAO for table "TR_PLAN_TASK".
*/
public class PlanTaskDao extends AbstractDao<PlanTask, String> {

    public static final String TABLENAME = "TR_PLAN_TASK";

    /**
     * Properties of entity PlanTask.<br/>
     * Can be used for QueryBuilder and for referencing column names.
    */
    public static class Properties {
        public final static Property Uuid_plan_task = new Property(0, String.class, "uuid_plan_task", true, "UUID_PLAN_TASK");
        public final static Property Sequence = new Property(1, int.class, "sequence", false, "SEQUENCE");
        public final static Property Plan_status = new Property(2, String.class, "plan_status", false, "PLAN_STATUS");
        public final static Property Plan_start_date = new Property(3, java.util.Date.class, "plan_start_date", false, "PLAN_START_DATE");
        public final static Property Plan_crt_date = new Property(4, java.util.Date.class, "plan_crt_date", false, "PLAN_CRT_DATE");
        public final static Property View_sequence = new Property(5, Integer.class, "view_sequence", false, "VIEW_SEQUENCE");
        public final static Property Uuid_user = new Property(6, String.class, "uuid_user", false, "UUID_USER");
        public final static Property Uuid_task_h = new Property(7, String.class, "uuid_task_h", false, "UUID_TASK_H");
    };

    private DaoSession daoSession;

    private Query<PlanTask> user_PlanTaskListQuery;

    public PlanTaskDao(DaoConfig config) {
        super(config);
    }
    
    public PlanTaskDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
        this.daoSession = daoSession;
    }

    /** Creates the underlying database table. */
    public static void createTable(Database db, boolean ifNotExists) {
        String constraint = ifNotExists? "IF NOT EXISTS ": "";
        db.execSQL("CREATE TABLE " + constraint + "\"TR_PLAN_TASK\" (" + //
                "\"UUID_PLAN_TASK\" TEXT PRIMARY KEY NOT NULL ," + // 0: uuid_plan_task
                "\"SEQUENCE\" INTEGER NOT NULL ," + // 1: sequence
                "\"PLAN_STATUS\" TEXT NOT NULL ," + // 2: plan_status
                "\"PLAN_START_DATE\" INTEGER," + // 3: plan_start_date
                "\"PLAN_CRT_DATE\" INTEGER," + // 4: plan_crt_date
                "\"VIEW_SEQUENCE\" INTEGER," + // 5: view_sequence
                "\"UUID_USER\" TEXT NOT NULL ," + // 6: uuid_user
                "\"UUID_TASK_H\" TEXT NOT NULL );"); // 7: uuid_task_h
    }

    /** Drops the underlying database table. */
    public static void dropTable(Database db, boolean ifExists) {
        String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"TR_PLAN_TASK\"";
        db.execSQL(sql);
    }

    /** @inheritdoc */
    @Override
    protected void bindValues(DatabaseStatement stmt, PlanTask entity) {
        stmt.clearBindings();
        stmt.bindString(1, entity.getUuid_plan_task());
        stmt.bindLong(2, entity.getSequence());
        stmt.bindString(3, entity.getPlan_status());
 
        java.util.Date plan_start_date = entity.getPlan_start_date();
        if (plan_start_date != null) {
            stmt.bindLong(4, plan_start_date.getTime());
        }
 
        java.util.Date plan_crt_date = entity.getPlan_crt_date();
        if (plan_crt_date != null) {
            stmt.bindLong(5, plan_crt_date.getTime());
        }
 
        Integer view_sequence = entity.getView_sequence();
        if (view_sequence != null) {
            stmt.bindLong(6, view_sequence);
        }
        stmt.bindString(7, entity.getUuid_user());
        stmt.bindString(8, entity.getUuid_task_h());
    }

    @Override
    protected void attachEntity(PlanTask entity) {
        super.attachEntity(entity);
        entity.__setDaoSession(daoSession);
    }

    /** @inheritdoc */
    @Override
    public String readKey(Cursor cursor, int offset) {
        return cursor.getString(offset + 0);
    }    

    /** @inheritdoc */
    @Override
    public PlanTask readEntity(Cursor cursor, int offset) {
        PlanTask entity = new PlanTask( //
            cursor.getString(offset + 0), // uuid_plan_task
            cursor.getInt(offset + 1), // sequence
            cursor.getString(offset + 2), // plan_status
            cursor.isNull(offset + 3) ? null : new java.util.Date(cursor.getLong(offset + 3)), // plan_start_date
            cursor.isNull(offset + 4) ? null : new java.util.Date(cursor.getLong(offset + 4)), // plan_crt_date
            cursor.isNull(offset + 5) ? null : cursor.getInt(offset + 5), // view_sequence
            cursor.getString(offset + 6), // uuid_user
            cursor.getString(offset + 7) // uuid_task_h
        );
        return entity;
    }
     
    /** @inheritdoc */
    @Override
    public void readEntity(Cursor cursor, PlanTask entity, int offset) {
        entity.setUuid_plan_task(cursor.getString(offset + 0));
        entity.setSequence(cursor.getInt(offset + 1));
        entity.setPlan_status(cursor.getString(offset + 2));
        entity.setPlan_start_date(cursor.isNull(offset + 3) ? null : new java.util.Date(cursor.getLong(offset + 3)));
        entity.setPlan_crt_date(cursor.isNull(offset + 4) ? null : new java.util.Date(cursor.getLong(offset + 4)));
        entity.setView_sequence(cursor.isNull(offset + 5) ? null : cursor.getInt(offset + 5));
        entity.setUuid_user(cursor.getString(offset + 6));
        entity.setUuid_task_h(cursor.getString(offset + 7));
     }
    
    /** @inheritdoc */
    @Override
    protected String updateKeyAfterInsert(PlanTask entity, long rowId) {
        return entity.getUuid_plan_task();
    }
    
    /** @inheritdoc */
    @Override
    public String getKey(PlanTask entity) {
        if(entity != null) {
            return entity.getUuid_plan_task();
        } else {
            return null;
        }
    }

    /** @inheritdoc */
    @Override    
    protected boolean isEntityUpdateable() {
        return true;
    }
    
    /** Internal query to resolve the "planTaskList" to-many relationship of User. */
    public List<PlanTask> _queryUser_PlanTaskList(String uuid_user) {
        synchronized (this) {
            if (user_PlanTaskListQuery == null) {
                QueryBuilder<PlanTask> queryBuilder = queryBuilder();
                queryBuilder.where(Properties.Uuid_user.eq(null));
                user_PlanTaskListQuery = queryBuilder.build();
            }
        }
        Query<PlanTask> query = user_PlanTaskListQuery.forCurrentThread();
        query.setParameter(0, uuid_user);
        return query.list();
    }

    private String selectDeep;

    protected String getSelectDeep() {
        if (selectDeep == null) {
            StringBuilder builder = new StringBuilder("SELECT ");
            SqlUtils.appendColumns(builder, "T", getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T0", daoSession.getTaskHDao().getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T1", daoSession.getUserDao().getAllColumns());
            builder.append(" FROM TR_PLAN_TASK T");
            builder.append(" LEFT JOIN TR_TASK_H T0 ON T.\"UUID_TASK_H\"=T0.\"UUID_TASK_H\"");
            builder.append(" LEFT JOIN MS_USER T1 ON T.\"UUID_USER\"=T1.\"UUID_USER\"");
            builder.append(' ');
            selectDeep = builder.toString();
        }
        return selectDeep;
    }
    
    protected PlanTask loadCurrentDeep(Cursor cursor, boolean lock) {
        PlanTask entity = loadCurrent(cursor, 0, lock);
        int offset = getAllColumns().length;

        TaskH taskH = loadCurrentOther(daoSession.getTaskHDao(), cursor, offset);
         if(taskH != null) {
            entity.setTaskH(taskH);
        }
        offset += daoSession.getTaskHDao().getAllColumns().length;

        User user = loadCurrentOther(daoSession.getUserDao(), cursor, offset);
         if(user != null) {
            entity.setUser(user);
        }

        return entity;    
    }

    public PlanTask loadDeep(Long key) {
        assertSinglePk();
        if (key == null) {
            return null;
        }

        StringBuilder builder = new StringBuilder(getSelectDeep());
        builder.append("WHERE ");
        SqlUtils.appendColumnsEqValue(builder, "T", getPkColumns());
        String sql = builder.toString();
        
        String[] keyArray = new String[] { key.toString() };
        Cursor cursor = db.rawQuery(sql, keyArray);
        
        try {
            boolean available = cursor.moveToFirst();
            if (!available) {
                return null;
            } else if (!cursor.isLast()) {
                throw new IllegalStateException("Expected unique result, but count was " + cursor.getCount());
            }
            return loadCurrentDeep(cursor, true);
        } finally {
            cursor.close();
        }
    }
    
    /** Reads all available rows from the given cursor and returns a list of new ImageTO objects. */
    public List<PlanTask> loadAllDeepFromCursor(Cursor cursor) {
        int count = cursor.getCount();
        List<PlanTask> list = new ArrayList<PlanTask>(count);
        
        if (cursor.moveToFirst()) {
            if (identityScope != null) {
                identityScope.lock();
                identityScope.reserveRoom(count);
            }
            try {
                do {
                    list.add(loadCurrentDeep(cursor, false));
                } while (cursor.moveToNext());
            } finally {
                if (identityScope != null) {
                    identityScope.unlock();
                }
            }
        }
        return list;
    }
    
    protected List<PlanTask> loadDeepAllAndCloseCursor(Cursor cursor) {
        try {
            return loadAllDeepFromCursor(cursor);
        } finally {
            cursor.close();
        }
    }
    

    /** A raw-style query where you can pass any WHERE clause and arguments. */
    public List<PlanTask> queryDeep(String where, String... selectionArg) {
        Cursor cursor = db.rawQuery(getSelectDeep() + where, selectionArg);
        return loadDeepAllAndCloseCursor(cursor);
    }
 
}
