com.des.paperbase.ManageData.java Source code

Java tutorial

Introduction

Here is the source code for com.des.paperbase.ManageData.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.des.paperbase;

import com.des.generate.GenerateFile;
import com.des.random.Randomizer;
import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.simple.ItemList;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

/**
 *
 * @author Surachai
 */
public class ManageData {

    private static String PATH_FILE = "";
    private static String PROPERTIES_PATH = "";
    public GenerateFile g;

    public ManageData(String TablePath) {
        g = new GenerateFile();
        initTable(TablePath);
    }

    public static void main(String[] args) throws ParseException, IOException {
        ManageData p = new ManageData("DB");

        //p.Create("value");
        Map<String, Object> value = new HashMap<String, Object>();
        //value.put("lastname", "9999");
        value.put("age", "56");
        Map<String, Object> update = new HashMap<String, Object>();
        update.put("lastname", "333");
        update.put("age", "56");
        p.merge("value", value, update, "AND");
        List<Map<String, Object>> output = p.select("value", null, "OR");
        System.out.println(output.size());
        for (int i = 0; i < output.size(); i++) {
            System.out.println(output.get(i));
        }

    }

    public JSONObject getJsonFromString(String input) throws ParseException {
        return (JSONObject) new JSONParser().parse(input.toString());
    }

    public void initTable(String path) {
        GenerateFile g = new GenerateFile();
        PATH_FILE = path;
        g.createFolder(PATH_FILE);
    }

    public void Create(String Tablename) throws IOException {
        File data = new File(PATH_FILE + "\\" + Tablename + ".json");
        if (!data.exists()) {
            g.writefile("{\"data\":[]}", PATH_FILE + "\\" + Tablename + ".json");
        }
    }

    public void CleanTable(String Tablename) throws IOException {
        g.writefile("{\"data\":[]}", PATH_FILE + "\\" + Tablename + ".json");
    }

