I am pretty new in Room and currently doing one of my projects in which I'm supposed to insert some retrofit GSON data into it. First of all, let me show my JSON below which will give a clear structure.
{
"organization": {
"id": 0,
"title": "string",
"description": "HTML String",
"style": {
"navigationBackgroundColor": "#cd1325",
"navigationTextColor": "#ffffff",
"topBarLabel": "27July2015abcd",
"topBarBackgroundColor": "#cd1325",
"topBarTextColor": "#ffffff",
"bodyBackgroundColor": "#f5c233",
"bodyTextColor": "#646363",
"bannerBackgroundColor": "#ffffff",
"bannerTextColor": "#000000",
"buttonBackgroundColor": "#000000",
"buttonTextColor": "#ffffff",
"baseTextSize": 0,
"htmlWrapper": "string"
}
"login_options": [{
"name": "string",
"title": "EMAIL",
"description": "string",
"state": "string",
"allowed_email_domain": "string",
"restricted_email_domain": "string"
}, {
"name": "string",
"titl"e: "GOOGLE",
"description": "string",
"url": "string",
"clientId": "string",
"clientSecret": "string",
"redirectUri": "string",
"state": "string",
"nonce": "string",
"authorizationEndpointUri": "string",
"tokenEndpointUri": "string"
}
]
}
}
I am parsing this with retrofit which is working pretty well. Below are the model classes names which has created from https://www.jsonschema2pojo.org/
Ok. Now I have to insert these into the Room database through my repository for which I am facing a lot of difficulties. Can anyone help me with how to create the entities and how to insert these data models into the Room. Still not sure whether to insert the GSON models to Room or to create Entities and put the parsing data to those and then to insert. What I have tried till now.
LoginOptionsTable
@Parcelize
@Entity
public class LoginOptionsTable {
@ColumnInfo
@PrimaryKey(autoGenerate = true)
public long loginOpnId;
@ColumnInfo(name = "login_options_name")
public String name;
@ColumnInfo(name = "login_options_title")
public String title;
@ColumnInfo(name = "login_options_description")
public String description;
@ColumnInfo
public String state;
@ColumnInfo
public String allowedEmailDomain;
@ColumnInfo
public String restrictedEmailDomain;
@ColumnInfo
public String url;
@ColumnInfo
public String clientId;
@ColumnInfo
public String clientSecret;
@ColumnInfo
public String redirectUri;
@ColumnInfo
public String nonce;
@ColumnInfo
public String authorizationEndpointUri;
@ColumnInfo
public String tokenEndpointUri;
public static List<LoginOptionsTable> fromObject(List<LoginOption>
mOptions){
List<LoginOptionsTable> groups = new ArrayList<>();
for(int i=0; i<mOptions.size(); i++){
LoginOptionsTable st = new LoginOptionsTable();
st.name = mOptions.get(i).getName();
st.title = mOptions.get(i).getTitle();
st.description = mOptions.get(i).getDescription();
st.state = mOptions.get(i).getState();
st.allowedEmailDomain = mOptions.get(i).getAllowedEmailDomain();
st.restrictedEmailDomain =
mOptions.get(i).getRestrictedEmailDomain();
st.url = mOptions.get(i).getUrl();
st.clientId = mOptions.get(i).getClientId();
st.clientSecret = mOptions.get(i).getClientSecret();
st.redirectUri = mOptions.get(i).getRedirectUri();
st.nonce = mOptions.get(i).getNonce();
st.authorizationEndpointUri =
mOptions.get(i).getAuthorizationEndpointUri();
st.tokenEndpointUri = mOptions.get(i).getTokenEndpointUri();
groups.add(st);
}
return groups;
}
}
Style Entity:
@Parcelize
@Entity
public class StyleTable {
@ColumnInfo
@PrimaryKey(autoGenerate = true)
public long styleId;
@ColumnInfo
public String navigationBackgroundColor;
@ColumnInfo
public String navigationTextColor;
@ColumnInfo
public String topBarLabel;
@ColumnInfo
public String topBarBackgroundColor;
@ColumnInfo
public String topBarTextColor;
@ColumnInfo
public String bodyBackgroundColor;
@ColumnInfo
public String bodyTextColor;
@ColumnInfo
public String bannerBackgroundColor;
@ColumnInfo
public String bannerTextColor;
@ColumnInfo
public String buttonBackgroundColor;
@ColumnInfo
public String buttonTextColor;
@ColumnInfo
public Integer baseTextSize;
@ColumnInfo
public String htmlWrapper;
public static StyleTable fromObject(Style mStyle) {
StyleTable st = new StyleTable();
st.navigationBackgroundColor = mStyle.getNavigationBackgroundColor();
st.navigationTextColor = mStyle.getNavigationTextColor();
st.topBarLabel = mStyle.getTopBarLabel();
st.topBarBackgroundColor = mStyle.getTopBarBackgroundColor();
st.topBarTextColor = mStyle.getTopBarTextColor();
st.bannerBackgroundColor = mStyle.getBodyBackgroundColor();
st.bannerTextColor = mStyle.getBannerTextColor();
st.buttonBackgroundColor = mStyle.getButtonBackgroundColor();
st.buttonTextColor = mStyle.getButtonTextColor();
st.baseTextSize = mStyle.getBaseTextSize();
st.htmlWrapper = mStyle.getHtmlWrapper();
return st;
}
}
OrgEntity:
@Parcelize
@Entity(foreignKeys = {@ForeignKey(entity = StyleTable.class, parentColumns =
"styleId", childColumns = "stId"),
@ForeignKey(entity = LoginOptionsTable.class, parentColumns =
"loginOptionId", childColumns = "loginOpnId")
})
public class OrgTable {
@ColumnInfo
@PrimaryKey
public long id;
@ColumnInfo
public String title;
@ColumnInfo
public String description;
@ColumnInfo
public long stId;
//TODo make it for mutiple table
@ColumnInfo
public Long loginOptionsId;
@Ignore
public StyleTable style;
@Ignore
public List<LoginOptionsTable> loginOptions = null;
public static OrgTable fromObject(Organization organization){
OrgTable org = new OrgTable();
org.id = organization.getId();
org.title = organization.getTitle();
org.description = organization.getDescription();
StyleTable st = StyleTable.fromObject(organization.getStyle());
org.style = st;
//make the relation through Id
org.stId = st.styleId;
List<LoginOptionsTable> lo =
LoginOptionsTable.fromObject(organization.getLoginOptions());
org.loginOptions = lo;
//make the relation through Id
org.loginOptionsId = lo.get(0).loginOpnId;
return org;
}
}
DAO
@Dao
public interface OrgDAO {
@Query("SELECT * FROM OrgTable")
OrgTable getOrganization();
@Insert
void insertOrg(OrgTable org);
}
I have tried to create these, but not able to understand how to keep the relation between these and insert/get the saved data.
from Room with complex Json structure

No comments:
Post a Comment