001    /**
002     * Copyright (c) 2000-2013 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            @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    }