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