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.DataSource;
012: import com.jat.integration.DataSourceFactory;
013: import com.jat.integration.IntegrationException;
014: import com.jat.util.OrderVector;
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 is a list of Business Object ({@link com.jat.business.BusinessObject}).
022: * <br>Is possible to sort the Business Objects by {@link orderBy} method
023: * or get the XML format of an instance of this class ({@link toXML} and {@link getXmlDocument} methods).
024: * </p>
025: * <p>
026: * If you want to build your own Business Object, you must create:
027: * <ul>
028: * <li>The Business Object extending {@link com.jat.business.BusinessObject} class</li>
029: * <li>The Business Object Factory extending {@link com.jat.business.BusinessObjectFactory} class</li>
030: * <li>The Business Object List object extending {@link com.jat.business.BusinessObjectList} class</li>
031: * </ul>
032: * </p>
033: * <p>Copyright: Copyright (c) 2004 -2005 Stefano Fratini (stefano.fratini@gmail.com)</p>
034: * <p>Distributed under the terms of the GNU Lesser General Public License, v2.1 or later</p>
035: * @author stf
036: * @version 1.2
037: * @since 1.0
038: * @see com.com.jat.business.BusinessObject
039: * @see com.com.jat.business.BusinessObjectFactory
040: */
041:
042: public abstract class BusinessObjectList implements XMLable,
043: Serializable {
044:
045: public BusinessObjectList(String dataSourceName) {
046: this .dataSourceName = dataSourceName;
047: this .events = new OrderVector();
048: }
049:
050: public BusinessObjectList(String dataSourceName,
051: BusinessObjectPropertyList props) {
052: this (dataSourceName);
053: for (Enumeration e = props.elements(); e.hasMoreElements();) {
054: BusinessObjectProperties prop = (BusinessObjectProperties) e
055: .nextElement();
056: BusinessObject item = this .getBusinessObject(prop);
057: this .addElement(item);
058: }
059: }
060:
061: public BusinessObject firstElement() {
062: return (BusinessObject) this .events.firstElement();
063: }
064:
065: public BusinessObject elementAt(int index) {
066: return (BusinessObject) this .events.elementAt(index);
067: }
068:
069: public Enumeration elements() {
070: return this .events.elements();
071: }
072:
073: public int size() {
074: return this .events.size();
075: }
076:
077: public void orderBy(String field, boolean ascendent)
078: throws Exception {
079: if (ascendent)
080: this .events = OrderVector
081: .orderAscendent(this .events, field);
082: else
083: this .events = OrderVector.orderDescendent(this .events,
084: field);
085: }
086:
087: public String getDataSourceName() {
088: return this .dataSourceName;
089: }
090:
091: /**
092: * Execute the configured operation (execName) for all Business Object in the list
093: * and return the result list of each pperation
094: * @param execName the name of an operation configured
095: * @return the list of {@link com.jat.business.BusinessObjectProperties}
096: * containing the exection result for each operation
097: * @throws BusinessException if an exception occours
098: */
099: public BusinessObjectPropertyList persist(String execName)
100: throws BusinessException {
101: try {
102: MultiBusinessObjectProperties multi = new MultiBusinessObjectProperties();
103: for (Enumeration e = this .elements(); e.hasMoreElements();)
104: multi.put(execName, (BusinessObject) e.nextElement());
105: return DataSourceFactory.getFactory().getDataSource(
106: this .getDataSourceName()).execute(multi);
107: } catch (IntegrationException ex) {
108: throw new BusinessException(ex);
109: }
110: }
111:
112: /**
113: * Refresh the list of {@link com.jat.business.BusinessObject} reloading from the same data source
114: * and using the same parameters (query name and properties) of the current instance was loaded.
115: * Before use this method be sure that this object has been instanced throw a {@link com.jat.business.BusinessObjectFactory} object,
116: * otherwise a {@link com.jat.business.BusinessException} will be thrown.
117: * @return the {@link com.jat.business.BusinessObjectList} reloaded from the data source
118: * @throws BusinessException if this class has not been instanced throw a {@link com.jat.business.BusinessObjectFactory} object
119: * @throws IntegrationException whenever an Integration error occours
120: */
121: public BusinessObjectList refresh() throws BusinessException,
122: IntegrationException {
123: if (this .requestName == null)
124: throw new BusinessException(
125: this .getClass().getName()
126: + "::refresh: Operation name is not defined: impossible to refresh the list");
127: BusinessObjectList bol = this .getBusinessObjectList(
128: this .factory, this .getDataSourceName(),
129: this .requestName, this .requestProperties);
130: this .events = bol.events;
131: return bol;
132: }
133:
134: /**
135: * Return a list of 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 list of 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 BusinessObjectList getBusinessObjectList(
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: DataSource ds = DataSourceFactory.getFactory().getDataSource(
151: dataSourceName);
152: if (ds == null)
153: throw new BusinessException("Data Source '"
154: + dataSourceName + "' not found");
155: BusinessObjectPropertyList props = ds.getData(name, properties);
156: BusinessObjectList bol = factory.getBusinessObjectList(
157: dataSourceName, props);
158: bol.setRequestProperties(factory, name, properties);
159: return bol;
160: }
161:
162: public void addElement(BusinessObject item) {
163: //this.remove(item);
164: this .events.addAscElement(item);
165: LogManager.sendDebug(this .getClass().getName()
166: + ":: addElement: event: " + item.toString());
167: }
168:
169: public String toString() {
170: String ret = getClass().getName() + ": ";
171: for (Enumeration e = this .elements(); e.hasMoreElements();) {
172: ret += "{" + e.nextElement().toString() + "}";
173: }
174: return ret;
175: }
176:
177: public boolean remove(BusinessObject item) {
178: if (item == null)
179: return false;
180: return this .events.removeElement(item);
181: }
182:
183: public void add(BusinessObjectList list) {
184: for (Enumeration e = list.elements(); e.hasMoreElements();)
185: this .events.add(e.nextElement());
186: this .events = OrderVector.orderAscendent(this .events);
187: }
188:
189: protected void setRequestProperties(BusinessObjectFactory factory,
190: String requestName,
191: BusinessObjectProperties requestProperties) {
192: this .factory = factory;
193: this .requestName = requestName;
194: this .requestProperties = requestProperties;
195: }
196:
197: /**
198: * return a Business Object from a set of properties.
199: * <br/>Implement this method to build a new instance of your Business Object
200: * using the constructor with data source name and {@link com.jat.businessBusinessObjectProperties}
201: * @param props the list of properties
202: * @return a new instance of a Business Object implementation
203: */
204: protected abstract BusinessObject getBusinessObject(
205: BusinessObjectProperties props);
206:
207: /* From XMLable interface */
208: public Document getXmlDocument(boolean validate)
209: throws SAXException, ParserConfigurationException,
210: IOException {
211: String xml = this .toXML();
212: return XMLUtil.getXMLDocument(xml, validate, false)
213: .getDocument();
214: }
215:
216: public String toXML() {
217: initXml();
218: String xml = "";
219: if (schema != null && !schema.equals(""))
220: xml = "<"
221: + list
222: + " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\""
223: + schema + "\">";
224: else
225: xml = "<" + list + ">";
226: for (Enumeration e = this .elements(); e.hasMoreElements();) {
227: BusinessObject obj = (BusinessObject) e.nextElement();
228: xml += obj.toXML();
229: }
230: xml += "</" + list + ">";
231: return xml;
232: }
233:
234: private void initXml() {
235: try {
236: list = Config.getCurrent().getValue("xml_object", "list");
237: } catch (Exception ex) {
238: list = "list";
239: LogManager.sendWarning(this .getClass().getName()
240: + "::toXML: exception: " + ex);
241: }
242: try {
243: schema = Config.getCurrent().getValue("xml_object",
244: "schema");
245: } catch (Exception ex) {
246: schema = "";
247: LogManager.sendWarning(this .getClass().getName()
248: + "::toXML: exception: " + ex);
249: }
250: }
251:
252: static String list = null;
253: static String schema = null;
254:
255: private OrderVector events;
256: private String dataSourceName;
257: private BusinessObjectProperties requestProperties = null;
258: private BusinessObjectFactory factory = null;
259: private String requestName = null;
260:
261: /** @link dependency
262: * @stereotype instantiate
263: * @label objects*/
264: /*# BusinessObject lnkBusinessObject; */
265: }
|