branch jsonize_wikitty updated (7a81dca -> 3f01d6a)
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 from 7a81dca add GSON Adapter for Wikitty BusinessEntity new c548f7f add GSON Adapter for Wikitty BusinessEntity new 0d78c0c Merge branch 'jsonize_wikitty' of https://git.chorem.org/chorem into jsonize_wikitty new 3f01d6a complete Adapter for Wikitty BusinessEntity The 3 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 3f01d6ae5dc6b6db554973298cdaba08be8f68a3 Author: Yannick Martel <yannick.martel@gmail.com> Date: Fri Mar 27 17:04:09 2015 +0100 complete Adapter for Wikitty BusinessEntity commit 0d78c0c1b9d968ca2c7c4b7622392225e1ecf70b Merge: c548f7f 7a81dca Author: Yannick Martel <yannick.martel@gmail.com> Date: Thu Mar 26 17:29:02 2015 +0100 Merge branch 'jsonize_wikitty' of https://git.chorem.org/chorem into jsonize_wikitty commit c548f7fb18225ca47206598c0c34ca44dda40bd1 Author: Yannick Martel <yannick.martel@gmail.com> Date: Thu Mar 26 12:16:23 2015 +0100 add GSON Adapter for Wikitty BusinessEntity Summary of changes: .../src/main/java/org/chorem/webmotion/injector/InjectorListener.java | 4 +++- .../test/java/org/chorem/webmotion/converters/JsonConverterTest.java | 1 - 2 files changed, 3 insertions(+), 2 deletions(-) -- 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 c548f7fb18225ca47206598c0c34ca44dda40bd1 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>.
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 0d78c0c1b9d968ca2c7c4b7622392225e1ecf70b Merge: c548f7f 7a81dca Author: Yannick Martel <yannick.martel@gmail.com> Date: Thu Mar 26 17:29:02 2015 +0100 Merge branch 'jsonize_wikitty' of https://git.chorem.org/chorem into jsonize_wikitty -- 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 3f01d6ae5dc6b6db554973298cdaba08be8f68a3 Author: Yannick Martel <yannick.martel@gmail.com> Date: Fri Mar 27 17:04:09 2015 +0100 complete Adapter for Wikitty BusinessEntity --- .../src/main/java/org/chorem/webmotion/injector/InjectorListener.java | 4 +++- .../test/java/org/chorem/webmotion/converters/JsonConverterTest.java | 1 - 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/chorem-webmotion/src/main/java/org/chorem/webmotion/injector/InjectorListener.java b/chorem-webmotion/src/main/java/org/chorem/webmotion/injector/InjectorListener.java index 6c5cd5d..73c87c8 100644 --- a/chorem-webmotion/src/main/java/org/chorem/webmotion/injector/InjectorListener.java +++ b/chorem-webmotion/src/main/java/org/chorem/webmotion/injector/InjectorListener.java @@ -42,6 +42,7 @@ import org.debux.webmotion.server.call.ServerContext; import org.debux.webmotion.server.handler.ExecutorParametersInjectorHandler.Injector; import org.debux.webmotion.server.mapping.Mapping; import org.nuiton.wikitty.WikittyUtil; +import org.nuiton.wikitty.entities.BusinessEntity; /** * @author ymartel <martel@codelutin.com> @@ -51,7 +52,8 @@ public class InjectorListener implements WebMotionServerListener { protected static final Set<Class<?>> BEAN_TYPES = Sets.<Class<?>>newHashSet( ExpenseAccountBean.class, ExpenseAccountEntryBean.class, - PaginatedResult.class + PaginatedResult.class, + BusinessEntity.class ); @Override 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 57ecf45..009eec1 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 @@ -135,7 +135,6 @@ public class JsonConverterTest { JsonConverter<ExpenseAccount> expenseAccountJsonConverter = JsonConverter.newConverter(ExpenseAccount.class); String expenseAccountAsJson = expenseAccountJsonConverter.convertToString(expenseAccount); - System.out.println(expenseAccountAsJson); Assert.assertNotNull(expenseAccountAsJson); Assert.assertFalse(expenseAccountAsJson.isEmpty()); -- To stop receiving notification emails like this one, please contact chorem.org SCM administrator <admin+scm@chorem.org>.
participants (1)
-
chorem.org scm