Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use direct associations with Orma v2 #312

Merged
merged 4 commits into from
Feb 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ dependencies {
compile "org.parceler:parceler-api:1.0.4"
apt "org.parceler:parceler:1.0.4"

apt 'com.github.gfx.android.orma:orma-processor:1.3.0'
compile 'com.github.gfx.android.orma:orma:1.3.0'
apt 'com.github.gfx.android.orma:orma-processor:2.0.5'
compile 'com.github.gfx.android.orma:orma:2.0.5'
compile 'com.github.hotchemi:permissionsdispatcher:2.0.6'
apt 'com.github.hotchemi:permissionsdispatcher-processor:2.0.6'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.github.droidkaigi.confsched.activity;

import org.parceler.Parcels;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
Expand All @@ -26,8 +28,6 @@
import android.widget.Filterable;
import android.widget.TextView;

import org.parceler.Parcels;

import java.util.ArrayList;
import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package io.github.droidkaigi.confsched.dao;

import android.support.annotation.NonNull;

import com.github.gfx.android.orma.TransactionTask;

import java.util.List;
Expand All @@ -16,7 +14,6 @@
import io.github.droidkaigi.confsched.model.Place_Relation;
import io.github.droidkaigi.confsched.model.Session;
import io.github.droidkaigi.confsched.model.Session_Relation;
import io.github.droidkaigi.confsched.model.Session_Updater;
import io.github.droidkaigi.confsched.model.Speaker;
import io.github.droidkaigi.confsched.model.Speaker_Relation;
import rx.Observable;
Expand Down Expand Up @@ -47,61 +44,41 @@ private Category_Relation categoryRelation() {
return orma.relationOfCategory();
}

public void insertAll(@NonNull List<Session> sessions) {
orma.transactionAsync(new TransactionTask() {
@Override
public void execute() throws Exception {
for (Session session : sessions) {
session.prepareSave();
insertSpeaker(session.speaker);
insertCategory(session.category);
insertPlace(session.place);
}

sessionRelation().inserter().executeAll(sessions);
}
});
}

private void insertSpeaker(Speaker speaker) {
if (speaker != null && speakerRelation().selector().idEq(speaker.id).count() == 0) {
if (speaker != null && speakerRelation().selector().idEq(speaker.id).isEmpty()) {
speakerRelation().inserter().execute(speaker);
}
}

private void insertPlace(Place place) {
if (place != null && placeRelation().selector().idEq(place.id).count() == 0) {
if (place != null && placeRelation().selector().idEq(place.id).isEmpty()) {
placeRelation().inserter().execute(place);
}
}

private void insertCategory(Category category) {
if (category != null && categoryRelation().selector().idEq(category.id).count() == 0) {
if (category != null && categoryRelation().selector().idEq(category.id).isEmpty()) {
categoryRelation().inserter().execute(category);
}
}

public Observable<List<Session>> findAll() {
return sessionRelation().selector().executeAsObservable()
.map(session -> session.initAssociations(orma))
.toList();
}

public Observable<List<Session>> findByChecked() {
return sessionRelation().selector().checkedEq(true).executeAsObservable()
.map(session -> session.initAssociations(orma))
.toList();
}

public Observable<List<Session>> findByPlace(int placeId) {
return sessionRelation().selector().placeIdEq(placeId).executeAsObservable()
.map(session -> session.initAssociations(orma))
return sessionRelation().selector().placeEq(placeId).executeAsObservable()
.toList();
}

public Observable<List<Session>> findByCategory(int categoryId) {
return sessionRelation().selector().categoryIdEq(categoryId).executeAsObservable()
.map(session -> session.initAssociations(orma))
return sessionRelation().selector().categoryEq(categoryId).executeAsObservable()
.toList();
}

Expand All @@ -118,15 +95,10 @@ public void updateAllSync(List<Session> sessions) {
placeRelation().deleter().execute();

for (Session session : sessions) {
session.prepareSave();
insertSpeaker(session.speaker);
insertCategory(session.category);
insertPlace(session.place);
if (sessionRelation().idEq(session.id).count() == 0) {
sessionRelation().inserter().execute(session);
} else {
update(session);
}
sessionRelation().upserter().execute(session);
}
}

Expand All @@ -140,27 +112,6 @@ public void execute() throws Exception {
});
}

private void update(Session session) {
Session_Updater updater = sessionRelation().updater()
.idEq(session.id)
.title(session.title)
.description(session.description)
.speakerId(session.speaker.id)
.stime(session.stime)
.etime(session.etime)
.placeId(session.place.id)
.languageId(session.languageId)
.slideUrl(session.slideUrl)
.movieUrl(session.movieUrl)
.shareUrl(session.shareUrl);

if (session.category != null) {
updater.categoryId(session.category.id);
}

updater.execute();
}

public void updateChecked(Session session) {
sessionRelation().updater()
.idEq(session.id)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.github.droidkaigi.confsched.fragment;

import org.parceler.Parcels;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
Expand All @@ -19,8 +21,6 @@
import android.view.View;
import android.view.ViewGroup;

import org.parceler.Parcels;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package io.github.droidkaigi.confsched.model;

import com.google.gson.annotations.SerializedName;

import com.github.gfx.android.orma.annotation.Column;
import com.github.gfx.android.orma.annotation.PrimaryKey;
import com.github.gfx.android.orma.annotation.Table;
import com.google.gson.annotations.SerializedName;

import org.parceler.Parcel;

Expand All @@ -12,6 +14,7 @@
@Table
public class Category implements SearchGroup {

@PrimaryKey(auto = false)
@Column(indexed = true)
@SerializedName("id")
public int id;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package io.github.droidkaigi.confsched.model;

import android.support.annotation.Nullable;
import com.google.gson.annotations.SerializedName;

import com.github.gfx.android.orma.annotation.Column;
import com.github.gfx.android.orma.annotation.PrimaryKey;
import com.github.gfx.android.orma.annotation.Table;
import com.google.gson.annotations.SerializedName;

import android.support.annotation.Nullable;

@Table
public class Contributor {

@PrimaryKey
@PrimaryKey(auto = false)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One question of Orma.
What is this option?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In default settings, Orma's @PrimaryKey is automatically assigned by SQLite, even if autoincrement = true is not specified.

In this case, each ID comes from the API so it is not an "auto" value.

You can see the difference in Contributor_Schema.java by comparing both cases: @PrimaryKey(auto = true) (default) and @PrimaryKey(auto = false).

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I realized it. Thanks!

@Column("name")
@SerializedName("login")
public String name;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package io.github.droidkaigi.confsched.model;

import com.google.gson.annotations.SerializedName;

import com.github.gfx.android.orma.annotation.Column;
import com.github.gfx.android.orma.annotation.PrimaryKey;
import com.github.gfx.android.orma.annotation.Table;
import com.google.gson.annotations.SerializedName;

import org.parceler.Parcel;

@Parcel
@Table
public class Place implements SearchGroup {

@PrimaryKey(auto = false)
@Column(indexed = true)
@SerializedName("id")
public int id;
Expand Down
37 changes: 10 additions & 27 deletions app/src/main/java/io/github/droidkaigi/confsched/model/Session.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package io.github.droidkaigi.confsched.model;

import android.content.Context;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import com.google.gson.annotations.SerializedName;

import com.github.gfx.android.orma.annotation.Column;
import com.github.gfx.android.orma.annotation.PrimaryKey;
import com.github.gfx.android.orma.annotation.Table;
import com.google.gson.annotations.SerializedName;

import org.parceler.Parcel;

import android.content.Context;
import android.support.annotation.Nullable;
import android.text.TextUtils;

import java.util.Date;

import io.github.droidkaigi.confsched.R;
Expand All @@ -19,6 +21,7 @@
@Table
public class Session {

@PrimaryKey(auto = false)
@Column(indexed = true)
@SerializedName("id")
public int id;
Expand All @@ -31,9 +34,7 @@ public class Session {
@SerializedName("description")
public String description;

@Column
public int speakerId;

@Column(value = "speakerId", indexed = true)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome...! This is just what I wanted to do!

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just suggestion of Orma.
I guess it is a little diffecult to understand value, and foreign_key like ActiveRecord (RoR) is better name.
How do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might not what you think 💃

Column#value is the column name of the table.

As of konifer/droidkaigi2016 v1.0.0, there is @Column public int speakerId in the table; I prefer to use the existing speakerId column for the new @Column Speaker speaker defined in this PR, instead of defining migration steps. That's it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In other words, foreign_key definition is automatically set by Orma!

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Maybe I understood.

@SerializedName("speaker")
public Speaker speaker;

Expand All @@ -45,16 +46,12 @@ public class Session {
@SerializedName("etime")
public Date etime;

@Column(indexed = true)
public int categoryId;

@Column(value = "categoryId", indexed = true)
@Nullable
@SerializedName("category")
public Category category;

@Column(indexed = true)
public int placeId;

@Column(value = "placeId", indexed = true)
@SerializedName("place")
public Place place;

Expand Down Expand Up @@ -91,20 +88,6 @@ public Date getDisplayETime(Context context) {
return LocaleUtil.getDisplayDate(etime, context);
}

public void prepareSave() {
speakerId = speaker.id;
if (category != null) categoryId = category.id;
placeId = place.id;
}

public Session initAssociations(OrmaDatabase orma) {
if (category == null) category = orma.selectFromCategory().idEq(categoryId).value();
if (place == null) place = orma.selectFromPlace().idEq(placeId).value();
if (speaker == null) speaker = orma.selectFromSpeaker().idEq(speakerId).value();

return this;
}

public int getLanguageResId() {
switch (languageId) {
case LocaleUtil.LANG_EN_ID:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
package io.github.droidkaigi.confsched.model;

import android.support.annotation.Nullable;
import com.google.gson.annotations.SerializedName;

import com.github.gfx.android.orma.annotation.Column;
import com.github.gfx.android.orma.annotation.PrimaryKey;
import com.github.gfx.android.orma.annotation.Table;
import com.google.gson.annotations.SerializedName;

import org.parceler.Parcel;

import android.support.annotation.Nullable;

@Parcel
@Table
public class Speaker {

@PrimaryKey(auto = false)
@Column(indexed = true)
@SerializedName("id")
public int id;
Expand Down
Loading