package com.temporaryteam.noticeditor.model;

import java.util.ArrayList;

import org.json.JSONObject;
import org.json.JSONArray;
import org.json.JSONException;

public class NoticeCategory {

	private String name;
	private ArrayList<NoticeCategory> subcategories;
	private String content;

	public NoticeCategory() {
		name = null;
		content = null;
		subcategories = null;
	}

	public NoticeCategory(String name, String content) {
		this.name = name;
		this.content = content;
	}

	public NoticeCategory(String name, ArrayList<NoticeCategory> subcategories) {
		this.name = name;
		this.subcategories = subcategories;
		content = "";
	}

	public String getName() {
		return name;
	}

	public ArrayList<NoticeCategory> getSubCategories() {
		return subcategories;
	}

	public String getContent() {
		return content;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setSubCategories(ArrayList<NoticeCategory> subcategories) {
		this.subcategories = subcategories;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public JSONObject toJson() throws JSONException {
		JSONObject obj = new JSONObject();
		obj.put("name", name);
		obj.put("content", content);
		ArrayList vect = new ArrayList<JSONObject>();
		if(subcategories!=null) for(NoticeCategory subcategory : subcategories) vect.add(subcategory.toJson());
		obj.put("subcategories", new JSONArray(vect));
		return obj;
	}

	public void fromJson(JSONObject jsobj) throws JSONException {
		name = jsobj.getString("name");
		JSONArray arr = jsobj.getJSONArray("subcategories");
		subcategories = new ArrayList<NoticeCategory>();
		NoticeCategory category = new NoticeCategory();
		if(arr.length()!=0) {
			for(int i = 0; i<arr.length(); i++) {
				category.fromJson(arr.getJSONObject(i));
				subcategories.add(category);
			}
		}
		else subcategories = null;
		content = jsobj.getString("content");
	}
}

@@ -629,10 +629,12 @@
 t = 
-%22%22
+null
 ;%0A%09%7D
@@ -1174,24 +1174,42 @@
 %22, name);%0A%09%09
+if(content!=null) 
 obj.put(%22con
@@ -1225,16 +1225,47 @@
 ntent);%0A
+%09%09else obj.put(%22content%22, %22%22);%0A
 %09%09ArrayL
@@ -1574,16 +1574,92 @@
 name%22);%0A
+%09%09content = jsobj.getString(%22content%22);%0A%09%09if(content == %22%22) content = null;%0A
 %09%09JSONAr
@@ -1785,31 +1785,8 @@
 gory
- = new NoticeCategory()
 ;%0A%09%09
@@ -1848,16 +1848,53 @@
  i++) %7B%0A
+%09%09%09%09category = new NoticeCategory();%0A
 %09%09%09%09cate
@@ -1972,16 +1972,16 @@
 %09%09%7D%0A%09%09%7D%0A
+
 %09%09else s
@@ -2005,48 +2005,8 @@
 ll;%0A
-%09%09content = jsobj.getString(%22content%22);%0A
 %09%7D%0A%7D

Fix opening №2