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