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.portlet.documentlibrary.util;
016    
017    import com.liferay.portal.kernel.lock.BaseLockListener;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.util.GetterUtil;
021    import com.liferay.portal.service.ServiceContext;
022    import com.liferay.portal.util.PropsValues;
023    import com.liferay.portlet.documentlibrary.model.DLFileEntryConstants;
024    import com.liferay.portlet.documentlibrary.service.DLFileEntryServiceUtil;
025    
026    /**
027     * @author Alexander Chow
028     */
029    public class DLFileEntryLockListener extends BaseLockListener {
030    
031            public String getClassName() {
032                    return DLFileEntryConstants.getClassName();
033            }
034    
035            @Override
036            public void onAfterExpire(String key) {
037                    long fileEntryId = GetterUtil.getLong(key);
038    
039                    try {
040                            if (PropsValues.DL_FILE_ENTRY_LOCK_POLICY == 1) {
041                                    DLFileEntryServiceUtil.checkInFileEntry(
042                                            fileEntryId, true, "Automatic timeout checkin",
043                                            new ServiceContext());
044    
045                                    if (_log.isDebugEnabled()) {
046                                            _log.debug("Lock expired and checked in " + fileEntryId);
047                                    }
048                            }
049                            else {
050                                    DLFileEntryServiceUtil.cancelCheckOut(fileEntryId);
051    
052                                    if (_log.isDebugEnabled()) {
053                                            _log.debug(
054                                                    "Lock expired and canceled check out of " +
055                                                            fileEntryId);
056                                    }
057                            }
058                    }
059                    catch (Exception e) {
060                            _log.error(e, e);
061                    }
062            }
063    
064            private static Log _log = LogFactoryUtil.getLog(
065                    DLFileEntryLockListener.class);
066    
067    }