View Javadoc

1   package org.apache.torque.betwixt;
2   
3   /*
4    * Copyright 2001-2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License")
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.util.ArrayList;
20  import java.util.Collections;
21  import java.util.Iterator;
22  import java.util.List;
23  
24  import org.apache.commons.betwixt.io.BeanWriter;
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.apache.torque.Torque;
28  
29  /***
30   * A container Bean class for records to be exported or imported.  It also
31   * supports the name, action, onError, description processing attributes.
32   * 
33   * @author <a href="mailto:greg.monroe@dukece.com">Greg Monroe</a>
34   */
35  public class Dataset 
36  {
37      static Log logger = LogFactory.getLog(Dataset.class);
38  
39      private List list = Collections.synchronizedList(new ArrayList());
40      private BeanWriter beanWriter;
41  
42      private String action;
43      private String description;
44      private String onError;
45      private String name;
46  
47      /***
48       * Generic bean constructor
49       */
50      public Dataset() 
51      {
52          super();
53      }
54  
55      /***
56       * Add an individual item to the data set.
57       * 
58       * @param item
59       */
60      public void addRecord(Object item) 
61      {
62          this.list.add(item);
63      }
64  
65      /***
66       * Add a list or records to the end of the current collection.
67       * 
68       * @param list A list of records to add to the end of the existing list.
69       */
70      public void addRecordList(List list) 
71      {
72          this.list.addAll(list);
73      }
74  
75      /***
76       * Get the record set list.
77       * 
78       * @return The list of records.
79       */
80      public List getRecord() 
81      {
82          return this.list;
83      }
84  
85      /***
86       * Get the size of the current data set.
87       * 
88       * @return The number of records in this dataset
89       */
90      public int size() 
91      {
92          return this.list.size();
93      }
94  
95      /***
96       * Get an iterator for this data record set.
97       * 
98       * @return The unsynchronized iterator
99       */
100     public Iterator iterator() 
101     {
102         return this.list.iterator();
103     }
104 
105     /***
106      * Replace the record set with the supplied set.  Note that value should
107      * be a synchronized collection (e.g 
108      * Collections.synchronizedList(new ArrayList(...));
109      * 
110      * @param value
111      */
112     public void setRecord(List value) 
113     {
114         this.list = value;
115     }
116 
117     /***
118      * Get the value of the action attribute.
119      * 
120      * @return Returns the action.
121      */
122     public String getAction() 
123     {
124         return action;
125     }
126 
127     /***
128      * Sets the value of the action attribute
129      * 
130      * @param action
131      *            The action to set.
132      */
133     public void setAction(String action) 
134     {
135         this.action = action;
136     }
137 
138     /***
139      * Gets the value of the onError attribute.
140      * 
141      * @return Returns the onError.
142      */
143     public String getOnError() 
144     {
145         return onError;
146     }
147 
148     /***
149      * Set the value of the onError attribute.
150      * 
151      * @param onError
152      *            The onError to set.
153      */
154     public void setOnError(String onError) 
155     {
156         this.onError = onError;
157     }
158 
159     /***
160      * Get the value of the description attribute.
161      * 
162      * @return Returns the description.
163      */
164     public String getDescription() 
165     {
166         return description;
167     }
168 
169     /***
170      * Set the value of the description attribute.
171      * 
172      * @param description
173      *            The description to set.
174      */
175     public void setDescription(String description) 
176     {
177         this.description = description;
178     }
179 
180     /***
181      * Get the bean writer.  Only valid after createBeanWriter has
182      * been called.
183      * 
184      * @return The current BeanWriter object.
185      * @throws IllegalStateException If called before createBeanWriter has
186      *                                  been called.
187      */
188     public BeanWriter getBeanWriter() 
189                 throws IllegalStateException
190     {
191         if (beanWriter == null) 
192         {
193             throw new IllegalStateException(
194                     "Bean Writer object has not been created yet!");
195         }
196         return beanWriter;
197     }
198     
199     /***
200      * Dump debug info.
201      */
202     public String toString() 
203     {
204         final StringBuilder sb = new StringBuilder();
205         sb.append("action: ").append(getAction()).append("\n");
206         sb.append("onError: ").append(getOnError()).append("\n");
207         sb.append("description: ").append(getDescription()).append("\n");
208         int recNum = 0;
209         for (final Iterator it = iterator(); it.hasNext();) 
210         {
211             Object record = (Object) it.next();
212             sb.append("Record: ").append(recNum).append(" Class: ").append(
213                     record.getClass().getName()).append("\n");
214 
215             recNum++;
216         }
217         return sb.toString();
218     }
219 
220     /***
221      * Get the value of the name attribute
222      * 
223      * @return Returns the name.
224      */
225     public String getName() 
226     {
227         if (name == null ) 
228         {
229             name = Torque.getDefaultDB();
230         }
231         return name;
232     }
233 
234     /***
235      * Set the value of the name attribute
236      * 
237      * @param name The name to set.
238      */
239     public void setName(String name) 
240     {
241         this.name = name;
242     }
243 }