Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.hengyi.japp.execution; import com.google.common.collect.Lists; import com.hengyi.japp.execution.data.TaskStatus; import com.hengyi.japp.execution.data.TaskType; import com.hengyi.japp.execution.domain.Customer; import com.hengyi.japp.execution.domain.Customer_; import com.hengyi.japp.execution.domain.Experience; import com.hengyi.japp.execution.domain.ExperienceTag; import com.hengyi.japp.execution.domain.ExperienceTag_; import com.hengyi.japp.execution.domain.Experience_; import com.hengyi.japp.execution.domain.LogInfo_; import com.hengyi.japp.execution.domain.Operator; import com.hengyi.japp.execution.domain.Operator_; import com.hengyi.japp.execution.domain.PayOrder; import com.hengyi.japp.execution.domain.PayOrder_; import com.hengyi.japp.execution.domain.Task; import com.hengyi.japp.execution.domain.TaskSmsTemplate; import com.hengyi.japp.execution.domain.TaskSmsTemplate_; import com.hengyi.japp.execution.domain.Task_; import com.hengyi.japp.execution.web.command.CustomerQueryCommand; import com.hengyi.japp.execution.web.command.ExperienceQueryCommand; import com.hengyi.japp.execution.web.command.ExperienceTagQueryCommand; import com.hengyi.japp.execution.web.command.OperatorQueryCommand; import com.hengyi.japp.execution.web.command.PayOrderQueryCommand; import com.hengyi.japp.execution.web.command.TaskQueryCommand; import com.hengyi.japp.execution.web.command.TaskSmsTemplateQueryCommand; import java.util.Collection; import java.util.Date; import java.util.List; import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.JoinType; import javax.persistence.criteria.ListJoin; import javax.persistence.criteria.Predicate; import javax.persistence.criteria.Root; import static org.apache.commons.lang3.StringUtils.isBlank; import org.joda.time.LocalDate; /** * * @author jzb */ public class Util { public static long getMilliSecondsDelay(Date date) { long delay = date.getTime() - System.currentTimeMillis(); return delay < 0 ? 0 : delay; } public static void queryCommand(CriteriaBuilder cb, CriteriaQuery<?> cq, Root<Customer> root, CustomerQueryCommand command) { List<Predicate> ps = Lists.newArrayListWithCapacity(2); ps.add(cb.equal(root.get(Customer_.deleteFlag), command.isDeleteFlag())); if (!isBlank(command.getName())) { ps.add(cb.like(root.get(Customer_.name), command.getNameQuery())); } cq.where(ps.toArray(new Predicate[ps.size()])); } public static void queryCommand(CriteriaBuilder cb, CriteriaQuery<?> cq, Root<PayOrder> root, PayOrderQueryCommand command) { List<Predicate> ps = Lists.newArrayListWithCapacity(3); if (command.getCustomer() != null) { ps.add(cb.equal(root.get(PayOrder_.customer), command.getCustomer())); } if (command.getPayDateStart() != null) { ps.add(cb.greaterThanOrEqualTo(root.get(PayOrder_.payDate), command.getPayDateStart())); } if (command.getPayDateEnd() != null) { ps.add(cb.lessThanOrEqualTo(root.get(PayOrder_.payDate), command.getPayDateStart())); } cq.where(ps.toArray(new Predicate[ps.size()])); } public static void queryCommand(CriteriaBuilder cb, CriteriaQuery<?> cq, Root<Task> root, TaskQueryCommand command) { Predicate p1 = cb.equal(root.get(Task_.charger), command.getOperator()); ListJoin<Task, Operator> joinFollowers = root.join(Task_.followers, JoinType.LEFT); Predicate p2 = cb.equal(joinFollowers.get(Operator_.id), command.getOperator().getId()); ListJoin<Task, Operator> joinExecutors = root.join(Task_.executors, JoinType.LEFT); Predicate p3 = cb.equal(joinExecutors.get(Operator_.id), command.getOperator().getId()); Predicate p = cb.or(p1, p2, p3); if (command.getExecutor() != null) { p = cb.and(p, cb.equal(p, cb.isMember(command.getExecutor(), root.get(Task_.executors)))); } if (command.getCustomer() != null) { p = cb.and(p, cb.equal(root.get(Task_.customer), command.getCustomer())); } if (!isBlank(command.getContent())) { p = cb.and(p, cb.like(root.get(Task_.content), command.getContentQuery())); } Collection<TaskType> TaskTypes = command.getTypes(); if (TaskTypes != null && !TaskTypes.isEmpty()) { p = cb.and(p, root.get(Task_.type).in(TaskTypes)); } Collection<TaskStatus> statuses = command.getStatuses(); if (statuses != null && !statuses.isEmpty()) { p = cb.and(p, root.get(Task_.status).in(statuses)); } if (command.getCreateDate() != null) { Date createDateStart = LocalDate.fromDateFields(command.getCreateDate()).toDate(); Date createDateEnd = LocalDate.fromDateFields(command.getCreateDate()).plusDays(1).toDate(); p = cb.and(p, cb.between(root.get(Task_.logInfo).get(LogInfo_.createDateTime), createDateStart, createDateEnd)); // TODO timestamp date convert //p = cb.and(p, cb.equal(root.get(Task_.logInfo).get(LogInfo_.createDateTime).as(java.sql.Date.class), command.getCreateDate())); } cq.where(p); } public static void queryCommand(CriteriaBuilder cb, CriteriaQuery<?> cq, Root<Operator> root, OperatorQueryCommand command) { List<Predicate> ps = Lists.newArrayListWithCapacity(3); ps.add(cb.equal(root.get(Operator_.deleteFlag), command.isDeleteFlag())); if (command.getCustomer() != null) { ps.add(cb.isMember(command.getCustomer(), root.get(Operator_.customers))); } if (!isBlank(command.getName())) { ps.add(cb.like(root.get(Operator_.name), command.getNameQuery())); } cq.where(ps.toArray(new Predicate[ps.size()])); } public static void queryCommand(CriteriaBuilder cb, CriteriaQuery<?> cq, Root<TaskSmsTemplate> root, TaskSmsTemplateQueryCommand command) { List<Predicate> ps = Lists.newArrayListWithCapacity(2); if (command.getCustomers() != null) { ps.add(root.get(TaskSmsTemplate_.customer).in(command.getCustomers())); } if (!isBlank(command.getName())) { ps.add(cb.like(root.get(TaskSmsTemplate_.name), command.getNameQuery())); } cq.where(ps.toArray(new Predicate[ps.size()])); } public static void queryCommand(CriteriaBuilder cb, CriteriaQuery<?> cq, Root<Experience> root, ExperienceQueryCommand command) { List<Predicate> ps = Lists.newArrayListWithCapacity(2); if (command.getCustomers() != null) { ps.add(root.get(Experience_.customer).in(command.getCustomers())); } if (!isBlank(command.getName())) { ps.add(cb.like(root.get(Experience_.name), command.getNameQuery())); } cq.where(ps.toArray(new Predicate[ps.size()])); } public static void queryCommand(CriteriaBuilder cb, CriteriaQuery<?> cq, Root<ExperienceTag> root, ExperienceTagQueryCommand command) { List<Predicate> ps = Lists.newArrayListWithCapacity(2); if (command.getCustomers() != null) { ps.add(root.get(ExperienceTag_.customer).in(command.getCustomers())); } if (!isBlank(command.getName())) { ps.add(cb.like(root.get(ExperienceTag_.name), command.getNameQuery())); } cq.where(ps.toArray(new Predicate[ps.size()])); } }