1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   * 
13   */
14  
15  package com.liferay.portlet.documentlibrary.service.persistence;
16  
17  import com.liferay.portal.SystemException;
18  import com.liferay.portal.kernel.dao.orm.QueryPos;
19  import com.liferay.portal.kernel.dao.orm.QueryUtil;
20  import com.liferay.portal.kernel.dao.orm.SQLQuery;
21  import com.liferay.portal.kernel.dao.orm.Session;
22  import com.liferay.portal.kernel.dao.orm.Type;
23  import com.liferay.portal.kernel.util.StringUtil;
24  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
25  import com.liferay.portlet.documentlibrary.model.DLFileEntry;
26  import com.liferay.util.dao.orm.CustomSQLUtil;
27  
28  import java.util.ArrayList;
29  import java.util.Iterator;
30  import java.util.List;
31  
32  /**
33   * <a href="DLFileEntryAndShortcutFinderImpl.java.html"><b><i>View Source</i>
34   * </b></a>
35   *
36   * @author Brian Wing Shun Chan
37   */
38  public class DLFileEntryAndShortcutFinderImpl
39      extends BasePersistenceImpl<DLFileEntry>
40      implements DLFileEntryAndShortcutFinder {
41  
42      public static String COUNT_BY_FOLDER_IDS =
43          DLFileEntryAndShortcutFinder.class.getName() + ".countByFolderIds";
44  
45      public static String FIND_BY_FOLDER_IDS =
46          DLFileEntryAndShortcutFinder.class.getName() + ".findByFolderIds";
47  
48      public int countByFolderIds(List<Long> folderIds) throws SystemException {
49          Session session = null;
50  
51          try {
52              session = openSession();
53  
54              String sql = CustomSQLUtil.get(COUNT_BY_FOLDER_IDS);
55  
56              sql = StringUtil.replace(
57                  sql, "[$FILE_ENTRY_FOLDER_ID$]",
58                  getFolderIds(folderIds, "DLFileEntry"));
59              sql = StringUtil.replace(
60                  sql, "[$FILE_SHORTCUT_FOLDER_ID$]",
61                  getFolderIds(folderIds, "DLFileShortcut"));
62  
63              SQLQuery q = session.createSQLQuery(sql);
64  
65              q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
66  
67              QueryPos qPos = QueryPos.getInstance(q);
68  
69              for (int i = 0; i < folderIds.size(); i++) {
70                  Long folderId = folderIds.get(i);
71  
72                  qPos.add(folderId);
73              }
74  
75              for (int i = 0; i < folderIds.size(); i++) {
76                  Long folderId = folderIds.get(i);
77  
78                  qPos.add(folderId);
79              }
80  
81              int count = 0;
82  
83              Iterator<Long> itr = q.list().iterator();
84  
85              while (itr.hasNext()) {
86                  Long l = itr.next();
87  
88                  if (l != null) {
89                      count += l.intValue();
90                  }
91              }
92  
93              return count;
94          }
95          catch (Exception e) {
96              throw new SystemException(e);
97          }
98          finally {
99              closeSession(session);
100         }
101     }
102 
103     public List<Object> findByFolderIds(
104             List<Long> folderIds, int start, int end)
105         throws SystemException {
106 
107         Session session = null;
108 
109         try {
110             session = openSession();
111 
112             String sql = CustomSQLUtil.get(FIND_BY_FOLDER_IDS);
113 
114             sql = StringUtil.replace(
115                 sql, "[$FILE_ENTRY_FOLDER_ID$]",
116                 getFolderIds(folderIds, "DLFileEntry"));
117             sql = StringUtil.replace(
118                 sql, "[$FILE_SHORTCUT_FOLDER_ID$]",
119                 getFolderIds(folderIds, "DLFileShortcut"));
120 
121             SQLQuery q = session.createSQLQuery(sql);
122 
123             q.addScalar("modelFolderId", Type.LONG);
124             q.addScalar("name", Type.STRING);
125             q.addScalar("title", Type.STRING);
126             q.addScalar("fileShortcutId", Type.LONG);
127 
128             QueryPos qPos = QueryPos.getInstance(q);
129 
130             for (int i = 0; i < folderIds.size(); i++) {
131                 Long folderId = folderIds.get(i);
132 
133                 qPos.add(folderId);
134             }
135 
136             for (int i = 0; i < folderIds.size(); i++) {
137                 Long folderId = folderIds.get(i);
138 
139                 qPos.add(folderId);
140             }
141 
142             List<Object> fileEntriesAndShortcuts = new ArrayList<Object>();
143 
144             Iterator<Object[]> itr = (Iterator<Object[]>)QueryUtil.iterate(
145                 q, getDialect(), start, end);
146 
147             while (itr.hasNext()) {
148                 Object[] array = itr.next();
149 
150                 Long folderId = (Long)array[0];
151                 String name = (String)array[1];
152                 //String title = (String)array[2];
153                 long fileShortcutId = ((Long)array[3]).longValue();
154 
155                 Object obj = null;
156 
157                 if (fileShortcutId > 0) {
158                     obj = DLFileShortcutUtil.findByPrimaryKey(fileShortcutId);
159                 }
160                 else {
161                     obj = DLFileEntryUtil.findByF_N(folderId.longValue(), name);
162                 }
163 
164                 fileEntriesAndShortcuts.add(obj);
165             }
166 
167             return fileEntriesAndShortcuts;
168         }
169         catch (Exception e) {
170             throw new SystemException(e);
171         }
172         finally {
173             closeSession(session);
174         }
175     }
176 
177     protected String getFolderIds(List<Long> folderIds, String table) {
178         StringBuilder sb = new StringBuilder();
179 
180         for (int i = 0; i < folderIds.size(); i++) {
181             sb.append(table);
182             sb.append(".folderId = ? ");
183 
184             if ((i + 1) != folderIds.size()) {
185                 sb.append("OR ");
186             }
187         }
188 
189         return sb.toString();
190     }
191 
192 }