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.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.ArrayUtil;
024    import com.liferay.portal.kernel.util.OrderByComparator;
025    import com.liferay.portal.kernel.util.StringBundler;
026    import com.liferay.portal.kernel.util.StringPool;
027    import com.liferay.portal.kernel.util.StringUtil;
028    import com.liferay.portal.kernel.util.Validator;
029    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
030    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
031    import com.liferay.portlet.dynamicdatamapping.model.DDMStructureConstants;
032    import com.liferay.portlet.dynamicdatamapping.model.impl.DDMStructureImpl;
033    import com.liferay.util.dao.orm.CustomSQLUtil;
034    
035    import java.util.Iterator;
036    import java.util.List;
037    
038    /**
039     * @author Eduardo Lundgren
040     * @author Connor McKay
041     */
042    public class DDMStructureFinderImpl
043            extends BasePersistenceImpl<DDMStructure> implements DDMStructureFinder {
044    
045            public static String COUNT_BY_C_G_C_N_D_S_T =
046                    DDMStructureFinder.class.getName() + ".countByC_G_C_N_D_S_T";
047    
048            public static String FIND_BY_C_G_C_N_D_S_T =
049                    DDMStructureFinder.class.getName() + ".findByC_G_C_N_D_S_T";
050    
051            public int countByKeywords(
052                            long companyId, long[] groupIds, long[] classNameIds,
053                            String keywords)
054                    throws SystemException {
055    
056                    String[] names = null;
057                    String[] descriptions = null;
058                    boolean andOperator = false;
059    
060                    if (Validator.isNotNull(keywords)) {
061                            names = CustomSQLUtil.keywords(keywords);
062                            descriptions = CustomSQLUtil.keywords(keywords, false);
063                    }
064                    else {
065                            andOperator = true;
066                    }
067    
068                    return countByC_G_C_N_D_S_T(
069                            companyId, groupIds, classNameIds, names, descriptions, null,
070                            DDMStructureConstants.TYPE_DEFAULT, andOperator);
071            }
072    
073            public int countByC_G_C_N_D_S_T(
074                            long companyId, long[] groupIds, long[] classNameIds, String name,
075                            String description, String storageType, int type,
076                            boolean andOperator)
077                    throws SystemException {
078    
079                    String[] names = CustomSQLUtil.keywords(name);
080                    String[] descriptions = CustomSQLUtil.keywords(description, false);
081                    String[] storageTypes = CustomSQLUtil.keywords(storageType, false);
082    
083                    return countByC_G_C_N_D_S_T(
084                            companyId, groupIds, classNameIds, names, descriptions,
085                            storageTypes, type, andOperator);
086            }
087    
088            public int countByC_G_C_N_D_S_T(
089                            long companyId, long[] groupIds, long[] classNameIds,
090                            String[] names, String[] descriptions, String[] storageTypes,
091                            int type, boolean andOperator)
092                    throws SystemException {
093    
094                    String[] classNameIdsString = null;
095    
096                    if (classNameIds == null) {
097                            classNameIdsString = new String[] {null};
098                    }
099                    else {
100                            classNameIdsString = ArrayUtil.toStringArray(classNameIds);
101                    }
102    
103                    names = CustomSQLUtil.keywords(names);
104                    descriptions = CustomSQLUtil.keywords(descriptions, false);
105                    storageTypes = CustomSQLUtil.keywords(storageTypes, false);
106    
107                    Session session = null;
108    
109                    try {
110                            session = openSession();
111    
112                            String sql = CustomSQLUtil.get(COUNT_BY_C_G_C_N_D_S_T);
113    
114                            sql = StringUtil.replace(
115                                    sql, "[$GROUP_ID$]", getGroupIds(groupIds));
116                            sql = CustomSQLUtil.replaceKeywords(
117                                    sql, "classNameId", StringPool.EQUAL, false,
118                                    classNameIdsString);
119                            sql = CustomSQLUtil.replaceKeywords(
120                                    sql, "lower(name)", StringPool.LIKE, false, names);
121                            sql = CustomSQLUtil.replaceKeywords(
122                                    sql, "description", StringPool.LIKE, false,
123                                    descriptions);
124                            sql = CustomSQLUtil.replaceKeywords(
125                                    sql, "storageType", StringPool.LIKE, true, storageTypes);
126                            sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
127    
128                            SQLQuery q = session.createSQLQuery(sql);
129    
130                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
131    
132                            QueryPos qPos = QueryPos.getInstance(q);
133    
134                            qPos.add(companyId);
135                            qPos.add(groupIds);
136                            qPos.add(classNameIds, 2);
137                            qPos.add(names, 2);
138                            qPos.add(descriptions, 2);
139                            qPos.add(storageTypes, 2);
140                            qPos.add(type);
141    
142                            Iterator<Long> itr = q.iterate();
143    
144                            if (itr.hasNext()) {
145                                    Long count = itr.next();
146    
147                                    if (count != null) {
148                                            return count.intValue();
149                                    }
150                            }
151    
152                            return 0;
153                    }
154                    catch (Exception e) {
155                            throw new SystemException(e);
156                    }
157                    finally {
158                            closeSession(session);
159                    }
160            }
161    
162            public List<DDMStructure> findByKeywords(
163                            long companyId, long[] groupIds, long[] classNameIds,
164                            String keywords, int start, int end,
165                            OrderByComparator orderByComparator)
166                    throws SystemException {
167    
168                    String[] names = null;
169                    String[] descriptions = null;
170                    boolean andOperator = false;
171    
172                    if (Validator.isNotNull(keywords)) {
173                            names = CustomSQLUtil.keywords(keywords);
174                            descriptions = CustomSQLUtil.keywords(keywords, false);
175                    }
176                    else {
177                            andOperator = true;
178                    }
179    
180                    return findByC_G_C_N_D_S_T(
181                            companyId, groupIds, classNameIds, names, descriptions, null,
182                            DDMStructureConstants.TYPE_DEFAULT, andOperator, start, end,
183                            orderByComparator);
184            }
185    
186            public List<DDMStructure> findByC_G_C_N_D_S_T(
187                            long companyId, long[] groupIds, long[] classNameIds, String name,
188                            String description, String storageType, int type,
189                            boolean andOperator, int start, int end,
190                            OrderByComparator orderByComparator)
191                    throws SystemException {
192    
193                    String[] names = CustomSQLUtil.keywords(name);
194                    String[] descriptions = CustomSQLUtil.keywords(description, false);
195                    String[] storageTypes = CustomSQLUtil.keywords(storageType, false);
196    
197                    return findByC_G_C_N_D_S_T(
198                            companyId, groupIds, classNameIds, names, descriptions,
199                            storageTypes, type, andOperator, start, end, orderByComparator);
200            }
201    
202            public List<DDMStructure> findByC_G_C_N_D_S_T(
203                            long companyId, long[] groupIds, long[] classNameIds,
204                            String[] names, String[] descriptions, String[] storageTypes,
205                            int type, boolean andOperator, int start, int end,
206                            OrderByComparator orderByComparator)
207                    throws SystemException {
208    
209                    String[] classNameIdsString = null;
210    
211                    if (classNameIds == null) {
212                            classNameIdsString = new String[] {null};
213                    }
214                    else {
215                            classNameIdsString = ArrayUtil.toStringArray(classNameIds);
216                    }
217    
218                    names = CustomSQLUtil.keywords(names);
219                    descriptions = CustomSQLUtil.keywords(descriptions, false);
220                    storageTypes = CustomSQLUtil.keywords(storageTypes, false);
221    
222                    Session session = null;
223    
224                    try {
225                            session = openSession();
226    
227                            String sql = CustomSQLUtil.get(FIND_BY_C_G_C_N_D_S_T);
228    
229                            sql = StringUtil.replace(
230                                    sql, "[$GROUP_ID$]", getGroupIds(groupIds));
231                            sql = CustomSQLUtil.replaceKeywords(
232                                    sql, "classNameId", StringPool.EQUAL, false,
233                                    classNameIdsString);
234                            sql = CustomSQLUtil.replaceKeywords(
235                                    sql, "lower(name)", StringPool.LIKE, false, names);
236                            sql = CustomSQLUtil.replaceKeywords(
237                                    sql, "description", StringPool.LIKE, false,
238                                    descriptions);
239                            sql = CustomSQLUtil.replaceKeywords(
240                                    sql, "storageType", StringPool.LIKE, true, storageTypes);
241                            sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
242    
243                            if (orderByComparator != null) {
244                                    String orderByFields = StringUtil.merge(
245                                            orderByComparator.getOrderByFields(), StringPool.COMMA);
246    
247                                    sql = StringUtil.replace(
248                                            sql, "structureId DESC", orderByFields.concat(" DESC"));
249                            }
250    
251                            SQLQuery q = session.createSQLQuery(sql);
252    
253                            q.addEntity("DDMStructure", DDMStructureImpl.class);
254    
255                            QueryPos qPos = QueryPos.getInstance(q);
256    
257                            qPos.add(companyId);
258                            qPos.add(groupIds);
259                            qPos.add(classNameIds, 2);
260                            qPos.add(names, 2);
261                            qPos.add(descriptions, 2);
262                            qPos.add(storageTypes, 2);
263                            qPos.add(type);
264    
265                            return (List<DDMStructure>)QueryUtil.list(
266                                    q, getDialect(), start, end);
267                    }
268                    catch (Exception e) {
269                            throw new SystemException(e);
270                    }
271                    finally {
272                            closeSession(session);
273                    }
274            }
275    
276            protected String getGroupIds(long[] groupIds) {
277                    if (groupIds.length == 0) {
278                            return StringPool.BLANK;
279                    }
280    
281                    StringBundler sb = new StringBundler(groupIds.length * 2);
282    
283                    sb.append("(");
284    
285                    for (int i = 0; i < groupIds.length; i++) {
286                            sb.append("groupId = ?");
287    
288                            if ((i + 1) < groupIds.length) {
289                                    sb.append(" OR ");
290                            }
291                    }
292    
293                    sb.append(") AND");
294    
295                    return sb.toString();
296            }
297    
298    }