Java tutorial
/* * Copyright 2016 Pivotal Software, Inc.. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package vn.vnpttech.ssdc.nms.dao.hibernate; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.commons.lang.StringUtils; import org.hibernate.Criteria; import org.hibernate.criterion.MatchMode; import org.hibernate.criterion.Order; import org.hibernate.criterion.Projections; import org.hibernate.criterion.Restrictions; import vn.vnpttech.ssdc.nms.dao.CounterDao; import vn.vnpttech.ssdc.nms.model.Counter; import vn.vnpttech.ssdc.nms.model.Counter; /** * * @author LongDQ */ public class CounterDaoHibernate extends GenericDaoHibernate<Counter, Long> implements CounterDao { public CounterDaoHibernate() { super(Counter.class); } @Override public Map searchCounter(Integer vanId, Integer materialsId, String type, Date startDate, Date endDate, Integer start, Integer limit) { try { Long starttime = System.currentTimeMillis(); Long total = 0L; Map pagingMap = new HashMap(); List<Counter> listCounter = new ArrayList<Counter>(); Criteria criteria = getSession().createCriteria(Counter.class); Criteria criteria2 = getSession().createCriteria(Counter.class).setProjection(Projections.rowCount()); // criteria2.setProjection(Projections.rowCount()); //Van if (vanId != null) { criteria.add(Restrictions.eq("van.id", vanId)); criteria2.add(Restrictions.eq("van.id", vanId)); } //Materials if (materialsId != null) { criteria.add(Restrictions.eq("materials.id", materialsId)); criteria2.add(Restrictions.eq("materials.id", materialsId)); } //Type if (StringUtils.isNotBlank(type)) { criteria.add(Restrictions.like("type", type.trim(), MatchMode.ANYWHERE).ignoreCase()); criteria2.add(Restrictions.like("type", type.trim(), MatchMode.ANYWHERE).ignoreCase()); } //startDate if (startDate != null) { criteria.add(Restrictions.gt("incomingDate", startDate)); criteria2.add(Restrictions.gt("incomingDate", startDate)); } //endDate if (endDate != null) { criteria.add(Restrictions.lt("incomingDate", endDate)); criteria2.add(Restrictions.lt("incomingDate", endDate)); } criteria.addOrder(Order.desc("incomingDate")); // criteria2.addOrder(Order.desc("incomingDate")); //Paging if (limit != null && limit > 0) { criteria.setFirstResult(start.intValue()); criteria.setMaxResults(limit.intValue()); } total = (Long) criteria2.uniqueResult(); listCounter = criteria.list(); pagingMap.put("list", listCounter); pagingMap.put("totalCount", total); log.debug("Time to search van: " + (System.currentTimeMillis() - starttime) + " (ms)"); return pagingMap; } catch (Exception ex) { log.error("ERROR searchCounter: ", ex); return null; } } }