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 = CustomSQLUtil.keywords(name)[0];
063 description = CustomSQLUtil.keywords(description)[0];
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 = CustomSQLUtil.keywords(name)[0];
117 description = CustomSQLUtil.keywords(description)[0];
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 for (Map.Entry<String, Object> entry : params.entrySet()) {
162 String key = entry.getKey();
163 Object value = entry.getValue();
164
165 if (Validator.isNotNull(value)) {
166 sb.append(getJoin(key));
167 }
168 }
169
170 return sb.toString();
171 }
172
173 protected String getJoin(String key) {
174 String join = StringPool.BLANK;
175
176 if (key.equals("usersUserGroups")) {
177 join = CustomSQLUtil.get(JOIN_BY_USERS_USER_GROUPS);
178 }
179 else if (key.equals("usersTeams")) {
180 join = CustomSQLUtil.get(JOIN_BY_USERS_TEAMS);
181 }
182
183 if (Validator.isNotNull(join)) {
184 int pos = join.indexOf("WHERE");
185
186 if (pos != -1) {
187 join = join.substring(0, pos);
188 }
189 }
190
191 return join;
192 }
193
194 protected String getWhere(LinkedHashMap<String, Object> params) {
195 if ((params == null) || params.isEmpty()) {
196 return StringPool.BLANK;
197 }
198
199 StringBundler sb = new StringBundler(params.size());
200
201 for (Map.Entry<String, Object> entry : params.entrySet()) {
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("usersUserGroups")) {
217 join = CustomSQLUtil.get(JOIN_BY_USERS_USER_GROUPS);
218 }
219 else if (key.equals("usersTeams")) {
220 join = CustomSQLUtil.get(JOIN_BY_USERS_TEAMS);
221 }
222
223 if (Validator.isNotNull(join)) {
224 int pos = join.indexOf("WHERE");
225
226 if (pos != -1) {
227 join = join.substring(pos + 5, join.length()).concat(" AND ");
228 }
229 else {
230 join = StringPool.BLANK;
231 }
232 }
233
234 return join;
235 }
236
237 protected void setJoin(
238 QueryPos qPos, LinkedHashMap<String, Object> params) {
239
240 if (params == null) {
241 return;
242 }
243
244 for (Map.Entry<String, Object> entry : params.entrySet()) {
245 Object value = entry.getValue();
246
247 if (value instanceof Long) {
248 Long valueLong = (Long)value;
249
250 if (Validator.isNotNull(valueLong)) {
251 qPos.add(valueLong);
252 }
253 }
254 }
255 }
256
257 }