branch jsonize_wikitty created (now 7a81dca)
This is an automated email from the git hooks/post-receive script. New change to branch jsonize_wikitty in repository chorem. See http://git.chorem.org/chorem.git at 7a81dca add GSON Adapter for Wikitty BusinessEntity This branch includes the following new commits: new 7a81dca add GSON Adapter for Wikitty BusinessEntity The 1 revisions listed above as "new" are entirely new to this repository and will be described in separate emails. The revisions listed as "adds" were already present in the repository and have only been added to this reference. Detailed log of new commits: commit 7a81dca242021b3cf7098872568e5a441bcb3637 Author: Yannick Martel <yannick.martel@gmail.com> Date: Thu Mar 26 12:16:23 2015 +0100 add GSON Adapter for Wikitty BusinessEntity -- To stop receiving notification emails like this one, please contact chorem.org SCM administrator <admin+scm@chorem.org>.
This is an automated email from the git hooks/post-receive script. New commit to branch jsonize_wikitty in repository chorem. See http://git.chorem.org/chorem.git commit 7a81dca242021b3cf7098872568e5a441bcb3637 Author: Yannick Martel <yannick.martel@gmail.com> Date: Thu Mar 26 12:16:23 2015 +0100 add GSON Adapter for Wikitty BusinessEntity --- .../chorem/webmotion/converters/JsonConverter.java | 4 +- .../chorem/webmotion/converters/JsonHelper.java | 47 +++++++++++++ .../webmotion/converters/JsonConverterTest.java | 77 ++++++++++++++++++++++ 3 files changed, 126 insertions(+), 2 deletions(-) diff --git a/chorem-webmotion/src/main/java/org/chorem/webmotion/converters/JsonConverter.java b/chorem-webmotion/src/main/java/org/chorem/webmotion/converters/JsonConverter.java index 9ebce4c..0a7745f 100644 --- a/chorem-webmotion/src/main/java/org/chorem/webmotion/converters/JsonConverter.java +++ b/chorem-webmotion/src/main/java/org/chorem/webmotion/converters/JsonConverter.java @@ -43,14 +43,14 @@ public class JsonConverter<O> extends AbstractConverter { } @Override - protected String convertToString(Object value) throws Throwable { + protected String convertToString(Object value) { String result = jsonHelper.toJson(value); return result; } @Override - protected <T> T convertToType(Class<T> type, Object value) throws Throwable { + protected <T> T convertToType(Class<T> type, Object value) { String stringValue; diff --git a/chorem-webmotion/src/main/java/org/chorem/webmotion/converters/JsonHelper.java b/chorem-webmotion/src/main/java/org/chorem/webmotion/converters/JsonHelper.java index 65b795d..024eb15 100644 --- a/chorem-webmotion/src/main/java/org/chorem/webmotion/converters/JsonHelper.java +++ b/chorem-webmotion/src/main/java/org/chorem/webmotion/converters/JsonHelper.java @@ -21,8 +21,13 @@ package org.chorem.webmotion.converters; * #L% */ +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import java.lang.reflect.Type; +import java.util.Collection; import java.util.Date; +import java.util.HashMap; +import java.util.Map; import com.google.gson.ExclusionStrategy; import com.google.gson.FieldAttributes; @@ -36,6 +41,9 @@ import com.google.gson.JsonParseException; import com.google.gson.JsonPrimitive; import com.google.gson.JsonSerializationContext; import com.google.gson.JsonSerializer; +import org.apache.commons.lang.StringUtils; +import org.nuiton.wikitty.WikittyException; +import org.nuiton.wikitty.entities.BusinessEntity; /** * @author ymartel <martel@codelutin.com> @@ -87,6 +95,45 @@ public class JsonHelper { }); + gsonBuilder.registerTypeHierarchyAdapter(BusinessEntity.class, new JsonSerializer<BusinessEntity>() { + + @Override + public JsonElement serialize(BusinessEntity src, Type typeOfSrc, JsonSerializationContext context) { + + JsonElement result; + + if (src == null) { + result = JsonNull.INSTANCE; + + } else { + Map<String, Object> srcAsMap = new HashMap(); + srcAsMap.put("id", src.getWikittyId()); + + Collection<String> extensionNames = src.getExtensionNames(); + for (String extensionName : extensionNames) { + Collection<String> extensionFields = src.getExtensionFields(extensionName); + Map<String, Object> extension = new HashMap(extensionFields.size()); + for (String extensionField : extensionFields) { + try { + Method getter = src.getClass().getMethod("get" + StringUtils.capitalize(extensionField), boolean.class); + Object value = getter.invoke(src, false); + extension.put(extensionField, value); + + } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException | WikittyException e) { + // If noSuchMethod, it's because field is not a wikitty + extension.put(extensionField, src.getFieldAsObject(extensionName, extensionField)); + } + } + srcAsMap.put(extensionName, extension); + } + result = context.serialize(srcAsMap); + } + + return result; + } + + }); + this.gson = gsonBuilder.create(); } diff --git a/chorem-webmotion/src/test/java/org/chorem/webmotion/converters/JsonConverterTest.java b/chorem-webmotion/src/test/java/org/chorem/webmotion/converters/JsonConverterTest.java index e264ece..57ecf45 100644 --- a/chorem-webmotion/src/test/java/org/chorem/webmotion/converters/JsonConverterTest.java +++ b/chorem-webmotion/src/test/java/org/chorem/webmotion/converters/JsonConverterTest.java @@ -21,9 +21,25 @@ package org.chorem.webmotion.converters; * #L% */ +import java.util.Arrays; import java.util.Date; import java.util.Set; +import org.apache.commons.lang3.StringUtils; +import org.chorem.entities.Company; +import org.chorem.entities.CompanyImpl; +import org.chorem.entities.Employee; +import org.chorem.entities.EmployeeImpl; +import org.chorem.entities.ExpenseAccount; +import org.chorem.entities.ExpenseAccountEntry; +import org.chorem.entities.ExpenseAccountEntryImpl; +import org.chorem.entities.ExpenseAccountImpl; +import org.chorem.entities.Person; +import org.chorem.entities.PersonImpl; +import org.chorem.entities.Project; +import org.chorem.entities.ProjectImpl; +import org.chorem.entities.Quotation; +import org.chorem.entities.QuotationImpl; import org.chorem.webmotion.bean.financial.ExpenseAccountBean; import org.chorem.webmotion.bean.financial.ExpenseAccountEntryBean; import org.junit.Assert; @@ -68,4 +84,65 @@ public class JsonConverterTest { } + @Test + public void testConvertBusinessEntityToJson() throws Exception { + + Date startDate = DateUtil.createDate(01, 02, 2012); + + Date endDate = DateUtil.createDate(28, 02, 2012); + Date dateEmittedDate = DateUtil.createDate(23, 02, 2012); + Date phoneBillEmittedDate = DateUtil.createDate(28, 02, 2012); + Date luciusDate = DateUtil.createDate(05, 01, 1979); + + Company wayneCorp = new CompanyImpl(); + wayneCorp.setName("Wayne Corp."); + wayneCorp.setType("Multinational"); + + Person luciusFox = new PersonImpl(); + luciusFox.setFirstName("Lucius"); + luciusFox.setLastName("Fox"); + luciusFox.setBirthDate(luciusDate); + + Employee employeeFox = new EmployeeImpl(); + employeeFox.setCompany(wayneCorp); + employeeFox.setPerson(luciusFox); + + Project projectOne = new ProjectImpl(); + projectOne.setDescription("A simple project"); + projectOne.setName("Project One"); + + Quotation quotation = new QuotationImpl(); + quotation.setDescription("Project One start"); + quotation.setProject(projectOne); + quotation.setSupplier(employeeFox); + + ExpenseAccountEntry expenseAccountEntryOne = new ExpenseAccountEntryImpl(); + expenseAccountEntryOne.setQuotation(quotation); + expenseAccountEntryOne.setDescription("Business Date"); + expenseAccountEntryOne.setAmount(131.73); + expenseAccountEntryOne.setEmittedDate(dateEmittedDate); + + ExpenseAccountEntry expenseAccountEntryTwo = new ExpenseAccountEntryImpl(); + expenseAccountEntryTwo.setDescription("Phone Bill"); + expenseAccountEntryTwo.setAmount(235.12); + expenseAccountEntryTwo.setEmittedDate(phoneBillEmittedDate); + + ExpenseAccount expenseAccount = new ExpenseAccountImpl(); + expenseAccount.setEmployee(employeeFox); + expenseAccount.setExpenseAccountEntryEntity(Arrays.asList(expenseAccountEntryOne, expenseAccountEntryTwo)); + expenseAccount.setBeginDate(startDate); + expenseAccount.setEndDate(endDate); + + JsonConverter<ExpenseAccount> expenseAccountJsonConverter = JsonConverter.newConverter(ExpenseAccount.class); + String expenseAccountAsJson = expenseAccountJsonConverter.convertToString(expenseAccount); + System.out.println(expenseAccountAsJson); + + Assert.assertNotNull(expenseAccountAsJson); + Assert.assertFalse(expenseAccountAsJson.isEmpty()); + // check contains well recursive wikitty values : employee -> person -> name + Assert.assertTrue(expenseAccountAsJson.contains("\"lastName\":\"Fox\"")); + // person name should occurs two times : one from expenseAccount#Employee and other from expenseAccount#expenseAccountEntry#Quotation#Supplier + Assert.assertEquals(2, StringUtils.countMatches(expenseAccountAsJson, "\"lastName\":\"Fox\"")); + } + } -- To stop receiving notification emails like this one, please contact chorem.org SCM administrator <admin+scm@chorem.org>.
participants (1)
-
chorem.org scm