001: package net.xoetrope.optional.service;
002:
003: import java.util.Date;
004:
005: import net.xoetrope.xui.data.XModel;
006:
007: /**
008: * Models a service as a model node. Getting the node value causes the service
009: * to be invoked. The node attributes are used as the arguments for the service
010: * call.
011: * <p>Copyright: Copyright (c) Xoetrope Ltd. 2002-2003</p>
012: * $Revision: 1.1 $
013: */
014: public class XServiceModelNode extends XModel {
015: protected boolean dirty;
016: protected long expires;
017:
018: protected String serviceId;
019: protected String fieldNames[];
020: protected Object fieldValues[];
021:
022: protected ServiceProxy callProxy;
023:
024: public XServiceModelNode() {
025: dirty = true;
026: }
027:
028: public void setupService(String methodName,
029: ServiceProxy routeService, String[] argNames) {
030: serviceId = methodName;
031: callProxy = routeService;
032: fieldNames = argNames;
033: if (argNames != null)
034: fieldValues = new String[argNames.length];
035: dirty = true;
036: }
037:
038: /**
039: * Get the service proxy that implements this service call
040: * @return the service proxy
041: */
042: public ServiceProxy getServiceProxy() {
043: return callProxy;
044: }
045:
046: /**
047: * Get the service proxy that implements this service call
048: * @return the service proxy
049: */
050: public ServiceProxy getServiceProxy(String reqdClass) {
051: ServiceProxy target = callProxy;
052: while (target != null) {
053: if (reqdClass.compareTo(target.getClass().getName()) != 0)
054: target = target.nextProxy;
055: else
056: return target;
057: }
058: return callProxy;
059: }
060:
061: public int hashCode() {
062: return serviceId.hashCode();
063: }
064:
065: /**
066: * Syncs the model with the service response is the request has been changed
067: * or if the node has timed out.
068: */
069: protected void sync() {
070: if (dirty || hasExpired()) {
071: get();
072: dirty = false;
073: }
074: }
075:
076: public Object get() {
077: try {
078: if (serviceId == null) {
079: return callProxy.call();
080: }
081: return callProxy.call(serviceId, fieldNames, fieldValues);
082: } catch (ServiceProxyException ex) {
083: ex.printStackTrace();
084: return null;
085: }
086: }
087:
088: public Object append(String id) {
089: return null;
090: }
091:
092: public XModel get(int i) {
093: /**@todo Implement this net.xoetrope.xui.data.XModel abstract method*/
094: throw new java.lang.UnsupportedOperationException(
095: "Method get() not yet implemented.");
096: }
097:
098: public void append(XModel newObject) {
099: dirty = true;
100: /**@todo Implement this net.xoetrope.xui.data.XModel abstract method*/
101: }
102:
103: public void set(Object s) {
104: dirty = true;
105: /**@todo Implement this net.xoetrope.xui.data.XModel abstract method*/
106:
107: fireModelUpdated();
108: }
109:
110: public void set(String attribName, Object newObject) {
111: dirty = true;
112: /**@todo Implement this net.xoetrope.xui.data.XModel abstract method*/
113: }
114:
115: public String getId() {
116: return serviceId;
117: }
118:
119: /**
120: * Get the name of an argument
121: * @param i
122: * @return the argument name
123: */
124: public String getAttribName(int i) {
125: return fieldNames[i];
126: }
127:
128: /**
129: * Look up an argument name
130: * @param argName the argument name
131: * @return the argument index or -1 if not found
132: */
133: public int getAttribute(String argName) {
134: int numFields = fieldNames.length;
135: for (int i = 0; i < numFields; i++) {
136: if (fieldNames[i].compareTo(argName) == 0)
137: return i;
138: }
139:
140: return -1;
141: }
142:
143: /**
144: * Set the value of an argument
145: * @param i the argument index
146: * @param value the new argument value
147: */
148: public void setAttribValue(int i, Object value) {
149: dirty = true;
150: fieldValues[i] = value.toString();
151: }
152:
153: /**
154: * Set the value of an argument
155: * @param i the argument index
156: * @param attribName the name of teh attribute
157: * @param value the new argument value
158: */
159: public void setAttribValue(int i, String attribName, Object value) {
160: dirty = true;
161: fieldValues[i] = value.toString();
162: }
163:
164: public double getValueAsDouble(String elementName) {
165: /**@todo Implement this net.xoetrope.xui.data.XModel abstract method*/
166: throw new java.lang.UnsupportedOperationException(
167: "Method getValueAsDouble() not yet implemented.");
168: }
169:
170: public int getAttribValueAsInt(int i) {
171: /**@todo Implement this net.xoetrope.xui.data.XModel abstract method*/
172: throw new java.lang.UnsupportedOperationException(
173: "Method getAttribValueAsInt() not yet implemented.");
174: }
175:
176: public int getValueAsInt(String elementName) {
177: /**@todo Implement this net.xoetrope.xui.data.XModel abstract method*/
178: throw new java.lang.UnsupportedOperationException(
179: "Method getValueAsInt() not yet implemented.");
180: }
181:
182: public String getAttribValueAsString(int i) {
183: /**@todo Implement this net.xoetrope.xui.data.XModel abstract method*/
184: throw new java.lang.UnsupportedOperationException(
185: "Method getAttribValue() not yet implemented.");
186: }
187:
188: /**
189: * Gets the value attribute of the specified node as a string.
190: * @param elementName
191: * @return the value as a string
192: */
193: public String getValueAsString(String elementName) {
194: /**@todo Implement this net.xoetrope.xui.data.XModel abstract method*/
195: throw new java.lang.UnsupportedOperationException(
196: "Method getValueAsString() not yet implemented.");
197: }
198:
199: public Object getAttribValue(int i) {
200: return fieldValues[i];
201: }
202:
203: public double getAttribValueAsDouble(int i) {
204: /**@todo Implement this net.xoetrope.xui.data.XModel abstract method*/
205: throw new java.lang.UnsupportedOperationException(
206: "Method getAttribValueAsDouble() not yet implemented.");
207: }
208:
209: public void remove(XModel child) {
210: /**@todo Implement this net.xoetrope.xui.data.XModel abstract method*/
211: throw new java.lang.UnsupportedOperationException(
212: "Method remove() not yet implemented.");
213: }
214:
215: /**
216: * Checks to see if the response has timed out or expired
217: * @return true if the response is out-of-date
218: */
219: boolean hasExpired() {
220: if ((expires > 0) && (new Date().getTime() > expires))
221: return true;
222:
223: return false;
224: }
225:
226: public void setNumAttributes(int num) {
227: num = Math.max(num, 2);
228: Object[] temp = new Object[num];
229: String[] tempNames = new String[num];
230: if (fieldNames != null) {
231: if (num != fieldNames.length) {
232: int numAttributes = Math.min(num, fieldNames.length);
233: for (int i = 0; i < numAttributes; i++) {
234: tempNames[i] = fieldNames[i];
235: temp[i] = fieldValues[i];
236: }
237: } else
238: return;
239: }
240: fieldValues = temp;
241: fieldNames = tempNames;
242: }
243: }
|