package divestoclimb.checklist.data;
import divestoclimb.checklist.storage.DbAdapter;
//Superclass for all items in a checklist or template.
public abstract class Item {
protected long mID; // The database index of the record corresponding to
// this item
protected int mContainerType;
protected long mContainerId;
protected int mOrder; // The order for this item as stored in the database
protected boolean mDirty; // Tracks whether or not something in this item has changed
protected String mText; // The text of this item
// Getters and setters
public int getContainerType() { return mContainerType; }
public long getContainerID() { return mContainerId; }
public long getID() { return mID; }
protected void setID(long id) { mID = id; }
public int getOrder() { return mOrder; }
public void setOrder(int o) { mOrder = o; mDirty = true; }
public String getText() { return mText; }
public void setText(String t) { mText = t; mDirty = true; }
// The generic constructor used for new items
//public Item(Template t) {
public Item(int container_type, long container_id) {
//setTemplate(t);
mContainerType = container_type;
mContainerId = container_id;
mID = -1;
mOrder = -1;
// The record can't be updated until some text is set
mDirty = false;
}
// The database constructor. Subclasses should call this to set global fields.
//public Item(Template t, long id, int order, String text) {
public Item(int container_type, long container_id, long id, int order, String text) {
//setTemplate(t);
mContainerType = container_type;
mContainerId = container_id;
setID(id);
setText(text);
mOrder=order;
mDirty=false;
}
boolean equals(Item i2) {
return mContainerType == i2.getContainerType()
&& mContainerId == i2.getContainerID()
&& mID == i2.getID();
}
/**
* Save this item.
*
* @param d The DatabaseAdapter to use to save the object. In the future this will accept a more generic adapter for storage to XML.
*/
public boolean save(DbAdapter d) {
if(! mDirty) { return false; }
boolean success;
if(mID == -1) {
if(mContainerType == DbAdapter.ITEM_CONTAINER_CHECKLIST) {
mID = d.createChecklistItem(this);
success = mID != -1;
} else if(mContainerType == DbAdapter.ITEM_CONTAINER_TEMPLATE) {
mID = d.createTemplateItem(this);
success = mID != -1;
} else {
success = false;
}
} else {
if(mContainerType == DbAdapter.ITEM_CONTAINER_CHECKLIST) {
success = d.updateChecklistItem(this);
} else if(mContainerType == DbAdapter.ITEM_CONTAINER_TEMPLATE) {
success = d.updateTemplateItem(this);
} else {
success = false;
}
}
mDirty = ! success;
return success;
}
}
|