1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.service.persistence;
16  
17  import com.liferay.portal.kernel.dao.orm.QueryPos;
18  import com.liferay.portal.kernel.dao.orm.QueryUtil;
19  import com.liferay.portal.kernel.dao.orm.SQLQuery;
20  import com.liferay.portal.kernel.dao.orm.Session;
21  import com.liferay.portal.kernel.dao.orm.Type;
22  import com.liferay.portal.kernel.exception.SystemException;
23  import com.liferay.portal.kernel.util.OrderByComparator;
24  import com.liferay.portal.kernel.util.StringBundler;
25  import com.liferay.portal.kernel.util.StringPool;
26  import com.liferay.portal.kernel.util.StringUtil;
27  import com.liferay.portal.kernel.util.Validator;
28  import com.liferay.portal.model.Team;
29  import com.liferay.portal.model.impl.TeamImpl;
30  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
31  import com.liferay.util.dao.orm.CustomSQLUtil;
32  
33  import java.util.Iterator;
34  import java.util.LinkedHashMap;
35  import java.util.List;
36  import java.util.Map;
37  
38  /**
39   * <a href="TeamFinderImpl.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Brian Wing Shun Chan
42   */
43  public class TeamFinderImpl
44      extends BasePersistenceImpl<Team> implements TeamFinder {
45  
46      public static String COUNT_BY_G_N_D =
47          TeamFinder.class.getName() + ".countByG_N_D";
48  
49      public static String FIND_BY_G_N_D =
50          TeamFinder.class.getName() + ".findByG_N_D";
51  
52      public static String JOIN_BY_USERS_TEAMS =
53          TeamFinder.class.getName() + ".joinByUsersTeams";
54  
55      public int countByG_N_D(
56              long groupId, String name, String description,
57              LinkedHashMap<String, Object> params)
58          throws SystemException {
59  
60          name = StringUtil.lowerCase(name);
61          description = StringUtil.lowerCase(description);
62  
63          Session session = null;
64  
65          try {
66              session = openSession();
67  
68              String sql = CustomSQLUtil.get(COUNT_BY_G_N_D);
69  
70              sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params));
71              sql = StringUtil.replace(sql, "[$WHERE$]", getWhere(params));
72  
73              SQLQuery q = session.createSQLQuery(sql);
74  
75              q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
76  
77              QueryPos qPos = QueryPos.getInstance(q);
78  
79              setJoin(qPos, params);
80              qPos.add(groupId);
81              qPos.add(name);
82              qPos.add(name);
83              qPos.add(description);
84              qPos.add(description);
85  
86              Iterator<Long> itr = q.list().iterator();
87  
88              if (itr.hasNext()) {
89                  Long count = itr.next();
90  
91                  if (count != null) {
92                      return count.intValue();
93                  }
94              }
95  
96              return 0;
97          }
98          catch (Exception e) {
99              throw new SystemException(e);
100         }
101         finally {
102             closeSession(session);
103         }
104     }
105 
106     public List<Team> findByG_N_D(
107             long groupId, String name, String description,
108             LinkedHashMap<String, Object> params, int start, int end,
109             OrderByComparator obc)
110         throws SystemException {
111 
112         name = StringUtil.lowerCase(name);
113         description = StringUtil.lowerCase(description);
114 
115         Session session = null;
116 
117         try {
118             session = openSession();
119 
120             String sql = CustomSQLUtil.get(FIND_BY_G_N_D);
121 
122             sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params));
123             sql = StringUtil.replace(sql, "[$WHERE$]", getWhere(params));
124             sql = CustomSQLUtil.replaceOrderBy(sql, obc);
125 
126             SQLQuery q = session.createSQLQuery(sql);
127 
128             q.addEntity("Team", TeamImpl.class);
129 
130             QueryPos qPos = QueryPos.getInstance(q);
131 
132             setJoin(qPos, params);
133             qPos.add(groupId);
134             qPos.add(name);
135             qPos.add(name);
136             qPos.add(description);
137             qPos.add(description);
138 
139             return (List<Team>)QueryUtil.list(q, getDialect(), start, end);
140         }
141         catch (Exception e) {
142             throw new SystemException(e);
143         }
144         finally {
145             closeSession(session);
146         }
147     }
148 
149     protected String getJoin(LinkedHashMap<String, Object> params) {
150         if ((params == null) || params.isEmpty()) {
151             return StringPool.BLANK;
152         }
153 
154         StringBundler sb = new StringBundler(params.size());
155 
156         Iterator<Map.Entry<String, Object>> itr = params.entrySet().iterator();
157 
158         while (itr.hasNext()) {
159             Map.Entry<String, Object> entry = itr.next();
160 
161             String key = entry.getKey();
162             Object value = entry.getValue();
163 
164             if (Validator.isNotNull(value)) {
165                 sb.append(getJoin(key));
166             }
167         }
168 
169         return sb.toString();
170     }
171 
172     protected String getJoin(String key) {
173         String join = StringPool.BLANK;
174 
175         if (key.equals("usersTeams")) {
176             join = CustomSQLUtil.get(JOIN_BY_USERS_TEAMS);
177         }
178 
179         if (Validator.isNotNull(join)) {
180             int pos = join.indexOf("WHERE");
181 
182             if (pos != -1) {
183                 join = join.substring(0, pos);
184             }
185         }
186 
187         return join;
188     }
189 
190     protected String getWhere(LinkedHashMap<String, Object> params) {
191         if ((params == null) || params.isEmpty()) {
192             return StringPool.BLANK;
193         }
194 
195         StringBundler sb = new StringBundler(params.size());
196 
197         Iterator<Map.Entry<String, Object>> itr = params.entrySet().iterator();
198 
199         while (itr.hasNext()) {
200             Map.Entry<String, Object> entry = itr.next();
201 
202             String key = entry.getKey();
203             Object value = entry.getValue();
204 
205             if (Validator.isNotNull(value)) {
206                 sb.append(getWhere(key));
207             }
208         }
209 
210         return sb.toString();
211     }
212 
213     protected String getWhere(String key) {
214         String join = StringPool.BLANK;
215 
216         if (key.equals("usersTeams")) {
217             join = CustomSQLUtil.get(JOIN_BY_USERS_TEAMS);
218         }
219 
220         if (Validator.isNotNull(join)) {
221             int pos = join.indexOf("WHERE");
222 
223             if (pos != -1) {
224                 join = join.substring(pos + 5, join.length()).concat(" AND ");
225             }
226             else {
227                 join = StringPool.BLANK;
228             }
229         }
230 
231         return join;
232     }
233 
234     protected void setJoin(
235         QueryPos qPos, LinkedHashMap<String, Object> params) {
236 
237         if (params != null) {
238             Iterator<Map.Entry<String, Object>> itr =
239                 params.entrySet().iterator();
240 
241             while (itr.hasNext()) {
242                 Map.Entry<String, Object> entry = itr.next();
243 
244                 Object value = entry.getValue();
245 
246                 if (value instanceof Long) {
247                     Long valueLong = (Long)value;
248 
249                     if (Validator.isNotNull(valueLong)) {
250                         qPos.add(valueLong);
251                     }
252                 }
253             }
254         }
255     }
256 
257 }