1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.documentlibrary.service.persistence;
21  
22  import com.liferay.portal.SystemException;
23  import com.liferay.portal.kernel.dao.orm.QueryPos;
24  import com.liferay.portal.kernel.dao.orm.QueryUtil;
25  import com.liferay.portal.kernel.dao.orm.SQLQuery;
26  import com.liferay.portal.kernel.dao.orm.Session;
27  import com.liferay.portal.kernel.dao.orm.Type;
28  import com.liferay.portal.kernel.util.StringUtil;
29  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
30  import com.liferay.util.dao.orm.CustomSQLUtil;
31  
32  import java.util.ArrayList;
33  import java.util.Iterator;
34  import java.util.List;
35  
36  /**
37   * <a href="DLFileEntryAndShortcutFinderImpl.java.html"><b><i>View Source</i>
38   * </b></a>
39   *
40   * @author Brian Wing Shun Chan
41   *
42   */
43  public class DLFileEntryAndShortcutFinderImpl
44      extends BasePersistenceImpl implements DLFileEntryAndShortcutFinder {
45  
46      public static String COUNT_BY_FOLDER_IDS =
47          DLFileEntryAndShortcutFinder.class.getName() + ".countByFolderIds";
48  
49      public static String FIND_BY_FOLDER_IDS =
50          DLFileEntryAndShortcutFinder.class.getName() + ".findByFolderIds";
51  
52      public int countByFolderIds(List<Long> folderIds) throws SystemException {
53          Session session = null;
54  
55          try {
56              session = openSession();
57  
58              String sql = CustomSQLUtil.get(COUNT_BY_FOLDER_IDS);
59  
60              sql = StringUtil.replace(
61                  sql, "[$FILE_ENTRY_FOLDER_ID$]",
62                  getFolderIds(folderIds, "DLFileEntry"));
63              sql = StringUtil.replace(
64                  sql, "[$FILE_SHORTCUT_FOLDER_ID$]",
65                  getFolderIds(folderIds, "DLFileShortcut"));
66  
67              SQLQuery q = session.createSQLQuery(sql);
68  
69              q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
70  
71              QueryPos qPos = QueryPos.getInstance(q);
72  
73              for (int i = 0; i < folderIds.size(); i++) {
74                  Long folderId = folderIds.get(i);
75  
76                  qPos.add(folderId);
77              }
78  
79              for (int i = 0; i < folderIds.size(); i++) {
80                  Long folderId = folderIds.get(i);
81  
82                  qPos.add(folderId);
83              }
84  
85              int count = 0;
86  
87              Iterator<Long> itr = q.list().iterator();
88  
89              while (itr.hasNext()) {
90                  Long l = itr.next();
91  
92                  if (l != null) {
93                      count += l.intValue();
94                  }
95              }
96  
97              return count;
98          }
99          catch (Exception e) {
100             throw new SystemException(e);
101         }
102         finally {
103             closeSession(session);
104         }
105     }
106 
107     public List<Object> findByFolderIds(
108             List<Long> folderIds, int start, int end)
109         throws SystemException {
110 
111         Session session = null;
112 
113         try {
114             session = openSession();
115 
116             String sql = CustomSQLUtil.get(FIND_BY_FOLDER_IDS);
117 
118             sql = StringUtil.replace(
119                 sql, "[$FILE_ENTRY_FOLDER_ID$]",
120                 getFolderIds(folderIds, "DLFileEntry"));
121             sql = StringUtil.replace(
122                 sql, "[$FILE_SHORTCUT_FOLDER_ID$]",
123                 getFolderIds(folderIds, "DLFileShortcut"));
124 
125             SQLQuery q = session.createSQLQuery(sql);
126 
127             q.addScalar("modelFolderId", Type.LONG);
128             q.addScalar("name", Type.STRING);
129             q.addScalar("title", Type.STRING);
130             q.addScalar("fileShortcutId", Type.LONG);
131 
132             QueryPos qPos = QueryPos.getInstance(q);
133 
134             for (int i = 0; i < folderIds.size(); i++) {
135                 Long folderId = folderIds.get(i);
136 
137                 qPos.add(folderId);
138             }
139 
140             for (int i = 0; i < folderIds.size(); i++) {
141                 Long folderId = folderIds.get(i);
142 
143                 qPos.add(folderId);
144             }
145 
146             List<Object> fileEntriesAndShortcuts = new ArrayList<Object>();
147 
148             Iterator<Object[]> itr = (Iterator<Object[]>)QueryUtil.iterate(
149                 q, getDialect(), start, end);
150 
151             while (itr.hasNext()) {
152                 Object[] array = itr.next();
153 
154                 Long folderId = (Long)array[0];
155                 String name = (String)array[1];
156                 //String title = (String)array[2];
157                 long fileShortcutId = ((Long)array[3]).longValue();
158 
159                 Object obj = null;
160 
161                 if (fileShortcutId > 0) {
162                     obj = DLFileShortcutUtil.findByPrimaryKey(fileShortcutId);
163                 }
164                 else {
165                     obj = DLFileEntryUtil.findByF_N(folderId.longValue(), name);
166                 }
167 
168                 fileEntriesAndShortcuts.add(obj);
169             }
170 
171             return fileEntriesAndShortcuts;
172         }
173         catch (Exception e) {
174             throw new SystemException(e);
175         }
176         finally {
177             closeSession(session);
178         }
179     }
180 
181     protected String getFolderIds(List<Long> folderIds, String table) {
182         StringBuilder sb = new StringBuilder();
183 
184         for (int i = 0; i < folderIds.size(); i++) {
185             sb.append(table);
186             sb.append(".folderId = ? ");
187 
188             if ((i + 1) != folderIds.size()) {
189                 sb.append("OR ");
190             }
191         }
192 
193         return sb.toString();
194     }
195 
196 }