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 java.io.Serializable;
022import java.util.Date;
023
024import org.apache.wiki.WikiPage;
025
026/**
027 *  Describes a lock acquired by an user on a page.  For the most part,
028 *  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
031 *  keep a reference to a page for a long time.
032 *
033 */
034public class PageLock
035    implements Serializable
036{
037    private static final long serialVersionUID = 0L;
038    
039    private String   m_page;
040    private String   m_locker;
041    private Date     m_lockAcquired;
042    private Date     m_lockExpiry;
043
044    /**
045     *  Creates a new PageLock.  The lock is not attached to any objects at this point.
046     *  
047     *  @param page     WikiPage which is locked.
048     *  @param locker   The username who locked this page (for display purposes).
049     *  @param acquired The timestamp when the lock is acquired
050     *  @param expiry   The timestamp when the lock expires.
051     */
052    public PageLock( WikiPage page, 
053                     String locker,
054                     Date acquired,
055                     Date expiry )
056    {
057        m_page         = page.getName();
058        m_locker       = locker;
059        m_lockAcquired = (Date)acquired.clone();
060        m_lockExpiry   = (Date)expiry.clone();
061    }
062
063    /**
064     *  Returns the name of the page which is locked.
065     *  
066     *  @return The name of the page.
067     */
068    public String getPage()
069    {
070        return m_page;
071    }
072
073    /**
074     *  Returns the locker name.
075     *  
076     *  @return The name of the locker.
077     */
078    public String getLocker()
079    {
080        return m_locker;
081    }
082
083    /**
084     *  Returns the timestamp on which this lock was acquired.
085     *  
086     *  @return The acquisition time.
087     */
088    public Date getAcquisitionTime()
089    {
090        return m_lockAcquired;
091    }
092
093    /**
094     *  Returns the timestamp on which this lock will expire.
095     *  
096     *  @return The expiry date.
097     */
098    public Date getExpiryTime()
099    {
100        return m_lockExpiry;
101    }
102
103    /**
104     *  Returns the amount of time left in minutes, rounded up to the nearest
105     *  minute (so you get a zero only at the last minute).
106     *  
107     *  @return Time left in minutes.
108     */
109    public long getTimeLeft()
110    {
111        long time = m_lockExpiry.getTime() - new Date().getTime();
112
113        return (time / (1000L * 60)) + 1;
114    }
115    
116    public boolean isExpired() {
117        Date now = new Date();
118        return now.after( getExpiryTime() );
119    }
120}