001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portlet.dynamicdatamapping.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.StringPool;
025    import com.liferay.portal.kernel.util.StringUtil;
026    import com.liferay.portal.kernel.util.Validator;
027    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
028    import com.liferay.portlet.dynamicdatamapping.model.DDMTemplate;
029    import com.liferay.portlet.dynamicdatamapping.model.impl.DDMTemplateImpl;
030    import com.liferay.util.dao.orm.CustomSQLUtil;
031    
032    import java.util.Iterator;
033    import java.util.List;
034    
035    /**
036     * @author Eduardo Lundgren
037     * @author Connor McKay
038     */
039    public class DDMTemplateFinderImpl
040            extends BasePersistenceImpl<DDMTemplate> implements DDMTemplateFinder {
041    
042            public static String COUNT_BY_C_G_S_N_D_T_M_L =
043                    DDMTemplateFinder.class.getName() + ".countByC_G_S_N_D_T_M_L";
044    
045            public static String FIND_BY_C_G_S_N_D_T_M_L =
046                    DDMTemplateFinder.class.getName() + ".findByC_G_S_N_D_T_M_L";
047    
048            public int countByKeywords(
049                            long companyId, long groupId, long structureId, String keywords,
050                            String type, String mode)
051                    throws SystemException {
052    
053                    String[] names = null;
054                    String[] descriptions = null;
055                    String[] types = CustomSQLUtil.keywords(type, false);
056                    String[] modes = CustomSQLUtil.keywords(mode, false);
057                    String[] languages = null;
058                    boolean andOperator = false;
059    
060                    if (Validator.isNotNull(keywords)) {
061                            names = CustomSQLUtil.keywords(keywords);
062                            descriptions = CustomSQLUtil.keywords(keywords, false);
063                            languages = CustomSQLUtil.keywords(keywords, false);
064                    }
065                    else {
066                            andOperator = true;
067                    }
068    
069                    return countByC_G_S_N_D_T_M_L(
070                            companyId, groupId, structureId, names, descriptions, types, modes,
071                            languages, andOperator);
072            }
073    
074            public int countByC_G_S_N_D_T_M_L(
075                            long companyId, long groupId, long structureId, String name,
076                            String description, String type, String mode, String language,
077                            boolean andOperator)
078                    throws SystemException {
079    
080                    String[] names = CustomSQLUtil.keywords(name);
081                    String[] descriptions = CustomSQLUtil.keywords(description, false);
082                    String[] types = CustomSQLUtil.keywords(type, false);
083                    String[] modes = CustomSQLUtil.keywords(mode, false);
084                    String[] languages = CustomSQLUtil.keywords(language, false);
085    
086                    return countByC_G_S_N_D_T_M_L(
087                            companyId, groupId, structureId, names, descriptions, types, modes,
088                            languages, andOperator);
089            }
090    
091            public int countByC_G_S_N_D_T_M_L(
092                            long companyId, long groupId, long structureId, String[] names,
093                            String[] descriptions, String[] types, String[] modes,
094                            String[] languages, boolean andOperator)
095                    throws SystemException {
096    
097                    names = CustomSQLUtil.keywords(names);
098                    descriptions = CustomSQLUtil.keywords(descriptions, false);
099                    types = CustomSQLUtil.keywords(types, false);
100                    modes = CustomSQLUtil.keywords(modes, false);
101                    languages = CustomSQLUtil.keywords(languages, false);
102    
103                    Session session = null;
104    
105                    try {
106                            session = openSession();
107    
108                            String sql = CustomSQLUtil.get(COUNT_BY_C_G_S_N_D_T_M_L);
109    
110                            if (groupId <= 0) {
111                                    sql = StringUtil.replace(sql, "(groupId = ?) AND", "");
112                            }
113    
114                            if (structureId <= 0) {
115                                    sql = StringUtil.replace(sql, "(structureId = ?) AND", "");
116                            }
117    
118                            sql = CustomSQLUtil.replaceKeywords(
119                                    sql, "lower(name)", StringPool.LIKE, false, names);
120                            sql = CustomSQLUtil.replaceKeywords(
121                                    sql, "description", StringPool.LIKE, false, descriptions);
122                            sql = CustomSQLUtil.replaceKeywords(
123                                    sql, "type", StringPool.LIKE, false, types);
124                            sql = CustomSQLUtil.replaceKeywords(
125                                    sql, "mode", StringPool.LIKE, false, modes);
126                            sql = CustomSQLUtil.replaceKeywords(
127                                    sql, "language", StringPool.LIKE, false, languages);
128                            sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
129    
130                            SQLQuery q = session.createSQLQuery(sql);
131    
132                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
133    
134                            QueryPos qPos = QueryPos.getInstance(q);
135    
136                            qPos.add(companyId);
137    
138                            if (groupId > 0) {
139                                    qPos.add(groupId);
140                            }
141    
142                            if (structureId > 0) {
143                                    qPos.add(structureId);
144                            }
145    
146                            qPos.add(names, 2);
147                            qPos.add(descriptions, 2);
148                            qPos.add(types, 2);
149                            qPos.add(modes, 2);
150                            qPos.add(languages, 2);
151    
152                            Iterator<Long> itr = q.iterate();
153    
154                            if (itr.hasNext()) {
155                                    Long count = itr.next();
156    
157                                    if (count != null) {
158                                            return count.intValue();
159                                    }
160                            }
161    
162                            return 0;
163                    }
164                    catch (Exception e) {
165                            throw new SystemException(e);
166                    }
167                    finally {
168                            closeSession(session);
169                    }
170            }
171    
172            public List<DDMTemplate> findByKeywords(
173                            long companyId, long groupId, long structureId, String keywords,
174                            String type, String mode, int start, int end,
175                            OrderByComparator orderByComparator)
176                    throws SystemException {
177    
178                    String[] names = null;
179                    String[] descriptions = null;
180                    String[] types = CustomSQLUtil.keywords(type, false);
181                    String[] modes = CustomSQLUtil.keywords(mode, false);
182                    String[] languages = null;
183                    boolean andOperator = false;
184    
185                    if (Validator.isNotNull(keywords)) {
186                            names = CustomSQLUtil.keywords(keywords);
187                            descriptions = CustomSQLUtil.keywords(keywords, false);
188                            languages = CustomSQLUtil.keywords(languages, false);
189                    }
190                    else {
191                            andOperator = true;
192                    }
193    
194                    return findByC_G_S_N_D_T_M_L(
195                            companyId, groupId, structureId, names, descriptions, types, modes,
196                            languages, andOperator, start, end, orderByComparator);
197            }
198    
199            public List<DDMTemplate> findByC_G_S_N_D_T_M_L(
200                            long companyId, long groupId, long structureId, String name,
201                            String description, String type, String mode, String language,
202                            boolean andOperator, int start, int end,
203                            OrderByComparator orderByComparator)
204                    throws SystemException {
205    
206                    String[] names = CustomSQLUtil.keywords(name);
207                    String[] descriptions = CustomSQLUtil.keywords(description, false);
208                    String[] types = CustomSQLUtil.keywords(type, false);
209                    String[] modes = CustomSQLUtil.keywords(mode, false);
210                    String[] languages = CustomSQLUtil.keywords(language, false);
211    
212                    return findByC_G_S_N_D_T_M_L(
213                            companyId, groupId, structureId, names, descriptions, types, modes,
214                            languages, andOperator, start, end, orderByComparator);
215            }
216    
217            public List<DDMTemplate> findByC_G_S_N_D_T_M_L(
218                            long companyId, long groupId, long structureId, String[] names,
219                            String[] descriptions, String[] types, String[] modes,
220                            String[] languages, boolean andOperator, int start, int end,
221                            OrderByComparator orderByComparator)
222                    throws SystemException {
223    
224                    names = CustomSQLUtil.keywords(names);
225                    descriptions = CustomSQLUtil.keywords(descriptions, false);
226                    types = CustomSQLUtil.keywords(types, false);
227                    modes = CustomSQLUtil.keywords(modes, false);
228                    languages = CustomSQLUtil.keywords(languages, false);
229    
230                    Session session = null;
231    
232                    try {
233                            session = openSession();
234    
235                            String sql = CustomSQLUtil.get(FIND_BY_C_G_S_N_D_T_M_L);
236    
237                            if (groupId <= 0) {
238                                    sql = StringUtil.replace(sql, "(groupId = ?) AND", "");
239                            }
240    
241                            if (structureId <= 0) {
242                                    sql = StringUtil.replace(sql, "(structureId = ?) AND", "");
243                            }
244    
245                            sql = CustomSQLUtil.replaceKeywords(
246                                    sql, "lower(name)", StringPool.LIKE, false, names);
247                            sql = CustomSQLUtil.replaceKeywords(
248                                    sql, "description", StringPool.LIKE, false, descriptions);
249                            sql = CustomSQLUtil.replaceKeywords(
250                                    sql, "type", StringPool.LIKE, false, types);
251                            sql = CustomSQLUtil.replaceKeywords(
252                                    sql, "mode", StringPool.LIKE, false, modes);
253                            sql = CustomSQLUtil.replaceKeywords(
254                                    sql, "language", StringPool.LIKE, false, languages);
255                            sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
256    
257                            if (orderByComparator != null) {
258                                    String orderByFields = StringUtil.merge(
259                                            orderByComparator.getOrderByFields(), StringPool.COMMA);
260    
261                                    sql = StringUtil.replace(
262                                            sql, "templateId DESC", orderByFields.concat(" DESC"));
263                            }
264    
265                            SQLQuery q = session.createSQLQuery(sql);
266    
267                            q.addEntity("DDMTemplate", DDMTemplateImpl.class);
268    
269                            QueryPos qPos = QueryPos.getInstance(q);
270    
271                            qPos.add(companyId);
272    
273                            if (groupId > 0) {
274                                    qPos.add(groupId);
275                            }
276    
277                            if (structureId > 0) {
278                                    qPos.add(structureId);
279                            }
280    
281                            qPos.add(names, 2);
282                            qPos.add(descriptions, 2);
283                            qPos.add(types, 2);
284                            qPos.add(modes, 2);
285                            qPos.add(languages, 2);
286    
287                            return (List<DDMTemplate>)QueryUtil.list(
288                                    q, getDialect(), start, end);
289                    }
290                    catch (Exception e) {
291                            throw new SystemException(e);
292                    }
293                    finally {
294                            closeSession(session);
295                    }
296            }
297    
298    }