001    /**
002     * Copyright (c) 2000-2010 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.asset.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.GetterUtil;
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.service.persistence.impl.BasePersistenceImpl;
028    import com.liferay.portlet.asset.NoSuchTagException;
029    import com.liferay.portlet.asset.model.AssetTag;
030    import com.liferay.portlet.asset.model.impl.AssetTagImpl;
031    import com.liferay.util.dao.orm.CustomSQLUtil;
032    
033    import java.util.Iterator;
034    import java.util.List;
035    
036    /**
037     * @author Brian Wing Shun Chan
038     * @author Bruno Farache
039     */
040    public class AssetTagFinderImpl
041            extends BasePersistenceImpl<AssetTag> implements AssetTagFinder {
042    
043            public static String COUNT_BY_G_C_N =
044                    AssetTagFinder.class.getName() + ".countByG_C_N";
045    
046            public static String COUNT_BY_G_N_P =
047                    AssetTagFinder.class.getName() + ".countByG_N_P";
048    
049            public static String FIND_BY_ENTRY_ID =
050                    AssetTagFinder.class.getName() + ".findByEntryId";
051    
052            public static String FIND_BY_G_N =
053                    AssetTagFinder.class.getName() + ".findByG_N";
054    
055            public static String FIND_BY_C_C =
056                    AssetTagFinder.class.getName() + ".findByC_C";
057    
058            public static String FIND_BY_G_C_N =
059                    AssetTagFinder.class.getName() + ".findByG_C_N";
060    
061            public static String FIND_BY_G_N_P =
062                    AssetTagFinder.class.getName() + ".findByG_N_P";
063    
064            public int countByG_C_N(long groupId, long classNameId, String name)
065                    throws SystemException {
066    
067                    Session session = null;
068    
069                    try {
070                            session = openSession();
071    
072                            String sql = CustomSQLUtil.get(COUNT_BY_G_C_N);
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                            qPos.add(groupId);
081                            qPos.add(classNameId);
082                            qPos.add(name);
083                            qPos.add(name);
084    
085                            Iterator<Long> itr = q.list().iterator();
086    
087                            if (itr.hasNext()) {
088                                    Long count = itr.next();
089    
090                                    if (count != null) {
091                                            return count.intValue();
092                                    }
093                            }
094    
095                            return 0;
096                    }
097                    catch (Exception e) {
098                            throw new SystemException(e);
099                    }
100                    finally {
101                            closeSession(session);
102                    }
103            }
104    
105            public int countByG_N_P(long groupId, String name, String[] tagProperties)
106                    throws SystemException {
107    
108                    Session session = null;
109    
110                    try {
111                            session = openSession();
112    
113                            String sql = CustomSQLUtil.get(COUNT_BY_G_N_P);
114    
115                            SQLQuery q = session.createSQLQuery(sql);
116    
117                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
118    
119                            QueryPos qPos = QueryPos.getInstance(q);
120    
121                            setJoin(qPos, tagProperties);
122                            qPos.add(groupId);
123                            qPos.add(name);
124                            qPos.add(name);
125    
126                            Iterator<Long> itr = q.list().iterator();
127    
128                            if (itr.hasNext()) {
129                                    Long count = itr.next();
130    
131                                    if (count != null) {
132                                            return count.intValue();
133                                    }
134                            }
135    
136                            return 0;
137                    }
138                    catch (Exception e) {
139                            throw new SystemException(e);
140                    }
141                    finally {
142                            closeSession(session);
143                    }
144            }
145    
146            public List<AssetTag> findByEntryId(long entryId)
147                    throws SystemException {
148    
149                    Session session = null;
150    
151                    try {
152                            session = openSession();
153    
154                            String sql = CustomSQLUtil.get(FIND_BY_ENTRY_ID);
155    
156                            SQLQuery q = session.createSQLQuery(sql);
157    
158                            q.addEntity("AssetTag", AssetTagImpl.class);
159    
160                            QueryPos qPos = QueryPos.getInstance(q);
161    
162                            qPos.add(entryId);
163    
164                            return (List<AssetTag>) QueryUtil.list(
165                                    q, getDialect(), QueryUtil.ALL_POS, QueryUtil.ALL_POS);
166                    }
167                    catch (Exception e) {
168                            throw new SystemException(e);
169                    }
170                    finally {
171                            closeSession(session);
172                    }
173            }
174    
175            public AssetTag findByG_N(long groupId, String name)
176                    throws NoSuchTagException, SystemException {
177    
178                    name = name.trim().toLowerCase();
179    
180                    Session session = null;
181    
182                    try {
183                            session = openSession();
184    
185                            String sql = CustomSQLUtil.get(FIND_BY_G_N);
186    
187                            SQLQuery q = session.createSQLQuery(sql);
188    
189                            q.addEntity("AssetTag", AssetTagImpl.class);
190    
191                            QueryPos qPos = QueryPos.getInstance(q);
192    
193                            qPos.add(groupId);
194                            qPos.add(name);
195    
196                            List<AssetTag> list = q.list();
197    
198                            if (list.size() == 0) {
199                                    StringBundler sb = new StringBundler(6);
200    
201                                    sb.append("No AssetTag exists with the key ");
202                                    sb.append("{groupId=");
203                                    sb.append(groupId);
204                                    sb.append(", name=");
205                                    sb.append(name);
206                                    sb.append("}");
207    
208                                    throw new NoSuchTagException(sb.toString());
209                            }
210                            else {
211                                    return list.get(0);
212                            }
213                    }
214                    catch (NoSuchTagException nste) {
215                            throw nste;
216                    }
217                    catch (Exception e) {
218                            throw new SystemException(e);
219                    }
220                    finally {
221                            closeSession(session);
222                    }
223            }
224    
225            public List<AssetTag> findByC_C(long classNameId, long classPK)
226                    throws SystemException {
227    
228                    Session session = null;
229    
230                    try {
231                            session = openSession();
232    
233                            String sql = CustomSQLUtil.get(FIND_BY_C_C);
234    
235                            SQLQuery q = session.createSQLQuery(sql);
236    
237                            q.addEntity("AssetTag", AssetTagImpl.class);
238    
239                            QueryPos qPos = QueryPos.getInstance(q);
240    
241                            qPos.add(classNameId);
242                            qPos.add(classPK);
243    
244                            return (List<AssetTag>) QueryUtil.list(
245                                    q, getDialect(), QueryUtil.ALL_POS, QueryUtil.ALL_POS);
246                    }
247                    catch (Exception e) {
248                            throw new SystemException(e);
249                    }
250                    finally {
251                            closeSession(session);
252                    }
253            }
254    
255            public List<AssetTag> findByG_C_N(
256                            long groupId, long classNameId, String name)
257                    throws SystemException {
258    
259                    return findByG_C_N(
260                            groupId, classNameId, name, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
261            }
262    
263            public List<AssetTag> findByG_C_N(
264                            long groupId, long classNameId, String name, int start, int end)
265                    throws SystemException {
266    
267                    Session session = null;
268    
269                    try {
270                            session = openSession();
271    
272                            String sql = CustomSQLUtil.get(FIND_BY_G_C_N);
273    
274                            SQLQuery q = session.createSQLQuery(sql);
275    
276                            q.addEntity("AssetTag", AssetTagImpl.class);
277    
278                            QueryPos qPos = QueryPos.getInstance(q);
279    
280                            qPos.add(groupId);
281                            qPos.add(classNameId);
282                            qPos.add(name);
283                            qPos.add(name);
284    
285                            return (List<AssetTag>)QueryUtil.list(q, getDialect(), start, end);
286                    }
287                    catch (Exception e) {
288                            throw new SystemException(e);
289                    }
290                    finally {
291                            closeSession(session);
292                    }
293            }
294    
295            public List<AssetTag> findByG_N_P(
296                            long groupId, String name, String[] tagProperties)
297                    throws SystemException {
298    
299                    return findByG_N_P(
300                            groupId, name, tagProperties, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
301            }
302    
303            public List<AssetTag> findByG_N_P(
304                            long groupId, String name, String[] tagProperties, int start,
305                            int end)
306                    throws SystemException {
307    
308                    Session session = null;
309    
310                    try {
311                            session = openSession();
312    
313                            String sql = CustomSQLUtil.get(FIND_BY_G_N_P);
314    
315                            sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(tagProperties));
316    
317                            SQLQuery q = session.createSQLQuery(sql);
318    
319                            q.addEntity("AssetTag", AssetTagImpl.class);
320    
321                            QueryPos qPos = QueryPos.getInstance(q);
322    
323                            setJoin(qPos, tagProperties);
324                            qPos.add(groupId);
325                            qPos.add(name);
326                            qPos.add(name);
327    
328                            return (List<AssetTag>)QueryUtil.list(q, getDialect(), start, end);
329                    }
330                    catch (Exception e) {
331                            throw new SystemException(e);
332                    }
333                    finally {
334                            closeSession(session);
335                    }
336            }
337    
338            protected String getJoin(String[] tagProperties) {
339                    if (tagProperties.length == 0) {
340                            return StringPool.BLANK;
341                    }
342                    else {
343                            StringBundler sb = new StringBundler(tagProperties.length * 3 + 1);
344    
345                            sb.append(" INNER JOIN AssetTagProperty ON ");
346                            sb.append(" (AssetTagProperty.tagId = AssetTag.tagId) AND ");
347    
348                            for (int i = 0; i < tagProperties.length; i++) {
349                                    sb.append("(AssetTagProperty.key_ = ? AND ");
350                                    sb.append("AssetTagProperty.value = ?) ");
351    
352                                    if ((i + 1) < tagProperties.length) {
353                                            sb.append(" AND ");
354                                    }
355                            }
356    
357                            return sb.toString();
358                    }
359            }
360    
361            protected void setJoin(QueryPos qPos, String[] tagProperties) {
362                    for (int i = 0; i < tagProperties.length; i++) {
363                            String[] tagProperty = StringUtil.split(
364                                    tagProperties[i], StringPool.COLON);
365    
366                            String key = StringPool.BLANK;
367    
368                            if (tagProperty.length > 0) {
369                                    key = GetterUtil.getString(tagProperty[0]);
370                            }
371    
372                            String value = StringPool.BLANK;
373    
374                            if (tagProperty.length > 1) {
375                                    value = GetterUtil.getString(tagProperty[1]);
376                            }
377    
378                            qPos.add(key);
379                            qPos.add(value);
380                    }
381            }
382    
383    }