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.portal.service.persistence;
022:
023: import com.liferay.portal.NoSuchUserGroupException;
024: import com.liferay.portal.SystemException;
025: import com.liferay.portal.kernel.util.OrderByComparator;
026: import com.liferay.portal.kernel.util.StringMaker;
027: import com.liferay.portal.kernel.util.StringPool;
028: import com.liferay.portal.kernel.util.StringUtil;
029: import com.liferay.portal.kernel.util.Validator;
030: import com.liferay.portal.model.UserGroup;
031: import com.liferay.portal.model.impl.UserGroupImpl;
032: import com.liferay.portal.model.impl.UserGroupModelImpl;
033: import com.liferay.portal.spring.hibernate.CustomSQLUtil;
034: import com.liferay.portal.spring.hibernate.FinderCache;
035: import com.liferay.portal.spring.hibernate.HibernateUtil;
036: import com.liferay.util.dao.hibernate.QueryPos;
037: import com.liferay.util.dao.hibernate.QueryUtil;
038:
039: import java.util.Iterator;
040: import java.util.LinkedHashMap;
041: import java.util.List;
042: import java.util.Map;
043:
044: import org.hibernate.Hibernate;
045: import org.hibernate.SQLQuery;
046: import org.hibernate.Session;
047:
048: /**
049: * <a href="UserGroupFinderImpl.java.html"><b><i>View Source</i></b></a>
050: *
051: * @author Charles May
052: *
053: */
054: public class UserGroupFinderImpl implements UserGroupFinder {
055:
056: public static String COUNT_BY_C_N_D = UserGroupFinder.class
057: .getName()
058: + ".countByC_N_D";
059:
060: public static String FIND_BY_C_N = UserGroupFinder.class.getName()
061: + ".findByC_N";
062:
063: public static String FIND_BY_C_N_D = UserGroupFinder.class
064: .getName()
065: + ".findByC_N_D";
066:
067: public static String JOIN_BY_GROUPS_PERMISSIONS = UserGroupFinder.class
068: .getName()
069: + ".joinByGroupsPermissions";
070:
071: public static String JOIN_BY_USER_GROUPS_GROUPS = UserGroupFinder.class
072: .getName()
073: + ".joinByUserGroupsGroups";
074:
075: public static String JOIN_BY_USER_GROUPS_ROLES = UserGroupFinder.class
076: .getName()
077: + ".joinByUserGroupsRoles";
078:
079: public int countByC_N_D(long companyId, String name,
080: String description, LinkedHashMap params)
081: throws SystemException {
082:
083: name = StringUtil.lowerCase(name);
084: description = StringUtil.lowerCase(description);
085:
086: Session session = null;
087:
088: try {
089: session = HibernateUtil.openSession();
090:
091: String sql = CustomSQLUtil.get(COUNT_BY_C_N_D);
092:
093: sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params));
094: sql = StringUtil
095: .replace(sql, "[$WHERE$]", getWhere(params));
096:
097: SQLQuery q = session.createSQLQuery(sql);
098:
099: q.addScalar(HibernateUtil.getCountColumnName(),
100: Hibernate.LONG);
101:
102: QueryPos qPos = QueryPos.getInstance(q);
103:
104: setJoin(qPos, params);
105: qPos.add(companyId);
106: qPos.add(name);
107: qPos.add(name);
108: qPos.add(description);
109: qPos.add(description);
110:
111: Iterator itr = q.list().iterator();
112:
113: if (itr.hasNext()) {
114: Long count = (Long) itr.next();
115:
116: if (count != null) {
117: return count.intValue();
118: }
119: }
120:
121: return 0;
122: } catch (Exception e) {
123: throw new SystemException(e);
124: } finally {
125: HibernateUtil.closeSession(session);
126: }
127: }
128:
129: public UserGroup findByC_N(long companyId, String name)
130: throws NoSuchUserGroupException, SystemException {
131:
132: name = StringUtil.lowerCase(name);
133:
134: boolean finderClassNameCacheEnabled = UserGroupModelImpl.CACHE_ENABLED;
135: String finderClassName = UserGroup.class.getName();
136: String finderMethodName = "customFindByC_N";
137: String finderParams[] = new String[] { Long.class.getName(),
138: String.class.getName() };
139: Object finderArgs[] = new Object[] { new Long(companyId), name };
140:
141: Object result = FinderCache.getResult(finderClassName,
142: finderMethodName, finderParams, finderArgs);
143:
144: if (result == null) {
145: Session session = null;
146:
147: try {
148: session = HibernateUtil.openSession();
149:
150: String sql = CustomSQLUtil.get(FIND_BY_C_N);
151:
152: SQLQuery q = session.createSQLQuery(sql);
153:
154: q.addEntity("UserGroup", UserGroupImpl.class);
155:
156: QueryPos qPos = QueryPos.getInstance(q);
157:
158: qPos.add(companyId);
159: qPos.add(name);
160:
161: Iterator itr = q.list().iterator();
162:
163: if (itr.hasNext()) {
164: UserGroup userGroup = (UserGroup) itr.next();
165:
166: FinderCache.putResult(finderClassNameCacheEnabled,
167: finderClassName, finderMethodName,
168: finderParams, finderArgs, userGroup);
169:
170: return userGroup;
171: }
172: } catch (Exception e) {
173: throw new SystemException(e);
174: } finally {
175: HibernateUtil.closeSession(session);
176: }
177:
178: throw new NoSuchUserGroupException(
179: "No UserGroup exists with the key {companyId="
180: + companyId + ", name=" + name + "}");
181: } else {
182: return (UserGroup) result;
183: }
184: }
185:
186: public List findByC_N_D(long companyId, String name,
187: String description, LinkedHashMap params, int begin,
188: int end, OrderByComparator obc) throws SystemException {
189:
190: name = StringUtil.lowerCase(name);
191: description = StringUtil.lowerCase(description);
192:
193: Session session = null;
194:
195: try {
196: session = HibernateUtil.openSession();
197:
198: String sql = CustomSQLUtil.get(FIND_BY_C_N_D);
199:
200: sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params));
201: sql = StringUtil
202: .replace(sql, "[$WHERE$]", getWhere(params));
203: sql = CustomSQLUtil.replaceOrderBy(sql, obc);
204:
205: SQLQuery q = session.createSQLQuery(sql);
206:
207: q.addEntity("UserGroup", UserGroupImpl.class);
208:
209: QueryPos qPos = QueryPos.getInstance(q);
210:
211: setJoin(qPos, params);
212: qPos.add(companyId);
213: qPos.add(name);
214: qPos.add(name);
215: qPos.add(description);
216: qPos.add(description);
217:
218: return QueryUtil.list(q, HibernateUtil.getDialect(), begin,
219: end);
220: } catch (Exception e) {
221: throw new SystemException(e);
222: } finally {
223: HibernateUtil.closeSession(session);
224: }
225: }
226:
227: protected String getJoin(LinkedHashMap params) {
228: if (params == null) {
229: return StringPool.BLANK;
230: }
231:
232: StringMaker sm = new StringMaker();
233:
234: Iterator itr = params.entrySet().iterator();
235:
236: while (itr.hasNext()) {
237: Map.Entry entry = (Map.Entry) itr.next();
238:
239: String key = (String) entry.getKey();
240: Object value = entry.getValue();
241:
242: if (Validator.isNotNull(value)) {
243: sm.append(getJoin(key));
244: }
245: }
246:
247: return sm.toString();
248: }
249:
250: protected String getJoin(String key) {
251: String join = StringPool.BLANK;
252:
253: if (key.equals("permissionsResourceId")) {
254: join = CustomSQLUtil.get(JOIN_BY_GROUPS_PERMISSIONS);
255: } else if (key.equals("userGroupsGroups")) {
256: join = CustomSQLUtil.get(JOIN_BY_USER_GROUPS_GROUPS);
257: } else if (key.equals("userGroupsRoles")) {
258: join = CustomSQLUtil.get(JOIN_BY_USER_GROUPS_ROLES);
259: }
260:
261: if (Validator.isNotNull(join)) {
262: int pos = join.indexOf("WHERE");
263:
264: if (pos != -1) {
265: join = join.substring(0, pos);
266: }
267: }
268:
269: return join;
270: }
271:
272: protected String getWhere(LinkedHashMap params) {
273: if (params == null) {
274: return StringPool.BLANK;
275: }
276:
277: StringMaker sm = new StringMaker();
278:
279: Iterator itr = params.entrySet().iterator();
280:
281: while (itr.hasNext()) {
282: Map.Entry entry = (Map.Entry) itr.next();
283:
284: String key = (String) entry.getKey();
285: Object value = entry.getValue();
286:
287: if (Validator.isNotNull(value)) {
288: sm.append(getWhere(key));
289: }
290: }
291:
292: return sm.toString();
293: }
294:
295: protected String getWhere(String key) {
296: String join = StringPool.BLANK;
297:
298: if (key.equals("permissionsResourceId")) {
299: join = CustomSQLUtil.get(JOIN_BY_GROUPS_PERMISSIONS);
300: } else if (key.equals("userGroupsGroups")) {
301: join = CustomSQLUtil.get(JOIN_BY_USER_GROUPS_GROUPS);
302: } else if (key.equals("userGroupsRoles")) {
303: join = CustomSQLUtil.get(JOIN_BY_USER_GROUPS_ROLES);
304: }
305:
306: if (Validator.isNotNull(join)) {
307: int pos = join.indexOf("WHERE");
308:
309: if (pos != -1) {
310: join = join.substring(pos + 5, join.length()) + " AND ";
311: }
312: }
313:
314: return join;
315: }
316:
317: protected void setJoin(QueryPos qPos, LinkedHashMap params) {
318: if (params != null) {
319: Iterator itr = params.entrySet().iterator();
320:
321: while (itr.hasNext()) {
322: Map.Entry entry = (Map.Entry) itr.next();
323:
324: Object value = entry.getValue();
325:
326: if (value instanceof Long) {
327: Long valueLong = (Long) value;
328:
329: if (Validator.isNotNull(valueLong)) {
330: qPos.add(valueLong);
331: }
332: } else if (value instanceof String) {
333: String valueString = (String) value;
334:
335: if (Validator.isNotNull(valueString)) {
336: qPos.add(valueString);
337: }
338: }
339: }
340: }
341: }
342:
343: }
|