001
014
015 package com.liferay.portlet.documentlibrary.service.persistence;
016
017 import com.liferay.portal.kernel.dao.orm.QueryDefinition;
018 import com.liferay.portal.kernel.dao.orm.QueryPos;
019 import com.liferay.portal.kernel.dao.orm.QueryUtil;
020 import com.liferay.portal.kernel.dao.orm.SQLQuery;
021 import com.liferay.portal.kernel.dao.orm.Session;
022 import com.liferay.portal.kernel.dao.orm.Type;
023 import com.liferay.portal.kernel.exception.SystemException;
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.kernel.workflow.WorkflowConstants;
028 import com.liferay.portal.security.permission.InlineSQLHelperUtil;
029 import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
030 import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
031 import com.liferay.portlet.documentlibrary.model.DLFileEntry;
032 import com.liferay.portlet.documentlibrary.model.impl.DLFileEntryImpl;
033 import com.liferay.portlet.documentlibrary.model.impl.DLFileVersionImpl;
034 import com.liferay.util.dao.orm.CustomSQLUtil;
035
036 import java.util.Collections;
037 import java.util.Iterator;
038 import java.util.List;
039
040
044 public class DLFileEntryFinderImpl
045 extends BasePersistenceImpl<DLFileEntry> implements DLFileEntryFinder {
046
047 public static final String COUNT_BY_EXTRA_SETTINGS =
048 DLFileEntryFinder.class.getName() + ".countByExtraSettings";
049
050 public static final String COUNT_BY_G_F =
051 DLFileEntryFinder.class.getName() + ".countByG_F";
052
053 public static final String COUNT_BY_G_U_F =
054 DLFileEntryFinder.class.getName() + ".countByG_U_F";
055
056 public static final String FIND_BY_ANY_IMAGE_ID =
057 DLFileEntryFinder.class.getName() + ".findByAnyImageId";
058
059 public static final String FIND_BY_EXTRA_SETTINGS =
060 DLFileEntryFinder.class.getName() + ".findByExtraSettings";
061
062 public static final String FIND_BY_DDM_STRUCTURE_IDS =
063 DLFileEntryFinder.class.getName() + ".findByDDMStructureIds";
064
065 public static final String FIND_BY_MISVERSIONED =
066 DLFileEntryFinder.class.getName() + ".findByMisversioned";
067
068 public static final String FIND_BY_NO_ASSETS =
069 DLFileEntryFinder.class.getName() + ".findByNoAssets";
070
071 public static final String FIND_BY_ORPHANED_FILE_ENTRIES =
072 DLFileEntryFinder.class.getName() + ".findByOrphanedFileEntries";
073
074 public static final String FIND_BY_G_F =
075 DLFileEntryFinder.class.getName() + ".findByG_F";
076
077 public static final String FIND_BY_G_U_F =
078 DLFileEntryFinder.class.getName() + ".findByG_U_F";
079
080 @Override
081 public int countByExtraSettings() throws SystemException {
082 Session session = null;
083
084 try {
085 session = openSession();
086
087 String sql = CustomSQLUtil.get(COUNT_BY_EXTRA_SETTINGS);
088
089 SQLQuery q = session.createSQLQuery(sql);
090
091 q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
092
093 Iterator<Long> itr = q.iterate();
094
095 if (itr.hasNext()) {
096 Long count = itr.next();
097
098 if (count != null) {
099 return count.intValue();
100 }
101 }
102
103 return 0;
104 }
105 catch (Exception e) {
106 throw new SystemException(e);
107 }
108 finally {
109 closeSession(session);
110 }
111 }
112
113 @Override
114 public int countByG_F(
115 long groupId, List<Long> folderIds, QueryDefinition queryDefinition)
116 throws SystemException {
117
118 return doCountByG_U_F_M(
119 groupId, 0, folderIds, null, queryDefinition, false);
120 }
121
122 @Override
123 public int countByG_U_F_M(
124 long groupId, long userId, List<Long> folderIds, String[] mimeTypes,
125 QueryDefinition queryDefinition)
126 throws SystemException {
127
128 return doCountByG_U_F_M(
129 groupId, userId, folderIds, mimeTypes, queryDefinition, false);
130 }
131
132 @Override
133 public DLFileEntry fetchByAnyImageId(long imageId) throws SystemException {
134 Session session = null;
135
136 try {
137 session = openSession();
138
139 String sql = CustomSQLUtil.get(FIND_BY_ANY_IMAGE_ID);
140
141 SQLQuery q = session.createSQLQuery(sql);
142
143 q.addEntity(DLFileEntryImpl.TABLE_NAME, DLFileEntryImpl.class);
144
145 QueryPos qPos = QueryPos.getInstance(q);
146
147 qPos.add(imageId);
148 qPos.add(imageId);
149 qPos.add(imageId);
150 qPos.add(imageId);
151
152 List<DLFileEntry> dlFileEntries = q.list();
153
154 if (!dlFileEntries.isEmpty()) {
155 return dlFileEntries.get(0);
156 }
157
158 return null;
159 }
160 catch (Exception e) {
161 throw new SystemException(e);
162 }
163 finally {
164 closeSession(session);
165 }
166 }
167
168 @Override
169 public int filterCountByG_F(
170 long groupId, List<Long> folderIds, QueryDefinition queryDefinition)
171 throws SystemException {
172
173 return doCountByG_U_F_M(
174 groupId, 0, folderIds, null, queryDefinition, true);
175 }
176
177 @Override
178 public List<DLFileEntry> filterFindByG_F(
179 long groupId, List<Long> folderIds, QueryDefinition queryDefinition)
180 throws SystemException {
181
182 return doFindByG_U_F_M(
183 groupId, 0, folderIds, null, queryDefinition, true);
184 }
185
186 @Override
187 public DLFileEntry findByAnyImageId(long imageId)
188 throws NoSuchFileEntryException, SystemException {
189
190 DLFileEntry dlFileEntry = fetchByAnyImageId(imageId);
191
192 if (dlFileEntry != null) {
193 return dlFileEntry;
194 }
195
196 throw new NoSuchFileEntryException(
197 "No DLFileEntry exists with the imageId " + imageId);
198 }
199
200 @Override
201 public List<DLFileEntry> findByDDMStructureIds(
202 long[] ddmStructureIds, int start, int end)
203 throws SystemException {
204
205 Session session = null;
206
207 try {
208 session = openSession();
209
210 String sql = CustomSQLUtil.get(FIND_BY_DDM_STRUCTURE_IDS);
211
212 if ((ddmStructureIds == null) || (ddmStructureIds.length <= 0)) {
213 return Collections.emptyList();
214 }
215
216 sql = StringUtil.replace(
217 sql, "[$DDM_STRUCTURE_ID$]",
218 getDDMStructureIds(ddmStructureIds));
219
220 SQLQuery q = session.createSQLQuery(sql);
221
222 q.addEntity(DLFileEntryImpl.TABLE_NAME, DLFileEntryImpl.class);
223
224 QueryPos qPos = QueryPos.getInstance(q);
225
226 qPos.add(ddmStructureIds);
227
228 return (List<DLFileEntry>)QueryUtil.list(
229 q, getDialect(), start, end);
230 }
231 catch (Exception e) {
232 throw new SystemException(e);
233 }
234 finally {
235 closeSession(session);
236 }
237 }
238
239 @Override
240 public List<DLFileEntry> findByExtraSettings(int start, int end)
241 throws SystemException {
242
243 Session session = null;
244
245 try {
246 session = openSession();
247
248 String sql = CustomSQLUtil.get(FIND_BY_EXTRA_SETTINGS);
249
250 SQLQuery q = session.createSQLQuery(sql);
251
252 q.addEntity(DLFileEntryImpl.TABLE_NAME, DLFileEntryImpl.class);
253
254 return (List<DLFileEntry>)QueryUtil.list(
255 q, getDialect(), start, end);
256 }
257 catch (Exception e) {
258 throw new SystemException(e);
259 }
260 finally {
261 closeSession(session);
262 }
263 }
264
265 @Override
266 public List<DLFileEntry> findByMisversioned() throws SystemException {
267 Session session = null;
268
269 try {
270 session = openSession();
271
272 String sql = CustomSQLUtil.get(FIND_BY_MISVERSIONED);
273
274 SQLQuery q = session.createSQLQuery(sql);
275
276 q.addEntity(DLFileEntryImpl.TABLE_NAME, DLFileEntryImpl.class);
277
278 return q.list(true);
279 }
280 catch (Exception e) {
281 throw new SystemException(e);
282 }
283 finally {
284 closeSession(session);
285 }
286 }
287
288 @Override
289 public List<DLFileEntry> findByNoAssets() throws SystemException {
290 Session session = null;
291
292 try {
293 session = openSession();
294
295 String sql = CustomSQLUtil.get(FIND_BY_NO_ASSETS);
296
297 SQLQuery q = session.createSQLQuery(sql);
298
299 q.addEntity(DLFileEntryImpl.TABLE_NAME, DLFileEntryImpl.class);
300
301 return q.list(true);
302 }
303 catch (Exception e) {
304 throw new SystemException(e);
305 }
306 finally {
307 closeSession(session);
308 }
309 }
310
311 @Override
312 public List<DLFileEntry> findByOrphanedFileEntries()
313 throws SystemException {
314
315 Session session = null;
316
317 try {
318 session = openSession();
319
320 String sql = CustomSQLUtil.get(FIND_BY_ORPHANED_FILE_ENTRIES);
321
322 SQLQuery q = session.createSQLQuery(sql);
323
324 q.addEntity(DLFileEntryImpl.TABLE_NAME, DLFileEntryImpl.class);
325
326 return q.list(true);
327 }
328 catch (Exception e) {
329 throw new SystemException(e);
330 }
331 finally {
332 closeSession(session);
333 }
334 }
335
336 @Override
337 public List<DLFileEntry> findByG_F(
338 long groupId, List<Long> folderIds, QueryDefinition queryDefinition)
339 throws SystemException {
340
341 return doFindByG_U_F_M(
342 groupId, 0, folderIds, null, queryDefinition, false);
343 }
344
345 @Override
346 public List<DLFileEntry> findByG_U_F_M(
347 long groupId, long userId, List<Long> folderIds, String[] mimeTypes,
348 QueryDefinition queryDefinition)
349 throws SystemException {
350
351 return doFindByG_U_F_M(
352 groupId, userId, folderIds, mimeTypes, queryDefinition, false);
353 }
354
355 protected int doCountByG_U_F_M(
356 long groupId, long userId, List<Long> folderIds, String[] mimeTypes,
357 QueryDefinition queryDefinition, boolean inlineSQLHelper)
358 throws SystemException {
359
360 Session session = null;
361
362 try {
363 session = openSession();
364
365 String id = null;
366
367 if (userId <= 0) {
368 id = COUNT_BY_G_F;
369 }
370 else {
371 id = COUNT_BY_G_U_F;
372 }
373
374 String sql = getFileEntriesSQL(
375 id, groupId, folderIds, mimeTypes, queryDefinition,
376 inlineSQLHelper);
377
378 SQLQuery q = session.createSQLQuery(sql);
379
380 q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
381
382 QueryPos qPos = QueryPos.getInstance(q);
383
384 qPos.add(groupId);
385
386 if (userId > 0) {
387 qPos.add(userId);
388 }
389
390 qPos.add(queryDefinition.getStatus());
391
392 for (Long folderId : folderIds) {
393 qPos.add(folderId);
394 }
395
396 if (mimeTypes != null) {
397 qPos.add(mimeTypes);
398 }
399
400 Iterator<Long> itr = q.iterate();
401
402 if (itr.hasNext()) {
403 Long count = itr.next();
404
405 if (count != null) {
406 return count.intValue();
407 }
408 }
409
410 return 0;
411 }
412 catch (Exception e) {
413 throw new SystemException(e);
414 }
415 finally {
416 closeSession(session);
417 }
418 }
419
420 protected List<DLFileEntry> doFindByG_U_F_M(
421 long groupId, long userId, List<Long> folderIds, String[] mimeTypes,
422 QueryDefinition queryDefinition, boolean inlineSQLHelper)
423 throws SystemException {
424
425 Session session = null;
426
427 try {
428 session = openSession();
429
430 String id = null;
431
432 if (userId <= 0) {
433 id = FIND_BY_G_F;
434 }
435 else {
436 id = FIND_BY_G_U_F;
437 }
438
439 String sql = getFileEntriesSQL(
440 id, groupId, folderIds, mimeTypes, queryDefinition,
441 inlineSQLHelper);
442
443 sql = CustomSQLUtil.replaceOrderBy(
444 sql, queryDefinition.getOrderByComparator());
445
446 SQLQuery q = session.createSQLQuery(sql);
447
448 q.addEntity(DLFileEntryImpl.TABLE_NAME, DLFileEntryImpl.class);
449
450 QueryPos qPos = QueryPos.getInstance(q);
451
452 qPos.add(groupId);
453
454 if (userId > 0) {
455 qPos.add(userId);
456 }
457
458 qPos.add(queryDefinition.getStatus());
459
460 for (Long folderId : folderIds) {
461 qPos.add(folderId);
462 }
463
464 if (mimeTypes != null) {
465 qPos.add(mimeTypes);
466 }
467
468 return (List<DLFileEntry>)QueryUtil.list(
469 q, getDialect(), queryDefinition.getStart(),
470 queryDefinition.getEnd());
471 }
472 catch (Exception e) {
473 throw new SystemException(e);
474 }
475 finally {
476 closeSession(session);
477 }
478 }
479
480 protected String getDDMStructureIds(long[] ddmStructureIds) {
481 StringBundler sb = new StringBundler(
482 (ddmStructureIds.length * 2 - 1) + 2);
483
484 sb.append(StringPool.OPEN_PARENTHESIS);
485
486 for (int i = 0; i < ddmStructureIds.length; i++) {
487 sb.append("DLFileEntryTypes_DDMStructures.structureId = ?");
488
489 if ((i + 1) != ddmStructureIds.length) {
490 sb.append(WHERE_OR);
491 }
492 }
493
494 sb.append(StringPool.CLOSE_PARENTHESIS);
495
496 return sb.toString();
497 }
498
499 protected String getFileEntriesSQL(
500 String id, long groupId, List<Long> folderIds, String[] mimeTypes,
501 QueryDefinition queryDefinition, boolean inlineSQLHelper) {
502
503 String tableName = DLFileVersionImpl.TABLE_NAME;
504
505 String sql = CustomSQLUtil.get(id, queryDefinition, tableName);
506
507 if (queryDefinition.getStatus() == WorkflowConstants.STATUS_ANY) {
508 sql = StringUtil.replace(sql, "[$JOIN$]", StringPool.BLANK);
509
510 tableName = DLFileEntryImpl.TABLE_NAME;
511 }
512 else {
513 sql = StringUtil.replace(
514 sql, "[$JOIN$]",
515 CustomSQLUtil.get(
516 DLFolderFinderImpl.JOIN_FE_BY_DL_FILE_VERSION));
517 }
518
519 if (inlineSQLHelper && InlineSQLHelperUtil.isEnabled()) {
520 sql = InlineSQLHelperUtil.replacePermissionCheck(
521 sql, DLFileEntry.class.getName(), "DLFileEntry.fileEntryId",
522 groupId);
523 }
524
525 StringBundler sb = new StringBundler(7);
526
527 if (!folderIds.isEmpty()) {
528 sb.append(StringPool.OPEN_PARENTHESIS);
529 sb.append(getFolderIds(folderIds, tableName));
530 sb.append(StringPool.CLOSE_PARENTHESIS);
531 }
532
533 if ((mimeTypes != null) && (mimeTypes.length > 0)) {
534 sb.append(WHERE_AND);
535 sb.append(StringPool.OPEN_PARENTHESIS);
536 sb.append(getMimeTypes(mimeTypes, tableName));
537 sb.append(StringPool.CLOSE_PARENTHESIS);
538 }
539
540 return StringUtil.replace(sql, "[$FOLDER_ID$]", sb.toString());
541 }
542
543 protected String getFolderIds(List<Long> folderIds, String tableName) {
544 if (folderIds.isEmpty()) {
545 return StringPool.BLANK;
546 }
547
548 StringBundler sb = new StringBundler(folderIds.size() * 3 + 1);
549
550 sb.append(StringPool.OPEN_PARENTHESIS);
551
552 for (int i = 0; i < folderIds.size(); i++) {
553 sb.append(tableName);
554 sb.append(".folderId = ? ");
555
556 if ((i + 1) != folderIds.size()) {
557 sb.append(WHERE_OR);
558 }
559 }
560
561 sb.append(StringPool.CLOSE_PARENTHESIS);
562
563 return sb.toString();
564 }
565
566 protected String getMimeTypes(String[] mimeTypes, String tableName) {
567 if (mimeTypes.length == 0) {
568 return StringPool.BLANK;
569 }
570
571 StringBundler sb = new StringBundler(mimeTypes.length * 2 - 1);
572
573 for (int i = 0; i < mimeTypes.length; i++) {
574 sb.append(tableName);
575 sb.append(".mimeType = ?");
576
577 if ((i + 1) != mimeTypes.length) {
578 sb.append(WHERE_OR);
579 }
580 }
581
582 return sb.toString();
583 }
584
585 }