001
014
015 package com.liferay.portlet.journal.service.persistence;
016
017 import com.liferay.portal.kernel.dao.orm.QueryPos;
018 import com.liferay.portal.kernel.dao.orm.QueryUtil;
019 import com.liferay.portal.kernel.dao.orm.SQLQuery;
020 import com.liferay.portal.kernel.dao.orm.Session;
021 import com.liferay.portal.kernel.dao.orm.Type;
022 import com.liferay.portal.kernel.exception.SystemException;
023 import com.liferay.portal.kernel.util.OrderByComparator;
024 import com.liferay.portal.kernel.util.StringBundler;
025 import com.liferay.portal.kernel.util.StringPool;
026 import com.liferay.portal.kernel.util.StringUtil;
027 import com.liferay.portal.security.permission.InlineSQLHelperUtil;
028 import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
029 import com.liferay.portlet.journal.model.JournalArticle;
030 import com.liferay.portlet.journal.model.JournalFolder;
031 import com.liferay.util.dao.orm.CustomSQLUtil;
032
033 import java.util.ArrayList;
034 import java.util.Iterator;
035 import java.util.List;
036
037
040 public class JournalFolderFinderImpl extends BasePersistenceImpl<JournalFolder>
041 implements JournalFolderFinder {
042
043 public static final String COUNT_A_BY_G_F =
044 JournalFolderFinder.class.getName() + ".countA_ByG_F";
045
046 public static final String COUNT_F_BY_G_F =
047 JournalFolderFinder.class.getName() + ".countF_ByG_F";
048
049 public static final String FIND_A_BY_G_F =
050 JournalFolderFinder.class.getName() + ".findA_ByG_F";
051
052 public static final String FIND_F_BY_G_F =
053 JournalFolderFinder.class.getName() + ".findF_ByG_F";
054
055 public int countF_A_ByG_F(long groupId, long folderId)
056 throws SystemException {
057
058 return doCountF_A_ByG_F(groupId, folderId, false);
059 }
060
061 public int filterCountF_A_ByG_F(long groupId, long folderId)
062 throws SystemException {
063
064 return doCountF_A_ByG_F(groupId, folderId, true);
065 }
066
067 public List<Object> filterFindF_AByG_F(
068 long groupId, long folderId, int start, int end,
069 OrderByComparator obc)
070 throws SystemException {
071
072 return doFindF_AByG_F(groupId, folderId, start, end, obc, true);
073 }
074
075 public List<Object> findF_AByG_F(
076 long groupId, long folderId, int start, int end,
077 OrderByComparator obc)
078 throws SystemException {
079
080 return doFindF_AByG_F(groupId, folderId, start, end, obc, false);
081 }
082
083 protected int doCountF_A_ByG_F(
084 long groupId, long folderId, boolean inlineSQLHelper)
085 throws SystemException {
086
087 Session session = null;
088
089 try {
090 session = openSession();
091
092 StringBundler sb = new StringBundler(5);
093
094 sb.append(StringPool.OPEN_PARENTHESIS);
095
096 String sql = CustomSQLUtil.get(COUNT_F_BY_G_F);
097
098 if (inlineSQLHelper) {
099 sql = InlineSQLHelperUtil.replacePermissionCheck(
100 sql, JournalFolder.class.getName(),
101 "JournalFolder.folderId", groupId);
102 }
103
104 sb.append(sql);
105 sb.append(") UNION ALL (");
106 sb.append(getCountArticlesSQL(groupId, inlineSQLHelper));
107 sb.append(StringPool.CLOSE_PARENTHESIS);
108
109 sql = sb.toString();
110
111 sql = updateSQL(sql, folderId);
112
113 SQLQuery q = session.createSQLQuery(sql);
114
115 q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
116
117 QueryPos qPos = QueryPos.getInstance(q);
118
119 qPos.add(groupId);
120
121 if (folderId >= 0) {
122 qPos.add(folderId);
123 }
124
125 qPos.add(groupId);
126
127 if (folderId >= 0) {
128 qPos.add(folderId);
129 }
130
131 int count = 0;
132
133 Iterator<Long> itr = q.iterate();
134
135 while (itr.hasNext()) {
136 Long l = itr.next();
137
138 if (l != null) {
139 count += l.intValue();
140 }
141 }
142
143 return count;
144 }
145 catch (Exception e) {
146 throw new SystemException(e);
147 }
148 finally {
149 closeSession(session);
150 }
151 }
152
153 protected List<Object> doFindF_AByG_F(
154 long groupId, long folderId, int start, int end,
155 OrderByComparator obc, boolean inlineSQLHelper)
156 throws SystemException {
157
158 Session session = null;
159
160 try {
161 session = openSession();
162
163 StringBundler sb = new StringBundler(5);
164
165 sb.append(StringPool.OPEN_PARENTHESIS);
166
167 String sql = CustomSQLUtil.get(FIND_F_BY_G_F);
168
169 if (inlineSQLHelper) {
170 sql = InlineSQLHelperUtil.replacePermissionCheck(
171 sql, JournalFolder.class.getName(),
172 "JournalFolder.folderId", groupId);
173 }
174
175 sb.append(sql);
176 sb.append(") UNION ALL (");
177 sb.append(getArticlesSQL(groupId, inlineSQLHelper));
178 sb.append(StringPool.CLOSE_PARENTHESIS);
179
180 sql = updateSQL(sb.toString(), folderId);
181
182 sql = CustomSQLUtil.replaceOrderBy(sql, obc);
183
184 SQLQuery q = session.createSQLQuery(sql);
185
186 q.addScalar("modelFolderId", Type.LONG);
187 q.addScalar("modelFolder", Type.LONG);
188 q.addScalar("articleId", Type.STRING);
189 q.addScalar("version", Type.DOUBLE);
190
191 QueryPos qPos = QueryPos.getInstance(q);
192
193 qPos.add(groupId);
194
195 if (folderId >= 0) {
196 qPos.add(folderId);
197 }
198
199 qPos.add(groupId);
200
201 if (folderId >= 0) {
202 qPos.add(folderId);
203 }
204
205 List<Object> models = new ArrayList<Object>();
206
207 Iterator<Object[]> itr = (Iterator<Object[]>)QueryUtil.iterate(
208 q, getDialect(), start, end);
209
210 while (itr.hasNext()) {
211 Object[] array = itr.next();
212
213 long curFolderId = (Long)array[0];
214 long modelFolder = (Long)array[1];
215
216 Object obj = null;
217
218 if (modelFolder == 1) {
219 obj = JournalFolderUtil.findByPrimaryKey(curFolderId);
220 }
221 else {
222 String articleId = (String)array[2];
223 double version = (Double)array[3];
224
225 obj = JournalArticleUtil.findByG_A_V(
226 groupId, articleId, version);
227 }
228
229 models.add(obj);
230 }
231
232 return models;
233 }
234 catch (Exception e) {
235 throw new SystemException(e);
236 }
237 finally {
238 closeSession(session);
239 }
240 }
241
242 protected String getArticlesSQL(long groupId, boolean inlineSQLHelper) {
243 String sql = CustomSQLUtil.get(FIND_A_BY_G_F);
244
245 if (inlineSQLHelper) {
246 sql = InlineSQLHelperUtil.replacePermissionCheck(
247 sql, JournalArticle.class.getName(),
248 "JournalArticle.resourcePrimKey", groupId);
249 }
250
251 return sql;
252 }
253
254 protected String getCountArticlesSQL(
255 long groupId, boolean inlineSQLHelper) {
256
257 String sql = CustomSQLUtil.get(COUNT_A_BY_G_F);
258
259 if (inlineSQLHelper) {
260 sql = InlineSQLHelperUtil.replacePermissionCheck(
261 sql, JournalArticle.class.getName(),
262 "JournalArticle.resourcePrimKey", groupId);
263 }
264
265 return sql;
266 }
267
268 protected String getFolderId(String table, long folderId) {
269 if (folderId < 0) {
270 return StringPool.BLANK;
271 }
272
273 StringBundler sb = new StringBundler(5);
274
275 sb.append(" AND ");
276 sb.append(table);
277 sb.append(".");
278
279 if (table.equals("JournalFolder")) {
280 sb.append("parentFolderId");
281 }
282 else {
283 sb.append("folderId");
284 }
285
286 sb.append(" = ? ");
287
288 return sb.toString();
289 }
290
291 protected String updateSQL(String sql, long folderId) {
292 sql = StringUtil.replace(
293 sql,
294 new String[] {
295 "[$ARTICLE_FOLDER_ID$]", "[$FOLDER_PARENT_FOLDER_ID$]"
296 },
297 new String[] {
298 getFolderId("JournalArticle", folderId),
299 getFolderId("JournalFolder", folderId)
300 });
301
302 return sql;
303 }
304
305 }