diff --git a/.idea/vcs.xml b/.idea/vcs.xml index 6564d52..94a25f7 100644 --- a/.idea/vcs.xml +++ b/.idea/vcs.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/app/app.iml b/app/app.iml index 5f44d5d..e1ab2b1 100644 --- a/app/app.iml +++ b/app/app.iml @@ -96,6 +96,7 @@ + \ No newline at end of file diff --git a/app/build.gradle b/app/build.gradle index 6a0e552..7ff0920 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -24,4 +24,6 @@ dependencies { testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:23.1.0' compile 'com.android.support:design:23.1.0' + + compile 'de.greenrobot:greendao:2.1.0' } diff --git a/app/src/main/java/com/apaza/moises/practicegreendao/database/DaoMaster.java b/app/src/main/java/com/apaza/moises/practicegreendao/database/DaoMaster.java new file mode 100644 index 0000000..540b9c6 --- /dev/null +++ b/app/src/main/java/com/apaza/moises/practicegreendao/database/DaoMaster.java @@ -0,0 +1,74 @@ +package com.apaza.moises.practicegreendao.database; + +import android.content.Context; +import android.database.sqlite.SQLiteDatabase; +import android.database.sqlite.SQLiteDatabase.CursorFactory; +import android.database.sqlite.SQLiteOpenHelper; +import android.util.Log; +import de.greenrobot.dao.AbstractDaoMaster; +import de.greenrobot.dao.identityscope.IdentityScopeType; + +import com.apaza.moises.practicegreendao.database.UserDao; +import com.apaza.moises.practicegreendao.database.PlaceDao; + +// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT. +/** + * Master of DAO (schema version 1): knows all DAOs. +*/ +public class DaoMaster extends AbstractDaoMaster { + public static final int SCHEMA_VERSION = 1; + + /** Creates underlying database table using DAOs. */ + public static void createAllTables(SQLiteDatabase db, boolean ifNotExists) { + UserDao.createTable(db, ifNotExists); + PlaceDao.createTable(db, ifNotExists); + } + + /** Drops underlying database table using DAOs. */ + public static void dropAllTables(SQLiteDatabase db, boolean ifExists) { + UserDao.dropTable(db, ifExists); + PlaceDao.dropTable(db, ifExists); + } + + public static abstract class OpenHelper extends SQLiteOpenHelper { + + public OpenHelper(Context context, String name, CursorFactory factory) { + super(context, name, factory, SCHEMA_VERSION); + } + + @Override + public void onCreate(SQLiteDatabase db) { + Log.i("greenDAO", "Creating tables for schema version " + SCHEMA_VERSION); + createAllTables(db, false); + } + } + + /** WARNING: Drops all table on Upgrade! Use only during development. */ + public static class DevOpenHelper extends OpenHelper { + public DevOpenHelper(Context context, String name, CursorFactory factory) { + super(context, name, factory); + } + + @Override + public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { + Log.i("greenDAO", "Upgrading schema from version " + oldVersion + " to " + newVersion + " by dropping all tables"); + dropAllTables(db, true); + onCreate(db); + } + } + + public DaoMaster(SQLiteDatabase db) { + super(db, SCHEMA_VERSION); + registerDaoClass(UserDao.class); + registerDaoClass(PlaceDao.class); + } + + public DaoSession newSession() { + return new DaoSession(db, IdentityScopeType.Session, daoConfigMap); + } + + public DaoSession newSession(IdentityScopeType type) { + return new DaoSession(db, type, daoConfigMap); + } + +} diff --git a/app/src/main/java/com/apaza/moises/practicegreendao/database/DaoSession.java b/app/src/main/java/com/apaza/moises/practicegreendao/database/DaoSession.java new file mode 100644 index 0000000..d1563e6 --- /dev/null +++ b/app/src/main/java/com/apaza/moises/practicegreendao/database/DaoSession.java @@ -0,0 +1,63 @@ +package com.apaza.moises.practicegreendao.database; + +import android.database.sqlite.SQLiteDatabase; + +import java.util.Map; + +import de.greenrobot.dao.AbstractDao; +import de.greenrobot.dao.AbstractDaoSession; +import de.greenrobot.dao.identityscope.IdentityScopeType; +import de.greenrobot.dao.internal.DaoConfig; + +import com.apaza.moises.practicegreendao.database.User; +import com.apaza.moises.practicegreendao.database.Place; + +import com.apaza.moises.practicegreendao.database.UserDao; +import com.apaza.moises.practicegreendao.database.PlaceDao; + +// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT. + +/** + * {@inheritDoc} + * + * @see de.greenrobot.dao.AbstractDaoSession + */ +public class DaoSession extends AbstractDaoSession { + + private final DaoConfig userDaoConfig; + private final DaoConfig placeDaoConfig; + + private final UserDao userDao; + private final PlaceDao placeDao; + + public DaoSession(SQLiteDatabase db, IdentityScopeType type, Map>, DaoConfig> + daoConfigMap) { + super(db); + + userDaoConfig = daoConfigMap.get(UserDao.class).clone(); + userDaoConfig.initIdentityScope(type); + + placeDaoConfig = daoConfigMap.get(PlaceDao.class).clone(); + placeDaoConfig.initIdentityScope(type); + + userDao = new UserDao(userDaoConfig, this); + placeDao = new PlaceDao(placeDaoConfig, this); + + registerDao(User.class, userDao); + registerDao(Place.class, placeDao); + } + + public void clear() { + userDaoConfig.getIdentityScope().clear(); + placeDaoConfig.getIdentityScope().clear(); + } + + public UserDao getUserDao() { + return userDao; + } + + public PlaceDao getPlaceDao() { + return placeDao; + } + +} diff --git a/app/src/main/java/com/apaza/moises/practicegreendao/database/Place.java b/app/src/main/java/com/apaza/moises/practicegreendao/database/Place.java new file mode 100644 index 0000000..6bb53c1 --- /dev/null +++ b/app/src/main/java/com/apaza/moises/practicegreendao/database/Place.java @@ -0,0 +1,164 @@ +package com.apaza.moises.practicegreendao.database; + +import com.apaza.moises.practicegreendao.database.DaoSession; +import de.greenrobot.dao.DaoException; + +// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT. Enable "keep" sections if you want to edit. +/** + * Entity mapped to table "PLACE". + */ +public class Place { + + private Long id; + /** Not-null value. */ + private String name; + private Float rating; + private String address; + private String description; + private String pathImage; + private long userId; + + /** Used to resolve relations */ + private transient DaoSession daoSession; + + /** Used for active entity operations. */ + private transient PlaceDao myDao; + + private User user; + private Long user__resolvedKey; + + + public Place() { + } + + public Place(Long id) { + this.id = id; + } + + public Place(Long id, String name, Float rating, String address, String description, String pathImage, long userId) { + this.id = id; + this.name = name; + this.rating = rating; + this.address = address; + this.description = description; + this.pathImage = pathImage; + this.userId = userId; + } + + /** called by internal mechanisms, do not call yourself. */ + public void __setDaoSession(DaoSession daoSession) { + this.daoSession = daoSession; + myDao = daoSession != null ? daoSession.getPlaceDao() : null; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + /** Not-null value. */ + public String getName() { + return name; + } + + /** Not-null value; ensure this value is available before it is saved to the database. */ + public void setName(String name) { + this.name = name; + } + + public Float getRating() { + return rating; + } + + public void setRating(Float rating) { + this.rating = rating; + } + + public String getAddress() { + return address; + } + + public void setAddress(String address) { + this.address = address; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public String getPathImage() { + return pathImage; + } + + public void setPathImage(String pathImage) { + this.pathImage = pathImage; + } + + public long getUserId() { + return userId; + } + + public void setUserId(long userId) { + this.userId = userId; + } + + /** To-one relationship, resolved on first access. */ + public User getUser() { + long __key = this.userId; + if (user__resolvedKey == null || !user__resolvedKey.equals(__key)) { + if (daoSession == null) { + throw new DaoException("Entity is detached from DAO context"); + } + UserDao targetDao = daoSession.getUserDao(); + User userNew = targetDao.load(__key); + synchronized (this) { + user = userNew; + user__resolvedKey = __key; + } + } + return user; + } + + public void setUser(User user) { + if (user == null) { + throw new DaoException("To-one property 'userId' has not-null constraint; cannot set to-one to null"); + } + synchronized (this) { + this.user = user; + userId = user.getId(); + user__resolvedKey = userId; + } + } + + /** Convenient call for {@link AbstractDao#delete(Object)}. Entity must attached to an entity context. */ + public void delete() { + if (myDao == null) { + throw new DaoException("Entity is detached from DAO context"); + } + myDao.delete(this); + } + + /** Convenient call for {@link AbstractDao#update(Object)}. Entity must attached to an entity context. */ + public void update() { + if (myDao == null) { + throw new DaoException("Entity is detached from DAO context"); + } + myDao.update(this); + } + + /** Convenient call for {@link AbstractDao#refresh(Object)}. Entity must attached to an entity context. */ + public void refresh() { + if (myDao == null) { + throw new DaoException("Entity is detached from DAO context"); + } + myDao.refresh(this); + } + +} diff --git a/app/src/main/java/com/apaza/moises/practicegreendao/database/PlaceDao.java b/app/src/main/java/com/apaza/moises/practicegreendao/database/PlaceDao.java new file mode 100644 index 0000000..04b03f6 --- /dev/null +++ b/app/src/main/java/com/apaza/moises/practicegreendao/database/PlaceDao.java @@ -0,0 +1,275 @@ +package com.apaza.moises.practicegreendao.database; + +import java.util.List; +import java.util.ArrayList; +import android.database.Cursor; +import android.database.sqlite.SQLiteDatabase; +import android.database.sqlite.SQLiteStatement; + +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.query.Query; +import de.greenrobot.dao.query.QueryBuilder; + +import com.apaza.moises.practicegreendao.database.Place; + +// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT. +/** + * DAO for table "PLACE". +*/ +public class PlaceDao extends AbstractDao { + + public static final String TABLENAME = "PLACE"; + + /** + * Properties of entity Place.
+ * Can be used for QueryBuilder and for referencing column names. + */ + public static class Properties { + public final static Property Id = new Property(0, Long.class, "id", true, "_id"); + public final static Property Name = new Property(1, String.class, "name", false, "NAME"); + public final static Property Rating = new Property(2, Float.class, "rating", false, "RATING"); + public final static Property Address = new Property(3, String.class, "address", false, "ADDRESS"); + public final static Property Description = new Property(4, String.class, "description", false, "DESCRIPTION"); + public final static Property PathImage = new Property(5, String.class, "pathImage", false, "PATH_IMAGE"); + public final static Property UserId = new Property(6, long.class, "userId", false, "USER_ID"); + }; + + private DaoSession daoSession; + + private Query user_PlacesQuery; + + public PlaceDao(DaoConfig config) { + super(config); + } + + public PlaceDao(DaoConfig config, DaoSession daoSession) { + super(config, daoSession); + this.daoSession = daoSession; + } + + /** Creates the underlying database table. */ + public static void createTable(SQLiteDatabase db, boolean ifNotExists) { + String constraint = ifNotExists? "IF NOT EXISTS ": ""; + db.execSQL("CREATE TABLE " + constraint + "\"PLACE\" (" + // + "\"_id\" INTEGER PRIMARY KEY ," + // 0: id + "\"NAME\" TEXT NOT NULL ," + // 1: name + "\"RATING\" REAL," + // 2: rating + "\"ADDRESS\" TEXT," + // 3: address + "\"DESCRIPTION\" TEXT," + // 4: description + "\"PATH_IMAGE\" TEXT," + // 5: pathImage + "\"USER_ID\" INTEGER NOT NULL );"); // 6: userId + } + + /** Drops the underlying database table. */ + public static void dropTable(SQLiteDatabase db, boolean ifExists) { + String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"PLACE\""; + db.execSQL(sql); + } + + /** @inheritdoc */ + @Override + protected void bindValues(SQLiteStatement stmt, Place entity) { + stmt.clearBindings(); + + Long id = entity.getId(); + if (id != null) { + stmt.bindLong(1, id); + } + stmt.bindString(2, entity.getName()); + + Float rating = entity.getRating(); + if (rating != null) { + stmt.bindDouble(3, rating); + } + + String address = entity.getAddress(); + if (address != null) { + stmt.bindString(4, address); + } + + String description = entity.getDescription(); + if (description != null) { + stmt.bindString(5, description); + } + + String pathImage = entity.getPathImage(); + if (pathImage != null) { + stmt.bindString(6, pathImage); + } + stmt.bindLong(7, entity.getUserId()); + } + + @Override + protected void attachEntity(Place entity) { + super.attachEntity(entity); + entity.__setDaoSession(daoSession); + } + + /** @inheritdoc */ + @Override + public Long readKey(Cursor cursor, int offset) { + return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0); + } + + /** @inheritdoc */ + @Override + public Place readEntity(Cursor cursor, int offset) { + Place entity = new Place( // + cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id + cursor.getString(offset + 1), // name + cursor.isNull(offset + 2) ? null : cursor.getFloat(offset + 2), // rating + cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3), // address + cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4), // description + cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5), // pathImage + cursor.getLong(offset + 6) // userId + ); + return entity; + } + + /** @inheritdoc */ + @Override + public void readEntity(Cursor cursor, Place entity, int offset) { + entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0)); + entity.setName(cursor.getString(offset + 1)); + entity.setRating(cursor.isNull(offset + 2) ? null : cursor.getFloat(offset + 2)); + entity.setAddress(cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3)); + entity.setDescription(cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4)); + entity.setPathImage(cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5)); + entity.setUserId(cursor.getLong(offset + 6)); + } + + /** @inheritdoc */ + @Override + protected Long updateKeyAfterInsert(Place entity, long rowId) { + entity.setId(rowId); + return rowId; + } + + /** @inheritdoc */ + @Override + public Long getKey(Place entity) { + if(entity != null) { + return entity.getId(); + } else { + return null; + } + } + + /** @inheritdoc */ + @Override + protected boolean isEntityUpdateable() { + return true; + } + + /** Internal query to resolve the "Places" to-many relationship of User. */ + public List _queryUser_Places(long userId) { + synchronized (this) { + if (user_PlacesQuery == null) { + QueryBuilder queryBuilder = queryBuilder(); + queryBuilder.where(Properties.UserId.eq(null)); + queryBuilder.orderRaw("T.'NAME' ASC"); + user_PlacesQuery = queryBuilder.build(); + } + } + Query query = user_PlacesQuery.forCurrentThread(); + query.setParameter(0, userId); + 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.getUserDao().getAllColumns()); + builder.append(" FROM PLACE T"); + builder.append(" LEFT JOIN USER T0 ON T.\"USER_ID\"=T0.\"_id\""); + builder.append(' '); + selectDeep = builder.toString(); + } + return selectDeep; + } + + protected Place loadCurrentDeep(Cursor cursor, boolean lock) { + Place 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 Place 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 loadAllDeepFromCursor(Cursor cursor) { + int count = cursor.getCount(); + List list = new ArrayList(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 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 queryDeep(String where, String... selectionArg) { + Cursor cursor = db.rawQuery(getSelectDeep() + where, selectionArg); + return loadDeepAllAndCloseCursor(cursor); + } + +} diff --git a/app/src/main/java/com/apaza/moises/practicegreendao/database/User.java b/app/src/main/java/com/apaza/moises/practicegreendao/database/User.java new file mode 100644 index 0000000..a4cbd84 --- /dev/null +++ b/app/src/main/java/com/apaza/moises/practicegreendao/database/User.java @@ -0,0 +1,144 @@ +package com.apaza.moises.practicegreendao.database; + +import java.util.List; +import com.apaza.moises.practicegreendao.database.DaoSession; +import de.greenrobot.dao.DaoException; + +// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT. Enable "keep" sections if you want to edit. +/** + * Entity mapped to table "USER". + */ +public class User { + + private Long id; + private String fullName; + private String address; + private String phone; + private String email; + private String pathImage; + + /** Used to resolve relations */ + private transient DaoSession daoSession; + + /** Used for active entity operations. */ + private transient UserDao myDao; + + private List Places; + + public User() { + } + + public User(Long id) { + this.id = id; + } + + public User(Long id, String fullName, String address, String phone, String email, String pathImage) { + this.id = id; + this.fullName = fullName; + this.address = address; + this.phone = phone; + this.email = email; + this.pathImage = pathImage; + } + + /** called by internal mechanisms, do not call yourself. */ + public void __setDaoSession(DaoSession daoSession) { + this.daoSession = daoSession; + myDao = daoSession != null ? daoSession.getUserDao() : null; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getFullName() { + return fullName; + } + + public void setFullName(String fullName) { + this.fullName = fullName; + } + + public String getAddress() { + return address; + } + + public void setAddress(String address) { + this.address = address; + } + + public String getPhone() { + return phone; + } + + public void setPhone(String phone) { + this.phone = phone; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + public String getPathImage() { + return pathImage; + } + + public void setPathImage(String pathImage) { + this.pathImage = pathImage; + } + + /** To-many relationship, resolved on first access (and after reset). Changes to to-many relations are not persisted, make changes to the target entity. */ + public List getPlaces() { + if (Places == null) { + if (daoSession == null) { + throw new DaoException("Entity is detached from DAO context"); + } + PlaceDao targetDao = daoSession.getPlaceDao(); + List PlacesNew = targetDao._queryUser_Places(id); + synchronized (this) { + if(Places == null) { + Places = PlacesNew; + } + } + } + return Places; + } + + /** Resets a to-many relationship, making the next get call to query for a fresh result. */ + public synchronized void resetPlaces() { + Places = null; + } + + /** Convenient call for {@link AbstractDao#delete(Object)}. Entity must attached to an entity context. */ + public void delete() { + if (myDao == null) { + throw new DaoException("Entity is detached from DAO context"); + } + myDao.delete(this); + } + + /** Convenient call for {@link AbstractDao#update(Object)}. Entity must attached to an entity context. */ + public void update() { + if (myDao == null) { + throw new DaoException("Entity is detached from DAO context"); + } + myDao.update(this); + } + + /** Convenient call for {@link AbstractDao#refresh(Object)}. Entity must attached to an entity context. */ + public void refresh() { + if (myDao == null) { + throw new DaoException("Entity is detached from DAO context"); + } + myDao.refresh(this); + } + +} diff --git a/app/src/main/java/com/apaza/moises/practicegreendao/database/UserDao.java b/app/src/main/java/com/apaza/moises/practicegreendao/database/UserDao.java new file mode 100644 index 0000000..732057a --- /dev/null +++ b/app/src/main/java/com/apaza/moises/practicegreendao/database/UserDao.java @@ -0,0 +1,160 @@ +package com.apaza.moises.practicegreendao.database; + +import android.database.Cursor; +import android.database.sqlite.SQLiteDatabase; +import android.database.sqlite.SQLiteStatement; + +import de.greenrobot.dao.AbstractDao; +import de.greenrobot.dao.Property; +import de.greenrobot.dao.internal.DaoConfig; + +import com.apaza.moises.practicegreendao.database.User; + +// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT. +/** + * DAO for table "USER". +*/ +public class UserDao extends AbstractDao { + + public static final String TABLENAME = "USER"; + + /** + * Properties of entity User.
+ * Can be used for QueryBuilder and for referencing column names. + */ + public static class Properties { + public final static Property Id = new Property(0, Long.class, "id", true, "_id"); + public final static Property FullName = new Property(1, String.class, "fullName", false, "FULL_NAME"); + public final static Property Address = new Property(2, String.class, "address", false, "ADDRESS"); + public final static Property Phone = new Property(3, String.class, "phone", false, "PHONE"); + public final static Property Email = new Property(4, String.class, "email", false, "EMAIL"); + public final static Property PathImage = new Property(5, String.class, "pathImage", false, "PATH_IMAGE"); + }; + + private DaoSession daoSession; + + + public UserDao(DaoConfig config) { + super(config); + } + + public UserDao(DaoConfig config, DaoSession daoSession) { + super(config, daoSession); + this.daoSession = daoSession; + } + + /** Creates the underlying database table. */ + public static void createTable(SQLiteDatabase db, boolean ifNotExists) { + String constraint = ifNotExists? "IF NOT EXISTS ": ""; + db.execSQL("CREATE TABLE " + constraint + "\"USER\" (" + // + "\"_id\" INTEGER PRIMARY KEY ," + // 0: id + "\"FULL_NAME\" TEXT," + // 1: fullName + "\"ADDRESS\" TEXT," + // 2: address + "\"PHONE\" TEXT," + // 3: phone + "\"EMAIL\" TEXT," + // 4: email + "\"PATH_IMAGE\" TEXT);"); // 5: pathImage + } + + /** Drops the underlying database table. */ + public static void dropTable(SQLiteDatabase db, boolean ifExists) { + String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"USER\""; + db.execSQL(sql); + } + + /** @inheritdoc */ + @Override + protected void bindValues(SQLiteStatement stmt, User entity) { + stmt.clearBindings(); + + Long id = entity.getId(); + if (id != null) { + stmt.bindLong(1, id); + } + + String fullName = entity.getFullName(); + if (fullName != null) { + stmt.bindString(2, fullName); + } + + String address = entity.getAddress(); + if (address != null) { + stmt.bindString(3, address); + } + + String phone = entity.getPhone(); + if (phone != null) { + stmt.bindString(4, phone); + } + + String email = entity.getEmail(); + if (email != null) { + stmt.bindString(5, email); + } + + String pathImage = entity.getPathImage(); + if (pathImage != null) { + stmt.bindString(6, pathImage); + } + } + + @Override + protected void attachEntity(User entity) { + super.attachEntity(entity); + entity.__setDaoSession(daoSession); + } + + /** @inheritdoc */ + @Override + public Long readKey(Cursor cursor, int offset) { + return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0); + } + + /** @inheritdoc */ + @Override + public User readEntity(Cursor cursor, int offset) { + User entity = new User( // + cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id + cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1), // fullName + cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2), // address + cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3), // phone + cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4), // email + cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5) // pathImage + ); + return entity; + } + + /** @inheritdoc */ + @Override + public void readEntity(Cursor cursor, User entity, int offset) { + entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0)); + entity.setFullName(cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1)); + entity.setAddress(cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2)); + entity.setPhone(cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3)); + entity.setEmail(cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4)); + entity.setPathImage(cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5)); + } + + /** @inheritdoc */ + @Override + protected Long updateKeyAfterInsert(User entity, long rowId) { + entity.setId(rowId); + return rowId; + } + + /** @inheritdoc */ + @Override + public Long getKey(User entity) { + if(entity != null) { + return entity.getId(); + } else { + return null; + } + } + + /** @inheritdoc */ + @Override + protected boolean isEntityUpdateable() { + return true; + } + +} diff --git a/greendao-generator/greendao-generator.iml b/greendao-generator/greendao-generator.iml index 68538e9..c2edc20 100644 --- a/greendao-generator/greendao-generator.iml +++ b/greendao-generator/greendao-generator.iml @@ -96,8 +96,8 @@ - + diff --git a/greendao-generator/src/main/java/com/apaza/moises/greendao_generator/MainGenerator.java b/greendao-generator/src/main/java/com/apaza/moises/greendao_generator/MainGenerator.java index 1b65cc8..3d86a93 100644 --- a/greendao-generator/src/main/java/com/apaza/moises/greendao_generator/MainGenerator.java +++ b/greendao-generator/src/main/java/com/apaza/moises/greendao_generator/MainGenerator.java @@ -1,19 +1,71 @@ package com.apaza.moises.greendao_generator; +import android.util.Log; + +import de.greenrobot.daogenerator.DaoGenerator; +import de.greenrobot.daogenerator.Entity; +import de.greenrobot.daogenerator.Property; import de.greenrobot.daogenerator.Schema; +import de.greenrobot.daogenerator.ToMany; public class MainGenerator { private String name; - private String description; - private String address; private float rating; - private String image; + private String address; + private String description; + private String pathImage; + + public static final String TAG = "GENERATOR GREEN DAO"; public static void main(String[] args){ - Schema schema = new Schema(1, "com.apaza.moises.practicegreendao"); + Schema schema = new Schema(1, "com.apaza.moises.practicegreendao.database"); + + Entity user = schema.addEntity("User"); + user.addIdProperty(); + user.addStringProperty("fullName"); + user.addStringProperty("address"); + user.addStringProperty("phone"); + user.addStringProperty("email"); + user.addStringProperty("pathImage"); + + Entity place = schema.addEntity("Place"); + place.addIdProperty(); + Property namePlace = place.addStringProperty("name").notNull().getProperty(); + place.addFloatProperty("rating"); + place.addStringProperty("address"); + place.addStringProperty("description"); + place.addStringProperty("pathImage"); + + //Property idUser = user.addLongProperty("idUser").getProperty(); + Property userId = place.addLongProperty("userId").notNull().getProperty(); + + //Llave foranea de usuario en la tabla de lugares + place.addToOne(user, userId); //Un mismo lugar solo puede ser sugerido por un usuario + + //Para obtener los lugares sugeridos por el usuario + ToMany userToPlace = user.addToMany(place, userId); //Es decir que un usuario puede sugerir varios lugares + userToPlace.setName("Places"); + userToPlace.orderAsc(namePlace); + + /*Entity rating = schema.addEntity("Rating"); + Property idPlaceInRating = rating.addLongProperty("idPlace").notNull().getProperty(); + Property idUserInRating = rating.addLongProperty("idUser").notNull().getProperty(); + rating.addFloatProperty("qualification"); + rating.addStringProperty("comment"); + + rating.addToOne(user, idUserInRating); + ToMany ratingOfPlace = place.addToMany(rating, idPlaceInRating); + ratingOfPlace.setName("RatingPlace");*/ + try{ + DaoGenerator daoGenerator = new DaoGenerator(); + daoGenerator.generateAll(schema, "./app/src/main/java"); + }catch(Exception e){ + e.printStackTrace(); + Log.d(TAG, "Error in generator DAO"); + } } }