1
22
23 package com.liferay.portlet.documentlibrary.service.persistence;
24
25 import com.liferay.portal.SystemException;
26 import com.liferay.portal.kernel.util.StringMaker;
27 import com.liferay.portal.kernel.util.StringUtil;
28 import com.liferay.portal.spring.hibernate.CustomSQLUtil;
29 import com.liferay.portal.spring.hibernate.HibernateUtil;
30 import com.liferay.util.dao.hibernate.QueryPos;
31 import com.liferay.util.dao.hibernate.QueryUtil;
32
33 import java.util.ArrayList;
34 import java.util.Iterator;
35 import java.util.List;
36
37 import org.hibernate.Hibernate;
38 import org.hibernate.SQLQuery;
39 import org.hibernate.Session;
40
41
48 public class DLFileEntryAndShortcutFinderImpl
49 implements DLFileEntryAndShortcutFinder {
50
51 public static String COUNT_BY_FOLDER_IDS =
52 DLFileEntryAndShortcutFinder.class.getName() + ".countByFolderIds";
53
54 public static String FIND_BY_FOLDER_IDS =
55 DLFileEntryAndShortcutFinder.class.getName() + ".findByFolderIds";
56
57 public int countByFolderIds(List<Long> folderIds) throws SystemException {
58 Session session = null;
59
60 try {
61 session = HibernateUtil.openSession();
62
63 String sql = CustomSQLUtil.get(COUNT_BY_FOLDER_IDS);
64
65 sql = StringUtil.replace(
66 sql, "[$FILE_ENTRY_FOLDER_ID$]",
67 getFolderIds(folderIds, "DLFileEntry"));
68 sql = StringUtil.replace(
69 sql, "[$FILE_SHORTCUT_FOLDER_ID$]",
70 getFolderIds(folderIds, "DLFileShortcut"));
71
72 SQLQuery q = session.createSQLQuery(sql);
73
74 q.addScalar(HibernateUtil.getCountColumnName(), Hibernate.LONG);
75
76 QueryPos qPos = QueryPos.getInstance(q);
77
78 for (int i = 0; i < folderIds.size(); i++) {
79 Long folderId = folderIds.get(i);
80
81 qPos.add(folderId);
82 }
83
84 for (int i = 0; i < folderIds.size(); i++) {
85 Long folderId = folderIds.get(i);
86
87 qPos.add(folderId);
88 }
89
90 int count = 0;
91
92 Iterator<Long> itr = q.list().iterator();
93
94 while (itr.hasNext()) {
95 Long l = itr.next();
96
97 if (l != null) {
98 count += l.intValue();
99 }
100 }
101
102 return count;
103 }
104 catch (Exception e) {
105 throw new SystemException(e);
106 }
107 finally {
108 HibernateUtil.closeSession(session);
109 }
110 }
111
112 public List findByFolderIds(List<Long> folderIds, int begin, int end)
113 throws SystemException {
114
115 Session session = null;
116
117 try {
118 session = HibernateUtil.openSession();
119
120 String sql = CustomSQLUtil.get(FIND_BY_FOLDER_IDS);
121
122 sql = StringUtil.replace(
123 sql, "[$FILE_ENTRY_FOLDER_ID$]",
124 getFolderIds(folderIds, "DLFileEntry"));
125 sql = StringUtil.replace(
126 sql, "[$FILE_SHORTCUT_FOLDER_ID$]",
127 getFolderIds(folderIds, "DLFileShortcut"));
128
129 SQLQuery q = session.createSQLQuery(sql);
130
131 q.addScalar("folderId", Hibernate.LONG);
132 q.addScalar("name", Hibernate.STRING);
133 q.addScalar("title", Hibernate.STRING);
134 q.addScalar("fileShortcutId", Hibernate.LONG);
135
136 QueryPos qPos = QueryPos.getInstance(q);
137
138 for (int i = 0; i < folderIds.size(); i++) {
139 Long folderId = folderIds.get(i);
140
141 qPos.add(folderId);
142 }
143
144 for (int i = 0; i < folderIds.size(); i++) {
145 Long folderId = folderIds.get(i);
146
147 qPos.add(folderId);
148 }
149
150 List fileEntriesAndShortcuts = new ArrayList();
151
152 Iterator itr = QueryUtil.iterate(
153 q, HibernateUtil.getDialect(), begin, end);
154
155 while (itr.hasNext()) {
156 Object[] array = (Object[])itr.next();
157
158 Long folderId = (Long)array[0];
159 String name = (String)array[1];
160 long fileShortcutId = ((Long)array[3]).longValue();
162
163 Object obj = null;
164
165 if (fileShortcutId > 0) {
166 obj = DLFileShortcutUtil.findByPrimaryKey(fileShortcutId);
167 }
168 else {
169 obj = DLFileEntryUtil.findByF_N(folderId.longValue(), name);
170 }
171
172 fileEntriesAndShortcuts.add(obj);
173 }
174
175 return fileEntriesAndShortcuts;
176 }
177 catch (Exception e) {
178 throw new SystemException(e);
179 }
180 finally {
181 HibernateUtil.closeSession(session);
182 }
183 }
184
185 protected String getFolderIds(List<Long> folderIds, String table) {
186 StringMaker sm = new StringMaker();
187
188 for (int i = 0; i < folderIds.size(); i++) {
189 sm.append(table);
190 sm.append(".folderId = ? ");
191
192 if ((i + 1) != folderIds.size()) {
193 sm.append("OR ");
194 }
195 }
196
197 return sm.toString();
198 }
199
200 }