/** * Save the given object to the database. Creates a new object if model id property has not been * set * * @return true on success. */ public boolean persist(TYPE item) { if (item.getId() == AbstractModel.NO_ID) { return createNew(item); } else { ContentValues values = item.getSetValues(); if (values.size() == 0) // nothing changed return true; return saveExisting(item); } }
/** * Saves the given item. Will not create a new item! * * @param database * @param table table name * @param item item model * @return returns true on success. */ public boolean saveExisting(TYPE item) { ContentValues values = item.getSetValues(); if (values == null || values.size() == 0) // nothing changed return true; boolean result = database.update( table.name, values, AbstractModel.ID_PROPERTY.eq(item.getId()).toString(), null) > 0; if (result) { onModelUpdated(item); item.markSaved(); } return result; }
/** * Create the object of {@link AbstractIdentifiableEntity} type * * @param object the object to create * @return <code>TYPE</code> that was created */ public default Optional<TYPE> create(final TYPE object) { this.getDAO().create(object); return this.getDAO().getById(object.getId()); }