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