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