// Copyright (C) 2003-2009 by Object Mentor, Inc. All rights reserved.
// Released under the terms of the CPL Common Public License version 1.0.
package fitnesse.wiki;

import java.text.ParseException;
import java.util.Date;

public class WikiImportProperty extends WikiPageProperty {
  private static final long serialVersionUID = 1L;

  public static final String PROPERTY_NAME = "WikiImport";

  private WikiImportProperty() {
  }

  public WikiImportProperty(String source) {
    set("Source", source);
  }

  public String getSourceUrl() {
    return get("Source");
  }

  public boolean isRoot() {
    return has("IsRoot");
  }

  public void setRoot(boolean value) {
    if (value)
      set("IsRoot");
    else
      remove("IsRoot");
  }

  public boolean isAutoUpdate() {
    return has("AutoUpdate");
  }

  public void setAutoUpdate(boolean value) {
    if (value)
      set("AutoUpdate");
    else
      remove("AutoUpdate");
  }

  public static boolean isImported(PageData pageData) {
    try {
      WikiImportProperty importProperty = WikiImportProperty.createFrom(pageData.getProperties());
      return importProperty != null && !importProperty.isRoot();
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }

  public static WikiImportProperty createFrom(WikiPageProperty property) {
    if (property.has(PROPERTY_NAME)) {
      WikiImportProperty importProperty = new WikiImportProperty();
      WikiPageProperty rawImportProperty = property.getProperty(PROPERTY_NAME);
      importProperty.set("Source", rawImportProperty.getProperty("Source"));
      importProperty.set("LastRemoteModification", rawImportProperty.getProperty("LastRemoteModification"));
      if (rawImportProperty.has("IsRoot"))
        importProperty.set("IsRoot", rawImportProperty.getProperty("IsRoot"));
      if (rawImportProperty.has("AutoUpdate"))
        importProperty.set("AutoUpdate", rawImportProperty.getProperty("AutoUpdate"));

      return importProperty;
    } else
      return null;
  }

  public void addTo(WikiPageProperty rootProperty) {
    rootProperty.set(PROPERTY_NAME, this);
  }

  public void setLastRemoteModificationTime(Date date) {
    set("LastRemoteModification", getTimeFormat().format(date));
  }

  public Date getLastRemoteModificationTime() {
    Date date = new Date(0);
    String strValue = get("LastRemoteModification");
    if (strValue != null) {
      try {
        date = getTimeFormat().parse(strValue);
      } catch (ParseException e) {
        date = new Date(0);
      }
    }
    return date;
  }

}

@@ -388,16 +388,259 @@
 Import%22;
+%0A  %0A  private static final String SOURCE = %22Source%22;%0A  private static final String IS_ROOT = %22IsRoot%22;%0A  private static final String LAST_REMOTE_MODIFICATION = %22LastRemoteModification%22;%0A  private static final String AUTO_UPDATE = %22AutoUpdate%22;
 %0A%0A  priv
@@ -724,24 +724,22 @@
     set(
-%22Source%22
+SOURCE
 , source
@@ -794,24 +794,22 @@
 urn get(
-%22Source%22
+SOURCE
 );%0A  %7D%0A%0A
@@ -851,24 +851,23 @@
 urn has(
-%22IsRoot%22
+IS_ROOT
 );%0A  %7D%0A%0A
@@ -930,24 +930,23 @@
     set(
-%22IsRoot%22
+IS_ROOT
 );%0A    e
@@ -962,24 +962,23 @@
  remove(
-%22IsRoot%22
+IS_ROOT
 );%0A  %7D%0A%0A
@@ -1026,28 +1026,27 @@
 urn has(
-%22AutoUpdate%22
+AUTO_UPDATE
 );%0A  %7D%0A%0A
@@ -1115,28 +1115,27 @@
     set(
-%22AutoUpdate%22
+AUTO_UPDATE
 );%0A    e
@@ -1151,28 +1151,27 @@
  remove(
-%22AutoUpdate%22
+AUTO_UPDATE
 );%0A  %7D%0A%0A
@@ -1763,24 +1763,22 @@
 rty.set(
-%22Source%22
+SOURCE
 , rawImp
@@ -1801,24 +1801,22 @@
 roperty(
-%22Source%22
+SOURCE
 ));%0A    
@@ -1840,32 +1840,32 @@
 set(
-%22LastRemoteModification%22
+LAST_REMOTE_MODIFICATION
 , ra
@@ -1896,32 +1896,32 @@
 rty(
-%22LastRemoteModification%22
+LAST_REMOTE_MODIFICATION
 ));%0A
@@ -1952,24 +1952,23 @@
 rty.has(
-%22IsRoot%22
+IS_ROOT
 ))%0A     
@@ -1989,24 +1989,23 @@
 rty.set(
-%22IsRoot%22
+IS_ROOT
 , rawImp
@@ -2028,24 +2028,23 @@
 roperty(
-%22IsRoot%22
+IS_ROOT
 ));%0A    
@@ -2071,28 +2071,27 @@
 rty.has(
-%22AutoUpdate%22
+AUTO_UPDATE
 ))%0A     
@@ -2112,28 +2112,27 @@
 rty.set(
-%22AutoUpdate%22
+AUTO_UPDATE
 , rawImp
@@ -2155,28 +2155,27 @@
 roperty(
-%22AutoUpdate%22
+AUTO_UPDATE
 ));%0A%0A   
@@ -2405,32 +2405,32 @@
 set(
-%22LastRemoteModification%22
+LAST_REMOTE_MODIFICATION
 , ge
@@ -2570,32 +2570,32 @@
 get(
-%22LastRemoteModification%22
+LAST_REMOTE_MODIFICATION
 );%0A 

Use constants in WikiImportProperty