예제 #1
0
  /**
   * 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);
    }
  }
예제 #2
0
 /**
  * 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;
 }
예제 #3
0
 /**
  * Update all matching a clause to have the values set on template object.
  *
  * <p>Example (updates "joe" => "bob" in metadata value1): {code} Metadata item = new Metadata();
  * item.setValue(Metadata.VALUE1, "bob"); update(item, Metadata.VALUE1.eq("joe")); {code}
  *
  * @param where sql criteria
  * @param template set fields on this object in order to set them in the db.
  * @return # of updated items
  */
 public int update(Criterion where, TYPE template) {
   return database.update(table.name, template.getSetValues(), where.toString(), null);
 }