Created
September 6, 2015 06:11
-
-
Save dayerdl/7c57b13618bf159ac036 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.dayerdl.firstround.database.daos; | |
import android.content.Context; | |
import android.database.Cursor; | |
import android.database.sqlite.SQLiteException; | |
import android.text.TextUtils; | |
import android.util.Log; | |
import com.dayerdl.firstround.model.Bar; | |
import com.dayerdl.firstround.model.BarRealm; | |
import com.parse.ParseFile; | |
import com.parse.ParseGeoPoint; | |
import java.util.ArrayList; | |
import java.util.List; | |
import io.realm.Realm; | |
import io.realm.RealmObject; | |
public class BarDao { | |
private static final String LOG_TAG = "BarDao"; | |
private Realm realm ; | |
public BarDao(Context ctx){ | |
realm = Realm.getInstance(ctx); | |
} | |
/** | |
* Insert a Bar into the database | |
* | |
* @param bar | |
* @return | |
*/ | |
public void insertDataToDatabase(Bar bar) throws SQLiteException { | |
if (bar == null) { | |
Log.e(LOG_TAG, "Error inserting data into the database. The Bar cannot be null"); | |
} | |
realm.beginTransaction(); | |
BarRealm barRealm = realm.createObject(BarRealm.class); | |
populateBarRealm(bar, barRealm); | |
realm.commitTransaction(); | |
} | |
private BarRealm populateBarRealm(Bar source, BarRealm dest) { | |
dest.setId(source.getObjectId()); | |
if(source.getName() != null) { | |
dest.setName(source.getName()); | |
} | |
if(source.getDescription() != null) { | |
dest.setDescription(source.getDescription()); | |
} | |
dest.setLatitude(source.getLatitude()); | |
dest.setLongitude(source.getLongitude()); | |
ParseGeoPoint parseGeoPoint = new ParseGeoPoint(source.getLatitude(), source.getLongitude()); | |
if(parseGeoPoint != null) { | |
dest.setPosition(parseGeoPoint); | |
} | |
dest.setVisible(source.isVisible()); | |
dest.setMen(source.hasMen()); | |
dest.setLadies(source.hasLadies()); | |
dest.setPromo(source.hasPromo()); | |
if(source.getLadiesDesc() != null) { | |
dest.setLadiesDesc(source.getLadiesDesc()); | |
} | |
if(source.getMenDesc() != null) { | |
dest.setMenDesc(source.getMenDesc()); | |
} | |
ParseFile file = source.getPhotoFile(); | |
if (file != null) { | |
String url = file.getUrl(); | |
if (url != null && !TextUtils.isEmpty(url)) { | |
dest.setPhoto(file.getUrl()); | |
} | |
} | |
if(source.getAddress() != null) { | |
dest.setAddress(source.getAddress()); | |
} | |
if(source.getRating() != null) { | |
dest.setRate((int) source.getRating()); | |
} | |
String daysOpen = source.getDaysOpenString(); | |
if (daysOpen != null && !TextUtils.isEmpty(daysOpen)) { | |
dest.setDaysOpen(daysOpen); | |
} | |
return dest; | |
} | |
private Bar populateBarParse(BarRealm source, Bar dest) { | |
dest.setObjectId(source.getId()); | |
dest.setName(source.getName()); | |
dest.setDescription(source.getDescription()); | |
dest.setLatitude(source.getLatitude()); | |
dest.setLongitude(source.getLongitude()); | |
ParseGeoPoint parseGeoPoint = new ParseGeoPoint(source.getLatitude(), source.getLongitude()); | |
dest.setLocation(parseGeoPoint); | |
dest.setVisible(source.isVisible()); | |
dest.setMen(source.isMen()); | |
dest.setLadies(source.isLadies()); | |
dest.setPromo(source.isPromo()); | |
dest.setLadiesDesc(source.getLadiesDesc()); | |
dest.setMenDesc(source.getMenDesc()); | |
String uri = source.getPhoto(); | |
if (!TextUtils.isEmpty(uri)) { | |
dest.setPhotoURI(uri); | |
} | |
dest.setAddress(source.getAddress()); | |
dest.setRating(source.getRate()); | |
String daysOpen = source.getDaysOpen(); | |
if (!TextUtils.isEmpty(daysOpen)) { | |
dest.setDaysOpen(daysOpen); | |
} | |
return dest; | |
} | |
public Bar getBar(String objectId) { | |
BarRealm barStored = realm.where(BarRealm.class).equalTo(Bar.ID, objectId).findFirst(); | |
Bar bar = populateBarParse(barStored, new Bar()); | |
return bar; | |
} | |
public List<Bar> queryAllData() { | |
List<BarRealm> listBars = realm.where(BarRealm.class).findAll(); | |
List<Bar> listParseBars = new ArrayList<>(); | |
for(BarRealm br : listBars){ | |
Bar b = new Bar(); | |
listParseBars.add(populateBarParse(br, b)); | |
} | |
return listParseBars; | |
} | |
public void deleteAll() { | |
realm.beginTransaction(); | |
realm.allObjects(BarRealm.class).clear(); | |
realm.commitTransaction(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment