/*
 * $Id: PermissionMap.java,v 1.2 2004-09-19 01:35:21 tlipkis Exp $
 */

/*

Copyright (c) 2000-2003 Board of Trustees of Leland Stanford Jr. University,
all rights reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
STANFORD UNIVERSITY BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Except as contained in this notice, the name of Stanford University shall not
be used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from Stanford University.

*/
package org.lockss.crawler;
import java.util.HashMap;
import org.lockss.util.*;
import java.net.MalformedURLException;

/**
 * This Map facilitate the action of putting a permission URL and its status to a hash map
 *
 * @author Chun D. Fok
 * @version 0.0
 */
public class PermissionMap{
  public static final int PERMISSION_MISSING = PermissionRecord.PERMISSION_MISSING;
  public static final int PERMISSION_UNCHECKED = PermissionRecord.PERMISSION_UNCHECKED;
  public static final int PERMISSION_OK = PermissionRecord.PERMISSION_OK;
  public static final int PERMISSION_NOT_OK = PermissionRecord.PERMISSION_NOT_OK;
  public static final int FETCH_PERMISSION_FAILED = PermissionRecord.FETCH_PERMISSION_FAILED;

  private HashMap hMap;

  public PermissionMap(){
    hMap = new HashMap();
  }
  
  /**
   * Put a object to a hashmap using the lowercased host name of permissionUrl as the key.
   * The object contains the host's permission url and permission status. 
   *
   * @param permissionUrl the host's permission url
   * @param status the host's permission status  
   */
  public void putStatus(String permissionUrl, int status) throws MalformedURLException {
    hMap.put(UrlUtil.getHost(permissionUrl).toLowerCase(), new PermissionRecord(permissionUrl,status));
  }
  
  /**
   * Get a PermissionRecord from host name's url as the key
   *
   * @param url the host's url
   * @return PermissionRecord of the host of url
   */
  public PermissionRecord get(String url) throws MalformedURLException{
    return (PermissionRecord) hMap.get(UrlUtil.getHost(url).toLowerCase());
  }

  /**
   * Get the host's permission url from a url
   *
   * @param url a url 
   * @return the host's permission url of the given url
   */
  public String getPermissionUrl(String url) throws MalformedURLException{
    PermissionRecord pr = get(url);
    if (pr == null) {
      return null;
    }
    return pr.getPermissionUrl();
  }

  /**
   * Get the host's permission status from a url
   *
   * @param url a url
   * @return the host's permission status of the given url
   */
  public int getStatus(String url) throws MalformedURLException{
    PermissionRecord pr = get(url);
    if (pr == null) {
      return PERMISSION_MISSING;
    }
    return pr.getPermissionStatus();
  }
}

// class PermissionRecord{
// //   public static final int PERMISSION_UNCHECKED = 0;
// //   public static final int PERMISSION_OK = 1;
// //   public static final int PERMISSION_NOT_OK = 2;
// //   public static final int FETCH_PERMISSION_FAILED = 3;
  
//   private String permissionUrl="";
//   private int permissionStatus=PermissionMap.PERMISSION_UNCHECKED;
  
//   PermissionRecord(String permissionUrl,int permissionStatus){
//     if (permissionUrl == null) {
//       throw new IllegalArgumentException("Called with null permissionUrl");
//     }
     
//     setPermissionUrl(permissionUrl);
//     setPermissionStatus(permissionStatus);
//   }
  
//   String getPermissionUrl(){
//     return permissionUrl;
//   }
  
//   void setPermissionUrl(String permissionUrl){
//     this.permissionUrl=permissionUrl;
//   }

//   int getPermissionStatus(){
//     return permissionStatus;
//   }
  
//   void setPermissionStatus(int permissionStatus){
//     this.permissionStatus=permissionStatus;
//   }

// }

@@ -27,17 +27,17 @@
 ava,v 1.
-2
+3
  2004-09
@@ -41,27 +41,25 @@
 -09-
-19 01:35:21 tlipkis
+27 23:33:57 dcfok
  Exp
@@ -3707,1020 +3707,4 @@
 %7D%0A%7D%0A
-%0A// class PermissionRecord%7B%0A// //   public static final int PERMISSION_UNCHECKED = 0;%0A// //   public static final int PERMISSION_OK = 1;%0A// //   public static final int PERMISSION_NOT_OK = 2;%0A// //   public static final int FETCH_PERMISSION_FAILED = 3;%0A  %0A//   private String permissionUrl=%22%22;%0A//   private int permissionStatus=PermissionMap.PERMISSION_UNCHECKED;%0A  %0A//   PermissionRecord(String permissionUrl,int permissionStatus)%7B%0A//     if (permissionUrl == null) %7B%0A//       throw new IllegalArgumentException(%22Called with null permissionUrl%22);%0A//     %7D%0A     %0A//     setPermissionUrl(permissionUrl);%0A//     setPermissionStatus(permissionStatus);%0A//   %7D%0A  %0A//   String getPermissionUrl()%7B%0A//     return permissionUrl;%0A//   %7D%0A  %0A//   void setPermissionUrl(String permissionUrl)%7B%0A//     this.permissionUrl=permissionUrl;%0A//   %7D%0A%0A//   int getPermissionStatus()%7B%0A//     return permissionStatus;%0A//   %7D%0A  %0A//   void setPermissionStatus(int permissionStatus)%7B%0A//     this.permissionStatus=permissionStatus;%0A//   %7D%0A%0A// %7D%0A

clean up the comments