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