001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.service.persistence.impl;
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.security.permission.InlineSQLHelperUtil;
031    import com.liferay.portal.service.persistence.TeamFinder;
032    import com.liferay.util.dao.orm.CustomSQLUtil;
033    
034    import java.util.Iterator;
035    import java.util.LinkedHashMap;
036    import java.util.List;
037    import java.util.Map;
038    
039    /**
040     * @author Brian Wing Shun Chan
041     */
042    public class TeamFinderImpl extends TeamFinderBaseImpl 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    
061                    return doCountByG_N_D(groupId, name, description, params, false);
062            }
063    
064            @Override
065            public int filterCountByG_N_D(
066                    long groupId, String name, String description,
067                    LinkedHashMap<String, Object> params) {
068    
069                    return doCountByG_N_D(groupId, name, description, params, true);
070            }
071    
072            @Override
073            public List<Team> filterFindByG_N_D(
074                    long groupId, String name, String description,
075                    LinkedHashMap<String, Object> params, int start, int end,
076                    OrderByComparator<Team> obc) {
077    
078                    return doFindByG_N_D(
079                            groupId, name, description, params, start, end, obc, true);
080            }
081    
082            @Override
083            public List<Team> findByG_N_D(
084                    long groupId, String name, String description,
085                    LinkedHashMap<String, Object> params, int start, int end,
086                    OrderByComparator<Team> obc) {
087    
088                    return doFindByG_N_D(
089                            groupId, name, description, params, start, end, obc, false);
090            }
091    
092            protected int doCountByG_N_D(
093                    long groupId, String name, String description,
094                    LinkedHashMap<String, Object> params, boolean inlineSQLHelper) {
095    
096                    name = CustomSQLUtil.keywords(name)[0];
097                    description = CustomSQLUtil.keywords(description)[0];
098    
099                    Session session = null;
100    
101                    try {
102                            session = openSession();
103    
104                            String sql = CustomSQLUtil.get(COUNT_BY_G_N_D);
105    
106                            if (inlineSQLHelper) {
107                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
108                                            sql, Team.class.getName(), "Team.teamId", groupId);
109                            }
110    
111                            sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params));
112                            sql = StringUtil.replace(sql, "[$WHERE$]", getWhere(params));
113    
114                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
115    
116                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
117    
118                            QueryPos qPos = QueryPos.getInstance(q);
119    
120                            setJoin(qPos, params);
121    
122                            qPos.add(groupId);
123                            qPos.add(name);
124                            qPos.add(name);
125                            qPos.add(description);
126                            qPos.add(description);
127    
128                            Iterator<Long> itr = q.iterate();
129    
130                            if (itr.hasNext()) {
131                                    Long count = itr.next();
132    
133                                    if (count != null) {
134                                            return count.intValue();
135                                    }
136                            }
137    
138                            return 0;
139                    }
140                    catch (Exception e) {
141                            throw new SystemException(e);
142                    }
143                    finally {
144                            closeSession(session);
145                    }
146            }
147    
148            protected List<Team> doFindByG_N_D(
149                    long groupId, String name, String description,
150                    LinkedHashMap<String, Object> params, int start, int end,
151                    OrderByComparator<Team> obc, boolean inlineSQLHelper) {
152    
153                    name = CustomSQLUtil.keywords(name)[0];
154                    description = CustomSQLUtil.keywords(description)[0];
155    
156                    Session session = null;
157    
158                    try {
159                            session = openSession();
160    
161                            String sql = CustomSQLUtil.get(FIND_BY_G_N_D);
162    
163                            if (inlineSQLHelper) {
164                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
165                                            sql, Team.class.getName(), "Team.teamId", groupId);
166                            }
167    
168                            sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params));
169                            sql = StringUtil.replace(sql, "[$WHERE$]", getWhere(params));
170                            sql = CustomSQLUtil.replaceOrderBy(sql, obc);
171    
172                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
173    
174                            q.addEntity("Team", TeamImpl.class);
175    
176                            QueryPos qPos = QueryPos.getInstance(q);
177    
178                            setJoin(qPos, params);
179    
180                            qPos.add(groupId);
181                            qPos.add(name);
182                            qPos.add(name);
183                            qPos.add(description);
184                            qPos.add(description);
185    
186                            return (List<Team>)QueryUtil.list(q, getDialect(), start, end);
187                    }
188                    catch (Exception e) {
189                            throw new SystemException(e);
190                    }
191                    finally {
192                            closeSession(session);
193                    }
194            }
195    
196            protected String getJoin(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                    for (Map.Entry<String, Object> entry : params.entrySet()) {
204                            String key = entry.getKey();
205                            Object value = entry.getValue();
206    
207                            if (Validator.isNotNull(value)) {
208                                    sb.append(getJoin(key));
209                            }
210                    }
211    
212                    return sb.toString();
213            }
214    
215            protected String getJoin(String key) {
216                    String join = StringPool.BLANK;
217    
218                    if (key.equals("usersUserGroups")) {
219                            join = CustomSQLUtil.get(JOIN_BY_USERS_USER_GROUPS);
220                    }
221                    else if (key.equals("usersTeams")) {
222                            join = CustomSQLUtil.get(JOIN_BY_USERS_TEAMS);
223                    }
224    
225                    if (Validator.isNotNull(join)) {
226                            int pos = join.indexOf("WHERE");
227    
228                            if (pos != -1) {
229                                    join = join.substring(0, pos);
230                            }
231                    }
232    
233                    return join;
234            }
235    
236            protected String getWhere(LinkedHashMap<String, Object> params) {
237                    if ((params == null) || params.isEmpty()) {
238                            return StringPool.BLANK;
239                    }
240    
241                    StringBundler sb = new StringBundler(params.size());
242    
243                    for (Map.Entry<String, Object> entry : params.entrySet()) {
244                            String key = entry.getKey();
245                            Object value = entry.getValue();
246    
247                            if (Validator.isNotNull(value)) {
248                                    sb.append(getWhere(key));
249                            }
250                    }
251    
252                    return sb.toString();
253            }
254    
255            protected String getWhere(String key) {
256                    String join = StringPool.BLANK;
257    
258                    if (key.equals("usersUserGroups")) {
259                            join = CustomSQLUtil.get(JOIN_BY_USERS_USER_GROUPS);
260                    }
261                    else if (key.equals("usersTeams")) {
262                            join = CustomSQLUtil.get(JOIN_BY_USERS_TEAMS);
263                    }
264    
265                    if (Validator.isNotNull(join)) {
266                            int pos = join.indexOf("WHERE");
267    
268                            if (pos != -1) {
269                                    join = join.substring(pos + 5, join.length()).concat(" AND ");
270                            }
271                            else {
272                                    join = StringPool.BLANK;
273                            }
274                    }
275    
276                    return join;
277            }
278    
279            protected void setJoin(
280                    QueryPos qPos, LinkedHashMap<String, Object> params) {
281    
282                    if (params == null) {
283                            return;
284                    }
285    
286                    for (Map.Entry<String, Object> entry : params.entrySet()) {
287                            Object value = entry.getValue();
288    
289                            if (value instanceof Long) {
290                                    Long valueLong = (Long)value;
291    
292                                    if (Validator.isNotNull(valueLong)) {
293                                            qPos.add(valueLong);
294                                    }
295                            }
296                    }
297            }
298    
299    }