1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
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  /**
42   * <a href="DLFileEntryAndShortcutFinderImpl.java.html"><b><i>View Source</i>
43   * </b></a>
44   *
45   * @author Brian Wing Shun Chan
46   *
47   */
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                 //String title = (String)array[2];
161                 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 }