package com.horsefire.filecabinet.couch;

import java.io.IOException;

import org.joda.time.DateTime;
import org.json.simple.JSONArray;

@SuppressWarnings("unchecked")
public class Document extends com.horsefire.couchdb.Document {

	public Document(com.horsefire.couchdb.Document doc) {
		super(doc.getJsonObject());
	}

	public void setFilename(String filename) {
		m_json.put("filename", filename);
	}

	public boolean unseen() {
		return m_json.get("unseen") != null
				&& ((Boolean) m_json.get("unseen")).booleanValue();
	}

	public void setUnseen() throws IOException {
		m_json.put("unseen", Boolean.TRUE);
	}

	public void setUploaded(DateTime dateTime) {
		JSONArray date = new JSONArray();
		date.add(dateTime.getYear());
		date.add(dateTime.getMonthOfYear());
		date.add(dateTime.getDayOfMonth());
		m_json.put("uploaded", date);
	}

	public String getSha1() {
		return (String) m_json.get("sha1");
	}

	public void setSha1(String sha1) {
		m_json.put("sha1", sha1);
	}

	public boolean hasFailedThumb(String thumbName) {
		JSONArray failedThumbs = (JSONArray) m_json.get("failedThumbnails");
		if (failedThumbs != null) {
			for (Object failedThumb : failedThumbs) {
				if (failedThumb.toString().equals(thumbName)) {
					return true;
				}
			}
		}
		return false;
	}

	public void setFailedThumb(String thumbName) {
		JSONArray failedThumbs = (JSONArray) m_json.get("failedThumbnails");
		if (failedThumbs == null) {
			failedThumbs = new JSONArray();
			m_json.put("failedThumbnails", failedThumbs);
		}
		for (Object failedThumb : failedThumbs) {
			if (failedThumb.toString().equals(thumbName)) {
				return;
			}
		}
		failedThumbs.add(thumbName);
	}
}

@@ -128,16 +128,51 @@
 ONArray;
+%0Aimport org.json.simple.JSONObject;
 %0A%0A@Suppr
@@ -1053,32 +1053,115 @@
 ng thumbName) %7B%0A
+%09%09JSONObject thumb = (JSONObject) m_json.get(%22thumbnail%22);%0A%09%09if (thumb != null) %7B%0A%09
 %09%09JSONArray fail
@@ -1154,37 +1154,42 @@
 JSONArray failed
-Thumb
+Generation
 s = (JSONArray) 
@@ -1180,38 +1180,43 @@
 s = (JSONArray) 
-m_json
+thumb%0A%09%09%09%09%09
 .get(%22failedThum
@@ -1207,38 +1207,40 @@
 .get(%22failed
-Thumbnail
+Generation
 s%22);%0A
+%09
 %09%09if (failed
@@ -1239,21 +1239,26 @@
  (failed
-Thumb
+Generation
 s != nul
@@ -1258,24 +1258,25 @@
  != null) %7B%0A
+%09
 %09%09%09for (Obje
@@ -1276,37 +1276,36 @@
 r (Object failed
-Thumb
+Name
  : failedThumbs)
@@ -1297,26 +1297,32 @@
 : failed
-Thumb
+Generation
 s) %7B%0A
+%09
 %09%09%09%09if (
@@ -1319,37 +1319,36 @@
 %0A%09%09%09%09%09if (failed
-Thumb
+Name
 .toString().equa
@@ -1365,24 +1365,25 @@
 me)) %7B%0A%09%09%09%09%09
+%09
 return true;
@@ -1383,16 +1383,23 @@
 n true;%0A
+%09%09%09%09%09%7D%0A
 %09%09%09%09%7D%0A%09%09
@@ -1469,24 +1469,174 @@
 humbName) %7B%0A
+%09%09JSONObject thumb = (JSONObject) m_json.get(%22thumbnail%22);%0A%09%09if (thumb == null) %7B%0A%09%09%09thumb = new JSONObject();%0A%09%09%09m_json.put(%22thumbnail%22, thumb);%0A%09%09%7D%0A
 %09%09JSONArray 
@@ -1637,29 +1637,34 @@
 Array failed
-Thumb
+Generation
 s = (JSONArr
@@ -1667,22 +1667,26 @@
 NArray) 
-m_json
+thumb%0A%09%09%09%09
 .get(%22fa
@@ -1689,25 +1689,26 @@
 (%22failed
-Thumbnail
+Generation
 s%22);%0A%09%09i
@@ -1716,21 +1716,26 @@
  (failed
-Thumb
+Generation
 s == nul
@@ -1748,21 +1748,26 @@
 %09%09failed
-Thumb
+Generation
 s = new 
@@ -1778,30 +1778,29 @@
 Array();%0A%09%09%09
-m_json
+thumb
 .put(%22failed
@@ -1803,32 +1803,38 @@
 iled
-Thumbnails%22, failedThumb
+Generations%22, failedGeneration
 s);%0A
@@ -1871,21 +1871,26 @@
 : failed
-Thumb
+Generation
 s) %7B%0A%09%09%09
@@ -1966,21 +1966,26 @@
 %09%09failed
-Thumb
+Generation
 s.add(th

Use new thumbnail format