001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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            @Override
039            public Lock fetchByC_K(String className, String key, LockMode lockMode)
040                    throws SystemException {
041    
042                    if (lockMode == null) {
043                            return lockPersistence.fetchByC_K(className, key);
044                    }
045    
046                    Session session = null;
047    
048                    try {
049                            session = openSession();
050    
051                            String sql = CustomSQLUtil.get(FIND_BY_C_K);
052    
053                            Query q = session.createQuery(sql);
054    
055                            q.setLockMode("lock", lockMode);
056    
057                            QueryPos qPos = QueryPos.getInstance(q);
058    
059                            qPos.add(className);
060                            qPos.add(key);
061    
062                            List<Lock> locks = q.list();
063    
064                            if (!locks.isEmpty()) {
065                                    return locks.get(0);
066                            }
067    
068                            return null;
069                    }
070                    catch (Exception e) {
071                            throw processException(e);
072                    }
073                    finally {
074                            closeSession(session);
075                    }
076            }
077    
078            @BeanReference(type = LockPersistence.class)
079            protected LockPersistence lockPersistence;
080    
081    }