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