Persisting objects in SugarORM - java

I have a Book class:
public class Book extends SugarRecord {
private String mBookName;
private String mAuthorName;
private List<Page> mPageList;
public Book() {
}
public Book(String bookname, String authorName) {
mBookName = bookname;
mAuthorName = authorName;
mPageList = new ArrayList<>();
}
public String getAuthorName() {
return mAuthorName;
}
public void setAuthorName(String authorName) {
mAuthorName = authorName;
}
public String getBookName() {
return mBookName;
}
public void setBookName(String bookName) {
mBookName = bookName;
}
public void addPage(Page page) {
mPageList.add(page);
}
}
and the Page class:
public class Page extends SugarRecord {
private String mText;
public Page() {
}
public Page(String text) {
mText = text;
}
public String getText() {
return mText;
}
public void setText(String text) {
mText = text;
}
}
I am testing it with this:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Book book = new Book("Some Book Title", "John Doe");
Page page1 = new Page("Once upon a time there was a very lonely bunny who wanted some friends.");
Page page2 = new Page("So he found some friends, and everyone was happy.");
Page page3 = new Page("The end!");
book.addPage(page1);
book.addPage(page2);
book.addPage(page3);
book.save();
}
}
However it is not working as expected. It is trying to make mPageList its own column with this .schema:
CREATE TABLE BOOK ( ID INTEGER PRIMARY KEY AUTOINCREMENT , M_AUTHOR_NAME TEXT, M_BOOK_NAME TEXT, M_PAGE_LIST );
What I'd really like it to do is not treat the list as its own column but instead save the Pages to the PAGE table, with additional ids that reference this Book class (so what I am expecting is something like ID, BOOK_ID, M_TEXT). In short, persistence operations that cascade through nested child objects.
Can this be done in SugarORM?

No ORM database(SugarORm, DBFLow etc) supports List column. As you know sql don't have this datatype as well.
That's the reason why you are getting this error. If you ask me how you are saving list to ORM. I use Gson.
Declare Pagelist as string.
String Pagelist;
Before saving it to database convert it to Json string with the help Gson library.
Gson gson = new Gson();
String value = gson.toJson(your_page_list);
when retrieving from database convert the json string to List using Gson.
List<Page> page_list;
Type typeIndicatorForGson = new TypeToken<ArrayList<Page>>() {}.getType();
Gson myGson = new Gson();
page_list = myGson.fromJson(page_json_data_from_database, typeIndicatorForGson);

No List<Object> available on SugarORM. The way you can manage this is a little tricky. In few words, you can manage 1 to N relations, upside down. Take a look to the next example
Lets suppose a Team object which can have N Person objects. Normally you will use a List<Person> in your class Team in this way:
public class Team {
String teamName;
List<Person>
...
}
public class Person {
String name;
String rol;
...
}
Well, it is not possible on SugarORM. But you can add Team as a property in Person, so, any Person's instance should contain a reference to the Team object it belong.
public class Team extends SugarRecord<Team> {
String teamName;
...
}
public class Person extends SugarRecord<Person> {
String name;
String rol;
Team team;
...
}
Then you can get all the Person objects from Team with a method (in the Team class) like:
public class Team extends SugarRecord<Team> {
String teamName;
...
public List<Person> getPersons(){
return Person.find(Person.class, "id = ?", String.valueOf(this.getId()));
}
}
So, you can manage 1 to N relations, but you can't manage N to M relationships (Person belonging to more than one Team object).
IMO the way to manage this is using an Associative Entity in order to split N to M into two 1 to N relationships.
As you can see SugarORM is not allowing you to think just in terms of objects, but, any case you can save a lot of boiler plate coding.

Related

What to Store from my model objects in Room Database