    public void merge(String Tablename, Map<String, Object> Filter, Map<String, Object> Value, String FilterType)
            throws ParseException {
        List<Map<String, Object>> output = select(Tablename, Filter, "OR");
        System.out.println(output.size());
        for (int i = 0; i < output.size(); i++) {
            System.out.println(output.get(i));
        }
        try {
            if (output.size() != 0) {
                //update
                System.out.println("update");
                update(Tablename, Filter, Value, FilterType);
            } else {
                //insert
                System.out.println("insert");
                insert(Tablename, Value);

            }
        } catch (IOException ex) {
            Logger.getLogger(ManageData.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void insert(String Tablename, Map<String, Object> value) throws IOException, ParseException {
        String data = g.readFileToString(PATH_FILE + "\\" + Tablename + ".json");
        JSONObject table = new JSONObject();
        JSONObject field = new JSONObject();
        JSONArray record = new JSONArray();
        List<String> keySet = g.getKeyFromMap(value);
        if (data.equalsIgnoreCase("{\"data\":[]}")) {
            for (int i = 0; i < keySet.size(); i++) {
                field.put(keySet.get(i), value.get(keySet.get(i)));
            }
            record.add(field);
            table.put("data", record);
            g.writefile(table.toJSONString(), PATH_FILE + "\\" + Tablename + ".json");
        } else {
            JSONObject json = (JSONObject) new JSONParser().parse(data);
            JSONArray OldValue = (JSONArray) json.get("data");
            for (int i = 0; i < keySet.size(); i++) {
                field.put(keySet.get(i), value.get(keySet.get(i)));
            }
            OldValue.add(field);
            json.put("data", OldValue);
            g.writefile(json.toJSONString(), PATH_FILE + "\\" + Tablename + ".json");
        }
    }

    public void update(String Tablename, Map<String, Object> Filter, Map<String, Object> updateValue,
            String FilterType) throws IOException, ParseException {
        String data = g.readFileToString(PATH_FILE + "\\" + Tablename + ".json");
        JSONObject json = (JSONObject) new JSONParser().parse(data);
        List<String> keySet = g.getKeyFromMap(updateValue);
        if (!data.equalsIgnoreCase("{\"data\":[]}")) {
            JSONArray newValue = new JSONArray();
            JSONArray OldValue = (JSONArray) json.get("data");
            for (int s = 0; s < OldValue.size(); s++) {
                JSONObject record = (JSONObject) OldValue.get(s);
                if (MappingRecordAndKey(Filter, record, FilterType)) {
                    for (int i = 0; i < keySet.size(); i++) {
                        System.out.println("replace : " + keySet.get(i) + "  to " + updateValue.get(keySet.get(i)));
                        record.replace(keySet.get(i), updateValue.get(keySet.get(i)));
                    }

                }
                newValue.add(record);
            }
            json.put("data", newValue);
            System.out.println(json.toJSONString());
            g.writefile(json.toJSONString(), PATH_FILE + "\\" + Tablename + ".json");
        } else {
            g.writefile(data, PATH_FILE + "\\" + Tablename + ".json");
        }

    }

    public List<Map<String, Object>> select(String Tablename, Map<String, Object> value, String FilterType)
            throws ParseException {
        String data = g.readFileToString(PATH_FILE + "\\" + Tablename + ".json");
        System.out.println(PATH_FILE + "\\" + Tablename + ".json");
        JSONArray output = new JSONArray();
        JSONObject json = (JSONObject) new JSONParser().parse(data);
        List<Map<String, Object>> result = new LinkedList<Map<String, Object>>();
        GenerateFile g = new GenerateFile();
        if (!data.equalsIgnoreCase("{\"data\":[]}")) {
            JSONArray OldValue = (JSONArray) json.get("data");
            for (int s = 0; s < OldValue.size(); s++) {
                JSONObject record = (JSONObject) OldValue.get(s);
                if (MappingRecordAndKey(value, record, FilterType)) {
                    List<String> Listkey = g.getKeyFromJSONArray(record.keySet());
                    Map<String, Object> Maprecord = new HashMap<String, Object>();
                    for (int i = 0; i < Listkey.size(); i++) {
                        System.out.println(Listkey.get(i) + "," + record.get(Listkey.get(i)));
                        Maprecord.put(Listkey.get(i), record.get(Listkey.get(i)));
                    }
                    result.add(Maprecord);
                }
            }
        } else {

        }
        return result;
    }

    public void delete(String Tablename, Map<String, Object> value, String FilterType)
            throws ParseException, IOException {
        String data = g.readFileToString(PATH_FILE + "\\" + Tablename + ".json");
        List<Integer> ListDelete = new LinkedList<Integer>();
        if (!data.equalsIgnoreCase("{\"data\":[]}")) {
            JSONObject json = (JSONObject) new JSONParser().parse(data);
            JSONArray OldValue = (JSONArray) json.get("data");
            for (int s = 0; s < OldValue.size(); s++) {
                JSONObject record = (JSONObject) OldValue.get(s);
                if (MappingRecordAndKey(value, record, FilterType)) {
                    ListDelete.add(s);
                }
            }
            for (int i = ListDelete.size() - 1; i >= 0; i--) {
                System.out.print(ListDelete.get(i) + " ");
                OldValue.remove(ListDelete.get(i).intValue());
            }
            JSONObject table = new JSONObject();
            table.put("data", OldValue);
            g.writefile(table.toJSONString(), PATH_FILE + "\\" + Tablename + ".json");
        }

    }

    public boolean MappingRecordAndKey(Map<String, Object> value, JSONObject record, String FilterType) {
        List<String> keySet = new LinkedList<String>();

        if (value == null) {
            return true;
        } else if (value.isEmpty()) {
            return true;
        } else {
            keySet = g.getKeyFromMap(value);
        }

        int CheckEqual = 0;
        for (int i = 0; i < keySet.size(); i++) {
            if (record.get(keySet.get(i)) != null) {
                if (record.get(keySet.get(i)).toString().equalsIgnoreCase(value.get(keySet.get(i)).toString())) {
                    if (FilterType.equalsIgnoreCase("AND")) {
                        CheckEqual += 1;
                    } else if (FilterType.equalsIgnoreCase("OR")) {
                        CheckEqual = keySet.size();
                        break;
                    }

                }
            }
        }
        if (CheckEqual == keySet.size()) {

            return true;
        }
        return false;
    }

}