package de.rlexbuilding.rlexdb.command;

import de.rlexbuilding.rlexdb.ObjectSerializer;
import de.rlexbuilding.rlexdb.RLEXDB;
import de.rlexbuilding.rlexdb.Table;
import de.rlexbuilding.rlexdb.exception.EntryNotExistsException;
import de.rlexbuilding.rlexdb.exception.TableNotExistsException;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;

/**
 * Created by RLEXBuilding on 08.10.2017.
 * This is the get command
 */
public class GetCommand extends Command {

    public GetCommand() {
        super("get", "this returns a value");
    }

    @Override
    protected void onExecute(RLEXDB rlexdb, String[] args, OutputStream out) throws IOException {
        PrintWriter writer = new PrintWriter(out, true);
        if(args.length == 2){
            String tableName = args[0];
            String key = args[1];
            Table table = new Table(tableName);
            try {
                String result = "Result:" + ObjectSerializer.serializeObject(table.getValue(key));
                writer.println(result);
            } catch (TableNotExistsException e) {
                writer.println("Error: Table not exists");
            } catch (ClassNotFoundException e) {
                writer.println("Error: Class not found");
            } catch (EntryNotExistsException e) {
                writer.println("Error: Entry not exists");
            }
        }

    }
}