I have 3 POJO classes. Recipe, Ingredient and Step.
I want to be able to browse my recipes when offline, so I decided to use Room.
Recipe.class
#Entity(tableName = "recipe")
public class Recipe implements Parcelable {
#PrimaryKey(autoGenerate = false)
#SerializedName("id")
public int recipeId;
#ColumnInfo(name = "recipe_name")
public String name;
#TypeConverters(Converters.class)
public List<Ingredient> ingredients = null;
#TypeConverters(Converters.class)
public List<Step> steps = null;
#ColumnInfo(name = "recipe_servings")
public int servings;
#Ignore
public String image;
public Recipe(int recipeId, String name, List<Ingredient> ingredients, List<Step> steps, int servings, String image) {
this.recipeId = recipeId;
this.name = name;
this.ingredients = ingredients;
this.steps = steps;
this.servings = servings;
this.image = image;
}
...
//getters and setters
...
Converters.class
public class Converters {
static Gson gson = new Gson();
#TypeConverter
public static List<Ingredient> stringToIngredientList(String data) {
if (data == null) {
return Collections.emptyList();
}
Type listType = new TypeToken<List<Ingredient>>() {}.getType();
return gson.fromJson(data, listType);
}
#TypeConverter
public static String ingredientListToString(List<Ingredient> ingredients) {
return gson.toJson(ingredients);
}
#TypeConverter
public static List<Step> stringToStepList(String data) {
if (data == null) {
return Collections.emptyList();
}
Type listType = new TypeToken<List<Step>>() {}.getType();
return gson.fromJson(data, listType);
}
#TypeConverter
public static String stepListToString(List<Step> steps) {
return gson.toJson(steps);
}
}
RecipeDatabase.class
#Database(entities = {Recipe.class}, version = 1)
abstract class RecipeDatabase extends RoomDatabase {
private static RecipeDatabase INSTANCE;
public abstract RecipeDao recipeDao();
public static RecipeDatabase getRecipeDatabase(Context context) {
if (INSTANCE == null) {
INSTANCE =
Room.databaseBuilder(context.getApplicationContext(), RecipeDatabase.class, "recipe-database")
// allow queries on the main thread.
// Don't do this on a real app! See PersistenceBasicSample for an example.
.allowMainThreadQueries()
.build();
}
return INSTANCE;
}
public static void destroyInstance() {
INSTANCE = null;
}
}
RecipeDao.class
#Dao
public interface RecipeDao {
#Query("SELECT * FROM recipe")
List<Recipe> getAll();
#Query("SELECT * FROM recipe where recipe_name LIKE :name")
Recipe findByName(String name);
#Query("SELECT COUNT(*) from recipe")
int countRecipes();
#Update
void update(Recipe... recipes);
#Insert
void insertAll(Recipe... recipes);
#Delete
void delete(Recipe recipe);
}
My question: After saving the List<Step> and List<Ingredient> as Strings using the Converters class, should I also save a database of each of my Step.class and Ingredient.class? Should I include the #Entity annotation for these classes too? Should I make a StepDatabase and an IngredientDatabase? Is that also needed to be able to access my Recipes when offline?
should I also save a database of each of my Step.class and Ingredient.class. Should I make a StepDatabase and an IngredientDatabase?
By all means no. You do not create database per entity, but instead you create a table per entity. In most apps one database is enough.
Traditionally, SQL databases (relational database by nature) should enforce normalization rules, especially in enterprise systems on servers. Shortly, normalization rules refer to series of refactoring to a relational database design in a manner where each entity is extracted to its own table with foreign keys in the parent table. You'd then have to use SQL SELECT with JOINs to get the data together.
With that said, it's quite common to break the normalization rules for mobile apps and keep the data model as simple as possible by storing the nested entities, in your case, ingredients, and store them as JSON strings or whatever makes sense and easy for you to handle in your code.
So to summarize, what you currently have appear to be really good. As long as it simplifies your code - go for it. As far as I know, Room does not support JOINs, but there's something pretty close.
There's a good article on Medium that discusses advanced subjects with Room.
Yes, In my opinion it's a good practice to declare a model for each table first and Yes, Every model you should include #Entity annotation telling hibernate that what you are mapping is a table from your db.

Hibernate One-To-Many Mapping, printing mapped lists

I have used One-to-Many Mapping in my project. I have stored a list of clicks for every user.
But when I retrieve the list by calling getClicks() methodm Hibernate returns list in different format.
Something like this.
"[com.zednx.tech.persistence.Click#29df9a77]"
So I tried Reading Every value from the list and assign to a new List.
List<Click> clicks=new ArrayList<Click>();
for(Click c: e.getClicks()){
Click temp = new Click();
temp.setAff_source(c.getAff_source());
temp.setCb_to_award(c.getCb_to_award());
temp.setCb_type(c.getCb_type());
clicks.add(temp);
}
But when i print the items of new List it stills prints the same way.
I need to build a JSON from the resulting String of this list.
So if the list is returned in format, it wont help me.
I couldn't find anything regarding this except How to pretty print Hibernate query results?
I tried Arrays.ToString(Object o). But it doesn't work.
GSON builder part-
Gson gson = new GsonBuilder()
.registerTypeAdapter(Click.class, new MyTypeAdapter<Click>())
.create();
List<Click> clicks=new ArrayList<Click>();
for(Click c: e.getClicks()){
Click temp = new Click();
temp.setAff_source(c.getAff_source());
temp.setCb_to_award(c.getCb_to_award());
temp.setCb_type(c.getCb_type());
temp.setCom_to_recieve(c.getCom_to_recieve());
temp.setStore_name(c.getStore_name());
temp.setT_date(c.getT_date());
temp.setT_status(c.getT_status());
temp.setT_ticket(c.getT_ticket());
temp.setUid(c.getUid());
System.out.println(c.toString());
clicks.add(temp);
}
String json = gson.toJson(clicks, Click.class);
Click.java
#Entity
#Table(name="click")
public class Click {
#Id
#Column(name="t_ticket")
private String t_ticket;
#Column(name="uid",nullable=false)
private long uid;
public long getUid() {
return uid;
}
public void setUid(long uid) {
this.uid = uid;
}
#ManyToOne
#JoinColumn(name="uid",
insertable=false, updatable=false,
nullable=false)
private Earning earning;
#Column(name="store_name")
private String store_name;
#Column(name="t_status")
private String t_status;
#Column(name="aff_source")
private String aff_source;
#Column(name="com_to_recieve")
private float com_to_recieve;
#Column(name="t_date")
private Date t_date;
#Column(name="cb_to_award")
private float cb_to_award;
#Column(name="cb_type")
private String cb_type;
public String getT_ticket() {
return t_ticket;
}
public void setT_ticket(String t_ticket) {
this.t_ticket = t_ticket;
}
public Earning getEarning() {
return earning;
}
public void setEarning(Earning earning) {
this.earning = earning;
}
public String getStore_name() {
return store_name;
}
public void setStore_name(String store_name) {
this.store_name = store_name;
}
public String getT_status() {
return t_status;
}
public void setT_status(String t_status) {
this.t_status = t_status;
}
public String getAff_source() {
return aff_source;
}
public void setAff_source(String aff_source) {
this.aff_source = aff_source;
}
public float getCom_to_recieve() {
return com_to_recieve;
}
public void setCom_to_recieve(float com_to_recieve) {
this.com_to_recieve = com_to_recieve;
}
public Date getT_date() {
return t_date;
}
public void setT_date(Date t_date) {
this.t_date = t_date;
}
public float getCb_to_award() {
return cb_to_award;
}
public void setCb_to_award(float cb_to_award) {
this.cb_to_award = cb_to_award;
}
public String getCb_type() {
return cb_type;
}
public void setCb_type(String cb_type) {
this.cb_type = cb_type;
}
Any Help is appreciated.
You need to implement a toString method, as your current Click class likely doesn't have one, so it just prints as the name of the class and instance identifier.
Okay, I could solve my problem finally.
I made another POJO without any annotations and Mapped the List items to that POJO class.
I think the problem was with Annotation of mapping on another class which I had in original POJO.
Also getString() method only helps in changing format of identifier. So basically it has nothing to do with JSON building unless you format getString() in form of JSON.
Hope it helps. If anyone wants new temp POJO I made I can post it if requested.
Thanks.

Java: calling an Objectmethod with a variable

I would like to call up an Method for a library like:
%nameofbookvar%.islend();
but the user should assign the nameofbookvar to the book he wants. So I want java to know that nameofbookvar is a variable and not the name of the object.
How can I do this?
Thanks!!:)
That's something really weird to do on Java because you normally won't create variables like %nameofbookvar% without... defining its name, correct?
For a simple name/value pair, you can use a HashMap:
Map<String, Book> library = new HashMap<String, Book>();
library.put("Frankenstein", new Book("Frankenstein"));
// ....
Book userSearchedBook = library.get("Frankenstein");
userSearchedBook.isLend();
Of course, you'll need a Book class:
public class Book {
private final String title;
private boolean lend;
public Book(String title) {
this.title = title;
}
//...
public boolean isLend() {
return isLend;
}
}
You can store all the books in the library in a Map where the key is the name of the book the user wants and the mapped value is the Book object that belongs to the book in question. For different types of Maps refer to the Implementing Classes in the Map specification and determine which works best for your needs.
public class Library {
private HashMap<String, Book> collection;
public Library() {
collection = new HashMap<>();
}
public Book getBook(String name) {
return collection.get(name);
}
public void addBook(String name, Book book) {
collection.put(name, book);
}
}
public class Book {
private boolean isLend;
public boolean isLend() {
return isLend;
}
}
You can then call the following on a Library object
Library library = new Library(); // Don't forget to add books
library.getBook(%nameofbookvar%).isLend();
Book variable = BookStore.get(theBookTheUserWants);
variable.isLend();

