diff --git a/imixs-workflow-core/src/main/java/org/imixs/workflow/xml/XMLItemCollectionAdapter.java b/imixs-workflow-core/src/main/java/org/imixs/workflow/xml/XMLItemCollectionAdapter.java index d7084d5a1..ce31f30ea 100644 --- a/imixs-workflow-core/src/main/java/org/imixs/workflow/xml/XMLItemCollectionAdapter.java +++ b/imixs-workflow-core/src/main/java/org/imixs/workflow/xml/XMLItemCollectionAdapter.java @@ -43,6 +43,7 @@ import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Unmarshaller; +import javax.xml.datatype.XMLGregorianCalendar; import org.imixs.workflow.ItemCollection; @@ -61,15 +62,21 @@ public class XMLItemCollectionAdapter { .getLogger(XMLItemCollectionAdapter.class.getName()); /** - * This Methode converts a org.imixs.workflow.service.ItemArray - * into a org.imixs.workflow.ItemCollection Returns null if - * ItemArray == null + * This Methode converts a + * org.imixs.workflow.xml.XMLItemCollection into a + * org.imixs.workflow.ItemCollection Returns null if entity == + * null * - * @param ItemArray + * issue #52: + * + * We convert XMLGregorianCalendar into java.util.Date objects + * + * @param entity * @return ItemCollection */ @SuppressWarnings({ "unchecked", "rawtypes" }) - public static ItemCollection getItemCollection(XMLItemCollection entity) { + public static ItemCollection getItemCollection( + final XMLItemCollection entity) { ItemCollection itemCol = new ItemCollection(); if (entity == null) return itemCol; @@ -87,9 +94,11 @@ public static ItemCollection getItemCollection(XMLItemCollection entity) { // no value found itemCol.replaceItemValue(key, new Vector()); } else { - Vector myVector = new Vector(Arrays.asList(it - .getValue())); - itemCol.replaceItemValue(key, myVector); + // test the content for GregorianCalendar... (issue #52) + Object[] objectArray = convertXMLGregorianCalendar(it + .getValue()); + itemCol.replaceItemValue(key, + new Vector(Arrays.asList(objectArray))); } } } catch (Exception e) { @@ -118,7 +127,7 @@ public static ItemCollection getItemCollection(XMLItemCollection entity) { */ @SuppressWarnings({ "unchecked" }) public static XMLItemCollection putItemCollection( - ItemCollection aItemCollection, List itemNames) + final ItemCollection aItemCollection, final List itemNames) throws Exception { String sName = null; XMLItemCollection entity = new XMLItemCollection(); @@ -205,7 +214,7 @@ public static XMLItemCollection putItemCollection( * Collection Object to be converted */ public static XMLItemCollection putItemCollection( - ItemCollection aItemCollection) throws Exception { + final ItemCollection aItemCollection) throws Exception { return putItemCollection(aItemCollection, null); } @@ -217,8 +226,8 @@ public static XMLItemCollection putItemCollection( * @return * @throws Exception */ - public static EntityCollection putCollection(Collection col) - throws Exception { + public static EntityCollection putCollection( + final Collection col) throws Exception { return putCollection(col, null); } @@ -239,7 +248,7 @@ public static EntityCollection putCollection(Collection col) * @throws Exception */ public static EntityCollection putCollection( - Collection col, List itemNames) + final Collection col, final List itemNames) throws Exception { EntityCollection entiCol = new EntityCollection(); Iterator it = col.iterator(); @@ -402,4 +411,22 @@ private static byte[] getBytesFromStream(InputStream is) throws IOException { is.close(); return buffer.toByteArray(); } + + /** + * This helper method converts instances of XMLGregorianCalendar into + * java.util.Date objects. + * + */ + private static Object[] convertXMLGregorianCalendar( + final Object[] objectArray) { + // test the content for GregorianCalendar... (issue #52) + for (int j = 0; j < objectArray.length; j++) { + if (objectArray[j] instanceof XMLGregorianCalendar) { + XMLGregorianCalendar xmlCal = (XMLGregorianCalendar) objectArray[j]; + // convert into Date object + objectArray[j] = xmlCal.toGregorianCalendar().getTime(); + } + } + return objectArray; + } } diff --git a/imixs-workflow-core/src/test/java/org/imixs/workflow/xml/TestXMLItemCollectionAdapter.java b/imixs-workflow-core/src/test/java/org/imixs/workflow/xml/TestXMLItemCollectionAdapter.java new file mode 100644 index 000000000..fbbd52ddb --- /dev/null +++ b/imixs-workflow-core/src/test/java/org/imixs/workflow/xml/TestXMLItemCollectionAdapter.java @@ -0,0 +1,141 @@ +package org.imixs.workflow.xml; + +import java.util.Date; +import java.util.GregorianCalendar; +import java.util.List; + +import javax.xml.datatype.DatatypeConfigurationException; +import javax.xml.datatype.DatatypeFactory; +import javax.xml.datatype.XMLGregorianCalendar; + +import org.imixs.workflow.ItemCollection; +import org.junit.Assert; +import org.junit.Test; + +/** + * Test class for xmlItemCollection object + * + * @author rsoika + * + */ +public class TestXMLItemCollectionAdapter { + + @Test + public void testItemCollection() { + ItemCollection itemCollection = new ItemCollection(); + itemCollection.replaceItemValue("txtTitel", "Hello"); + XMLItemCollection xmlItemCollection = null; + try { + xmlItemCollection = XMLItemCollectionAdapter + .putItemCollection(itemCollection); + } catch (Exception e) { + e.printStackTrace(); + Assert.fail(); + } + + ItemCollection col2 = XMLItemCollectionAdapter + .getItemCollection(xmlItemCollection); + + Assert.assertEquals(itemCollection.getItemValueString("txttitel"), + "Hello"); + + Assert.assertEquals(col2.getItemValueString("txttitel"), "Hello"); + } + + /** + * test convertion of date values + */ + @Test + public void testDateValue() { + ItemCollection itemCollection = new ItemCollection(); + + Date datTest = new Date(); + itemCollection.replaceItemValue("datDate", datTest); + + XMLItemCollection xmlItemCollection = null; + try { + xmlItemCollection = XMLItemCollectionAdapter + .putItemCollection(itemCollection); + } catch (Exception e) { + e.printStackTrace(); + Assert.fail(); + } + + ItemCollection col2 = XMLItemCollectionAdapter + .getItemCollection(xmlItemCollection); + + // test if date is equals + Assert.assertEquals(col2.getItemValueDate("datDate"), + itemCollection.getItemValueDate("datDate")); + + Assert.assertEquals(col2.getItemValueDate("datDate"), datTest); + + } + + /** + * Issue #52 + * + * Test conversion of XMLGregorianCalener Instances + * + * @see http://stackoverflow.com/questions/835889/java-util-date-to- + * xmlgregoriancalendar + */ + @SuppressWarnings({ "rawtypes" }) + @Test + public void testXMLGregrianCalendar() { + + // first we test normal date objects + + Date datTest = new Date(); + + XMLItemCollection xmlItemCollection = new XMLItemCollection(); + XMLItem xmlItem = new XMLItem(); + xmlItem.setName("datdate"); + xmlItem.setValue(new Object[] { datTest }); + XMLItem[] xmlItemList = new XMLItem[] { xmlItem }; + xmlItemCollection.setItem(xmlItemList); + + ItemCollection itemCollection = XMLItemCollectionAdapter + .getItemCollection(xmlItemCollection); + Assert.assertEquals(itemCollection.getItemValueDate("datdate"), datTest); + + /* + * Test phase II. + */ + + // now we repeat the test with an XMLGregorianCalneder Object... + XMLGregorianCalendar xmlDate = null; + try { + GregorianCalendar c = new GregorianCalendar(); + c.setTime(datTest); + xmlDate = DatatypeFactory.newInstance().newXMLGregorianCalendar(c); + } catch (DatatypeConfigurationException e) { + + e.printStackTrace(); + Assert.fail(); + } + xmlItemCollection = new XMLItemCollection(); + xmlItem = new XMLItem(); + xmlItem.setName("datdate"); + xmlItem.setValue(new Object[] { xmlDate }); + xmlItemList = new XMLItem[] { xmlItem }; + xmlItemCollection.setItem(xmlItemList); + + itemCollection = XMLItemCollectionAdapter + .getItemCollection(xmlItemCollection); + + // now we expect that the XMLItemCollectionAdapter has converted + // the XMLGregorianCalendar impl into a java.util.Date object + + List resultDate = itemCollection.getItemValue("datdate"); + Assert.assertNotNull(resultDate); + Assert.assertEquals(1, resultDate.size()); + + Assert.assertFalse(resultDate.get(0) instanceof XMLGregorianCalendar); + Assert.assertTrue(resultDate.get(0) instanceof java.util.Date); + + Assert.assertEquals(itemCollection.getItemValueDate("datdate"), datTest); + + } + +}