001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.documentlibrary.service.persistence;
022:
023: import com.liferay.portal.SystemException;
024: import com.liferay.portal.kernel.util.StringMaker;
025: import com.liferay.portal.kernel.util.StringUtil;
026: import com.liferay.portal.spring.hibernate.CustomSQLUtil;
027: import com.liferay.portal.spring.hibernate.HibernateUtil;
028: import com.liferay.util.dao.hibernate.QueryPos;
029: import com.liferay.util.dao.hibernate.QueryUtil;
030:
031: import java.util.ArrayList;
032: import java.util.Iterator;
033: import java.util.List;
034:
035: import org.hibernate.Hibernate;
036: import org.hibernate.SQLQuery;
037: import org.hibernate.Session;
038:
039: /**
040: * <a href="DLFileEntryAndShortcutFinderImpl.java.html"><b><i>View Source</i>
041: * </b></a>
042: *
043: * @author Brian Wing Shun Chan
044: *
045: */
046: public class DLFileEntryAndShortcutFinderImpl implements
047: DLFileEntryAndShortcutFinder {
048:
049: public static String COUNT_BY_FOLDER_IDS = DLFileEntryAndShortcutFinder.class
050: .getName()
051: + ".countByFolderIds";
052:
053: public static String FIND_BY_FOLDER_IDS = DLFileEntryAndShortcutFinder.class
054: .getName()
055: + ".findByFolderIds";
056:
057: public int countByFolderIds(List folderIds) throws SystemException {
058: Session session = null;
059:
060: try {
061: session = HibernateUtil.openSession();
062:
063: String sql = CustomSQLUtil.get(COUNT_BY_FOLDER_IDS);
064:
065: sql = StringUtil.replace(sql, "[$FILE_ENTRY_FOLDER_ID$]",
066: getFolderIds(folderIds, "DLFileEntry"));
067: sql = StringUtil.replace(sql,
068: "[$FILE_SHORTCUT_FOLDER_ID$]", getFolderIds(
069: folderIds, "DLFileShortcut"));
070:
071: SQLQuery q = session.createSQLQuery(sql);
072:
073: q.addScalar(HibernateUtil.getCountColumnName(),
074: Hibernate.LONG);
075:
076: QueryPos qPos = QueryPos.getInstance(q);
077:
078: for (int i = 0; i < folderIds.size(); i++) {
079: Long folderId = (Long) folderIds.get(i);
080:
081: qPos.add(folderId);
082: }
083:
084: for (int i = 0; i < folderIds.size(); i++) {
085: Long folderId = (Long) folderIds.get(i);
086:
087: qPos.add(folderId);
088: }
089:
090: int count = 0;
091:
092: Iterator itr = q.list().iterator();
093:
094: while (itr.hasNext()) {
095: Long l = (Long) itr.next();
096:
097: if (l != null) {
098: count += l.intValue();
099: }
100: }
101:
102: return count;
103: } catch (Exception e) {
104: throw new SystemException(e);
105: } finally {
106: HibernateUtil.closeSession(session);
107: }
108: }
109:
110: public List findByFolderIds(List folderIds, int begin, int end)
111: throws SystemException {
112:
113: Session session = null;
114:
115: try {
116: session = HibernateUtil.openSession();
117:
118: String sql = CustomSQLUtil.get(FIND_BY_FOLDER_IDS);
119:
120: sql = StringUtil.replace(sql, "[$FILE_ENTRY_FOLDER_ID$]",
121: getFolderIds(folderIds, "DLFileEntry"));
122: sql = StringUtil.replace(sql,
123: "[$FILE_SHORTCUT_FOLDER_ID$]", getFolderIds(
124: folderIds, "DLFileShortcut"));
125:
126: SQLQuery q = session.createSQLQuery(sql);
127:
128: q.addScalar("folderId", Hibernate.LONG);
129: q.addScalar("name", Hibernate.STRING);
130: q.addScalar("title", Hibernate.STRING);
131: q.addScalar("fileShortcutId", Hibernate.LONG);
132:
133: QueryPos qPos = QueryPos.getInstance(q);
134:
135: for (int i = 0; i < folderIds.size(); i++) {
136: Long folderId = (Long) folderIds.get(i);
137:
138: qPos.add(folderId);
139: }
140:
141: for (int i = 0; i < folderIds.size(); i++) {
142: Long folderId = (Long) folderIds.get(i);
143:
144: qPos.add(folderId);
145: }
146:
147: List list = new ArrayList();
148:
149: Iterator itr = QueryUtil.iterate(q, HibernateUtil
150: .getDialect(), begin, end);
151:
152: while (itr.hasNext()) {
153: Object[] array = (Object[]) itr.next();
154:
155: Long folderId = (Long) array[0];
156: String name = (String) array[1];
157: //String title = (String)array[2];
158: long fileShortcutId = ((Long) array[3]).longValue();
159:
160: Object obj = null;
161:
162: if (fileShortcutId > 0) {
163: obj = DLFileShortcutUtil
164: .findByPrimaryKey(fileShortcutId);
165: } else {
166: obj = DLFileEntryUtil.findByF_N(folderId
167: .longValue(), name);
168: }
169:
170: list.add(obj);
171: }
172:
173: return list;
174: } catch (Exception e) {
175: throw new SystemException(e);
176: } finally {
177: HibernateUtil.closeSession(session);
178: }
179: }
180:
181: protected String getFolderIds(List folderIds, String table) {
182: StringMaker sm = new StringMaker();
183:
184: for (int i = 0; i < folderIds.size(); i++) {
185: sm.append(table);
186: sm.append(".folderId = ? ");
187:
188: if ((i + 1) != folderIds.size()) {
189: sm.append("OR ");
190: }
191: }
192:
193: return sm.toString();
194: }
195:
196: }
|