List of usage examples for java.sql Timestamp compareTo
public int compareTo(java.util.Date o)
From source file:ro.allevo.fintpws.test.TestUtils.java
/** * Method compareJSONObjects./*from w w w .j a v a 2 s .c o m*/ * * @param entity * Object * @param insertedEntity * JSONObject * @param readedEntity * JSONObject * @param String * fields excluded from comparison * @return boolean */ public static boolean compareJSONObjects(Object entity, JSONObject insertedEntity, JSONObject readedEntity, String fields) { String fieldName = ""; boolean isTransient = false; Field field = null; boolean haveAnnotation = false; final Method[] allMethods = entity.getClass().getDeclaredMethods(); boolean isEqual = true; for (Method classMethod : allMethods) { if (classMethod.getName().startsWith("set")) { isTransient = false; haveAnnotation = false; fieldName = classMethod.getName().substring(3).toLowerCase(); try { field = entity.getClass().getDeclaredField(fieldName); // Ex. Annotation : @javax.persistence.Column(name=, // unique=false, nullable=true, insertable=true, // updatable=true, columnDefinition=, table=, length=30, // precision=0, scale=0) Annotation[] annotations = field.getDeclaredAnnotations(); for (Annotation ant : annotations) { isTransient = ant.toString().contains("Transient"); haveAnnotation = true; } if (!fields.contains(fieldName) && (!isTransient || !haveAnnotation) && (!field.getType().toString().contains("ro.allevo.fintpws.model")) && (!field.getType().toString().contains("java.util.List"))) { if (field.getType().toString().contains("BigDecimal")) { if (insertedEntity.optDouble(fieldName) != readedEntity.optDouble(fieldName)) { logger.debug(fieldName); isEqual = false; } } else { if (field.getType().toString().contains("Timestamp")) { Timestamp first = new Timestamp(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'") .parse(insertedEntity.getString(fieldName)).getTime()); Timestamp second = new Timestamp( new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'") .parse(readedEntity.optString(fieldName)).getTime()); if (first.compareTo(second) != 0) { logger.debug(fieldName); isEqual = false; } } else { if (!insertedEntity.optString(fieldName) .equals(readedEntity.optString(fieldName))) { logger.debug(fieldName); isEqual = false; } } } } } catch (SecurityException e) { logger.error("reflectin security exception"); isEqual = false; } catch (NoSuchFieldException e) { logger.error("reflection field exception"); if (!fields.contains(fieldName)) isEqual = false; } catch (JSONException e) { logger.error("json exception"); isEqual = false; } catch (ParseException e) { logger.error("parse exception"); isEqual = false; } } } return isEqual; }