/*
 * Copyright (C) 2015 Willi Ye
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.grarak.kerneladiutor.utils.database;

import com.grarak.kerneladiutor.utils.Utils;

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

import java.util.ArrayList;
import java.util.List;

/**
 * Created by willi on 15.04.15.
 */
public abstract class JsonDB {

    private JSONObject databaseMain;
    private JSONArray databaseItems;

    private final String path;

    public JsonDB(String path, int version) {
        this.path = path;
        try {
            databaseMain = new JSONObject(Utils.readFile(path));
            if (databaseMain.getInt("version") == version)
                databaseItems = databaseMain.getJSONArray("database");
        } catch (JSONException ignored) {
            databaseMain = new JSONObject();
            databaseItems = new JSONArray();
        }
        try {
            databaseMain = new JSONObject();
            databaseMain.put("version", version);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public void putItem(JSONObject items) {
        databaseItems.put(items);
    }

    public List<DBJsonItem> getAllItems() {
        List<DBJsonItem> items = new ArrayList<>();
        try {
            for (int i = 0; i < length(); i++)
                items.add(getItem(databaseItems.getJSONObject(i)));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return items;
    }

    public abstract DBJsonItem getItem(JSONObject item);

    public void delete(int position) {
        JSONArray jsonArray = new JSONArray();
        try {
            for (int i = 0; i < length(); i++)
                if (i != position) jsonArray.put(databaseItems.getJSONObject(i));
            databaseItems = jsonArray;
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public int length() {
        return databaseItems.length();
    }

    public void commit() {
        try {
            databaseMain.put("database", databaseItems);
            Utils.writeFile(path, databaseMain.toString(), false);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public static class DBJsonItem {

        protected JSONObject item;

        public DBJsonItem() {
            item = new JSONObject();
        }

        public JSONObject getItem() {
            return item;
        }

    }

}

@@ -1084,32 +1084,116 @@
 ;%0A        try %7B%0A
+            String json = Utils.readFile(path);%0A            if (json != null) %7B%0A    
             data
@@ -1222,31 +1222,19 @@
 ect(
-Utils.readFile(path));%0A
+json);%0A    
     
@@ -1288,16 +1288,20 @@
 ersion)%0A
+    
         
@@ -1363,16 +1363,30 @@
 base%22);%0A
+            %7D%0A
         
@@ -1423,36 +1423,47 @@
 ) %7B%0A        
-    
+%7D%0A%0A        if (
 database
 Main = new J
@@ -1454,44 +1454,22 @@
 base
-Main
+Items
  =
+=
  n
-ew JSONObject();%0A           
+ull)
  dat
@@ -1494,34 +1494,24 @@
 SONArray();%0A
-        %7D%0A
         try 

Make sure database is not null