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