Skip to content

Commit

Permalink
issue #52
Browse files Browse the repository at this point in the history
  • Loading branch information
rsoika committed Oct 10, 2015
1 parent b39cd0c commit ae07a3a
Show file tree
Hide file tree
Showing 2 changed files with 181 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -61,15 +62,21 @@ public class XMLItemCollectionAdapter {
.getLogger(XMLItemCollectionAdapter.class.getName());

/**
* This Methode converts a <code>org.imixs.workflow.service.ItemArray</code>
* into a <code> org.imixs.workflow.ItemCollection</code> Returns null if
* ItemArray == null
* This Methode converts a
* <code>org.imixs.workflow.xml.XMLItemCollection</code> into a
* <code> org.imixs.workflow.ItemCollection</code> 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;
Expand All @@ -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) {
Expand Down Expand Up @@ -118,7 +127,7 @@ public static ItemCollection getItemCollection(XMLItemCollection entity) {
*/
@SuppressWarnings({ "unchecked" })
public static XMLItemCollection putItemCollection(
ItemCollection aItemCollection, List<String> itemNames)
final ItemCollection aItemCollection, final List<String> itemNames)
throws Exception {
String sName = null;
XMLItemCollection entity = new XMLItemCollection();
Expand Down Expand Up @@ -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);
}

Expand All @@ -217,8 +226,8 @@ public static XMLItemCollection putItemCollection(
* @return
* @throws Exception
*/
public static EntityCollection putCollection(Collection<ItemCollection> col)
throws Exception {
public static EntityCollection putCollection(
final Collection<ItemCollection> col) throws Exception {

return putCollection(col, null);
}
Expand All @@ -239,7 +248,7 @@ public static EntityCollection putCollection(Collection<ItemCollection> col)
* @throws Exception
*/
public static EntityCollection putCollection(
Collection<ItemCollection> col, List<String> itemNames)
final Collection<ItemCollection> col, final List<String> itemNames)
throws Exception {
EntityCollection entiCol = new EntityCollection();
Iterator<ItemCollection> it = col.iterator();
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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);

}

}

0 comments on commit ae07a3a

Please sign in to comment.