PageRenderTime 62ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/apps/journal/journal-service/src/main/java/com/liferay/journal/service/persistence/impl/JournalFolderFinderImpl.java

http://github.com/liferay/liferay-portal
Java | 473 lines | 341 code | 115 blank | 17 comment | 25 complexity | c4d07dfb21599bf4fe8feef23f3d94aa MD5 | raw file
Possible License(s): LGPL-2.0
  1. /**
  2. * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
  3. *
  4. * This library is free software; you can redistribute it and/or modify it under
  5. * the terms of the GNU Lesser General Public License as published by the Free
  6. * Software Foundation; either version 2.1 of the License, or (at your option)
  7. * any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  12. * details.
  13. */
  14. package com.liferay.journal.service.persistence.impl;
  15. import com.liferay.journal.model.JournalArticle;
  16. import com.liferay.journal.model.JournalFolder;
  17. import com.liferay.journal.model.impl.JournalArticleImpl;
  18. import com.liferay.journal.model.impl.JournalFolderImpl;
  19. import com.liferay.journal.service.persistence.JournalArticleUtil;
  20. import com.liferay.journal.service.persistence.JournalFolderFinder;
  21. import com.liferay.journal.service.persistence.JournalFolderUtil;
  22. import com.liferay.petra.string.StringBundler;
  23. import com.liferay.petra.string.StringPool;
  24. import com.liferay.portal.dao.orm.custom.sql.CustomSQL;
  25. import com.liferay.portal.kernel.dao.orm.QueryDefinition;
  26. import com.liferay.portal.kernel.dao.orm.QueryPos;
  27. import com.liferay.portal.kernel.dao.orm.QueryUtil;
  28. import com.liferay.portal.kernel.dao.orm.SQLQuery;
  29. import com.liferay.portal.kernel.dao.orm.Session;
  30. import com.liferay.portal.kernel.dao.orm.Type;
  31. import com.liferay.portal.kernel.exception.SystemException;
  32. import com.liferay.portal.kernel.security.permission.InlineSQLHelperUtil;
  33. import com.liferay.portal.kernel.util.LocaleUtil;
  34. import com.liferay.portal.kernel.util.Portal;
  35. import com.liferay.portal.kernel.util.StringUtil;
  36. import com.liferay.portal.kernel.workflow.WorkflowConstants;
  37. import java.util.ArrayList;
  38. import java.util.Iterator;
  39. import java.util.List;
  40. import java.util.Locale;
  41. import org.osgi.service.component.annotations.Component;
  42. import org.osgi.service.component.annotations.Reference;
  43. /**
  44. * @author Juan Fernández
  45. * @author Zsolt Berentey
  46. */
  47. @Component(service = JournalFolderFinder.class)
  48. public class JournalFolderFinderImpl
  49. extends JournalFolderFinderBaseImpl implements JournalFolderFinder {
  50. public static final String COUNT_A_BY_G_U_F =
  51. JournalFolderFinder.class.getName() + ".countA_ByG_U_F";
  52. public static final String COUNT_F_BY_G_F =
  53. JournalFolderFinder.class.getName() + ".countF_ByG_F";
  54. public static final String FIND_A_BY_G_U_F =
  55. JournalFolderFinder.class.getName() + ".findA_ByG_U_F";
  56. public static final String FIND_A_BY_G_U_F_L =
  57. JournalFolderFinder.class.getName() + ".findA_ByG_U_F_L";
  58. public static final String FIND_F_BY_NO_ASSETS =
  59. JournalFolderFinder.class.getName() + ".findF_ByNoAssets";
  60. public static final String FIND_F_BY_G_F =
  61. JournalFolderFinder.class.getName() + ".findF_ByG_F";
  62. public static final String FIND_F_BY_G_F_L =
  63. JournalFolderFinder.class.getName() + ".findF_ByG_F_L";
  64. @Override
  65. public int countF_A_ByG_F(
  66. long groupId, long folderId, QueryDefinition<?> queryDefinition) {
  67. return doCountF_A_ByG_F(groupId, folderId, queryDefinition, false);
  68. }
  69. @Override
  70. public int filterCountF_A_ByG_F(
  71. long groupId, long folderId, QueryDefinition<?> queryDefinition) {
  72. return doCountF_A_ByG_F(groupId, folderId, queryDefinition, true);
  73. }
  74. @Override
  75. public List<Object> filterFindF_A_ByG_F(
  76. long groupId, long folderId, QueryDefinition<?> queryDefinition) {
  77. return doFindF_A_ByG_F(groupId, folderId, queryDefinition, true);
  78. }
  79. @Override
  80. public List<Object> filterFindF_A_ByG_F_L(
  81. long groupId, long folderId, Locale locale,
  82. QueryDefinition<?> queryDefinition) {
  83. return doFindF_A_ByG_F_L(
  84. groupId, folderId, locale, queryDefinition, true);
  85. }
  86. @Override
  87. public List<Object> findF_A_ByG_F(
  88. long groupId, long folderId, QueryDefinition<?> queryDefinition) {
  89. return doFindF_A_ByG_F(groupId, folderId, queryDefinition, false);
  90. }
  91. @Override
  92. public List<JournalFolder> findF_ByNoAssets() {
  93. Session session = null;
  94. try {
  95. session = openSession();
  96. String sql = _customSQL.get(getClass(), FIND_F_BY_NO_ASSETS);
  97. SQLQuery sqlQuery = session.createSynchronizedSQLQuery(sql);
  98. sqlQuery.addEntity(
  99. JournalFolderImpl.TABLE_NAME, JournalFolderImpl.class);
  100. QueryPos queryPos = QueryPos.getInstance(sqlQuery);
  101. queryPos.add(_portal.getClassNameId(JournalFolder.class));
  102. return sqlQuery.list(true);
  103. }
  104. catch (Exception exception) {
  105. throw new SystemException(exception);
  106. }
  107. finally {
  108. closeSession(session);
  109. }
  110. }
  111. protected int doCountF_A_ByG_F(
  112. long groupId, long folderId, QueryDefinition<?> queryDefinition,
  113. boolean inlineSQLHelper) {
  114. Session session = null;
  115. try {
  116. session = openSession();
  117. String sql = updateSQL(
  118. StringBundler.concat(
  119. StringPool.OPEN_PARENTHESIS,
  120. getFoldersSQL(
  121. COUNT_F_BY_G_F, groupId, queryDefinition,
  122. inlineSQLHelper),
  123. ") UNION ALL (",
  124. getArticlesSQL(
  125. COUNT_A_BY_G_U_F, groupId, queryDefinition,
  126. inlineSQLHelper),
  127. StringPool.CLOSE_PARENTHESIS),
  128. folderId);
  129. SQLQuery sqlQuery = session.createSynchronizedSQLQuery(sql);
  130. sqlQuery.addScalar(COUNT_COLUMN_NAME, Type.LONG);
  131. QueryPos queryPos = QueryPos.getInstance(sqlQuery);
  132. queryPos.add(groupId);
  133. queryPos.add(queryDefinition.getStatus());
  134. if (folderId >= 0) {
  135. queryPos.add(folderId);
  136. }
  137. queryPos.add(groupId);
  138. if (queryDefinition.getOwnerUserId() > 0) {
  139. queryPos.add(queryDefinition.getOwnerUserId());
  140. queryPos.add(WorkflowConstants.STATUS_IN_TRASH);
  141. }
  142. queryPos.add(queryDefinition.getStatus());
  143. if (folderId >= 0) {
  144. queryPos.add(folderId);
  145. }
  146. int count = 0;
  147. Iterator<Long> iterator = sqlQuery.iterate();
  148. while (iterator.hasNext()) {
  149. Long l = iterator.next();
  150. if (l != null) {
  151. count += l.intValue();
  152. }
  153. }
  154. return count;
  155. }
  156. catch (Exception exception) {
  157. throw new SystemException(exception);
  158. }
  159. finally {
  160. closeSession(session);
  161. }
  162. }
  163. protected List<Object> doFindF_A_ByG_F(
  164. long groupId, long folderId, QueryDefinition<?> queryDefinition,
  165. boolean inlineSQLHelper) {
  166. Session session = null;
  167. try {
  168. session = openSession();
  169. String sql = updateSQL(
  170. StringBundler.concat(
  171. StringPool.OPEN_PARENTHESIS,
  172. getFoldersSQL(
  173. FIND_F_BY_G_F, groupId, queryDefinition,
  174. inlineSQLHelper),
  175. ") UNION ALL (",
  176. getArticlesSQL(
  177. FIND_A_BY_G_U_F, groupId, queryDefinition,
  178. inlineSQLHelper),
  179. StringPool.CLOSE_PARENTHESIS),
  180. folderId);
  181. sql = _customSQL.replaceOrderBy(
  182. sql, queryDefinition.getOrderByComparator());
  183. SQLQuery sqlQuery = session.createSynchronizedSQLQuery(sql);
  184. sqlQuery.addScalar("modelFolderId", Type.LONG);
  185. sqlQuery.addScalar("modelFolder", Type.LONG);
  186. sqlQuery.addScalar("articleId", Type.STRING);
  187. sqlQuery.addScalar("version", Type.DOUBLE);
  188. QueryPos queryPos = QueryPos.getInstance(sqlQuery);
  189. queryPos.add(groupId);
  190. queryPos.add(queryDefinition.getStatus());
  191. if (folderId >= 0) {
  192. queryPos.add(folderId);
  193. }
  194. queryPos.add(groupId);
  195. if (queryDefinition.getOwnerUserId() > 0) {
  196. queryPos.add(queryDefinition.getOwnerUserId());
  197. queryPos.add(WorkflowConstants.STATUS_IN_TRASH);
  198. }
  199. queryPos.add(queryDefinition.getStatus());
  200. if (folderId >= 0) {
  201. queryPos.add(folderId);
  202. }
  203. List<Object> models = new ArrayList<>();
  204. Iterator<Object[]> iterator = (Iterator<Object[]>)QueryUtil.iterate(
  205. sqlQuery, getDialect(), queryDefinition.getStart(),
  206. queryDefinition.getEnd());
  207. while (iterator.hasNext()) {
  208. Object[] array = iterator.next();
  209. long modelFolder = (Long)array[1];
  210. Object object = null;
  211. if (modelFolder == 1) {
  212. long curFolderId = (Long)array[0];
  213. object = JournalFolderUtil.findByPrimaryKey(curFolderId);
  214. }
  215. else {
  216. String articleId = (String)array[2];
  217. double version = (Double)array[3];
  218. object = JournalArticleUtil.findByG_A_V(
  219. groupId, articleId, version);
  220. }
  221. models.add(object);
  222. }
  223. return models;
  224. }
  225. catch (Exception exception) {
  226. throw new SystemException(exception);
  227. }
  228. finally {
  229. closeSession(session);
  230. }
  231. }
  232. protected List<Object> doFindF_A_ByG_F_L(
  233. long groupId, long folderId, Locale locale,
  234. QueryDefinition<?> queryDefinition, boolean inlineSQLHelper) {
  235. Session session = null;
  236. try {
  237. session = openSession();
  238. String sql = updateSQL(
  239. StringBundler.concat(
  240. StringPool.OPEN_PARENTHESIS,
  241. getFoldersSQL(
  242. FIND_F_BY_G_F_L, groupId, queryDefinition,
  243. inlineSQLHelper),
  244. ") UNION ALL (",
  245. getArticlesSQL(
  246. FIND_A_BY_G_U_F_L, groupId, queryDefinition,
  247. inlineSQLHelper),
  248. StringPool.CLOSE_PARENTHESIS),
  249. folderId);
  250. sql = _customSQL.replaceOrderBy(
  251. sql, queryDefinition.getOrderByComparator());
  252. SQLQuery sqlQuery = session.createSynchronizedSQLQuery(sql);
  253. sqlQuery.addScalar("modelFolderId", Type.LONG);
  254. sqlQuery.addScalar("modelFolder", Type.LONG);
  255. sqlQuery.addScalar("articleId", Type.STRING);
  256. sqlQuery.addScalar("version", Type.DOUBLE);
  257. QueryPos queryPos = QueryPos.getInstance(sqlQuery);
  258. queryPos.add(groupId);
  259. queryPos.add(queryDefinition.getStatus());
  260. if (folderId >= 0) {
  261. queryPos.add(folderId);
  262. }
  263. queryPos.add(groupId);
  264. if (queryDefinition.getOwnerUserId() > 0) {
  265. queryPos.add(queryDefinition.getOwnerUserId());
  266. queryPos.add(WorkflowConstants.STATUS_IN_TRASH);
  267. }
  268. queryPos.add(queryDefinition.getStatus());
  269. if (folderId >= 0) {
  270. queryPos.add(folderId);
  271. }
  272. queryPos.add(LocaleUtil.toLanguageId(locale));
  273. List<Object> models = new ArrayList<>();
  274. Iterator<Object[]> iterator = (Iterator<Object[]>)QueryUtil.iterate(
  275. sqlQuery, getDialect(), queryDefinition.getStart(),
  276. queryDefinition.getEnd());
  277. while (iterator.hasNext()) {
  278. Object[] array = iterator.next();
  279. long modelFolder = (Long)array[1];
  280. Object object = null;
  281. if (modelFolder == 1) {
  282. long curFolderId = (Long)array[0];
  283. object = JournalFolderUtil.findByPrimaryKey(curFolderId);
  284. }
  285. else {
  286. String articleId = (String)array[2];
  287. double version = (Double)array[3];
  288. object = JournalArticleUtil.findByG_A_V(
  289. groupId, articleId, version);
  290. }
  291. models.add(object);
  292. }
  293. return models;
  294. }
  295. catch (Exception exception) {
  296. throw new SystemException(exception);
  297. }
  298. finally {
  299. closeSession(session);
  300. }
  301. }
  302. protected String getArticlesSQL(
  303. String id, long groupId, QueryDefinition<?> queryDefinition,
  304. boolean inlineSQLHelper) {
  305. String sql = _customSQL.get(
  306. getClass(), id, queryDefinition, JournalArticleImpl.TABLE_NAME);
  307. if (inlineSQLHelper) {
  308. sql = InlineSQLHelperUtil.replacePermissionCheck(
  309. sql, JournalArticle.class.getName(),
  310. "JournalArticle.resourcePrimKey", groupId);
  311. }
  312. return sql;
  313. }
  314. protected String getFolderId(long folderId, String tableName) {
  315. if (folderId < 0) {
  316. return StringPool.BLANK;
  317. }
  318. StringBundler sb = new StringBundler(5);
  319. sb.append(" AND ");
  320. sb.append(tableName);
  321. sb.append(".");
  322. if (tableName.equals(JournalFolderImpl.TABLE_NAME)) {
  323. sb.append("parentFolderId");
  324. }
  325. else {
  326. sb.append("folderId");
  327. }
  328. sb.append(" = ? ");
  329. return sb.toString();
  330. }
  331. protected String getFoldersSQL(
  332. String id, long groupId, QueryDefinition<?> queryDefinition,
  333. boolean inlineSQLHelper) {
  334. String sql = _customSQL.get(
  335. getClass(), id, queryDefinition, JournalFolderImpl.TABLE_NAME);
  336. if (inlineSQLHelper) {
  337. sql = InlineSQLHelperUtil.replacePermissionCheck(
  338. sql, JournalFolder.class.getName(), "JournalFolder.folderId",
  339. groupId);
  340. }
  341. return sql;
  342. }
  343. protected String updateSQL(String sql, long folderId) {
  344. return StringUtil.replace(
  345. sql,
  346. new String[] {
  347. "[$ARTICLE_FOLDER_ID$]", "[$FOLDER_PARENT_FOLDER_ID$]"
  348. },
  349. new String[] {
  350. getFolderId(folderId, JournalArticleImpl.TABLE_NAME),
  351. getFolderId(folderId, JournalFolderImpl.TABLE_NAME)
  352. });
  353. }
  354. @Reference
  355. private CustomSQL _customSQL;
  356. @Reference
  357. private Portal _portal;
  358. }