@Test public void testReplaceSQLParameters() { final UserCriteria user = new UserCriteria(); final UserCredential cred = new UserCredential(); user.setFirstName("Test"); user.setEmailAddress("admin@ideyatech.com"); cred.setUsername("testname"); cred.setPassword("password"); cred.setId(123l); cred.setEnabled(true); user.setCredential(cred); Assert.assertEquals("firstName='Test' and credential.id=123", CrudUtil.replaceSQLParameters("firstName=:firstName and credential.id=:credential.id", user)); }
@SuppressWarnings("unchecked") public static String replaceSQLParameters(String sql, final Object obj) { final Matcher sqlMatcher = CrudUtil.SQL_PARAM_PATTERN.matcher(sql); while (sqlMatcher.find()) { final String param = sqlMatcher.group(1); final Object valueObject = CrudUtil.retrieveNullableObjectValue( obj, param); if (valueObject == null) { sql = sql.replace(sqlMatcher.group(), "null"); } else if (String.class.isAssignableFrom(valueObject.getClass())) { sql = sql.replace(sqlMatcher.group(), "'" + valueObject.toString() + "'"); } else if (Collection.class .isAssignableFrom(valueObject.getClass())) { final Collection<Object> list = (Collection<Object>) valueObject; int ctr = 0; final StringBuilder buff = new StringBuilder(); for (final Object item : list) { if (ctr++ > 0) { buff.append(", "); } if (SystemCodes.class.isAssignableFrom(item.getClass())) { final SystemCodes entity = (SystemCodes) item; buff.append("'").append(entity.getKey()).append("'"); } else if (BaseEntity.class.isAssignableFrom(item .getClass())) { final BaseEntity entity = (BaseEntity) item; buff.append(entity.getId()); } else { buff.append("'").append(item.toString()).append("'"); } } sql = sql.replace(sqlMatcher.group(), buff.toString()); } else { sql = sql.replace(sqlMatcher.group(), valueObject.toString()); } } return sql; }
CrudUtil { @SuppressWarnings("unchecked") public static String replaceSQLParameters(String sql, final Object obj) { final Matcher sqlMatcher = CrudUtil.SQL_PARAM_PATTERN.matcher(sql); while (sqlMatcher.find()) { final String param = sqlMatcher.group(1); final Object valueObject = CrudUtil.retrieveNullableObjectValue( obj, param); if (valueObject == null) { sql = sql.replace(sqlMatcher.group(), "null"); } else if (String.class.isAssignableFrom(valueObject.getClass())) { sql = sql.replace(sqlMatcher.group(), "'" + valueObject.toString() + "'"); } else if (Collection.class .isAssignableFrom(valueObject.getClass())) { final Collection<Object> list = (Collection<Object>) valueObject; int ctr = 0; final StringBuilder buff = new StringBuilder(); for (final Object item : list) { if (ctr++ > 0) { buff.append(", "); } if (SystemCodes.class.isAssignableFrom(item.getClass())) { final SystemCodes entity = (SystemCodes) item; buff.append("'").append(entity.getKey()).append("'"); } else if (BaseEntity.class.isAssignableFrom(item .getClass())) { final BaseEntity entity = (BaseEntity) item; buff.append(entity.getId()); } else { buff.append("'").append(item.toString()).append("'"); } } sql = sql.replace(sqlMatcher.group(), buff.toString()); } else { sql = sql.replace(sqlMatcher.group(), valueObject.toString()); } } return sql; } }
CrudUtil { @SuppressWarnings("unchecked") public static String replaceSQLParameters(String sql, final Object obj) { final Matcher sqlMatcher = CrudUtil.SQL_PARAM_PATTERN.matcher(sql); while (sqlMatcher.find()) { final String param = sqlMatcher.group(1); final Object valueObject = CrudUtil.retrieveNullableObjectValue( obj, param); if (valueObject == null) { sql = sql.replace(sqlMatcher.group(), "null"); } else if (String.class.isAssignableFrom(valueObject.getClass())) { sql = sql.replace(sqlMatcher.group(), "'" + valueObject.toString() + "'"); } else if (Collection.class .isAssignableFrom(valueObject.getClass())) { final Collection<Object> list = (Collection<Object>) valueObject; int ctr = 0; final StringBuilder buff = new StringBuilder(); for (final Object item : list) { if (ctr++ > 0) { buff.append(", "); } if (SystemCodes.class.isAssignableFrom(item.getClass())) { final SystemCodes entity = (SystemCodes) item; buff.append("'").append(entity.getKey()).append("'"); } else if (BaseEntity.class.isAssignableFrom(item .getClass())) { final BaseEntity entity = (BaseEntity) item; buff.append(entity.getId()); } else { buff.append("'").append(item.toString()).append("'"); } } sql = sql.replace(sqlMatcher.group(), buff.toString()); } else { sql = sql.replace(sqlMatcher.group(), valueObject.toString()); } } return sql; } private  CrudUtil(); }
CrudUtil { @SuppressWarnings("unchecked") public static String replaceSQLParameters(String sql, final Object obj) { final Matcher sqlMatcher = CrudUtil.SQL_PARAM_PATTERN.matcher(sql); while (sqlMatcher.find()) { final String param = sqlMatcher.group(1); final Object valueObject = CrudUtil.retrieveNullableObjectValue( obj, param); if (valueObject == null) { sql = sql.replace(sqlMatcher.group(), "null"); } else if (String.class.isAssignableFrom(valueObject.getClass())) { sql = sql.replace(sqlMatcher.group(), "'" + valueObject.toString() + "'"); } else if (Collection.class .isAssignableFrom(valueObject.getClass())) { final Collection<Object> list = (Collection<Object>) valueObject; int ctr = 0; final StringBuilder buff = new StringBuilder(); for (final Object item : list) { if (ctr++ > 0) { buff.append(", "); } if (SystemCodes.class.isAssignableFrom(item.getClass())) { final SystemCodes entity = (SystemCodes) item; buff.append("'").append(entity.getKey()).append("'"); } else if (BaseEntity.class.isAssignableFrom(item .getClass())) { final BaseEntity entity = (BaseEntity) item; buff.append(entity.getId()); } else { buff.append("'").append(item.toString()).append("'"); } } sql = sql.replace(sqlMatcher.group(), buff.toString()); } else { sql = sql.replace(sqlMatcher.group(), valueObject.toString()); } } return sql; } 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("unchecked") public static String replaceSQLParameters(String sql, final Object obj) { final Matcher sqlMatcher = CrudUtil.SQL_PARAM_PATTERN.matcher(sql); while (sqlMatcher.find()) { final String param = sqlMatcher.group(1); final Object valueObject = CrudUtil.retrieveNullableObjectValue( obj, param); if (valueObject == null) { sql = sql.replace(sqlMatcher.group(), "null"); } else if (String.class.isAssignableFrom(valueObject.getClass())) { sql = sql.replace(sqlMatcher.group(), "'" + valueObject.toString() + "'"); } else if (Collection.class .isAssignableFrom(valueObject.getClass())) { final Collection<Object> list = (Collection<Object>) valueObject; int ctr = 0; final StringBuilder buff = new StringBuilder(); for (final Object item : list) { if (ctr++ > 0) { buff.append(", "); } if (SystemCodes.class.isAssignableFrom(item.getClass())) { final SystemCodes entity = (SystemCodes) item; buff.append("'").append(entity.getKey()).append("'"); } else if (BaseEntity.class.isAssignableFrom(item .getClass())) { final BaseEntity entity = (BaseEntity) item; buff.append(entity.getId()); } else { buff.append("'").append(item.toString()).append("'"); } } sql = sql.replace(sqlMatcher.group(), buff.toString()); } else { sql = sql.replace(sqlMatcher.group(), valueObject.toString()); } } return sql; } 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);  }