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.kernel.bean.BeanReference;
018    import com.liferay.portal.kernel.dao.orm.LockMode;
019    import com.liferay.portal.kernel.dao.orm.Query;
020    import com.liferay.portal.kernel.dao.orm.QueryPos;
021    import com.liferay.portal.kernel.dao.orm.Session;
022    import com.liferay.portal.kernel.exception.SystemException;
023    import com.liferay.portal.model.Lock;
024    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
025    import com.liferay.util.dao.orm.CustomSQLUtil;
026    
027    import java.util.List;
028    
029    /**
030     * @author Shuyang Zhou
031     */
032    public class LockFinderImpl
033            extends BasePersistenceImpl<Lock> implements LockFinder {
034    
035            public static final String FIND_BY_C_K =
036                    LockFinder.class.getName() + ".findByC_K";
037    
038            public Lock fetchByC_K(String className, String key, LockMode lockMode)
039                    throws SystemException {
040    
041                    if (lockMode == null) {
042                            return lockPersistence.fetchByC_K(className, key);
043                    }
044    
045                    Session session = null;
046    
047                    try {
048                            session = openSession();
049    
050                            String sql = CustomSQLUtil.get(FIND_BY_C_K);
051    
052                            Query q = session.createQuery(sql);
053    
054                            q.setLockMode("lock", lockMode);
055    
056                            QueryPos qPos = QueryPos.getInstance(q);
057    
058                            qPos.add(className);
059                            qPos.add(key);
060    
061                            List<Lock> locks = q.list();
062    
063                            if (!locks.isEmpty()) {
064                                    return locks.get(0);
065                            }
066    
067                            return null;
068                    }
069                    catch (Exception e) {
070                            throw processException(e);
071                    }
072                    finally {
073                            closeSession(session);
074                    }
075            }
076    
077            @BeanReference(type = LockPersistence.class)
078            protected LockPersistence lockPersistence;
079    
080    }