001/*
002    Licensed to the Apache Software Foundation (ASF) under one
003    or more contributor license agreements.  See the NOTICE file
004    distributed with this work for additional information
005    regarding copyright ownership.  The ASF licenses this file
006    to you under the Apache License, Version 2.0 (the
007    "License"); you may not use this file except in compliance
008    with the License.  You may obtain a copy of the License at
009
010       http://www.apache.org/licenses/LICENSE-2.0
011
012    Unless required by applicable law or agreed to in writing,
013    software distributed under the License is distributed on an
014    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015    KIND, either express or implied.  See the License for the
016    specific language governing permissions and limitations
017    under the License.   
018 */
019package org.apache.wiki.pages;
020
021import org.apache.wiki.api.core.Page;
022
023import java.io.Serializable;
024import java.util.Date;
025
026
027/**
028 * Describes a lock acquired by an user on a page.  For the most part, the regular developer does not have to instantiate this class.
029 * <p>
030 * The PageLock keeps no reference to a WikiPage because otherwise it could keep a reference to a page for a long time.
031 */
032public class PageLock implements Serializable {
033
034    private static final long serialVersionUID = 0L;
035
036    private String m_page;
037    private String m_locker;
038    private Date m_lockAcquired;
039    private Date m_lockExpiry;
040
041    /**
042     * Creates a new PageLock.  The lock is not attached to any objects at this point.
043     *
044     * @param page     WikiPage which is locked.
045     * @param locker   The username who locked this page (for display purposes).
046     * @param acquired The timestamp when the lock is acquired
047     * @param expiry   The timestamp when the lock expires.
048     */
049    public PageLock( final Page page, final String locker, final Date acquired, final Date expiry ) {
050        m_page = page.getName();
051        m_locker = locker;
052        m_lockAcquired = ( Date )acquired.clone();
053        m_lockExpiry = ( Date )expiry.clone();
054    }
055
056    /**
057     * Returns the name of the page which is locked.
058     *
059     * @return The name of the page.
060     */
061    public String getPage() {
062        return m_page;
063    }
064
065    /**
066     * Returns the locker name.
067     *
068     * @return The name of the locker.
069     */
070    public String getLocker() {
071        return m_locker;
072    }
073
074    /**
075     * Returns the timestamp on which this lock was acquired.
076     *
077     * @return The acquisition time.
078     */
079    public Date getAcquisitionTime() {
080        return m_lockAcquired;
081    }
082
083    /**
084     * Returns the timestamp on which this lock will expire.
085     *
086     * @return The expiry date.
087     */
088    public Date getExpiryTime() {
089        return m_lockExpiry;
090    }
091
092    /**
093     * Returns the amount of time left in minutes, rounded up to the nearest minute (so you get a zero only at the last minute).
094     *
095     * @return Time left in minutes.
096     */
097    public long getTimeLeft() {
098        final long time = m_lockExpiry.getTime() - new Date().getTime();
099        return ( time / ( 1000L * 60 ) ) + 1;
100    }
101
102    public boolean isExpired() {
103        final Date now = new Date();
104        return now.after( getExpiryTime() );
105    }
106
107}