View Javadoc

1   /*******************************************************************************
2    *  Imixs Workflow 
3    *  Copyright (C) 2001, 2011 Imixs Software Solutions GmbH,  
4    *  http://www.imixs.com
5    *  
6    *  This program is free software; you can redistribute it and/or 
7    *  modify it under the terms of the GNU General Public License 
8    *  as published by the Free Software Foundation; either version 2 
9    *  of the License, or (at your option) any later version.
10   *  
11   *  This program is distributed in the hope that it will be useful, 
12   *  but WITHOUT ANY WARRANTY; without even the implied warranty of 
13   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
14   *  General Public License for more details.
15   *  
16   *  You can receive a copy of the GNU General Public
17   *  License at http://www.gnu.org/licenses/gpl.html
18   *  
19   *  Project: 
20   *  	http://www.imixs.org
21   *  	http://java.net/projects/imixs-workflow
22   *  
23   *  Contributors:  
24   *  	Imixs Software Solutions GmbH - initial API and implementation
25   *  	Ralph Soika - Software Developer
26   *******************************************************************************/
27  package org.imixs.workflow.xml;
28  
29  import java.util.Arrays;
30  import java.util.Collection;
31  import java.util.Iterator;
32  import java.util.List;
33  import java.util.Map;
34  import java.util.Vector;
35  
36  import org.imixs.workflow.ItemCollection;
37  
38  /**
39   * An XMLItemCollectionAdapter converts a
40   * <code>org.imixs.workflow.xml.XMLItemCollection</code> into a
41   * <code> org.imixs.workflow.ItemCollection</code> and reverse
42   * 
43   * @author imixs.com - Ralph Soika
44   * @version 1.1
45   * @see org.imixs.workflow.ItemCollection
46   */
47  
48  public class XMLItemCollectionAdapter {
49  
50  	/**
51  	 * This Methode converts a <code>org.imixs.workflow.service.ItemArray</code>
52  	 * into a <code> org.imixs.workflow.ItemCollection</code> Returns null if
53  	 * ItemArray == null
54  	 * 
55  	 * @param ItemArray
56  	 * @return ItemCollection
57  	 */
58  	@SuppressWarnings("unchecked")
59  	public static ItemCollection getItemCollection(XMLItemCollection entity) {
60  		ItemCollection itemCol = new ItemCollection();
61  		if (entity == null)
62  			return itemCol;
63  
64  		try {
65  			XMLItem items[] = entity.getItem();
66  			if (items != null)
67  				for (int i = 0; i < items.length; i++) {
68  					XMLItem it = items[i];
69  					if (it == null)
70  						continue;
71  					String key = it.getName();
72  					Object[] it_value = it.getValue();
73  					if (it_value == null) {
74  						// no value found
75  						itemCol.replaceItemValue(key, new Vector());
76  					} else {
77  						Vector myVector = new Vector(Arrays.asList(it
78  								.getValue()));
79  						itemCol.replaceItemValue(key, myVector);
80  					}
81  				}
82  		} catch (Exception e) {
83  			System.out
84  					.println("[XMLItemCollectionAdapter] getItemCollection error");
85  			System.out.println(e.toString());
86  			itemCol = null;
87  		}
88  
89  		return itemCol;
90  	}
91  
92  	/**
93  	 * This Method converts a <code> org.imixs.workflow.ItemCollection</code>
94  	 * into a <code>Entity</code>
95  	 * 
96  	 * <p>
97  	 * The method verifies if the values stored are basic java types. If not
98  	 * these values will not be converted!
99  	 * 
100 	 * @param aItemCollection
101 	 *            Collection Object to be converted
102 	 * @param itemNames
103 	 *            - optional list of item names to be converted. If null all
104 	 *            items will be converted
105 	 */
106 	@SuppressWarnings("unchecked")
107 	public static XMLItemCollection putItemCollection(
108 			ItemCollection aItemCollection, List<String> itemNames)
109 			throws Exception {
110 		String sName = null;
111 		XMLItemCollection entity = new XMLItemCollection();
112 		int i = 0;
113 		XMLItem[] items = null;
114 		try {
115 			if (aItemCollection != null) {
116 				// test if only a sublist of items should be converted
117 				if (itemNames != null && itemNames.size() > 0) {
118 					items = new XMLItem[itemNames.size()];
119 					for (String aField : itemNames) {
120 						// this code block guarantees that the order of items
121 						// returned
122 						sName = aField;
123 						XMLItem item = new XMLItem();
124 						// test the ItemValue
125 						Vector vOrg = (Vector) aItemCollection
126 								.getItemValue(aField);
127 						if (!isBasicType(vOrg,sName)) {
128 							// set dummy value
129 							item.setName(sName);
130 							vOrg=new Vector();
131 							vOrg.add(null);
132 							item.setValue(vOrg.toArray());
133 						} else {
134 							item.setName(sName);
135 							// if no value available, create a dummy entry
136 							if (vOrg.size()==0)
137 								vOrg.add(null);
138 							item.setValue(vOrg.toArray());
139 						}
140 						items[i] = item;
141 						i++;
142 					}
143 
144 				} else {
145 					// convert all items (no itemname list is provided)
146 					Iterator it = aItemCollection.getAllItems().entrySet()
147 							.iterator();
148 					int max = aItemCollection.getAllItems().entrySet().size();
149 					items = new XMLItem[max];
150 
151 					// iterate over all items if no itemNames are provided
152 					while (it.hasNext()) {
153 						Map.Entry entry = (Map.Entry) it.next();
154 						sName = (String) entry.getKey();
155 
156 						// test ItemValue
157 						Vector vOrg = (Vector) entry.getValue();
158 
159 						if (!isBasicType(vOrg,sName))
160 							continue;
161 						XMLItem item = new XMLItem();
162 						item.setName(sName);
163 						item.setValue(vOrg.toArray());
164 
165 						items[i] = item;
166 						i++;
167 					}
168 				}
169 
170 				entity.setItem(items);
171 			}
172 
173 		} catch (Exception e) {
174 			System.out
175 					.println("[XMLItemCollectionAdapter] Error putItemCollection ("
176 							+ sName + ")");
177 			throw e;
178 		}
179 
180 		return entity;
181 	}
182 
183 	/**
184 	 * This Method converts a <code> org.imixs.workflow.ItemCollection</code>
185 	 * into a <code>Entity</code>
186 	 * 
187 	 * <p>
188 	 * The method verifies if the values stored are basic java types. If not
189 	 * these values will not be converted!
190 	 * 
191 	 * @param aItemCollection
192 	 *            Collection Object to be converted
193 	 */
194 	public static XMLItemCollection putItemCollection(
195 			ItemCollection aItemCollection) throws Exception {
196 		return putItemCollection(aItemCollection,null);
197 	}
198 
199 	/**
200 	 * This method transforms a Collection<ItemCollection> into a
201 	 * EntityCollection
202 	 * 
203 	 * @param col
204 	 * @return
205 	 * @throws Exception
206 	 */
207 	public static EntityCollection putCollection(Collection<ItemCollection> col)
208 			throws Exception {
209 
210 		return putCollection(col, null);
211 	}
212 
213 	/**
214 	 * This method transforms a Collection<ItemCollection> into a
215 	 * EntityCollection
216 	 * 
217 	 * If the attribute List is provided only the corresponding properties will
218 	 * be returned.
219 	 * 
220 	 * @param col
221 	 *            - collection of ItemCollection objects to be converted
222 	 * @param itemNames
223 	 *            - optional list of item names to be converted. If null all
224 	 *            items will be converted
225 	 * @return
226 	 * @throws Exception
227 	 */
228 	public static EntityCollection putCollection(
229 			Collection<ItemCollection> col, List<String> itemNames)
230 			throws Exception {
231 		EntityCollection entiCol = new EntityCollection();
232 		Iterator<ItemCollection> it = col.iterator();
233 		int max = col.size();
234 		int i = 0;
235 		XMLItemCollection[] entities = new XMLItemCollection[max];
236 		while (it.hasNext()) {
237 			ItemCollection icw = (ItemCollection) it.next();
238 			XMLItemCollection entity = putItemCollection(icw,itemNames);
239 			entities[i] = entity;
240 			i++;
241 		}
242 		if (max > 0)
243 			entiCol.setEntity(entities);
244 		return entiCol;
245 	}
246 
247 	/**
248 	 * This method test if the values of an vector are basic types
249 	 * 
250 	 * check for java.lang.*, java.math.*
251 	 * 
252 	 * @return
253 	 */
254 	private static boolean isBasicType(Vector v, String fieldname) {
255 		for (Object o : v) {
256 			// Package p=o.getClass().getPackage();
257 			Class c = o.getClass();
258 			
259 			String name = c.getName();
260 			if (!name.startsWith("java.lang.")
261 					&& !name.startsWith("java.math.")
262 					&& !"java.util.Date".equals(name)) {
263 				System.out
264 						.println("WARNING : XMLItemCollectionAdapter - unsported java type for property '" + fieldname + "' = "
265 								+ name);
266 				return false;
267 			}
268 		}
269 		return true;
270 	}
271 
272 }