All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.siwenyan.potluck.GuestList Maven / Gradle / Ivy

The newest version!
package com.github.siwenyan.potluck;

import com.github.siwenyan.common.*;
import com.github.siwenyan.dish_parser.IDish;
import com.github.siwenyan.dish_parser.Constants;
import com.github.siwenyan.dish_parser.SyntaxSimple;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class GuestList extends AbstractSimpleGuest {
    private static final String KEYWORD_ADD_TO_LIST = "addToList";
    private static final String KEYWORD_ADD_TO_LIST_FROM_FILE = "addToListFromFile";
    private static final String KEYWORD_CLEAR_LIST = "clearList";
    private static final String KEYWORD_COUNT_LIST = "countList";
    private static final String KEYWORD_GET_LIST_ITEM = "getListItem";
    private static final String KEYWORD_INDEX_OF = "indexOf";
    private static final String KEYWORD_REMOVE_LIST_ITEM = "removeListItem";
    private static final String DEFAULT_SPLITTER = ":";
    private static final String ITEMS_KEY = "items";
    private static final String ITEM_KEY = "item";
    private static final String INTO_KEY = "into";
    private static final String INDEX_KEY = "index";
    private static final String SPLITTER_KEY = "splitter";
    private static final String NO_DUPLICATE_KEY = "noDuplicate";
    private static final String MAX_KEY = "max";

    @Override
    public String help(String flavor) {
        String help = "";

        if (flavor.equals(KEYWORD_ADD_TO_LIST)) {
            help += Constants.OUTPUT_INDENT + KEYWORD_ADD_TO_LIST + Constants.OUTPUT_FLAVOR_SEPARATOR + "yourListName"
                    + Constants.OUTPUT_PARAM_SEPARATOR + ITEMS_KEY + Constants.OUTPUT_PARAMNAME_SEPARATOR
                    + "itemsWithSplitter" + Constants.OUTPUT_PARAM_SEPARATOR + SPLITTER_KEY
                    + Constants.OUTPUT_PARAMNAME_SEPARATOR + "yourSplitter" + Constants.OUTPUT_PARAM_SEPARATOR
                    + NO_DUPLICATE_KEY + Constants.OUTPUT_PARAMNAME_SEPARATOR + "[False]|True"
                    + Constants.OUTPUT_PARAM_SEPARATOR + MAX_KEY + Constants.OUTPUT_PARAMNAME_SEPARATOR
                    + "maxNumberOfItems" + Constants.OUTPUT_LINE_SEPARATOR + "\r\n";
            help += Constants.OUTPUT_INDENT + KEYWORD_ADD_TO_LIST + Constants.OUTPUT_FLAVOR_SEPARATOR + "yourListName"
                    + Constants.OUTPUT_PARAM_SEPARATOR + ITEMS_KEY + Constants.OUTPUT_PARAMNAME_SEPARATOR + "item1"
                    + DEFAULT_SPLITTER + "item2" + DEFAULT_SPLITTER + "..." + Constants.OUTPUT_PARAM_SEPARATOR
                    + MAX_KEY + Constants.OUTPUT_PARAMNAME_SEPARATOR + "maxNumberOfItems"
                    + Constants.OUTPUT_LINE_SEPARATOR + "\r\n";
        }

        if (flavor.equals(KEYWORD_ADD_TO_LIST_FROM_FILE)) {
            help += Constants.OUTPUT_INDENT + KEYWORD_ADD_TO_LIST_FROM_FILE + Constants.OUTPUT_FLAVOR_SEPARATOR
                    + "yourListName" + Constants.OUTPUT_PARAM_SEPARATOR + ITEMS_KEY
                    + Constants.OUTPUT_PARAMNAME_SEPARATOR + "yourFileName" + Constants.OUTPUT_PARAM_SEPARATOR
                    + SPLITTER_KEY + Constants.OUTPUT_PARAMNAME_SEPARATOR + "yourSplitter"
                    + Constants.OUTPUT_PARAM_SEPARATOR + NO_DUPLICATE_KEY + Constants.OUTPUT_PARAMNAME_SEPARATOR
                    + "[False]|True" + Constants.OUTPUT_PARAM_SEPARATOR + MAX_KEY
                    + Constants.OUTPUT_PARAMNAME_SEPARATOR + "maxNumberOfItems" + Constants.OUTPUT_LINE_SEPARATOR
                    + "\r\n";
        }

        if (flavor.equals(KEYWORD_CLEAR_LIST)) {
            help += Constants.OUTPUT_INDENT + KEYWORD_CLEAR_LIST + Constants.OUTPUT_FLAVOR_SEPARATOR + "yourListName"
                    + Constants.OUTPUT_LINE_SEPARATOR + "\r\n";
        }

        if (flavor.equals(KEYWORD_COUNT_LIST)) {
            help += Constants.OUTPUT_INDENT + KEYWORD_COUNT_LIST + Constants.OUTPUT_FLAVOR_SEPARATOR + "yourListName"
                    + Constants.OUTPUT_PARAM_SEPARATOR + INTO_KEY + Constants.OUTPUT_PARAMNAME_SEPARATOR
                    + "yourVariableName" + Constants.OUTPUT_LINE_SEPARATOR + "\r\n";
        }

        if (flavor.equals(KEYWORD_GET_LIST_ITEM)) {
            help += Constants.OUTPUT_INDENT + KEYWORD_GET_LIST_ITEM + Constants.OUTPUT_FLAVOR_SEPARATOR
                    + "yourListName" + Constants.OUTPUT_PARAM_SEPARATOR + INDEX_KEY
                    + Constants.OUTPUT_PARAMNAME_SEPARATOR + "indexOfItem(start from 0)"
                    + Constants.OUTPUT_PARAM_SEPARATOR + INTO_KEY + Constants.OUTPUT_PARAMNAME_SEPARATOR
                    + "yourVariableName" + Constants.OUTPUT_LINE_SEPARATOR + "\r\n";
        }

        if (flavor.equals(KEYWORD_INDEX_OF)) {
            help += Constants.OUTPUT_INDENT + KEYWORD_INDEX_OF + Constants.OUTPUT_FLAVOR_SEPARATOR + "yourListName"
                    + Constants.OUTPUT_PARAM_SEPARATOR + ITEM_KEY + Constants.OUTPUT_PARAMNAME_SEPARATOR
                    + "lookupItem" + Constants.OUTPUT_PARAM_SEPARATOR + INTO_KEY
                    + Constants.OUTPUT_PARAMNAME_SEPARATOR + "yourVariableName (start from 0, -1 when not found)"
                    + Constants.OUTPUT_LINE_SEPARATOR + "\r\n";
        }

        if (flavor.equals(KEYWORD_REMOVE_LIST_ITEM)) {
            help += Constants.OUTPUT_INDENT + KEYWORD_REMOVE_LIST_ITEM + Constants.OUTPUT_FLAVOR_SEPARATOR
                    + "yourListName" + Constants.OUTPUT_PARAM_SEPARATOR + INDEX_KEY
                    + Constants.OUTPUT_PARAMNAME_SEPARATOR + "indexOfItem(start from 0)"
                    + Constants.OUTPUT_PARAM_SEPARATOR + INTO_KEY + Constants.OUTPUT_PARAMNAME_SEPARATOR
                    + "yourVariableName" + Constants.OUTPUT_LINE_SEPARATOR + "\r\n";
        }

        return help;
    }

    @Override
    public Feedback have(IDish dish) {
        try {
            String flavor = dish.getFlavor().getFlavorName();
            String listName = this.dynamic(dish.getStringElement(Constants.DEFAULT_KEY));
            if (listName == null) {
                return new Feedback(Feedback.EStatus.FAIL, "List name not specified.");
            }

            MyEnvironment myEnvironment = myTable.getMyEnvironment();
            @SuppressWarnings("unchecked")
            List list = (List) myEnvironment.get(listName);

            if (flavor.equals(KEYWORD_ADD_TO_LIST)) {
                if (list == null) {
                    list = new ArrayList();
                    myEnvironment.put(listName, list);
                }
                String sItems = this.dynamic(dish.getStringElement(ITEMS_KEY));
                String splitter = this.dynamic(dish.getStringElement(SPLITTER_KEY));
                if (splitter == null || splitter.isEmpty()) {
                    splitter = DEFAULT_SPLITTER;
                }

                boolean noDuplicate = getKeyNoDuplicate(dish);
                String sMax = this.dynamic(dish.getStringElement(MAX_KEY));

                List items = SyntaxSimple.splitWithQuotedString(sItems, SyntaxSimple.SYMBOL_QUOTE,
                        new String[]{splitter}, sMax.isEmpty() ? Integer.MAX_VALUE : Integer.parseInt(sMax), true,
                        null);
                addToList(list, noDuplicate, items);
            }
            if (flavor.equals(KEYWORD_ADD_TO_LIST_FROM_FILE)) {
                if (list == null) {
                    list = new ArrayList();
                    myEnvironment.put(listName, list);
                }
                String fullPath = this.dynamic(dish.getStringElement(ITEMS_KEY));
                String sItems = FileUtils.readFileToString(new File(fullPath), "utf-8");
                String splitter = this.dynamic(dish.getStringElement(SPLITTER_KEY));
                splitter = MyEscaper.unescape(splitter);
                if (splitter == null) {
                    splitter = DEFAULT_SPLITTER;
                }
                boolean noDuplicate = getKeyNoDuplicate(dish);
                String sMax = dish.containsElement(MAX_KEY) ? this.dynamic(dish.getStringElement(MAX_KEY)) : "0";
                List items = SyntaxSimple.splitWithQuotedString(sItems, SyntaxSimple.SYMBOL_QUOTE,
                        new String[]{splitter}, Integer.parseInt(sMax), true, null);
                for (int i = items.size() - 1; i >= 0; i--) {
                    if (items.get(i) == null || items.get(i).trim().isEmpty()
                            || items.get(i).trim().startsWith(SyntaxSimple.SYMBOL_COMMENT)) {
                        items.remove(i);
                    }
                }
                addToList(list, noDuplicate, items);
            }
            if (flavor.equals(KEYWORD_CLEAR_LIST)) {
                if (list == null) {
                    return new Feedback(Feedback.EStatus.YAMI, "Undefined list: " + listName);
                }
                list.clear();
            }
            if (flavor.equals(KEYWORD_COUNT_LIST)) {
                String into = this.dynamic(dish.getStringElement(INTO_KEY));
                if (into == null) {
                    return new Feedback(Feedback.EStatus.FAIL, "Missing parameter: " + INTO_KEY);
                }
                if (list == null) {
                    myEnvironment.put(into, "0");
                } else {
                    myEnvironment.put(into, "" + list.size());
                }
            }
            if (flavor.equals(KEYWORD_GET_LIST_ITEM)) {
                String sIndex = this.dynamic(dish.getStringElement(INDEX_KEY));
                if (sIndex == null) {
                    return new Feedback(Feedback.EStatus.FAIL, "Missing parameter: " + INDEX_KEY);
                }
                String into = this.dynamic(dish.getStringElement(INTO_KEY));
                if (into == null) {
                    return new Feedback(Feedback.EStatus.FAIL, "Missing parameter: " + INTO_KEY);
                }

                if (list == null) {
                    myEnvironment.put(into, "");
                } else {
                    myEnvironment.put(into, list.get(Integer.parseInt(sIndex)));
                }
            }
            if (flavor.equals(KEYWORD_INDEX_OF)) {
                String item = this.dynamic(dish.getStringElement(ITEM_KEY));
                if (item == null) {
                    return new Feedback(Feedback.EStatus.FAIL, "Missing parameter: " + ITEM_KEY);
                }
                String into = this.dynamic(dish.getStringElement(INTO_KEY));
                if (into == null) {
                    return new Feedback(Feedback.EStatus.FAIL, "Missing parameter: " + INTO_KEY);
                }

                if (list == null) {
                    myEnvironment.put(into, "");
                } else {
                    myEnvironment.put(into, "" + list.indexOf(item));
                }
            }
            if (flavor.equals(KEYWORD_REMOVE_LIST_ITEM)) {
                String sIndex = this.dynamic(dish.getStringElement(INDEX_KEY));
                if (sIndex == null) {
                    return new Feedback(Feedback.EStatus.FAIL, "Missing parameter: " + INDEX_KEY);
                }
                String into = this.dynamic(dish.getStringElement(INTO_KEY));

                if (list == null) {
                    myEnvironment.put(into, "");
                } else {

                    if (into == null || into.isEmpty()) {
                        list.remove(Integer.parseInt(sIndex));
                    }
                    myEnvironment.put(into, list.remove(Integer.parseInt(sIndex)));
                }
            }

            return Feedback.yami();
        } catch (Exception e) {
            return new Feedback(Feedback.EStatus.FAIL, e.getMessage());
        }

    }

    private void addToList(List list, boolean noDuplicate, List items) {
        if (noDuplicate) {
            for (String item : items) {
                if (!list.contains(item)) {
                    items.add(item);
                }
            }
        } else {
            list.addAll(items);
        }
    }

    private boolean getKeyNoDuplicate(IDish dish) {
        boolean noDuplicate = false;
        String sNoDuplicate = this.dynamic(dish.getStringElement(NO_DUPLICATE_KEY)).trim();
        if (!sNoDuplicate.isEmpty()) {
            noDuplicate = Boolean.parseBoolean(sNoDuplicate);
        }
        return noDuplicate;
    }

    public String[] getFlavors() {
        return new String[]{KEYWORD_ADD_TO_LIST, KEYWORD_ADD_TO_LIST_FROM_FILE, KEYWORD_CLEAR_LIST,
                KEYWORD_COUNT_LIST, KEYWORD_GET_LIST_ITEM, KEYWORD_INDEX_OF, KEYWORD_REMOVE_LIST_ITEM};
    }

}