@Test public void testBuildJpaQueryString4() { final UserCriteria user = new UserCriteria(); final SystemCodes sc = new SystemCodes("STATUS","ACTIVE","Active"); user.setStatus(sc); Assert.assertEquals( " where obj.status.key = 'ACTIVE' and obj.credential.enabled = true", CrudUtil.buildJpaQueryString(user, false)); }
@SuppressWarnings("rawtypes") public static String buildJpaQueryString(final BaseEntity example, final boolean exactMatch) { int count = 0; final StringBuilder clause = new StringBuilder(" where "); final List<String> exampleFields = CacheUtil .getSearchableFields(example); for (String property : exampleFields) { final Object ret = retrieveObjectValue(example, property); property = "obj." + property; if (ret != null) { if (String.class.isAssignableFrom(ret.getClass()) && !exactMatch) { if (ret.toString().trim().length() > 0) { if (count > 0) { clause.append(" and "); } clause.append(property) .append(" like '%") .append(StringUtil.escapeSql(ret.toString(), true)).append("%'") .append(" escape '\\'"); count++; } } else if (SystemCodes.class.isAssignableFrom(ret.getClass())) { final SystemCodes sc = (SystemCodes) ret; if (!StringUtil.isEmpty(sc.getKey())) { if (count > 0) { clause.append(" and "); } clause.append(property).append(".key").append(" = '") .append(sc.getKey() + "'"); count++; } } else if (BaseEntity.class.isAssignableFrom(ret.getClass())) { final BaseEntity be = (BaseEntity) ret; if (be.getId() != null) { if (count > 0) { clause.append(" and "); } clause.append(property).append(".id").append(" = ") .append(be.getId()); count++; } } else if (Integer.class.isAssignableFrom(ret.getClass()) || Float.class.isAssignableFrom(ret.getClass()) || Long.class.isAssignableFrom(ret.getClass()) || Double.class.isAssignableFrom(ret.getClass()) || BigDecimal.class.isAssignableFrom(ret.getClass()) || Boolean.class.isAssignableFrom(ret.getClass())) { if (ret.toString().trim().length() > 0) { if (count > 0) { clause.append(" and "); } clause.append(property).append(" = ") .append(ret.toString()); count++; } } else if (Class.class.isAssignableFrom(ret.getClass())) { if (count > 0) { clause.append(" and "); } final Class clazz = (Class) ret; clause.append(property).append(" = '") .append(clazz.getName()).append("'"); count++; } else if (Collection.class.isAssignableFrom(ret.getClass())) { _log.warn("FindByExample on type Collection is not supported."); } else if (ret.toString().trim().length() > 0) { if (count > 0) { clause.append(" and "); } clause.append(property) .append(" = '") .append(StringUtil.escapeSql(ret.toString(), false)) .append("'"); count++; } } } if (count > 0) { return clause.toString(); } else { return ""; } }
CrudUtil { @SuppressWarnings("rawtypes") public static String buildJpaQueryString(final BaseEntity example, final boolean exactMatch) { int count = 0; final StringBuilder clause = new StringBuilder(" where "); final List<String> exampleFields = CacheUtil .getSearchableFields(example); for (String property : exampleFields) { final Object ret = retrieveObjectValue(example, property); property = "obj." + property; if (ret != null) { if (String.class.isAssignableFrom(ret.getClass()) && !exactMatch) { if (ret.toString().trim().length() > 0) { if (count > 0) { clause.append(" and "); } clause.append(property) .append(" like '%") .append(StringUtil.escapeSql(ret.toString(), true)).append("%'") .append(" escape '\\'"); count++; } } else if (SystemCodes.class.isAssignableFrom(ret.getClass())) { final SystemCodes sc = (SystemCodes) ret; if (!StringUtil.isEmpty(sc.getKey())) { if (count > 0) { clause.append(" and "); } clause.append(property).append(".key").append(" = '") .append(sc.getKey() + "'"); count++; } } else if (BaseEntity.class.isAssignableFrom(ret.getClass())) { final BaseEntity be = (BaseEntity) ret; if (be.getId() != null) { if (count > 0) { clause.append(" and "); } clause.append(property).append(".id").append(" = ") .append(be.getId()); count++; } } else if (Integer.class.isAssignableFrom(ret.getClass()) || Float.class.isAssignableFrom(ret.getClass()) || Long.class.isAssignableFrom(ret.getClass()) || Double.class.isAssignableFrom(ret.getClass()) || BigDecimal.class.isAssignableFrom(ret.getClass()) || Boolean.class.isAssignableFrom(ret.getClass())) { if (ret.toString().trim().length() > 0) { if (count > 0) { clause.append(" and "); } clause.append(property).append(" = ") .append(ret.toString()); count++; } } else if (Class.class.isAssignableFrom(ret.getClass())) { if (count > 0) { clause.append(" and "); } final Class clazz = (Class) ret; clause.append(property).append(" = '") .append(clazz.getName()).append("'"); count++; } else if (Collection.class.isAssignableFrom(ret.getClass())) { _log.warn("FindByExample on type Collection is not supported."); } else if (ret.toString().trim().length() > 0) { if (count > 0) { clause.append(" and "); } clause.append(property) .append(" = '") .append(StringUtil.escapeSql(ret.toString(), false)) .append("'"); count++; } } } if (count > 0) { return clause.toString(); } else { return ""; } } }
CrudUtil { @SuppressWarnings("rawtypes") public static String buildJpaQueryString(final BaseEntity example, final boolean exactMatch) { int count = 0; final StringBuilder clause = new StringBuilder(" where "); final List<String> exampleFields = CacheUtil .getSearchableFields(example); for (String property : exampleFields) { final Object ret = retrieveObjectValue(example, property); property = "obj." + property; if (ret != null) { if (String.class.isAssignableFrom(ret.getClass()) && !exactMatch) { if (ret.toString().trim().length() > 0) { if (count > 0) { clause.append(" and "); } clause.append(property) .append(" like '%") .append(StringUtil.escapeSql(ret.toString(), true)).append("%'") .append(" escape '\\'"); count++; } } else if (SystemCodes.class.isAssignableFrom(ret.getClass())) { final SystemCodes sc = (SystemCodes) ret; if (!StringUtil.isEmpty(sc.getKey())) { if (count > 0) { clause.append(" and "); } clause.append(property).append(".key").append(" = '") .append(sc.getKey() + "'"); count++; } } else if (BaseEntity.class.isAssignableFrom(ret.getClass())) { final BaseEntity be = (BaseEntity) ret; if (be.getId() != null) { if (count > 0) { clause.append(" and "); } clause.append(property).append(".id").append(" = ") .append(be.getId()); count++; } } else if (Integer.class.isAssignableFrom(ret.getClass()) || Float.class.isAssignableFrom(ret.getClass()) || Long.class.isAssignableFrom(ret.getClass()) || Double.class.isAssignableFrom(ret.getClass()) || BigDecimal.class.isAssignableFrom(ret.getClass()) || Boolean.class.isAssignableFrom(ret.getClass())) { if (ret.toString().trim().length() > 0) { if (count > 0) { clause.append(" and "); } clause.append(property).append(" = ") .append(ret.toString()); count++; } } else if (Class.class.isAssignableFrom(ret.getClass())) { if (count > 0) { clause.append(" and "); } final Class clazz = (Class) ret; clause.append(property).append(" = '") .append(clazz.getName()).append("'"); count++; } else if (Collection.class.isAssignableFrom(ret.getClass())) { _log.warn("FindByExample on type Collection is not supported."); } else if (ret.toString().trim().length() > 0) { if (count > 0) { clause.append(" and "); } clause.append(property) .append(" = '") .append(StringUtil.escapeSql(ret.toString(), false)) .append("'"); count++; } } } if (count > 0) { return clause.toString(); } else { return ""; } } private  CrudUtil(); }
CrudUtil { @SuppressWarnings("rawtypes") public static String buildJpaQueryString(final BaseEntity example, final boolean exactMatch) { int count = 0; final StringBuilder clause = new StringBuilder(" where "); final List<String> exampleFields = CacheUtil .getSearchableFields(example); for (String property : exampleFields) { final Object ret = retrieveObjectValue(example, property); property = "obj." + property; if (ret != null) { if (String.class.isAssignableFrom(ret.getClass()) && !exactMatch) { if (ret.toString().trim().length() > 0) { if (count > 0) { clause.append(" and "); } clause.append(property) .append(" like '%") .append(StringUtil.escapeSql(ret.toString(), true)).append("%'") .append(" escape '\\'"); count++; } } else if (SystemCodes.class.isAssignableFrom(ret.getClass())) { final SystemCodes sc = (SystemCodes) ret; if (!StringUtil.isEmpty(sc.getKey())) { if (count > 0) { clause.append(" and "); } clause.append(property).append(".key").append(" = '") .append(sc.getKey() + "'"); count++; } } else if (BaseEntity.class.isAssignableFrom(ret.getClass())) { final BaseEntity be = (BaseEntity) ret; if (be.getId() != null) { if (count > 0) { clause.append(" and "); } clause.append(property).append(".id").append(" = ") .append(be.getId()); count++; } } else if (Integer.class.isAssignableFrom(ret.getClass()) || Float.class.isAssignableFrom(ret.getClass()) || Long.class.isAssignableFrom(ret.getClass()) || Double.class.isAssignableFrom(ret.getClass()) || BigDecimal.class.isAssignableFrom(ret.getClass()) || Boolean.class.isAssignableFrom(ret.getClass())) { if (ret.toString().trim().length() > 0) { if (count > 0) { clause.append(" and "); } clause.append(property).append(" = ") .append(ret.toString()); count++; } } else if (Class.class.isAssignableFrom(ret.getClass())) { if (count > 0) { clause.append(" and "); } final Class clazz = (Class) ret; clause.append(property).append(" = '") .append(clazz.getName()).append("'"); count++; } else if (Collection.class.isAssignableFrom(ret.getClass())) { _log.warn("FindByExample on type Collection is not supported."); } else if (ret.toString().trim().length() > 0) { if (count > 0) { clause.append(" and "); } clause.append(property) .append(" = '") .append(StringUtil.escapeSql(ret.toString(), false)) .append("'"); count++; } } } if (count > 0) { return clause.toString(); } else { return ""; } } private  CrudUtil(); static String buildCreateMessage(final BaseEntity obj); static List<String> getUpdatedFields(final BaseEntity oldObject,
			final BaseEntity newObject); static String convertAuditableFieldsToJson(BaseEntity obj); @SuppressWarnings({ "rawtypes", "unchecked" }) static String buildUpdateMessage(final BaseEntity oldObject,
			final BaseEntity newObject); static String buildDeleteMessage(final BaseEntity obj); static Map<String, Object> buildMapValues(final BaseEntity object); @SuppressWarnings("rawtypes") static Map<String, Object> buildMapValues(final BaseEntity object,
			final List<String> fields); static String buildURLParameters(final BaseEntity object); @SuppressWarnings("rawtypes") static String buildJpaQueryString(final BaseEntity example,
			final boolean exactMatch); @SuppressWarnings("rawtypes") static Object retrieveObjectValue(final Object obj,
			final String property); static Object retrieveNullableObjectValue(final Object obj,
			final String property); static Class<?> retrieveObjectType(final Object obj,
			final String property); static Boolean evaluateExpression(final Object obj,
			final String expression); @SuppressWarnings("unchecked") static String replaceSQLParameters(String sql, final Object obj); @SuppressWarnings({ "rawtypes" }) static List<Field> getAllFields(final Class clazz); @SuppressWarnings({ "rawtypes" }) static List<Field> getAllFields(final Class clazz,
			final boolean includeParent); @SuppressWarnings({ "rawtypes" }) static List<Method> getAllMethods(final Class clazz); @SuppressWarnings({ "rawtypes" }) static List<Method> getAllMethods(final Class clazz,
			final boolean includeParent); static List<MessageResponse> convertErrorMessage(
			final BindingResult bindingResult, final Locale locale,
			final MessageSource messageSource); static List<MessageResponse> buildSuccessMessage(
			final BaseEntity object, final String code, final Locale locale,
			final MessageSource messageSource); static boolean isCollection(final Object ob); static BaseEntity clone(final BaseEntity source); }
