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.imagegallery.service.persistence;
016    
017    import com.liferay.portal.kernel.dao.orm.QueryPos;
018    import com.liferay.portal.kernel.dao.orm.SQLQuery;
019    import com.liferay.portal.kernel.dao.orm.Session;
020    import com.liferay.portal.kernel.exception.SystemException;
021    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
022    import com.liferay.portlet.imagegallery.NoSuchImageException;
023    import com.liferay.portlet.imagegallery.model.IGImage;
024    import com.liferay.portlet.imagegallery.model.impl.IGImageImpl;
025    import com.liferay.util.dao.orm.CustomSQLUtil;
026    
027    import java.util.List;
028    
029    /**
030     * @author Brian Wing Shun Chan
031     * @author Julio Camarero
032     */
033    public class IGImageFinderImpl
034            extends BasePersistenceImpl<IGImage> implements IGImageFinder {
035    
036            public static String FIND_BY_ANY_IMAGE_ID =
037                    IGImageFinder.class.getName() + ".findByAnyImageId";
038    
039            public static String FIND_BY_NO_ASSETS =
040                    IGImageFinder.class.getName() + ".findByNoAssets";
041    
042            public IGImage fetchByAnyImageId(long imageId) throws SystemException {
043                    Session session = null;
044    
045                    try {
046                            session = openSession();
047    
048                            String sql = CustomSQLUtil.get(FIND_BY_ANY_IMAGE_ID);
049    
050                            SQLQuery q = session.createSQLQuery(sql);
051    
052                            q.addEntity("IGImage", IGImageImpl.class);
053    
054                            QueryPos qPos = QueryPos.getInstance(q);
055    
056                            qPos.add(imageId);
057                            qPos.add(imageId);
058                            qPos.add(imageId);
059                            qPos.add(imageId);
060    
061                            List<IGImage> list = q.list();
062    
063                            if (list.isEmpty()) {
064                                    return null;
065                            }
066                            else {
067                                    return list.get(0);
068                            }
069                    }
070                    catch (Exception e) {
071                            throw new SystemException(e);
072                    }
073                    finally {
074                            closeSession(session);
075                    }
076            }
077    
078            public IGImage findByAnyImageId(long imageId)
079                    throws NoSuchImageException, SystemException {
080    
081                    IGImage image = fetchByAnyImageId(imageId);
082    
083                    if (image == null) {
084                            throw new NoSuchImageException(
085                                    "No IGImage exists with the imageId " + imageId);
086                    }
087                    else {
088                            return image;
089                    }
090            }
091    
092            public List<IGImage> findByNoAssets() throws SystemException {
093                    Session session = null;
094    
095                    try {
096                            session = openSession();
097    
098                            String sql = CustomSQLUtil.get(FIND_BY_NO_ASSETS);
099    
100                            SQLQuery q = session.createSQLQuery(sql);
101    
102                            q.addEntity("IGImage", IGImageImpl.class);
103    
104                            return q.list();
105                    }
106                    catch (Exception e) {
107                            throw new SystemException(e);
108                    }
109                    finally {
110                            closeSession(session);
111                    }
112            }
113    
114    }