001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.admin.dso;
005:
006: import com.tc.admin.AdminClient;
007: import com.tc.admin.ConnectionContext;
008: import com.tc.object.LiteralValues;
009: import com.tc.object.ObjectID;
010: import com.tc.object.dna.impl.EnumInstance;
011: import com.tc.objectserver.mgmt.ManagedObjectFacade;
012:
013: import java.beans.PropertyChangeEvent;
014:
015: public class DSOField extends DSOObject {
016: private String m_name;
017: private boolean m_isPrimitive;
018: private boolean m_isMap;
019: private boolean m_isList;
020: private boolean m_isSet;
021: private boolean m_isArray;
022: private String m_type;
023: private Object m_value;
024: private String[] m_fieldNames;
025: private DSOObject[] m_fields;
026: private String m_label;
027: private boolean m_isCycle;
028: private DSOObject m_cycleRoot;
029: private boolean m_isLiteral;
030:
031: private final static LiteralValues m_literals = new LiteralValues();
032:
033: public DSOField(ConnectionContext cc, String name,
034: boolean isPrimitive, String type, Object value,
035: DSOObject parent) {
036: super (cc, parent);
037:
038: if (type != null
039: && (type.equals("java.util.Date") || type
040: .equals("java.sql.Timestamp"))) {
041: isPrimitive = true;
042: ManagedObjectFacade mof = (ManagedObjectFacade) value;
043: value = mof.getFieldValue("date");
044: }
045:
046: m_name = name;
047: m_isPrimitive = isPrimitive;
048: m_type = type;
049: m_value = value;
050: m_isLiteral = isLiteral();
051:
052: initFields();
053: updateLabel();
054: }
055:
056: private boolean isLiteral() {
057: if (m_value != null) {
058: Class c = m_value.getClass();
059: return !c.isArray() && m_literals.isLiteral(c.getName());
060: }
061: return false;
062: }
063:
064: public Object getFacade() {
065: return m_value instanceof ManagedObjectFacade ? (ManagedObjectFacade) m_value
066: : null;
067: }
068:
069: protected void updateLabel() {
070: String prefix = m_name;
071:
072: if (m_type != null) {
073: prefix += " (" + m_type + ")";
074: }
075:
076: m_label = prefix;
077:
078: if (m_isLiteral) {
079: if (m_value instanceof EnumInstance) {
080: EnumInstance enumInstance = (EnumInstance) m_value;
081: String enumType = enumInstance.getClassInstance()
082: .getName().asString();
083: m_label = getName() + " (" + enumType + ")" + "="
084: + enumInstance;
085: } else {
086: m_label = prefix + "=" + m_value;
087: }
088: } else if (isCollection()) {
089: ManagedObjectFacade mof = (ManagedObjectFacade) m_value;
090: m_label += " [" + mof.getFacadeSize() + "/"
091: + mof.getTrueObjectSize() + "]";
092: } else if (isPrimitive() || m_value == null
093: || m_value instanceof String) {
094: m_label = prefix + "=" + m_value;
095: }
096:
097: }
098:
099: public void initFields() {
100: if (!m_isPrimitive) {
101: if (m_value != null
102: && m_value instanceof ManagedObjectFacade) {
103: ManagedObjectFacade mof = (ManagedObjectFacade) m_value;
104:
105: m_isMap = mof.isMap();
106: m_isList = mof.isList();
107: m_isSet = mof.isSet();
108: m_isArray = mof.isArray();
109: m_isCycle = isCycle(mof);
110: }
111:
112: m_fields = new DSOObject[getFieldCount()];
113: }
114: }
115:
116: public String getName() {
117: return m_name;
118: }
119:
120: public boolean isPrimitive() {
121: return m_isPrimitive;
122: }
123:
124: public boolean isArray() {
125: return m_isArray;
126: }
127:
128: public boolean isCollection() {
129: return isMap() || isList() || isSet();
130: }
131:
132: public boolean isMap() {
133: return m_isMap;
134: }
135:
136: public boolean isList() {
137: return m_isList;
138: }
139:
140: public boolean isCycle() {
141: return m_isCycle;
142: }
143:
144: public DSOObject getCycleRoot() {
145: return m_cycleRoot;
146: }
147:
148: public boolean isSet() {
149: return m_isSet;
150: }
151:
152: public String getType() {
153: return m_type;
154: }
155:
156: public Object getValue() {
157: return m_value;
158: }
159:
160: public String[] getFieldNames() {
161: if (m_fieldNames == null) {
162: if (m_value != null
163: && m_value instanceof ManagedObjectFacade) {
164: ManagedObjectFacade facade = (ManagedObjectFacade) m_value;
165: m_fieldNames = RootsHelper.getHelper().trimFields(
166: facade.getFields());
167: } else {
168: m_fieldNames = new String[] {};
169: }
170: }
171:
172: return m_fieldNames;
173: }
174:
175: public String getFieldName(int index) {
176: String[] names = getFieldNames();
177: return names != null ? names[index] : null;
178: }
179:
180: public DSOObject getField(int index) {
181: DSOObject result = null;
182:
183: if (m_fields == null) {
184: m_fields = new DSOObject[getFieldCount()];
185: }
186:
187: if (m_fields[index] == null) {
188: ManagedObjectFacade mof = (ManagedObjectFacade) getValue();
189: String field = getFieldName(index);
190:
191: try {
192: result = createField(field, mof.getFieldValue(field),
193: mof.getFieldType(field));
194: } catch (Throwable t) {
195: //t.printStackTrace();
196: }
197: }
198:
199: m_fields[index] = result;
200:
201: return result;
202: }
203:
204: public int getFieldCount() {
205: return m_isCycle ? 0 : getFieldNames().length;
206: }
207:
208: public String toString() {
209: return m_label;
210: }
211:
212: public boolean isValid() {
213: boolean result = true;
214:
215: if (m_value != null && m_value instanceof ManagedObjectFacade) {
216: ManagedObjectFacade mof = (ManagedObjectFacade) m_value;
217: ObjectID id = mof.getObjectId();
218:
219: try {
220: m_value = DSOHelper.getHelper().lookupFacade(m_cc, id,
221: m_batchSize);
222: result = (m_value != null);
223: } catch (Exception e) {
224: result = false;
225: }
226: }
227:
228: return result;
229: }
230:
231: protected void updateFacade() throws Exception {
232: if (m_value != null && m_value instanceof ManagedObjectFacade) {
233: ManagedObjectFacade mof = (ManagedObjectFacade) m_value;
234: ObjectID id = mof.getObjectId();
235:
236: m_value = DSOHelper.getHelper().lookupFacade(m_cc, id,
237: m_batchSize);
238: }
239: }
240:
241: public boolean isCycle(ManagedObjectFacade mof) {
242: ObjectID oid = mof.getObjectId();
243: DSOObject parent = getParent();
244:
245: while (parent != null) {
246: Object facade = parent.getFacade();
247:
248: if (facade instanceof ManagedObjectFacade) {
249: ObjectID parentOID = ((ManagedObjectFacade) facade)
250: .getObjectId();
251:
252: if (oid.equals(parentOID)) {
253: m_cycleRoot = parent;
254: return true;
255: }
256: }
257: parent = parent.getParent();
258: }
259:
260: return false;
261: }
262:
263: public void refresh() {
264: try {
265: updateFacade();
266: updateLabel();
267: m_fieldNames = null;
268: m_fields = null;
269:
270: m_changeHelper.firePropertyChange(new PropertyChangeEvent(
271: this , null, null, null));
272: } catch (Exception e) {
273: AdminClient.getContext().log(e);
274: }
275: }
276:
277: public void accept(DSOObjectVisitor visitor) {
278: visitor.visitDSOField(this);
279: }
280: }
|