001: package com.xoetrope.data.pojo;
002:
003: import java.lang.reflect.Method;
004: import net.xoetrope.optional.data.pojo.XPojoContext;
005: import net.xoetrope.optional.data.pojo.XPersistenceController;
006: import net.xoetrope.xui.data.XModel;
007:
008: public class XPersistentPojoModelEx extends XPojoModelEx {
009: public static final int NUM_FIXED_ATTRIBUTES = 1;
010: public static final int TX_REQUIRED_ATTRIBUTE = 0;
011:
012: public XPersistentPojoModelEx(XModel pm, Method gtr, Method str,
013: String pn, XPersistentPojoDataSourceEx ds) {
014: super (pm, gtr, str, pn, ds);
015: }
016:
017: public XPersistentPojoModelEx(XModel pm, Object p,
018: XPersistentPojoDataSourceEx ds) {
019: super (pm, p, ds);
020: }
021:
022: public XPersistentPojoModelEx(XModel pm, Class pc, String pn,
023: XPersistentPojoDataSourceEx ds) {
024: super (pm, pc, pn, ds);
025: }
026:
027: public int getNumAttributes() {
028: return (NUM_FIXED_ATTRIBUTES + super .getNumAttributes());
029: }
030:
031: public String getAttribName(int i) {
032: int na = super .getNumAttributes();
033: if (i == na + TX_REQUIRED_ATTRIBUTE)
034: return "transaction required";
035: else
036: return super .getAttribName(i);
037: }
038:
039: /**
040: * Updates the value of the underlying property
041: */
042: public void obtainPropertyValue() {
043: Object pc = dataSource.getPojoContext();
044: if (pc instanceof XPersistenceController) {
045: XPersistenceController controller = (XPersistenceController) dataSource
046: .getPojoContext();
047: controller.beginTransaction();
048: }
049: super .obtainPropertyValue();
050: }
051:
052: protected boolean requiresTransaction(String propertyName) {
053: boolean txr = false;
054: if (adapter != null)
055: txr = ((XPersistentPojoAdapterEx) adapter)
056: .requiresTransaction(propertyName);
057: return txr;
058: }
059:
060: protected void setTransaction(String propertyName, boolean state) {
061: if (adapter == null)
062: return;
063: if (state)
064: ((XPersistentPojoAdapterEx) adapter)
065: .setTransaction(propertyName);
066: else
067: ((XPersistentPojoAdapterEx) adapter)
068: .unsetTransaction(propertyName);
069: }
070:
071: public Object getAttribValue(int i) {
072: int na = super .getNumAttributes();
073: if (i == na + TX_REQUIRED_ATTRIBUTE) {
074: boolean txr = false;
075: if (parentModel instanceof XPersistentPojoModelEx)
076: txr = ((XPersistentPojoModelEx) parentModel)
077: .requiresTransaction(propertyName);
078: return (txr ? "true" : "false");
079: } else {
080: return super .getAttribValue(i);
081: }
082: }
083:
084: /**
085: * Returns the value of the attribute at the specified index.
086: * The value is converted to a String object.
087: * @param i the index of the attribute
088: * @return String value of the attribute
089: */
090: public String getAttribValueAsString(int i) {
091: return (String) getAttribValue(i);
092: }
093:
094: public void setAttribValue(int i, Object value) {
095: int na = super .getNumAttributes();
096: if (i == na + TX_REQUIRED_ATTRIBUTE) {
097: if (!(parentModel instanceof XPersistentPojoModelEx))
098: return;
099: boolean txr = false;
100: if ("true".equals(value))
101: txr = true;
102: else if ("false".equals(value))
103: txr = false;
104: else
105: return;
106: XPersistentPojoModelEx pm = (XPersistentPojoModelEx) parentModel;
107: pm.setTransaction(propertyName, txr);
108: pm.setDirty(true);
109: } else {
110: super .setAttribValue(i, value);
111: }
112: }
113:
114: public boolean getAttribRuntime(int i) {
115: int na = super .getNumAttributes();
116: if (i == na + TX_REQUIRED_ATTRIBUTE)
117: return false;
118: else
119: return super .getAttribRuntime(i);
120: }
121:
122: public int getAttribType(int i) {
123: int na = super .getNumAttributes();
124: if (i == na + TX_REQUIRED_ATTRIBUTE)
125: return ATTRIB_COMBO;
126: else
127: return super .getAttribType(i);
128: }
129:
130: public String[] getAttribAvailableValues(int i) {
131: int na = super .getNumAttributes();
132: if (i == na + TX_REQUIRED_ATTRIBUTE)
133: return (new String[] { "true", "false" });
134: else
135: return super .getAttribAvailableValues(i);
136: }
137:
138: public boolean isAttribEditable(int i) {
139: int na = super .getNumAttributes();
140: if (i == na + TX_REQUIRED_ATTRIBUTE)
141: return ((parentModel instanceof XPersistentPojoModelEx) && (getAttribType(i) != ATTRIB_NEDITABLE));
142: else
143: return super.isAttribEditable(i);
144: }
145:
146: }
|