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