我有两个相似的实体(表),它们具有许多相同的字段,并且也执行相似的搜索。
如何重用以下代码,而不是复制粘贴?
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<RentDTO> cq = cb.createQuery(RentDTO.class);
Root<RentDTO> root = cq.from(RentDTO.class);
List<Predicate> predicates = new ArrayList<>();
if (params.getEast() == null || params.getWest() == null || params.getNorth() == null || params.getSouth() == null) {
return null;
}
if (params.getBuilding_type().equals("any")){
// TODO
}
else if (params.getBuilding_type() != null) {
predicates.add(cb.like(root.get("building_type"), "%" + params.getBuilding_type() + "%"));
} else {
params.setBuilding_type(DEFAULT_BUILDING_TYPE);
predicates.add(cb.like(root.get("building_type"), "%" + params.getBuilding_type() + "%"));
}
if (params.getMinBuildingFloor() != null) {
predicates.add(cb.greaterThanOrEqualTo(root.get("building_floors"), params.getMinBuildingFloor()));
}
if (params.getMaxBuildingFloor() != null) {
predicates.add(cb.lessThanOrEqualTo(root.get("building_floors"), params.getMaxBuildingFloor()));
}
if (params.getProperty_type().toString().equals("any") == false) {
predicates.add(cb.like(root.get("property_type"), "%" + params.getProperty_type() + "%"));
}
if (params.getFloor() != -1) {
predicates.add(cb.equal(root.get("floor"), params.getFloor()));
}
if (params.getMinYear() == null) {
params.setMaxYear("2049");
}
if (params.getMinAge() != null && params.getMinAge() != 0) {
Calendar cal = Calendar.getInstance();
Date today = cal.getTime();
cal.add(Calendar.YEAR, params.getMinAge() * -1); // to get previous year add -1
Date minAgeYear = cal.getTime();
predicates.add(cb.lessThanOrEqualTo(root.get("building_completed_date"), minAgeYear));
}
if (params.getMaxAge() != null && params.getMaxAge() != 99) {
Calendar cal = Calendar.getInstance();
Date today = cal.getTime();
cal.add(Calendar.YEAR, params.getMaxAge() * -1); // to get previous year add -1
predicates.add(cb.greaterThanOrEqualTo(root.get("building_completed_date"), cal.getTime()));
}
predicates.add(cb.greaterThanOrEqualTo(root.get("lot_size"), params.getMinPing() * PING_CONVERT_RATIO));
if (params.getMaxPing() < 200) {
predicates.add(cb.lessThanOrEqualTo(root.get("lot_size"), params.getMaxPing() * PING_CONVERT_RATIO));
}
try {
String pattern = "yyyy-MM-dd";
DateFormat df = new SimpleDateFormat(pattern);
Date startDate = df.parse(params.getMinYear() + "-01-01");
Date endDate = df.parse(params.getMaxYear() + "-12-31");
predicates.add(cb.greaterThanOrEqualTo(root.get("date"),
startDate
));
predicates.add(cb.lessThanOrEqualTo(root.get("date"),
endDate
));
} catch (ParseException e) {
e.printStackTrace();
}
Root<TransactionDTO> root = cq.from(TransactionDTO.class);
List<Predicate> predicates = new ArrayList<>();
if (params.getEast() == null || params.getWest() == null || params.getNorth() == null || params.getSouth() == null) {
return null;
}
if (params.getBuilding_type().equals("any")){
// TODO
}
else if (params.getBuilding_type() != null) {
predicates.add(cb.like(root.get("building_type"), "%" + params.getBuilding_type() + "%"));
} else {
params.setBuilding_type(DEFAULT_BUILDING_TYPE);
predicates.add(cb.like(root.get("building_type"), "%" + params.getBuilding_type() + "%"));
}
if (params.getMinBuildingFloor() != null) {
predicates.add(cb.greaterThanOrEqualTo(root.get("building_floors"), params.getMinBuildingFloor()));
}
if (params.getMaxBuildingFloor() != null) {
predicates.add(cb.lessThanOrEqualTo(root.get("building_floors"), params.getMaxBuildingFloor()));
}
if (params.getFloor() != -1) {
predicates.add(cb.equal(root.get("floor"), params.getFloor()));
}
if (params.getProperty_type().toString().equals("any") == false) {
predicates.add(cb.like(root.get("property_type"), "%" + params.getProperty_type() + "%"));
}
if (params.getMinYear() == null) {
params.setMaxYear("2049");
}
if (params.getMinAge() != null && params.getMinAge() != 0) {
Calendar cal = Calendar.getInstance();
Date today = cal.getTime();
cal.add(Calendar.YEAR, params.getMinAge() * -1); // to get previous year add -1
Date minAgeYear = cal.getTime();
predicates.add(cb.lessThanOrEqualTo(root.get("building_completed_date"), minAgeYear));
}
if (params.getMaxAge() != null && params.getMaxAge() != 99) {
Calendar cal = Calendar.getInstance();
Date today = cal.getTime();
cal.add(Calendar.YEAR, params.getMaxAge() * -1); // to get previous year add -1
predicates.add(cb.greaterThanOrEqualTo(root.get("building_completed_date"), cal.getTime()));
}
predicates.add(cb.greaterThanOrEqualTo(root.get("lot_size"), params.getMinPing() * PING_CONVERT_RATIO));
if (params.getMaxPing() < 200) {
predicates.add(cb.lessThanOrEqualTo(root.get("lot_size"), params.getMaxPing() * PING_CONVERT_RATIO));
}
try {
String pattern = "yyyy-MM-dd";
DateFormat df = new SimpleDateFormat(pattern);
Date startDate = df.parse(params.getMinYear() + "-01-01");
Date endDate = df.parse(params.getMaxYear() + "-12-31");
// System.out.println(“ Today =” + df.format(today)); predicates.add(cb.greaterThanOrEqualTo(root.get(“ date”), 开始日期 )); predicates.add(cb.lessThanOrEqualTo(root.get(“ date”), 结束日期 )); } catch(ParseException e){ e.printStackTrace(); }