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.ThemeItem;

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

    public static final String TABLENAME = "TR_THEME_ITEM";

    /**
     * Properties of entity ThemeItem.<br/>
     * Can be used for QueryBuilder and for referencing column names.
    */
    public static class Properties {
        public final static Property Uuid_theme_item = new Property(0, String.class, "uuid_theme_item", true, "UUID_THEME_ITEM");
        public final static Property Theme_item = new Property(1, String.class, "theme_item", false, "THEME_ITEM");
        public final static Property Value = new Property(2, String.class, "value", false, "VALUE");
        public final static Property Uuid_theme = new Property(3, String.class, "uuid_theme", false, "UUID_THEME");
    };

    private DaoSession daoSession;

    private Query<ThemeItem> theme_ThemeItemListQuery;

    public ThemeItemDao(DaoConfig config) {
        super(config);
    }
    
    public ThemeItemDao(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_THEME_ITEM\" (" + //
                "\"UUID_THEME_ITEM\" TEXT PRIMARY KEY NOT NULL ," + // 0: uuid_theme_item
                "\"THEME_ITEM\" TEXT NOT NULL ," + // 1: theme_item
                "\"VALUE\" TEXT," + // 2: value
                "\"UUID_THEME\" TEXT);"); // 3: uuid_theme
    }

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

    /** @inheritdoc */
    @Override
    protected void bindValues(DatabaseStatement stmt, ThemeItem entity) {
        stmt.clearBindings();
        stmt.bindString(1, entity.getUuid_theme_item());
        stmt.bindString(2, entity.getTheme_item());
 
        String value = entity.getValue();
        if (value != null) {
            stmt.bindString(3, value);
        }
 
        String uuid_theme = entity.getUuid_theme();
        if (uuid_theme != null) {
            stmt.bindString(4, uuid_theme);
        }
    }

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

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

    /** @inheritdoc */
    @Override
    public ThemeItem readEntity(Cursor cursor, int offset) {
        ThemeItem entity = new ThemeItem( //
            cursor.getString(offset + 0), // uuid_theme_item
            cursor.getString(offset + 1), // theme_item
            cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2), // value
            cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3) // uuid_theme
        );
        return entity;
    }
     
    /** @inheritdoc */
    @Override
    public void readEntity(Cursor cursor, ThemeItem entity, int offset) {
        entity.setUuid_theme_item(cursor.getString(offset + 0));
        entity.setTheme_item(cursor.getString(offset + 1));
        entity.setValue(cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2));
        entity.setUuid_theme(cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3));
     }
    
    /** @inheritdoc */
    @Override
    protected String updateKeyAfterInsert(ThemeItem entity, long rowId) {
        return entity.getUuid_theme_item();
    }
    
    /** @inheritdoc */
    @Override
    public String getKey(ThemeItem entity) {
        if(entity != null) {
            return entity.getUuid_theme_item();
        } else {
            return null;
        }
    }

    /** @inheritdoc */
    @Override    
    protected boolean isEntityUpdateable() {
        return true;
    }
    
    /** Internal query to resolve the "themeItemList" to-many relationship of Theme. */
    public List<ThemeItem> _queryTheme_ThemeItemList(String uuid_theme) {
        synchronized (this) {
            if (theme_ThemeItemListQuery == null) {
                QueryBuilder<ThemeItem> queryBuilder = queryBuilder();
                queryBuilder.where(Properties.Uuid_theme.eq(null));
                theme_ThemeItemListQuery = queryBuilder.build();
            }
        }
        Query<ThemeItem> query = theme_ThemeItemListQuery.forCurrentThread();
        query.setParameter(0, uuid_theme);
        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.getThemeDao().getAllColumns());
            builder.append(" FROM TR_THEME_ITEM T");
            builder.append(" LEFT JOIN TR_THEME T0 ON T.\"UUID_THEME\"=T0.\"UUID_THEME\"");
            builder.append(' ');
            selectDeep = builder.toString();
        }
        return selectDeep;
    }
    
    protected ThemeItem loadCurrentDeep(Cursor cursor, boolean lock) {
        ThemeItem entity = loadCurrent(cursor, 0, lock);
        int offset = getAllColumns().length;

        Theme theme = loadCurrentOther(daoSession.getThemeDao(), cursor, offset);
        entity.setTheme(theme);

        return entity;    
    }

    public ThemeItem 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<ThemeItem> loadAllDeepFromCursor(Cursor cursor) {
        int count = cursor.getCount();
        List<ThemeItem> list = new ArrayList<ThemeItem>(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<ThemeItem> 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<ThemeItem> queryDeep(String where, String... selectionArg) {
        Cursor cursor = db.rawQuery(getSelectDeep() + where, selectionArg);
        return loadDeepAllAndCloseCursor(cursor);
    }
 
}
