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 com.adins.mss.dao.Emergency;

// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
/** 
 * DAO for table "TR_EMERGENCY".
*/
public class EmergencyDao extends AbstractDao<Emergency, Long> {

    public static final String TABLENAME = "TR_EMERGENCY";

    /**
     * Properties of entity Emergency.<br/>
     * Can be used for QueryBuilder and for referencing column names.
    */
    public static class Properties {
        public final static Property Uuid_emergency = new Property(0, long.class, "uuid_emergency", true, "UUID_EMERGENCY");
        public final static Property Uuid_user = new Property(1, String.class, "uuid_user", false, "UUID_USER");
        public final static Property Longitude = new Property(2, String.class, "longitude", false, "LONGITUDE");
        public final static Property Latitude = new Property(3, String.class, "latitude", false, "LATITUDE");
        public final static Property Dtm_emergency = new Property(4, java.util.Date.class, "dtm_emergency", false, "DTM_EMERGENCY");
    };

    private DaoSession daoSession;


    public EmergencyDao(DaoConfig config) {
        super(config);
    }
    
    public EmergencyDao(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_EMERGENCY\" (" + //
                "\"UUID_EMERGENCY\" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL ," + // 0: uuid_emergency
                "\"UUID_USER\" TEXT NOT NULL ," + // 1: uuid_user
                "\"LONGITUDE\" TEXT," + // 2: longitude
                "\"LATITUDE\" TEXT," + // 3: latitude
                "\"DTM_EMERGENCY\" INTEGER);"); // 4: dtm_emergency
    }

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

    /** @inheritdoc */
    @Override
    protected void bindValues(DatabaseStatement stmt, Emergency entity) {
        stmt.clearBindings();
        stmt.bindLong(1, entity.getUuid_emergency());
        stmt.bindString(2, entity.getUuid_user());
 
        String longitude = entity.getLongitude();
        if (longitude != null) {
            stmt.bindString(3, longitude);
        }
 
        String latitude = entity.getLatitude();
        if (latitude != null) {
            stmt.bindString(4, latitude);
        }
 
        java.util.Date dtm_emergency = entity.getDtm_emergency();
        if (dtm_emergency != null) {
            stmt.bindLong(5, dtm_emergency.getTime());
        }
    }

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

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

    /** @inheritdoc */
    @Override
    public Emergency readEntity(Cursor cursor, int offset) {
        Emergency entity = new Emergency( //
            cursor.getLong(offset + 0), // uuid_emergency
            cursor.getString(offset + 1), // uuid_user
            cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2), // longitude
            cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3), // latitude
            cursor.isNull(offset + 4) ? null : new java.util.Date(cursor.getLong(offset + 4)) // dtm_emergency
        );
        return entity;
    }
     
    /** @inheritdoc */
    @Override
    public void readEntity(Cursor cursor, Emergency entity, int offset) {
        entity.setUuid_emergency(cursor.getLong(offset + 0));
        entity.setUuid_user(cursor.getString(offset + 1));
        entity.setLongitude(cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2));
        entity.setLatitude(cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3));
        entity.setDtm_emergency(cursor.isNull(offset + 4) ? null : new java.util.Date(cursor.getLong(offset + 4)));
     }
    
    /** @inheritdoc */
    @Override
    protected Long updateKeyAfterInsert(Emergency entity, long rowId) {
        entity.setUuid_emergency(rowId);
        return rowId;
    }
    
    /** @inheritdoc */
    @Override
    public Long getKey(Emergency entity) {
        if(entity != null) {
            return entity.getUuid_emergency();
        } else {
            return null;
        }
    }

    /** @inheritdoc */
    @Override    
    protected boolean isEntityUpdateable() {
        return true;
    }
    
    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.getUserDao().getAllColumns());
            builder.append(" FROM TR_EMERGENCY T");
            builder.append(" LEFT JOIN MS_USER T0 ON T.\"UUID_USER\"=T0.\"UUID_USER\"");
            builder.append(' ');
            selectDeep = builder.toString();
        }
        return selectDeep;
    }
    
    protected Emergency loadCurrentDeep(Cursor cursor, boolean lock) {
        Emergency entity = loadCurrent(cursor, 0, lock);
        int offset = getAllColumns().length;

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

        return entity;    
    }

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