001
014
015 package com.liferay.portal.service.persistence;
016
017 import com.liferay.portal.kernel.dao.orm.QueryPos;
018 import com.liferay.portal.kernel.dao.orm.QueryUtil;
019 import com.liferay.portal.kernel.dao.orm.SQLQuery;
020 import com.liferay.portal.kernel.dao.orm.Session;
021 import com.liferay.portal.kernel.dao.orm.Type;
022 import com.liferay.portal.kernel.exception.SystemException;
023 import com.liferay.portal.kernel.util.OrderByComparator;
024 import com.liferay.portal.kernel.util.StringBundler;
025 import com.liferay.portal.kernel.util.StringPool;
026 import com.liferay.portal.kernel.util.StringUtil;
027 import com.liferay.portal.kernel.util.Validator;
028 import com.liferay.portal.model.Team;
029 import com.liferay.portal.model.impl.TeamImpl;
030 import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
031 import com.liferay.util.dao.orm.CustomSQLUtil;
032
033 import java.util.Iterator;
034 import java.util.LinkedHashMap;
035 import java.util.List;
036 import java.util.Map;
037
038
041 public class TeamFinderImpl
042 extends BasePersistenceImpl<Team> implements TeamFinder {
043
044 public static String COUNT_BY_G_N_D =
045 TeamFinder.class.getName() + ".countByG_N_D";
046
047 public static String FIND_BY_G_N_D =
048 TeamFinder.class.getName() + ".findByG_N_D";
049
050 public static String JOIN_BY_USERS_TEAMS =
051 TeamFinder.class.getName() + ".joinByUsersTeams";
052
053 public int countByG_N_D(
054 long groupId, String name, String description,
055 LinkedHashMap<String, Object> params)
056 throws SystemException {
057
058 name = StringUtil.lowerCase(name);
059 description = StringUtil.lowerCase(description);
060
061 Session session = null;
062
063 try {
064 session = openSession();
065
066 String sql = CustomSQLUtil.get(COUNT_BY_G_N_D);
067
068 sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params));
069 sql = StringUtil.replace(sql, "[$WHERE$]", getWhere(params));
070
071 SQLQuery q = session.createSQLQuery(sql);
072
073 q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
074
075 QueryPos qPos = QueryPos.getInstance(q);
076
077 setJoin(qPos, params);
078 qPos.add(groupId);
079 qPos.add(name);
080 qPos.add(name);
081 qPos.add(description);
082 qPos.add(description);
083
084 Iterator<Long> itr = q.list().iterator();
085
086 if (itr.hasNext()) {
087 Long count = itr.next();
088
089 if (count != null) {
090 return count.intValue();
091 }
092 }
093
094 return 0;
095 }
096 catch (Exception e) {
097 throw new SystemException(e);
098 }
099 finally {
100 closeSession(session);
101 }
102 }
103
104 public List<Team> findByG_N_D(
105 long groupId, String name, String description,
106 LinkedHashMap<String, Object> params, int start, int end,
107 OrderByComparator obc)
108 throws SystemException {
109
110 name = StringUtil.lowerCase(name);
111 description = StringUtil.lowerCase(description);
112
113 Session session = null;
114
115 try {
116 session = openSession();
117
118 String sql = CustomSQLUtil.get(FIND_BY_G_N_D);
119
120 sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params));
121 sql = StringUtil.replace(sql, "[$WHERE$]", getWhere(params));
122 sql = CustomSQLUtil.replaceOrderBy(sql, obc);
123
124 SQLQuery q = session.createSQLQuery(sql);
125
126 q.addEntity("Team", TeamImpl.class);
127
128 QueryPos qPos = QueryPos.getInstance(q);
129
130 setJoin(qPos, params);
131 qPos.add(groupId);
132 qPos.add(name);
133 qPos.add(name);
134 qPos.add(description);
135 qPos.add(description);
136
137 return (List<Team>)QueryUtil.list(q, getDialect(), start, end);
138 }
139 catch (Exception e) {
140 throw new SystemException(e);
141 }
142 finally {
143 closeSession(session);
144 }
145 }
146
147 protected String getJoin(LinkedHashMap<String, Object> params) {
148 if ((params == null) || params.isEmpty()) {
149 return StringPool.BLANK;
150 }
151
152 StringBundler sb = new StringBundler(params.size());
153
154 Iterator<Map.Entry<String, Object>> itr = params.entrySet().iterator();
155
156 while (itr.hasNext()) {
157 Map.Entry<String, Object> entry = itr.next();
158
159 String key = entry.getKey();
160 Object value = entry.getValue();
161
162 if (Validator.isNotNull(value)) {
163 sb.append(getJoin(key));
164 }
165 }
166
167 return sb.toString();
168 }
169
170 protected String getJoin(String key) {
171 String join = StringPool.BLANK;
172
173 if (key.equals("usersTeams")) {
174 join = CustomSQLUtil.get(JOIN_BY_USERS_TEAMS);
175 }
176
177 if (Validator.isNotNull(join)) {
178 int pos = join.indexOf("WHERE");
179
180 if (pos != -1) {
181 join = join.substring(0, pos);
182 }
183 }
184
185 return join;
186 }
187
188 protected String getWhere(LinkedHashMap<String, Object> params) {
189 if ((params == null) || params.isEmpty()) {
190 return StringPool.BLANK;
191 }
192
193 StringBundler sb = new StringBundler(params.size());
194
195 Iterator<Map.Entry<String, Object>> itr = params.entrySet().iterator();
196
197 while (itr.hasNext()) {
198 Map.Entry<String, Object> entry = itr.next();
199
200 String key = entry.getKey();
201 Object value = entry.getValue();
202
203 if (Validator.isNotNull(value)) {
204 sb.append(getWhere(key));
205 }
206 }
207
208 return sb.toString();
209 }
210
211 protected String getWhere(String key) {
212 String join = StringPool.BLANK;
213
214 if (key.equals("usersTeams")) {
215 join = CustomSQLUtil.get(JOIN_BY_USERS_TEAMS);
216 }
217
218 if (Validator.isNotNull(join)) {
219 int pos = join.indexOf("WHERE");
220
221 if (pos != -1) {
222 join = join.substring(pos + 5, join.length()).concat(" AND ");
223 }
224 else {
225 join = StringPool.BLANK;
226 }
227 }
228
229 return join;
230 }
231
232 protected void setJoin(
233 QueryPos qPos, LinkedHashMap<String, Object> params) {
234
235 if (params != null) {
236 Iterator<Map.Entry<String, Object>> itr =
237 params.entrySet().iterator();
238
239 while (itr.hasNext()) {
240 Map.Entry<String, Object> entry = itr.next();
241
242 Object value = entry.getValue();
243
244 if (value instanceof Long) {
245 Long valueLong = (Long)value;
246
247 if (Validator.isNotNull(valueLong)) {
248 qPos.add(valueLong);
249 }
250 }
251 }
252 }
253 }
254
255 }