PageRenderTime 76ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/core/infinit.e.data_model/src/com/ikanow/infinit/e/data_model/index/BaseIndexPojo.java

https://github.com/IKANOW/Infinit.e
Java | 168 lines | 117 code | 8 blank | 43 comment | 9 complexity | 64d8f773bcfcccc879a7d6aeaa5fa3bc MD5 | raw file
Possible License(s): BSD-3-Clause
  1. /*******************************************************************************
  2. * Copyright 2012 The Infinit.e Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. ******************************************************************************/
  16. package com.ikanow.infinit.e.data_model.index;
  17. import java.lang.reflect.ParameterizedType;
  18. import java.lang.reflect.Type;
  19. import java.util.Collection;
  20. import java.util.Date;
  21. import org.bson.types.ObjectId;
  22. import com.google.gson.GsonBuilder;
  23. import com.google.gson.JsonElement;
  24. import com.google.gson.JsonPrimitive;
  25. import com.google.gson.JsonSerializationContext;
  26. import com.google.gson.JsonSerializer;
  27. import com.google.gson.reflect.TypeToken;
  28. public class BaseIndexPojo {
  29. // Override this function to perform custom serialization (see BasePojoIndexMap)
  30. public GsonBuilder extendBuilder(GsonBuilder gp) {
  31. return extendBuilder_internal(gp);
  32. }
  33. // Allows Index owner to enforce some custom serializations
  34. final public static GsonBuilder getDefaultBuilder() {
  35. return new GsonBuilder()
  36. .registerTypeAdapter(ObjectId.class, new ObjectIdSerializer())
  37. .registerTypeAdapter(Date.class, new DateSerializer());
  38. }
  39. private static GsonBuilder extendBuilder_internal(GsonBuilder gp) {
  40. return gp;
  41. }
  42. //________________________________________________________________________________________________
  43. // Won't normally override these (but can)
  44. // DB format conversion
  45. // For BaseIndexPojos
  46. // 2 versions, 1 where you need dynamic mapping (eg runtime-specific)
  47. // (in both cases for single objects, you can use the nicer class<X> or
  48. // the nastier Google TypeToken, the latter has 2 advantages:
  49. // a. consisntency with list types, where you have to use the TypeToken
  50. // b. the class<X> doesn't work where X is generic)
  51. //
  52. // Note - the code is a bit unpleastant in places....
  53. /////////////////////////////////////////////////////////////////////////////////////
  54. // To the Index JSON From a single Object
  55. public static<S extends BaseIndexPojo> String toIndex(S s) {
  56. return toIndex(s, null);
  57. }
  58. // (Nicer version of the static "toIndex")
  59. public String toIndex() {
  60. return toIndex(this);
  61. }
  62. public static<S extends BaseIndexPojo> String toIndex(S s, BasePojoIndexMap<S> dynamicMap) {
  63. GsonBuilder gb = s.extendBuilder(BaseIndexPojo.getDefaultBuilder());
  64. if (null != dynamicMap) {
  65. gb = dynamicMap.extendBuilder(gb);
  66. }
  67. return gb.create().toJson(s);
  68. }
  69. /////////////////////////////////////////////////////////////////////////////////////
  70. // From the Index JSON To a single Object
  71. public static<S extends BaseIndexPojo> S fromIndex(String s, Class<S> type) {
  72. return fromIndex(s, type, null);
  73. }
  74. public static<S extends BaseIndexPojo> S fromIndex(String s, TypeToken<S> type) {
  75. return fromIndex(s, type, null);
  76. }
  77. public static<S extends BaseIndexPojo> S fromIndex(String s, Class<S> type, BasePojoIndexMap<S> dynamicMap) {
  78. // Create a new instance of the class in order to override it
  79. GsonBuilder gb = null;
  80. try {
  81. gb = type.newInstance().extendBuilder(BaseIndexPojo.getDefaultBuilder());
  82. } catch (Exception e) {
  83. return null;
  84. }
  85. if (null != dynamicMap) {
  86. gb = dynamicMap.extendBuilder(gb);
  87. }
  88. return gb.create().fromJson(s.toString(), type);
  89. }
  90. @SuppressWarnings("unchecked")
  91. public static<S extends BaseIndexPojo> S fromIndex(String s, TypeToken<S> type, BasePojoIndexMap<S> dynamicMap) {
  92. GsonBuilder gb = null;
  93. try {
  94. Class<S> clazz = (Class<S>)type.getType();
  95. gb = ((S)clazz.newInstance()).extendBuilder(BaseIndexPojo.getDefaultBuilder());
  96. } catch (Exception e) {
  97. return null;
  98. }
  99. if (null != dynamicMap) {
  100. gb = dynamicMap.extendBuilder(gb);
  101. }
  102. return (S)gb.create().fromJson(s.toString(), type.getType());
  103. }
  104. /////////////////////////////////////////////////////////////////////////////////////
  105. // To the Index JSON From a list of objects
  106. public static <S extends BaseIndexPojo> String listToIndex(Collection<S> list, TypeToken<? extends Collection<S>> listType) {
  107. return listToIndex(list, listType, null);
  108. }
  109. public static <S extends BaseIndexPojo> String listToIndex(Collection<S> list, TypeToken<? extends Collection<S>> listType, BasePojoIndexMap<S> dynamicMap) {
  110. GsonBuilder gb = null;
  111. try {
  112. if (!list.isEmpty()) {
  113. gb = list.iterator().next().extendBuilder(BaseIndexPojo.getDefaultBuilder());
  114. }
  115. } catch (Exception e) {
  116. return null;
  117. }
  118. return gb.create().toJson(list, listType.getType());
  119. }
  120. /////////////////////////////////////////////////////////////////////////////////////
  121. // From the Index JSON to a list of objects
  122. public static <S extends BaseIndexPojo, L extends Collection<S>> L listFromIndex(String bson, TypeToken<? extends L> listType) {
  123. return listFromIndex(bson, listType, null);
  124. }
  125. @SuppressWarnings("unchecked")
  126. public static <S extends BaseIndexPojo, L extends Collection<S>> L listFromIndex(String bson, TypeToken<? extends L> listType, BasePojoIndexMap<S> dynamicMap) {
  127. GsonBuilder gb = null;
  128. try {
  129. Class<S> clazz = (Class<S>)((ParameterizedType)listType.getType()).getActualTypeArguments()[0];
  130. // (know this works because of construction of listType)
  131. gb = (clazz.newInstance()).extendBuilder(BaseIndexPojo.getDefaultBuilder());
  132. } catch (Exception e) {
  133. return null;
  134. }
  135. if (null != dynamicMap) {
  136. gb = dynamicMap.extendBuilder(gb);
  137. }
  138. return (L)gb.create().fromJson(bson, listType.getType());
  139. }
  140. //___________________________________________________________________
  141. // Default MongoDB serialization rule:
  142. // 1. Object Ids
  143. protected static class ObjectIdSerializer implements JsonSerializer<ObjectId>
  144. {
  145. @Override
  146. public JsonElement serialize(ObjectId id, Type typeOfT, JsonSerializationContext context)
  147. {
  148. return new JsonPrimitive(id.toStringMongod());
  149. }
  150. }
  151. // 2. Dates
  152. protected static class DateSerializer implements JsonSerializer<Date>
  153. {
  154. @Override
  155. public JsonElement serialize(Date date, Type typeOfT, JsonSerializationContext context)
  156. {
  157. return new JsonPrimitive(date.getTime());
  158. }
  159. }
  160. }