/* ==================================================================
* MyBatisUserMetadataDao.java - 11/11/2016 5:42:00 PM
*
* Copyright 2007-2016 SolarNetwork.net Dev Team
*
* 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 2 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, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
* 02111-1307 USA
* ==================================================================
*/
package net.solarnetwork.central.user.dao.mybatis;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import net.solarnetwork.central.dao.mybatis.support.BaseMyBatisGenericDao;
import net.solarnetwork.central.domain.FilterResults;
import net.solarnetwork.central.domain.SortDescriptor;
import net.solarnetwork.central.support.BasicFilterResults;
import net.solarnetwork.central.user.dao.UserMetadataDao;
import net.solarnetwork.central.user.domain.UserMetadataEntity;
import net.solarnetwork.central.user.domain.UserMetadataFilter;
import net.solarnetwork.central.user.domain.UserMetadataFilterMatch;
/**
* MyBatis implementation of {@link UserMetadataDao}.
*
* @author matt
* @version 1.0
* @since 1.8
*/
public class MyBatisUserMetadataDao extends BaseMyBatisGenericDao<UserMetadataEntity, Long>
implements UserMetadataDao {
/** The query parameter for a general {@link Filter} object value. */
public static final String PARAM_FILTER = "filter";
/**
* Default constructor.
*/
public MyBatisUserMetadataDao() {
super(UserMetadataEntity.class, Long.class);
}
private Long executeCountQuery(final String countQueryName, final Map<String, ?> sqlProps) {
Number n = getSqlSession().selectOne(countQueryName, sqlProps);
if ( n != null ) {
return n.longValue();
}
return null;
}
private String getQueryForFilter(UserMetadataFilter filter) {
return getQueryForAll() + "-UserMetadataMatch";
}
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
@Override
public FilterResults<UserMetadataFilterMatch> findFiltered(UserMetadataFilter filter,
List<SortDescriptor> sortDescriptors, Integer offset, Integer max) {
final String query = getQueryForFilter(filter);
Map<String, Object> sqlProps = new HashMap<String, Object>(1);
sqlProps.put(PARAM_FILTER, filter);
if ( sortDescriptors != null && sortDescriptors.size() > 0 ) {
sqlProps.put(SORT_DESCRIPTORS_PROPERTY, sortDescriptors);
}
// attempt count first, if max NOT specified as -1
Long totalCount = null;
if ( max != null && max.intValue() != -1 ) {
totalCount = executeCountQuery(query + "-count", sqlProps);
}
List<UserMetadataFilterMatch> rows = selectList(query, sqlProps, offset, max);
BasicFilterResults<UserMetadataFilterMatch> results = new BasicFilterResults<UserMetadataFilterMatch>(
rows, (totalCount != null ? totalCount : Long.valueOf(rows.size())), offset,
rows.size());
return results;
}
}