com.wonders.bud.freshmommy.service.content.dao.impl.ContentDaoImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.wonders.bud.freshmommy.service.content.dao.impl.ContentDaoImpl.java

Source

/** 
* 
 * Copyright (c) 1995-2012 Wonders Information Co.,Ltd. 
 * 1518 Lianhang Rd,Shanghai 201112.P.R.C.
 * All Rights Reserved.
 * 
 * This software is the confidential and proprietary information of Wonders Group.
 * (Social Security Department). You shall not disclose such
 * Confidential Information and shall use it only in accordance with 
 * the terms of the license agreement you entered into with Wonders Group. 
 *
 * Distributable under GNU LGPL license by gnu.org
 */

package com.wonders.bud.freshmommy.service.content.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.CriteriaSpecification;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.LogicalExpression;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.wonders.bud.framework.common.hibernate.HibernateBaseDaoImpl;
import com.wonders.bud.framework.common.page.Page;
import com.wonders.bud.freshmommy.service.content.dao.ContentDao;
import com.wonders.bud.freshmommy.service.content.model.po.ContentPO;

/**<p>
 * Title: freshmommy_[?]_[?]
 * </p>
 * <p>
 * Description: [??]
 * </p>
 * 
 * @author HYH
 * @version $Revision$ 20141114
 * @author (lastest modification by GLJ)
 * @since 20100901
 */
@Repository("contentDaoImpl")
public class ContentDaoImpl extends HibernateBaseDaoImpl<ContentPO, String> implements ContentDao {

    @Override
    public Page<ContentPO> findByPageCustom(Page<ContentPO> page, String orQuery) {
        Criteria c = createCriteria();
        if (page.getParam() != null) {
            c = page.getParam().andCriteria(c);
        }
        if (orQuery != null) {
            Criterion cTitle = Restrictions.like("title", "%" + orQuery + "%");
            Criterion cShortTitle = Restrictions.like("shortTitle", "%" + orQuery + "%");
            LogicalExpression orExp1 = Restrictions.or(cTitle, cShortTitle);
            Criterion cTxt = Restrictions.like("txt", "%" + orQuery + "%");
            LogicalExpression orExp = Restrictions.or(orExp1, cTxt);
            c.add(orExp);
        }

        c.setProjection(null);

        // ??
        int totalCount = ((Long) c.setProjection(Projections.rowCount()).uniqueResult()).intValue();

        c.setProjection(null);
        c.setResultTransformer(CriteriaSpecification.ROOT_ENTITY);

        // page
        if (page.getStart() > -1) {
            c.setFirstResult(page.getStart());
        }
        if (page.getPagesize() > -1) {
            c.setMaxResults(page.getPagesize());
        }
        List<ContentPO> result = c.list();
        getSession().clear();
        page.setResult(result);
        page.setTotal(totalCount);
        return page;
    }
}