How to implement usage dependency, Enumeration data type in Java? Confusion to implement Aggregation, Composition

I have to implement the following class diagram to the java code. This diagram is very complicated and some parts creates confusion. This question definitely going to help me a lot as well as any reader because it contains several important aspects of UML diagram.
class Book{
String isbn;
String publisher;
String publishDate;
int pages;
}
class BookItem extends Book{
String barcode;
boolean isReferenceOnly;
}
class Author{
String name;
String biography;
Collection<Book> book;
}
class Account{
String number;
List<History> history;
String openDate;
AccountState state;
public Account(AccountState state){
this.state = state;
}
}
enum AccountState{
Active,
Frozen,
Closed
}
class Catalog implements Search, Manage{
List<BookItem> bookItem;
/* Implement the methods of Manage interface */
void add(BookItem item){ }
void remove(BookItem item){ }
/* Implement the methods of Search interface */
int search(BookItem item){ }
}
class Account{
String number;
List<History> history;
Student student = new Student();
void setStudent(Student student){
this.student = student;
}
}
interface Search{
int search(BookItem item);
}
interface Manage{
void add(BookItem item);
void remove(BookItem item);
}
class Student{
String name;
String address;
Search searchBook = new Catalog();
}
class Librarian{
String name;
String address;
String position;
Search searchBook = new Catalog();
Manage manage = new Catalog();
Account account = new Account();
void setAccount(Account account){
this.account = account;
}
class Library{
String name;
String Address;
List<BookItem> bookItem = new ArrayList<BookItem>();
Catalog catalog = new catalog();
List<Account> accounts = new ArrayList<Account>();
Library(Catalog catalog){
this.catalog = catalog;
}
void setBookItem(List<BookItem> bookItem){
this.bookItem = bookItem;
}
void setAccounts(List<Account> accounts){
this.accounts = accounts;
}
}
I implemented in the following way but confusion arise in various cases:
How to implement Class Student use the interface Search.
How to implement Class Librarian use the interfaces Search and Manage.
Why we are not use association instead of usage dependency.
How to implement that Enumeration data type in this case with usage dependency [I have just considered AccountState as a class, i the it is a wrong implementation].
How to use AccountState in the Account [I have just created a object of AccountState].
After read many blogs still unable to implement Aggregation and Composition confidently. Note: In this diagram 3 Aggregations and 1 Composition Exist. Those are:
(a) Library consists of many Account. {Aggregation}
(b) Many Book Item is the part of Library. {Aggregation}
(c) An Account is the part of a Student. {Aggregation}
(d) Library must have a Catalog. {Composition}
Please give your valuable advice so i can learn it well. Thanking you.
Since this question is homework for learning purposes, I will post only examples of how to implement the things you need to review and won't give a direct answer about how to apply them to your current design.
Enumeration in Java is implemented by using enum.
enum WeekDays {
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY,
SUNDAY;
}
Aggregation/Composition means to have a field of the other class. If it's a weak association (aggregation), it should be initialized by the setter or another method. If it's a strong association, it should be initialized in the class constructor since it is needed for the class to live/work.
class WeakAssociation { }
class StrongAssociation { }
class NeedWeekAndStrongAssociation {
private WeakAssociation weakAssociation;
private StrongAssociation strongAssociation;
public NeedWeekAndStrongAssociation(StrongAssociation strongAssociation) {
this.strongAssociation = strongAssociation;
}
public void setWeakAssociation(WeakAssociation weakAssociation) {
this.weakAssociation = weakAssociation;
}
}
Usage dependency means that the class/interface will use the other class/interface within one or more of its methods:
class WantToBeUsed {
public void methodToBeUsed(String data) {
//fancy implementation
}
}
class CannotDoThisAlone {
public void cannotDoItAlone(String data) {
WantToBeUsed wantToBeUsed = new WantToBeUsed();
wantToBeUsed.methodToBeUsed(data);
}
}

Update subclass given superclass

Problem Description
I have an abstract Paper class that contains common properties of all papers and one or more child classes of paper that add additional information for that type of paper. I then have a HashMap<String, Paper> to store multiple papers.
My application allows the user to update a paper by providing a the pid and then supplying the attributes and values to update. The issue I am having is how do I update the properties on the sub classes when all I have is the super class.
What is the best way/practice to handle this situation?
Class Structure
public abstract class Paper {
String pid;
String title;
String author;
}
public class Publication extends Paper {
int pages;
}
public class PHDThesis extends Paper {
String supervisor;
}
My Current Attempt
This is what I currently have** and it works by using instance of; but I feel there should be a better way to do this.
import java.util.*;
public class App {
public static abstract class Paper {
private String title;
private String author;
public Paper(String title, String author) {
this.title = title;
this.author = author;
}
public void update(String title, String author) {
this.title = title;
this.author = author;
}
}
public static class Publication extends Paper {
private int pages;
public Publication(int pages, String title, String author) {
super(title, author);
this.pages = pages;
}
public void update(String title, String author, int pages) {
super.update(title, author);
this.pages = pages;
}
}
public static class PHDThesis extends Paper {
private String supervisor;
public PHDThesis(String supervisor, String title, String author) {
super(title, author);
this.supervisor = supervisor;
}
public void update(String title, String author, String supervisor) {
super.update(title, author);
this.supervisor = supervisor;
}
}
public static void main(String[] args) {
HashMap<String, Paper> papers = new HashMap<String, Paper>();
papers.put("P001", new PHDThesis("My Super", "My PHD Title", "My Author"));
papers.put("P002", new Publication(22, "My Pub Title", "My Author"));
Paper p = papers.get("P001");
if (p instanceof PHDThesis) {
((PHDThesis)p).update("New Title", "New author", "New Super");
} else if (p instanceof Publication) {
((Publication)p).update("New Title", "New author", 33);
}
}
}
** reduced test code, actual code is much more complex and better laid out.
You can create an object called UpdateBundle with getters for each attribute.
Then the Paper class will have a method update(UpdateBundle) which each child will implement differently.
All you have to do is call that method for each child and they will know how to handle it.
On a separate note, i don't see why the paper class is abstract. You seem to have no abstract methods in it.
public abstract class Paper {
String pid;
String title;
String author;
public void update(PaperUpdateBundle bundle)
{
pid = bundle.getPID();
title = budnle.getTitle();
author = bundle.getAuthor();
}
}
public class Publication extends Paper {
int pages;
public void update(PaperUpdateBundle bundle)
{
super.update(bundle);
pages = bundle.getPages();
}
}
public class PHDThesis {
String supervisor;
public void update(PaperUpdateBundle bundle)
{
super.update(bundle);
supervisor = bundle.getSupervisor();
}
}
public interface PaperUpdateBundle
{
String getPID();
String getTitle();
String getAuthor();
int getPages();
String getSupervisor();
}
Create a method
public void update( Map<String, Object> parameters );
to all Papers and pull the relevant properties from it in the Paper implementations.
In Publication it might look like:
public void update( Map<String, Object> parameters ) {
super.update( parameters );
this.pages = parameters.get( "pages" );
}
The problem with the accepted answer is that it requires you to update all of the properties manually. If the list of properties changes, you have to change the update() method or things will get out of sync. In my experience this happens frequently. And then you've got to spend a lot of time trying to track down the bug.
A different way (I won't call it a "better" way) is to use reflection or some third party library to copy the fields. There are some tradeoffs, though. The advantage is that your code requires a lot less work and will (probably) have fewer bugs. The downside is that your code will be slower, less flexible, and lack compile-time checks.
I have sometimes used Jackson's ObjectMapper.convertValue() to do this. You can find other ways to do it here: Copy all values from fields in one class to another through reflection.

Categories

Resources