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.NoSuchLayoutSetBranchException;
018    import com.liferay.portal.kernel.dao.orm.QueryPos;
019    import com.liferay.portal.kernel.dao.orm.SQLQuery;
020    import com.liferay.portal.kernel.dao.orm.Session;
021    import com.liferay.portal.kernel.exception.SystemException;
022    import com.liferay.portal.kernel.util.StringBundler;
023    import com.liferay.portal.model.Layout;
024    import com.liferay.portal.model.LayoutSetBranch;
025    import com.liferay.portal.model.impl.LayoutSetBranchImpl;
026    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
027    import com.liferay.util.dao.orm.CustomSQLUtil;
028    
029    import java.util.List;
030    
031    /**
032     * @author Julio Camarero
033     */
034    public class LayoutSetBranchFinderImpl
035            extends BasePersistenceImpl<Layout> implements LayoutSetBranchFinder {
036    
037            public static final String FIND_BY_MASTER =
038                    LayoutSetBranchFinder.class.getName() + ".findByMaster";
039    
040            public LayoutSetBranch findByMaster(long groupId, boolean privateLayout)
041                    throws NoSuchLayoutSetBranchException, SystemException {
042    
043                    Session session = null;
044    
045                    try {
046                            session = openSession();
047    
048                            String sql = CustomSQLUtil.get(FIND_BY_MASTER);
049    
050                            SQLQuery q = session.createSQLQuery(sql);
051    
052                            q.addEntity("LayoutSetBranch", LayoutSetBranchImpl.class);
053    
054                            QueryPos qPos = QueryPos.getInstance(q);
055    
056                            qPos.add(groupId);
057                            qPos.add(privateLayout);
058                            qPos.add(true);
059    
060                            List<LayoutSetBranch> layoutSetBranches = q.list();
061    
062                            if (!layoutSetBranches.isEmpty()) {
063                                    return layoutSetBranches.get(0);
064                            }
065                    }
066                    catch (Exception e) {
067                            throw new SystemException(e);
068                    }
069                    finally {
070                            closeSession(session);
071                    }
072    
073                    StringBundler sb = new StringBundler(5);
074    
075                    sb.append("No LayoutSetBranch exists with the key {groupId=");
076                    sb.append(groupId);
077                    sb.append(", privateLayout=");
078                    sb.append(privateLayout);
079                    sb.append(", master=true}");
080    
081                    throw new NoSuchLayoutSetBranchException(sb.toString());
082            }
083    
084    }