org.sigmah.server.dao.impl.ValueHibernateDAO.java Source code

Java tutorial

Introduction

Here is the source code for org.sigmah.server.dao.impl.ValueHibernateDAO.java

Source

package org.sigmah.server.dao.impl;

/*
 * #%L
 * Sigmah
 * %%
 * Copyright (C) 2010 - 2016 URD
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/gpl-3.0.html>.
 * #L%
 */

import java.util.Collection;
import java.util.List;

import javax.persistence.TypedQuery;

import org.apache.commons.collections4.CollectionUtils;
import org.sigmah.server.dao.ValueDAO;
import org.sigmah.server.dao.base.AbstractDAO;
import org.sigmah.server.domain.OrgUnit;
import org.sigmah.server.domain.value.Value;

/**
 * {@link ValueDAO} implementation.
 * 
 * @author Denis Colliot (dcolliot@ideia.fr)
 */
public class ValueHibernateDAO extends AbstractDAO<Value, Integer> implements ValueDAO {

    /**
     * {@inheritDoc}
     */
    @Override
    public List<Value> findValuesForOrgUnits(final Collection<OrgUnit> orgUnits) {

        if (CollectionUtils.isEmpty(orgUnits)) {
            throw new IllegalArgumentException("Invalid OrgUnits collection.");
        }

        final StringBuilder builder = new StringBuilder();
        builder.append("SELECT v ");
        builder.append(" FROM Value v ");
        builder.append(" WHERE v.containerId IN (");
        builder.append("   SELECT ");
        builder.append("     ud.id ");
        builder.append("   FROM ");
        builder.append("     OrgUnit o");
        builder.append("     INNER JOIN o.databases ud ");
        builder.append("   WHERE o IN (:orgUnits)");
        builder.append(" )");
        builder.append(" OR v.containerId IN (:orgUnits)");

        final TypedQuery<Value> query = em().createQuery(builder.toString(), Value.class);
        query.setParameter("orgUnits", orgUnits);

        return query.getResultList();
    }

}