CrudUtil { @SuppressWarnings("rawtypes") public static String buildJpaQueryString(final BaseEntity example, final boolean exactMatch) { int count = 0; final StringBuilder clause = new StringBuilder(" where "); final List<String> exampleFields = CacheUtil .getSearchableFields(example); for (String property : exampleFields) { final Object ret = retrieveObjectValue(example, property); property = "obj." + property; if (ret != null) { if (String.class.isAssignableFrom(ret.getClass()) && !exactMatch) { if (ret.toString().trim().length() > 0) { if (count > 0) { clause.append(" and "); } clause.append(property) .append(" like '%") .append(StringUtil.escapeSql(ret.toString(), true)).append("%'") .append(" escape '\\'"); count++; } } else if (SystemCodes.class.isAssignableFrom(ret.getClass())) { final SystemCodes sc = (SystemCodes) ret; if (!StringUtil.isEmpty(sc.getKey())) { if (count > 0) { clause.append(" and "); } clause.append(property).append(".key").append(" = '") .append(sc.getKey() + "'"); count++; } } else if (BaseEntity.class.isAssignableFrom(ret.getClass())) { final BaseEntity be = (BaseEntity) ret; if (be.getId() != null) { if (count > 0) { clause.append(" and "); } clause.append(property).append(".id").append(" = ") .append(be.getId()); count++; } } else if (Integer.class.isAssignableFrom(ret.getClass()) || Float.class.isAssignableFrom(ret.getClass()) || Long.class.isAssignableFrom(ret.getClass()) || Double.class.isAssignableFrom(ret.getClass()) || BigDecimal.class.isAssignableFrom(ret.getClass()) || Boolean.class.isAssignableFrom(ret.getClass())) { if (ret.toString().trim().length() > 0) { if (count > 0) { clause.append(" and "); } clause.append(property).append(" = ") .append(ret.toString()); count++; } } else if (Class.class.isAssignableFrom(ret.getClass())) { if (count > 0) { clause.append(" and "); } final Class clazz = (Class) ret; clause.append(property).append(" = '") .append(clazz.getName()).append("'"); count++; } else if (Collection.class.isAssignableFrom(ret.getClass())) { _log.warn("FindByExample on type Collection is not supported."); } else if (ret.toString().trim().length() > 0) { if (count > 0) { clause.append(" and "); } clause.append(property) .append(" = '") .append(StringUtil.escapeSql(ret.toString(), false)) .append("'"); count++; } } } if (count > 0) { return clause.toString(); } else { return ""; } } private  CrudUtil(); static String buildCreateMessage(final BaseEntity obj); static List<String> getUpdatedFields(final BaseEntity oldObject,
			final BaseEntity newObject); static String convertAuditableFieldsToJson(BaseEntity obj); @SuppressWarnings({ "rawtypes", "unchecked" }) static String buildUpdateMessage(final BaseEntity oldObject,
			final BaseEntity newObject); static String buildDeleteMessage(final BaseEntity obj); static Map<String, Object> buildMapValues(final BaseEntity object); @SuppressWarnings("rawtypes") static Map<String, Object> buildMapValues(final BaseEntity object,
			final List<String> fields); static String buildURLParameters(final BaseEntity object); @SuppressWarnings("rawtypes") static String buildJpaQueryString(final BaseEntity example,
			final boolean exactMatch); @SuppressWarnings("rawtypes") static Object retrieveObjectValue(final Object obj,
			final String property); static Object retrieveNullableObjectValue(final Object obj,
			final String property); static Class<?> retrieveObjectType(final Object obj,
			final String property); static Boolean evaluateExpression(final Object obj,
			final String expression); @SuppressWarnings("unchecked") static String replaceSQLParameters(String sql, final Object obj); @SuppressWarnings({ "rawtypes" }) static List<Field> getAllFields(final Class clazz); @SuppressWarnings({ "rawtypes" }) static List<Field> getAllFields(final Class clazz,
			final boolean includeParent); @SuppressWarnings({ "rawtypes" }) static List<Method> getAllMethods(final Class clazz); @SuppressWarnings({ "rawtypes" }) static List<Method> getAllMethods(final Class clazz,
			final boolean includeParent); static List<MessageResponse> convertErrorMessage(
			final BindingResult bindingResult, final Locale locale,
			final MessageSource messageSource); static List<MessageResponse> buildSuccessMessage(
			final BaseEntity object, final String code, final Locale locale,
			final MessageSource messageSource); static boolean isCollection(final Object ob); static BaseEntity clone(final BaseEntity source);  }