@GET @Path("search") public Json search( @QueryParam("id") String id, @QueryParam("name") String searchString, @QueryParam("providers") String providers) { if (id != null && !id.isEmpty()) { return Json.array().add(searchUserById(id)); } Json resultList = Json.array(); final int maxResults = 15; try { if (searchString == null || searchString.length() == 0) return null; else searchString = searchString.trim(); Json user = Json.object(); String name = searchString; name = name.trim(); int idx; // Parse search string if ((idx = name.indexOf(',')) > -1) { // Miller, Bob user.set("LastName", name.substring(0, idx).trim()); user.set("FirstName", name.substring(idx + 1).trim()); } else if ((idx = name.indexOf(' ')) > -1) { // Bob Miller user.set("LastName", name.substring(idx + 1).trim()); user.set("FirstName", name.substring(0, idx).trim()); } else { // Miller user.set("LastName", name); } if (user.is("FirstName", "")) user.delAt("FirstName"); if (user.is("LastName", "")) user.delAt("LastName"); if (user.asJsonMap().size() > 0) { Collection<String> P = providers != null ? Arrays.asList(providers.split(",")) : orderedProviders(); for (String providerName : P) resultList.with(searchProvider(providerName, user, maxResults)); } } catch (Exception e) { e.printStackTrace(); return ko(e); } return prepareReturn(resultList); }
@SuppressWarnings("unchecked") public Object from(Json x) { if (!x.isObject()) return x.getValue(); Json typeName = x.at("javaType"); if ("mjson.Json".equals(typeName)) return x.at("value"); if (typeName == null) { // Not a bean, must be simply a map HashMap<String, Object> m = new HashMap<String, Object>(); for (Map.Entry<String, Json> e : x.asJsonMap().entrySet()) m.put(e.getKey(), from(e.getValue())); return m; } String fullName = shortNameMap.getX(typeName.asString()); if (fullName == null) fullName = typeName.asString(); if (fullName.equals(String.class.getName())) return x.at("value").getValue(); else if (fullName.equals(Boolean.class.getName())) return x.at("value").getValue(); try { Class<?> beanClass = HGUtils.loadClass(graph, fullName); if (Number.class.isAssignableFrom(beanClass)) return castNumber(beanClass, x.at("value")); // return beanClass.getConstructor(new // Class[]{String.class}).newInstance(x.at("value").getValue().toString()); else if (Boolean.class.isAssignableFrom(beanClass)) return x.at("value").asBoolean(); Object bean = null; x = x.at("value"); if (HGLink.class.isAssignableFrom(beanClass) && x.has("_link")) { HGHandle[] targets = new HGHandle[x.at("_link").asJsonList().size()]; for (int i = 0; i < targets.length; i++) targets[i] = value(x.at("_link").at(i)); bean = beanClass .getDeclaredConstructor(new Class[] {HGHandle[].class}) .newInstance(new Object[] {targets}); } else bean = beanClass.newInstance(); if (x.has("_collection")) for (Json element : x.at("_collection").asJsonList()) ((Collection<Object>) bean).add(value(element)); if (x.has("_map")) for (Json entry : x.at("_map").asJsonList()) ((Map<Object, Object>) bean).put(value(entry.at("key")), value(entry.at("value"))); for (Map.Entry<String, Json> entry : x.asJsonMap().entrySet()) { PropertyDescriptor descriptor = BonesOfBeans.getPropertyDescriptor(bean, entry.getKey()); if (descriptor == null) continue; Class<?> propertyClass = descriptor.getPropertyType(); Object value = null; if (descriptor instanceof IndexedPropertyDescriptor) { int length = entry.getValue().asJsonList().size(); Object A = Array.newInstance( ((IndexedPropertyDescriptor) descriptor).getIndexedPropertyType(), length); for (int i = 0; i < length; i++) Array.set(A, i, from(entry.getValue().at(i))); } else if (propertyClass.equals(Class.class)) { if (entry.getValue().isString()) value = HGUtils.loadClass(graph, entry.getValue().asString()); else if (!entry.getValue().isNull()) value = HGUtils.loadClass(graph, entry.getValue().at("value").asString()); } else if (entry.getValue().isNumber()) value = castNumber(propertyClass, entry.getValue()); else value = value(entry.getValue()); BonesOfBeans.setProperty(bean, entry.getKey(), value); } return bean; } catch (Exception ex) { throw new RuntimeException("Failed to JSON-deserialize bean of type " + fullName, ex); } }