001: package com.jat.business;
002:
003: import java.io.IOException;
004: import java.util.Enumeration;
005: import javax.xml.parsers.ParserConfigurationException;
006:
007: import org.w3c.dom.Document;
008: import org.xml.sax.SAXException;
009: import com.jat.core.config.Config;
010: import com.jat.core.log.LogManager;
011: import com.jat.integration.DataSourceFactory;
012: import com.jat.integration.IntegrationException;
013: import com.jat.util.Orderable;
014: import com.jat.util.StringUtil;
015: import com.jat.util.xml.XMLUtil;
016: import com.jat.util.xml.XMLable;
017: import java.io.Serializable;
018:
019: /**
020: * <p>Title: JAT</p>
021: * <p>Description: this class represent the JAT main Business Object model of your application.
022: * <br/>It is composed of a set of properties (see {@link BusinessObjectProperties})
023: * that you can get with {@link getField}, {@link getFieldAsObject} and {@link getProperties} methods
024: * and set with the {@link putField} or {@link setProperties} methods.
025: * <br/>A Business Object instance is also sortable from a {@link com.jat.business.BusinessObjectList}
026: * and is possible to get its XML format ({@link toXML} and {@link getXmlDocument} methods).
027: * </p>
028: * <p>
029: * If you want to build your own Business Object, you must create:
030: * <ul>
031: * <li>The Business Object extending {@link com.jat.business.BusinessObject} class</li>
032: * <li>The Business Object Factory extending {@link com.jat.business.BusinessObjectFactory} class</li>
033: * <li>The Business Object List object extending {@link com.jat.business.BusinessObjectList} class</li>
034: * </ul>
035: * </p>
036: * <p>Copyright: Copyright (c) 2004 -2005 Stefano Fratini (stefano.fratini@gmail.com)</p>
037: * <p>Distributed under the terms of the GNU Lesser General Public License, v2.1 or later</p>
038: * @author stf
039: * @version 1.1
040: * @see com.com.jat.business.BusinessObjectList
041: * @see com.com.jat.business.BusinessObjectFactory
042: */
043:
044: public class BusinessObject implements Orderable, XMLable, Serializable {
045:
046: public BusinessObject(String dataSourceName) {
047: this .properties = new BusinessObjectProperties();
048: this .dataSourceName = dataSourceName;
049: }
050:
051: public BusinessObject(String dataSourceName, Object orderBy) {
052: this (dataSourceName);
053: this .orderBy(orderBy);
054: }
055:
056: public BusinessObject(String dataSourceName,
057: BusinessObjectProperties properties) {
058: this (dataSourceName);
059: this .setProperties(properties);
060: }
061:
062: public BusinessObject(String dataSourceName,
063: BusinessObjectProperties properties, Object orderBy) {
064: this (dataSourceName, properties);
065: this .orderBy(orderBy);
066: }
067:
068: public BusinessObjectProperties getProperties() {
069: return this .properties;
070: }
071:
072: public String getField(String name) {
073: Object field = this .properties.get(name);
074: if (field == null) {
075: return "";
076: }
077: return "" + field;
078: }
079:
080: public Object getFieldAsObject(String fieldName) {
081: return this .properties.get(fieldName);
082: }
083:
084: public void putField(String key, Object value) {
085: this .properties.put(key, value);
086: }
087:
088: public Object removeField(String key) {
089: return this .properties.remove(key);
090: }
091:
092: public Enumeration fieldNames() {
093: return this .properties.keys();
094: }
095:
096: public String toString() {
097: String ret = getClass().getName() + ": ";
098: for (Enumeration e = this .properties.keys(); e
099: .hasMoreElements();) {
100: String key = (String) e.nextElement();
101: ret += "[" + key + "=" + properties.get(key) + "]";
102: }
103: return ret;
104: }
105:
106: public void setProperties(BusinessObjectProperties properties) {
107: this .properties = properties;
108: }
109:
110: private long getDateMillisec(String fieldName) {
111: long date = Long.parseLong(this .getField(fieldName));
112: return date;
113: }
114:
115: public java.util.Date getDateFromMillisec(String fieldName) {
116: return new java.util.Date(this .getDateMillisec(fieldName));
117: }
118:
119: public String getDataSourceName() {
120: return this .dataSourceName;
121: }
122:
123: public BusinessObjectProperties persist(String operation)
124: throws BusinessException {
125: try {
126: return DataSourceFactory.getFactory().getDataSource(
127: this .getDataSourceName()).execute(operation,
128: this .getProperties());
129: } catch (IntegrationException ex) {
130: throw new BusinessException(ex);
131: }
132: }
133:
134: /**
135: * Return the business object loaded from a data source by an operation using the specific properties
136: * @param factory the {@link com.jat.business.BusinessObjectFactory} instance
137: * @param dataSourceName the configured data source (see {@link com.jat.integration.DataSource} configuration)
138: * @param name the operation name of the data source (see {@link com.jat.integration.DataSource} configuration)
139: * @param properties the list of property used by the specified operation. It can be null.
140: * @return the Business Object matching with properties in the operation of the data source
141: * @throws java.lang.Exception if an error occours
142: */
143: public static BusinessObject getBusinessObject(
144: BusinessObjectFactory factory, String dataSourceName,
145: String name, BusinessObjectProperties properties)
146: throws BusinessException, IntegrationException {
147: if (factory == null)
148: throw new BusinessException(
149: "Business Factory cannot be null");
150: BusinessObjectList bol = factory.getBusinessObjectList(
151: dataSourceName, name, properties);
152: if (bol.size() < 1)
153: throw new BusinessException("No object found");
154: if (bol.size() > 1)
155: throw new BusinessException("Too objects found ("
156: + bol.size() + ")");
157: return bol.firstElement();
158: }
159:
160: public boolean equals(Object other) {
161: if (other == this )
162: return true;
163: if (other == null)
164: return false;
165: if (getClass() != other.getClass())
166: return false;
167: BusinessObject bo = (BusinessObject) other;
168: if (bo.getProperties() == null && this .getProperties() == null)
169: return true;
170: else if (bo.getProperties() != null)
171: return bo.getProperties().equals(this .getProperties());
172: return false;
173: }
174:
175: /* From Orderbale Interface*/
176: public boolean greatThan(Orderable _obj) {
177: return !lessThan(_obj);
178: }
179:
180: public boolean lessThan(Orderable _obj) {
181: String val = (String) _obj.getValue();
182: boolean ret = StringUtil.less(this .getValue().toString(), val);
183: return ret;
184: }
185:
186: public Object getValue() {
187: if (getOrderBy() == null)
188: return "";
189: return this .getField(getOrderBy().toString());
190: }
191:
192: public void orderBy(Object order) {
193: this .orderBy = order.toString();
194: }
195:
196: public Object getOrderBy() {
197: return this .orderBy;
198: }
199:
200: /* From XMLable interface */
201: public Document getXmlDocument(boolean validate)
202: throws SAXException, ParserConfigurationException,
203: IOException {
204: String xml = this .toXML();
205: return XMLUtil.getXMLDocument(xml, validate, false)
206: .getDocument();
207: }
208:
209: public String toXML() {
210: initXml();
211: String xml = "<" + object + " data_source=\""
212: + XMLUtil.parseAttribute(this .getDataSourceName())
213: + "\"";
214: if (schema != null && !schema.equals(""))
215: xml += " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\""
216: + schema + "\"";
217: xml += ">";
218: for (Enumeration el = this .fieldNames(); el.hasMoreElements();) {
219: String name = (String) el.nextElement();
220: String value = this .getField(name);
221: xml += "<" + property + " name=\""
222: + XMLUtil.parseAttribute(name) + "\">"
223: + XMLUtil.parseText(value) + "</" + property + ">";
224: }
225: xml += "</" + object + ">";
226: return xml;
227: }
228:
229: private void initXml() {
230: try {
231: object = Config.getCurrent().getValue("xml_object",
232: "object");
233: } catch (Exception ex) {
234: object = "object";
235: LogManager.sendWarning(this .getClass().getName()
236: + "::toXML: exception: " + ex);
237: }
238: try {
239: property = Config.getCurrent().getValue("xml_object",
240: "property");
241: } catch (Exception ex) {
242: property = "property";
243: LogManager.sendWarning(this .getClass().getName()
244: + "::toXML: exception: " + ex);
245: }
246: try {
247: schema = Config.getCurrent().getValue("xml_object",
248: "schema");
249: } catch (Exception ex) {
250: schema = "";
251: LogManager.sendWarning(this .getClass().getName()
252: + "::toXML: exception: " + ex);
253: }
254: }
255:
256: static String object = null;
257: static String property = null;
258: static String schema = null;
259:
260: protected String orderBy = null;
261: private BusinessObjectProperties properties = null;
262: private String dataSourceName;
263: }
|