001
014
015 package com.liferay.portlet.asset.service.persistence;
016
017 import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
018 import com.liferay.portal.kernel.dao.orm.FinderPath;
019 import com.liferay.portal.kernel.dao.orm.QueryPos;
020 import com.liferay.portal.kernel.dao.orm.QueryUtil;
021 import com.liferay.portal.kernel.dao.orm.SQLQuery;
022 import com.liferay.portal.kernel.dao.orm.Session;
023 import com.liferay.portal.kernel.dao.orm.Type;
024 import com.liferay.portal.kernel.exception.SystemException;
025 import com.liferay.portal.kernel.util.CharPool;
026 import com.liferay.portal.kernel.util.GetterUtil;
027 import com.liferay.portal.kernel.util.StringBundler;
028 import com.liferay.portal.kernel.util.StringPool;
029 import com.liferay.portal.kernel.util.StringUtil;
030 import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
031 import com.liferay.portlet.asset.NoSuchCategoryException;
032 import com.liferay.portlet.asset.model.AssetCategory;
033 import com.liferay.portlet.asset.model.AssetCategoryConstants;
034 import com.liferay.portlet.asset.model.impl.AssetCategoryImpl;
035 import com.liferay.portlet.asset.model.impl.AssetCategoryModelImpl;
036 import com.liferay.util.dao.orm.CustomSQLUtil;
037
038 import java.util.Collections;
039 import java.util.Iterator;
040 import java.util.List;
041
042
048 public class AssetCategoryFinderImpl
049 extends BasePersistenceImpl<AssetCategory> implements AssetCategoryFinder {
050
051 public static final String COUNT_BY_G_C_N =
052 AssetCategoryFinder.class.getName() + ".countByG_C_N";
053
054 public static final String COUNT_BY_G_N_P =
055 AssetCategoryFinder.class.getName() + ".countByG_N_P";
056
057 public static final String FIND_BY_G_L =
058 AssetCategoryFinder.class.getName() + ".findByG_L";
059
060 public static final String FIND_BY_G_N =
061 AssetCategoryFinder.class.getName() + ".findByG_N";
062
063 public static final String FIND_BY_G_N_P =
064 AssetCategoryFinder.class.getName() + ".findByG_N_P";
065
066 public static final FinderPath FINDER_PATH_FIND_BY_G_L = new FinderPath(
067 AssetCategoryModelImpl.ENTITY_CACHE_ENABLED,
068 AssetCategoryModelImpl.FINDER_CACHE_ENABLED, List.class,
069 AssetCategoryPersistenceImpl.FINDER_CLASS_NAME_LIST_WITH_PAGINATION,
070 "findByG_L", new String[] {Long.class.getName()});
071
072 @Override
073 public int countByG_C_N(long groupId, long classNameId, String name)
074 throws SystemException {
075
076 Session session = null;
077
078 try {
079 session = openSession();
080
081 String sql = CustomSQLUtil.get(COUNT_BY_G_C_N);
082
083 SQLQuery q = session.createSQLQuery(sql);
084
085 q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
086
087 QueryPos qPos = QueryPos.getInstance(q);
088
089 qPos.add(groupId);
090 qPos.add(classNameId);
091 qPos.add(name);
092 qPos.add(name);
093
094 Iterator<Long> itr = q.iterate();
095
096 if (itr.hasNext()) {
097 Long count = itr.next();
098
099 if (count != null) {
100 return count.intValue();
101 }
102 }
103
104 return 0;
105 }
106 catch (Exception e) {
107 throw new SystemException(e);
108 }
109 finally {
110 closeSession(session);
111 }
112 }
113
114 @Override
115 public int countByG_N_P(
116 long groupId, String name, String[] categoryProperties)
117 throws SystemException {
118
119 Session session = null;
120
121 try {
122 session = openSession();
123
124 String sql = CustomSQLUtil.get(COUNT_BY_G_N_P);
125
126 SQLQuery q = session.createSQLQuery(sql);
127
128 q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
129
130 QueryPos qPos = QueryPos.getInstance(q);
131
132 setJoin(qPos, categoryProperties);
133
134 qPos.add(groupId);
135 qPos.add(name);
136 qPos.add(name);
137
138 Iterator<Long> itr = q.iterate();
139
140 if (itr.hasNext()) {
141 Long count = itr.next();
142
143 if (count != null) {
144 return count.intValue();
145 }
146 }
147
148 return 0;
149 }
150 catch (Exception e) {
151 throw new SystemException(e);
152 }
153 finally {
154 closeSession(session);
155 }
156 }
157
158 @Override
159 public List<Long> findByG_L(Long parentCategoryId) throws SystemException {
160 Object[] finderArgs = new Object[] {parentCategoryId};
161
162 List<Long> list = (List<Long>)FinderCacheUtil.getResult(
163 FINDER_PATH_FIND_BY_G_L, finderArgs, this);
164
165 if (list != null) {
166 return list;
167 }
168
169 AssetCategory parentAssetCategory = AssetCategoryUtil.fetchByPrimaryKey(
170 parentCategoryId);
171
172 if (parentAssetCategory == null) {
173 return Collections.emptyList();
174 }
175
176 Session session = null;
177
178 try {
179 session = openSession();
180
181 String sql = CustomSQLUtil.get(FIND_BY_G_L);
182
183 SQLQuery q = session.createSQLQuery(sql);
184
185 q.addScalar("categoryId", Type.LONG);
186
187 QueryPos qPos = QueryPos.getInstance(q);
188
189 qPos.add(parentAssetCategory.getGroupId());
190 qPos.add(parentAssetCategory.getLeftCategoryId());
191 qPos.add(parentAssetCategory.getRightCategoryId());
192
193 list = q.list();
194 }
195 catch (Exception e) {
196 throw processException(e);
197 }
198 finally {
199 if (list == null) {
200 FinderCacheUtil.removeResult(
201 FINDER_PATH_FIND_BY_G_L, finderArgs);
202 }
203 else {
204 FinderCacheUtil.putResult(
205 FINDER_PATH_FIND_BY_G_L, finderArgs, list);
206 }
207
208 closeSession(session);
209 }
210
211 return list;
212 }
213
214 @Override
215 public AssetCategory findByG_N(long groupId, String name)
216 throws NoSuchCategoryException, SystemException {
217
218 name = StringUtil.toLowerCase(name.trim());
219
220 Session session = null;
221
222 try {
223 session = openSession();
224
225 String sql = CustomSQLUtil.get(FIND_BY_G_N);
226
227 SQLQuery q = session.createSQLQuery(sql);
228
229 q.addEntity("AssetCategory", AssetCategoryImpl.class);
230
231 QueryPos qPos = QueryPos.getInstance(q);
232
233 qPos.add(groupId);
234 qPos.add(name);
235
236 List<AssetCategory> categories = q.list();
237
238 if (!categories.isEmpty()) {
239 return categories.get(0);
240 }
241 }
242 catch (Exception e) {
243 throw new SystemException(e);
244 }
245 finally {
246 closeSession(session);
247 }
248
249 StringBundler sb = new StringBundler(6);
250
251 sb.append("No AssetCategory exists with the key ");
252 sb.append("{groupId=");
253 sb.append(groupId);
254 sb.append(", name=");
255 sb.append(name);
256 sb.append("}");
257
258 throw new NoSuchCategoryException(sb.toString());
259 }
260
261 @Override
262 public List<AssetCategory> findByG_N_P(
263 long groupId, String name, String[] categoryProperties)
264 throws SystemException {
265
266 return findByG_N_P(
267 groupId, name, categoryProperties, QueryUtil.ALL_POS,
268 QueryUtil.ALL_POS);
269 }
270
271 @Override
272 public List<AssetCategory> findByG_N_P(
273 long groupId, String name, String[] categoryProperties, int start,
274 int end)
275 throws SystemException {
276
277 Session session = null;
278
279 try {
280 session = openSession();
281
282 String sql = CustomSQLUtil.get(FIND_BY_G_N_P);
283
284 sql = StringUtil.replace(
285 sql, "[$JOIN$]", getJoin(categoryProperties));
286
287 SQLQuery q = session.createSQLQuery(sql);
288
289 q.addEntity("AssetCategory", AssetCategoryImpl.class);
290
291 QueryPos qPos = QueryPos.getInstance(q);
292
293 setJoin(qPos, categoryProperties);
294
295 qPos.add(groupId);
296 qPos.add(name);
297 qPos.add(name);
298
299 return (List<AssetCategory>)QueryUtil.list(
300 q, getDialect(), start, end);
301 }
302 catch (Exception e) {
303 throw new SystemException(e);
304 }
305 finally {
306 closeSession(session);
307 }
308 }
309
310 protected String getJoin(String[] categoryProperties) {
311 if (categoryProperties.length == 0) {
312 return StringPool.BLANK;
313 }
314
315 StringBundler sb = new StringBundler(categoryProperties.length * 3 + 2);
316
317 sb.append(" INNER JOIN AssetCategoryProperty ON ");
318 sb.append(" (AssetCategoryProperty.categoryId = ");
319 sb.append(" AssetCategory.categoryId) AND ");
320
321 for (int i = 0; i < categoryProperties.length; i++) {
322 sb.append("(AssetCategoryProperty.key_ = ? AND ");
323 sb.append("AssetCategoryProperty.value = ?) ");
324
325 if ((i + 1) < categoryProperties.length) {
326 sb.append(" AND ");
327 }
328 }
329
330 return sb.toString();
331 }
332
333 protected void setJoin(QueryPos qPos, String[] categoryProperties) {
334 for (int i = 0; i < categoryProperties.length; i++) {
335 String[] categoryProperty = StringUtil.split(
336 categoryProperties[i],
337 AssetCategoryConstants.PROPERTY_KEY_VALUE_SEPARATOR);
338
339 if (categoryProperty.length <= 1) {
340 categoryProperty = StringUtil.split(
341 categoryProperties[i], CharPool.COLON);
342 }
343
344 String key = StringPool.BLANK;
345
346 if (categoryProperty.length > 0) {
347 key = GetterUtil.getString(categoryProperty[0]);
348 }
349
350 String value = StringPool.BLANK;
351
352 if (categoryProperty.length > 1) {
353 value = GetterUtil.getString(categoryProperty[1]);
354 }
355
356 qPos.add(key);
357 qPos.add(value);
358 }
359 }
360
361 }