/** * Copyright (c) 2000-present Liferay, Inc. All rights reserved. * * This library is free software; you can redistribute it and/or modify it under * the terms of the GNU Lesser General Public License as published by the Free * Software Foundation; either version 2.1 of the License, or (at your option) * any later version. * * This library 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 Lesser General Public License for more * details. */ package com.liferay.portlet.asset.service.persistence.impl; import com.liferay.asset.kernel.model.AssetTag; import com.liferay.asset.kernel.service.persistence.AssetTagFinder; import com.liferay.portal.kernel.dao.orm.QueryPos; import com.liferay.portal.kernel.dao.orm.QueryUtil; import com.liferay.portal.kernel.dao.orm.SQLQuery; import com.liferay.portal.kernel.dao.orm.Session; import com.liferay.portal.kernel.dao.orm.Type; import com.liferay.portal.kernel.exception.SystemException; import com.liferay.portal.kernel.util.GetterUtil; import com.liferay.portal.kernel.util.OrderByComparator; import com.liferay.portal.kernel.util.StringUtil; import com.liferay.portlet.asset.model.impl.AssetTagImpl; import com.liferay.util.dao.orm.CustomSQLUtil; import java.util.ArrayList; import java.util.Iterator; import java.util.List; /** * @author Brian Wing Shun Chan * @author Bruno Farache */ public class AssetTagFinderImpl extends AssetTagFinderBaseImpl implements AssetTagFinder { public static final String COUNT_BY_G_N = AssetTagFinder.class.getName() + ".countByG_N"; public static final String COUNT_BY_G_C_N = AssetTagFinder.class.getName() + ".countByG_C_N"; public static final String FIND_BY_G_C_N = AssetTagFinder.class.getName() + ".findByG_C_N"; public static final String FIND_BY_G_N_S_E = AssetTagFinder.class.getName() + ".findByG_N_S_E"; @Override public int countByG_N(long groupId, String name) { Session session = null; try { session = openSession(); String sql = CustomSQLUtil.get(COUNT_BY_G_N); SQLQuery q = session.createSynchronizedSQLQuery(sql); q.addScalar(COUNT_COLUMN_NAME, Type.LONG); QueryPos qPos = QueryPos.getInstance(q); qPos.add(groupId); String lowerCaseName = StringUtil.toLowerCase(name); qPos.add(lowerCaseName); Iterator<Long> itr = q.iterate(); if (itr.hasNext()) { Long count = itr.next(); if (count != null) { return count.intValue(); } } return 0; } catch (Exception e) { throw new SystemException(e); } finally { closeSession(session); } } @Override public int countByG_C_N(long groupId, long classNameId, String name) { Session session = null; try { session = openSession(); String sql = CustomSQLUtil.get(COUNT_BY_G_C_N); SQLQuery q = session.createSynchronizedSQLQuery(sql); q.addScalar(COUNT_COLUMN_NAME, Type.LONG); QueryPos qPos = QueryPos.getInstance(q); qPos.add(groupId); qPos.add(classNameId); String lowerCaseName = StringUtil.toLowerCase(name); qPos.add(lowerCaseName); qPos.add(lowerCaseName); Iterator<Long> itr = q.iterate(); if (itr.hasNext()) { Long count = itr.next(); if (count != null) { return count.intValue(); } } return 0; } catch (Exception e) { throw new SystemException(e); } finally { closeSession(session); } } @Override public List<AssetTag> findByG_C_N( long groupId, long classNameId, String name, int start, int end, OrderByComparator<AssetTag> obc) { Session session = null; try { session = openSession(); String sql = CustomSQLUtil.get(FIND_BY_G_C_N); sql = CustomSQLUtil.replaceOrderBy(sql, obc); SQLQuery q = session.createSynchronizedSQLQuery(sql); q.addEntity("AssetTag", AssetTagImpl.class); QueryPos qPos = QueryPos.getInstance(q); qPos.add(groupId); qPos.add(classNameId); String lowerCaseName = StringUtil.toLowerCase(name); qPos.add(lowerCaseName); qPos.add(lowerCaseName); return (List<AssetTag>)QueryUtil.list(q, getDialect(), start, end); } catch (Exception e) { throw new SystemException(e); } finally { closeSession(session); } } @Override public List<AssetTag> findByG_N_S_E( long groupId, String name, int startPeriod, int endPeriod, int periodLength) { Session session = null; try { session = openSession(); String sql = CustomSQLUtil.get(FIND_BY_G_N_S_E); SQLQuery q = session.createSynchronizedSQLQuery(sql); QueryPos qPos = QueryPos.getInstance(q); qPos.add(groupId); qPos.add(name); qPos.add(startPeriod); qPos.add(endPeriod); qPos.add(periodLength); qPos.add(endPeriod); List<AssetTag> assetTags = new ArrayList<>(); Iterator<Object[]> itr = q.iterate(); while (itr.hasNext()) { Object[] array = itr.next(); AssetTag assetTag = new AssetTagImpl(); assetTag.setTagId(GetterUtil.getLong(array[0])); assetTag.setName(GetterUtil.getString(array[1])); assetTag.setAssetCount(GetterUtil.getInteger(array[2])); assetTags.add(assetTag); } return assetTags; } catch (Exception e) { throw new SystemException(e); } finally { closeSession(session); } } }