/datastore/src/main/java/org/jboss/capedwarf/datastore/query/PropertyMapBridge.java

https://github.com/luksa/capedwarf-blue · Java · 244 lines · 187 code · 26 blank · 31 comment · 48 complexity · b9cec4ed9ce2c3cbd5620b91b4de5a3c MD5 · raw file

  1. /*
  2. *
  3. * * JBoss, Home of Professional Open Source.
  4. * * Copyright 2011, Red Hat, Inc., and individual contributors
  5. * * as indicated by the @author tags. See the copyright.txt file in the
  6. * * distribution for a full listing of individual contributors.
  7. * *
  8. * * This is free software; you can redistribute it and/or modify it
  9. * * under the terms of the GNU Lesser General Public License as
  10. * * published by the Free Software Foundation; either version 2.1 of
  11. * * the License, or (at your option) any later version.
  12. * *
  13. * * This software is distributed in the hope that it will be useful,
  14. * * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * * Lesser General Public License for more details.
  17. * *
  18. * * You should have received a copy of the GNU Lesser General Public
  19. * * License along with this software; if not, write to the Free
  20. * * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  21. * * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  22. *
  23. */
  24. package org.jboss.capedwarf.datastore.query;
  25. import com.google.appengine.api.blobstore.BlobKey;
  26. import com.google.appengine.api.datastore.Blob;
  27. import com.google.appengine.api.datastore.Category;
  28. import com.google.appengine.api.datastore.Email;
  29. import com.google.appengine.api.datastore.GeoPt;
  30. import com.google.appengine.api.datastore.IMHandle;
  31. import com.google.appengine.api.datastore.Key;
  32. import com.google.appengine.api.datastore.Link;
  33. import com.google.appengine.api.datastore.PhoneNumber;
  34. import com.google.appengine.api.datastore.PostalAddress;
  35. import com.google.appengine.api.datastore.Rating;
  36. import com.google.appengine.api.datastore.ShortBlob;
  37. import com.google.appengine.api.datastore.Text;
  38. import com.google.appengine.api.users.User;
  39. import org.apache.lucene.document.Document;
  40. import org.hibernate.search.bridge.FieldBridge;
  41. import org.hibernate.search.bridge.LuceneOptions;
  42. import org.hibernate.search.bridge.TwoWayFieldBridge;
  43. import org.hibernate.search.bridge.impl.BridgeFactory;
  44. import java.util.Date;
  45. import java.util.Map;
  46. /**
  47. * @author <a href="mailto:marko.luksa@gmail.com">Marko Luksa</a>
  48. */
  49. public class PropertyMapBridge implements FieldBridge {
  50. public static final TwoWayFieldBridge TEXT_BRIDGE = new TextBridge();
  51. public static final TwoWayFieldBridge PHONE_NUMBER_BRIDGE = new PhoneNumberBridge();
  52. public static final TwoWayFieldBridge POSTAL_ADDRESS_BRIDGE = new PostalAddressBridge();
  53. public static final TwoWayFieldBridge EMAIL_BRIDGE = new EmailBridge();
  54. public static final TwoWayFieldBridge USER_BRIDGE = new UserBridge();
  55. public static final TwoWayFieldBridge LINK_BRIDGE = new LinkBridge();
  56. public static final TwoWayFieldBridge KEY_BRIDGE = new KeyBridge();
  57. public static final TwoWayFieldBridge RATING_BRIDGE = new RatingBridge();
  58. public static final TwoWayFieldBridge GEO_PT_BRIDGE = new GeoPtBridge();
  59. public static final TwoWayFieldBridge CATEGORY_BRIDGE = new CategoryBridge();
  60. public static final TwoWayFieldBridge IM_HANDLE_BRIDGE = new IMHandleBridge();
  61. public static final TwoWayFieldBridge BLOB_KEY_BRIDGE = new BlobKeyBridge();
  62. public static final TwoWayFieldBridge BLOB_BRIDGE = new BlobBridge();
  63. public static final TwoWayFieldBridge SHORT_BLOB_BRIDGE = new ShortBlobBridge();
  64. public void set(String name, Object value, Document document, LuceneOptions luceneOptions) {
  65. Map<String, ?> entityProperties = (Map<String, ?>) value;
  66. for (Map.Entry<String, ?> entry : entityProperties.entrySet()) {
  67. luceneOptions.addFieldToDocument(entry.getKey(), convertToString(entry.getValue()), document);
  68. }
  69. }
  70. public String convertToString(Object value) {
  71. if (value instanceof String) {
  72. return String.valueOf(value);
  73. // return BridgeFactory.STRING.objectToString(value);
  74. } else if (value instanceof Boolean) {
  75. return BridgeFactory.BOOLEAN.objectToString(value);
  76. } else if (value instanceof Integer) {
  77. return BridgeFactory.INTEGER.objectToString(value);
  78. } else if (value instanceof Byte) {
  79. return BridgeFactory.INTEGER.objectToString(value);
  80. } else if (value instanceof Short) {
  81. return BridgeFactory.INTEGER.objectToString(value);
  82. } else if (value instanceof Long) {
  83. return BridgeFactory.LONG.objectToString(value);
  84. } else if (value instanceof Float) {
  85. return BridgeFactory.FLOAT.objectToString(value);
  86. } else if (value instanceof Double) {
  87. return BridgeFactory.DOUBLE.objectToString(value);
  88. } else if (value instanceof Date) {
  89. return BridgeFactory.DATE_MILLISECOND.objectToString(value);
  90. } else if (value instanceof Text) {
  91. return TEXT_BRIDGE.objectToString(value);
  92. } else if (value instanceof PhoneNumber) {
  93. return PHONE_NUMBER_BRIDGE.objectToString(value);
  94. } else if (value instanceof PostalAddress) {
  95. return POSTAL_ADDRESS_BRIDGE.objectToString(value);
  96. } else if (value instanceof Email) {
  97. return EMAIL_BRIDGE.objectToString(value);
  98. } else if (value instanceof User) {
  99. return USER_BRIDGE.objectToString(value);
  100. } else if (value instanceof Link) {
  101. return LINK_BRIDGE.objectToString(value);
  102. } else if (value instanceof Key) {
  103. return KEY_BRIDGE.objectToString(value);
  104. } else if (value instanceof Rating) {
  105. return RATING_BRIDGE.objectToString(value);
  106. } else if (value instanceof GeoPt) {
  107. return GEO_PT_BRIDGE.objectToString(value);
  108. } else if (value instanceof Category) {
  109. return CATEGORY_BRIDGE.objectToString(value);
  110. } else if (value instanceof IMHandle) {
  111. return IM_HANDLE_BRIDGE.objectToString(value);
  112. } else if (value instanceof BlobKey) {
  113. return BLOB_KEY_BRIDGE.objectToString(value);
  114. } else if (value instanceof Blob) {
  115. return BLOB_BRIDGE.objectToString(value);
  116. } else if (value instanceof ShortBlob) {
  117. return SHORT_BLOB_BRIDGE.objectToString(value);
  118. }
  119. throw new IllegalArgumentException("Cannot convert value to string. Value was " + value);
  120. }
  121. private static class TextBridge extends ObjectToStringFieldBridge {
  122. public String objectToString(Object object) {
  123. return ((Text) object).getValue();
  124. }
  125. }
  126. private static class PhoneNumberBridge extends ObjectToStringFieldBridge {
  127. public String objectToString(Object object) {
  128. return ((PhoneNumber) object).getNumber();
  129. }
  130. }
  131. private static class PostalAddressBridge extends ObjectToStringFieldBridge {
  132. public String objectToString(Object object) {
  133. return ((PostalAddress) object).getAddress();
  134. }
  135. }
  136. private static class EmailBridge extends ObjectToStringFieldBridge {
  137. public String objectToString(Object object) {
  138. return ((Email) object).getEmail();
  139. }
  140. }
  141. private static class UserBridge extends ObjectToStringFieldBridge {
  142. public String objectToString(Object object) {
  143. return ((User) object).getEmail(); // TODO: add other properties
  144. }
  145. }
  146. private static class LinkBridge extends ObjectToStringFieldBridge {
  147. public String objectToString(Object object) {
  148. return ((Link) object).getValue();
  149. }
  150. }
  151. private static class KeyBridge extends ObjectToStringFieldBridge {
  152. public String objectToString(Object object) {
  153. return ((Key) object).getKind(); // TODO: add other properties
  154. }
  155. }
  156. private static class RatingBridge extends ObjectToStringFieldBridge {
  157. public String objectToString(Object object) {
  158. return BridgeFactory.INTEGER.objectToString(((Rating) object).getRating());
  159. }
  160. }
  161. private static class GeoPtBridge extends ObjectToStringFieldBridge {
  162. public String objectToString(Object object) {
  163. return BridgeFactory.FLOAT.objectToString(((GeoPt) object).getLatitude())
  164. + ";"
  165. + BridgeFactory.FLOAT.objectToString(((GeoPt) object).getLongitude());
  166. }
  167. }
  168. private static class CategoryBridge extends ObjectToStringFieldBridge {
  169. public String objectToString(Object object) {
  170. return ((Category) object).getCategory();
  171. }
  172. }
  173. private static class IMHandleBridge extends ObjectToStringFieldBridge {
  174. public String objectToString(Object object) {
  175. return ((IMHandle) object).getProtocol()
  176. + ";"
  177. + ((IMHandle) object).getAddress();
  178. }
  179. }
  180. private static class BlobKeyBridge extends ObjectToStringFieldBridge {
  181. public String objectToString(Object object) {
  182. return ((BlobKey) object).getKeyString();
  183. }
  184. }
  185. private static class BlobBridge extends ObjectToStringFieldBridge {
  186. public String objectToString(Object object) {
  187. byte[] bytes = ((Blob) object).getBytes();
  188. return bytesToString(bytes);
  189. }
  190. }
  191. private static class ShortBlobBridge extends ObjectToStringFieldBridge {
  192. public String objectToString(Object object) {
  193. byte[] bytes = ((ShortBlob) object).getBytes();
  194. return bytesToString(bytes);
  195. }
  196. }
  197. private static String bytesToString(byte bytes[]) {
  198. // TODO: This impl is temporary. Find better one.
  199. StringBuilder sbuf = new StringBuilder();
  200. for (byte aByte : bytes) {
  201. String hex = Integer.toString(aByte, 16);
  202. String twoCharHex = (hex.length() == 1 ? "0" : "") + hex;
  203. sbuf.append(twoCharHex);
  204. }
  205. return sbuf.toString();
  206. }
  207. /**
  208. *
  209. */
  210. public abstract static class ObjectToStringFieldBridge implements TwoWayFieldBridge {
  211. public Object get(String name, Document document) {
  212. throw new UnsupportedOperationException("should not be called");
  213. }
  214. public void set(String name, Object value, Document document, LuceneOptions luceneOptions) {
  215. throw new UnsupportedOperationException("should not be called");
  216. }
  217. }
  218. }