view src/main/java/jp/ac/u_ryukyu/ie/cr/bbs/browsing/JungleBrowsingBulletinBoard.java @ 5:2b3542c5be34

move child
author tatsuki
date Tue, 02 Aug 2016 17:46:03 +0900
parents 5acde010c6db
children 36f0f18ce6a8
line wrap: on
line source

package jp.ac.u_ryukyu.ie.cr.bbs.browsing;


import jp.ac.u_ryukyu.ie.cr.jungle.DefaultJungle;
import jp.ac.u_ryukyu.ie.cr.jungle.Jungle;
import jp.ac.u_ryukyu.ie.cr.jungle.JungleTree;
import jp.ac.u_ryukyu.ie.cr.jungle.JungleTreeEditor;
import jp.ac.u_ryukyu.ie.cr.jungle.core.Children;
import jp.ac.u_ryukyu.ie.cr.jungle.store.NodePath;
import jp.ac.u_ryukyu.ie.cr.jungle.store.NulIterator;
import jp.ac.u_ryukyu.ie.cr.jungle.store.impl.DefaultNodePath;
import jp.ac.u_ryukyu.ie.cr.jungle.store.impl.DefaultTreeEditor;
import jp.ac.u_ryukyu.ie.cr.jungle.store.impl.TreeNode;
import jp.ac.u_ryukyu.ie.cr.jungle.store.impl.TreeNodeChildren;
import jp.ac.u_ryukyu.ie.cr.jungle.transaction.DefaultTreeNode;
import jp.ac.u_ryukyu.ie.cr.jungle.traverser.DefaultTraverser;
import jp.ac.u_ryukyu.ie.cr.jungle.traverser.InterfaceTraverser;
import jp.ac.u_ryukyu.ie.cr.jungle.util.Either;
import jp.ac.u_ryukyu.ie.cr.jungle.util.Error;
import plparser.Property;
import plparser.jungle.PropertyJungleTreeFactoryImpl;
import plparser.jungle.PropertyListJungleNodeFactory;
import plparser.jungle.PropertyListParserCreateTree;

import java.nio.ByteBuffer;
import java.util.Iterator;

public class JungleBrowsingBulletinBoard implements
        BrowsingBulletinBoard {
    private final Jungle jungle;

    public JungleBrowsingBulletinBoard() {
        jungle = new DefaultJungle(null, "hoge", new DefaultTreeEditor(
                new DefaultTraverser()));
        jungle.createNewTree("boards");
    }

    public Iterable<String> getBoards() {
        JungleTree tree = jungle.getTreeByName("boards");
        TreeNode node = tree.getRootNode();
        Children chs = node.getChildren();

        IterableConverter.Converter<String, TreeNode> converter = new IterableConverter.Converter<String, TreeNode>() {
            public String conv(TreeNode _b) {
                ByteBuffer e = _b.getAttributes().get("name");
                return new String(e.array());
            }
        };

        return new IterableConverter<String, TreeNode>(chs, converter);
    }

    public void createBoards(final String _name) {
        createBoards(_name, new DefaultTreeNode());
    }
        public void createBoards(final String _name, TreeNode rootNode) {
        if (null == jungle.createNewTree(_name, rootNode)) {
            throw new IllegalStateException();
        }
        JungleTree tree = jungle.getTreeByName("boards");
        JungleTreeEditor editor = tree.getTreeEditor();
        DefaultNodePath root = new DefaultNodePath();
        Either<Error, JungleTreeEditor> either = editor.addNewChildAt(root, 0);
        if (either.isA()) {
            throw new IllegalStateException();
        }
        editor = either.b();

        either = editor.putAttribute(root.add(0), "name", ByteBuffer.wrap(_name.getBytes()));
        if (either.isA()) {
            throw new IllegalStateException();
        }
        editor = either.b();
        Either<Error, JungleTreeEditor> result = editor.success();
        if (result.isA()) {
            throw new IllegalStateException();
        }

        tree = jungle.getTreeByName(_name);
        editor = tree.getTreeEditor();
        either = editor.putAttribute(root, "NodeName", ByteBuffer.wrap("root".getBytes()));
        if (either.isA()) {
            throw new IllegalStateException();
        }
        editor = either.b();
        editor.success();
    }

    public void createBoardMessage(final String boardName, final String key,
                                   final String attribute, final String pathStr) {
        if (key.equals("") | attribute.equals(""))
            return ;
        NodePath path = createNodePath(pathStr);
        JungleTree tree = jungle.getTreeByName(boardName);
        if (tree == null) {
            throw new IllegalStateException();
        }

        JungleTreeEditor editor;
        do {
            editor = tree.getTreeEditor();
            Either<Error, JungleTreeEditor> either = editor.putAttribute(path, key, ByteBuffer.wrap(attribute.getBytes()));
            if (either.isA()) {
                throw new IllegalStateException();
            }
            editor = either.b();
        } while (editor.success().isA());
    }

    @Override
    public void createChild(String bname, String nodeName, String pathStr) {
        NodePath path = createNodePath(pathStr);
        JungleTree tree = jungle.getTreeByName(bname);
        if (tree == null) {
            throw new IllegalStateException();
        }
        Either<Error,TreeNode> getNodeEither = tree.getNodeOfPath(path);
        if (getNodeEither.isA())
            return ;
        TreeNode currentNode = getNodeEither.b();
        int childCount = currentNode.getChildren().size();

        JungleTreeEditor editor;
        do {
            editor = tree.getTreeEditor();
            Either<Error,JungleTreeEditor> either = editor.addNewChildAt(path, childCount);
            if (either.isA()) {
                throw new IllegalStateException();
            }
            editor = either.b();

            either = editor.putAttribute(path.add(childCount),"NodeName",ByteBuffer.wrap(nodeName.getBytes()));
            if (either.isA()) {
                throw new IllegalStateException();
            }
            editor = either.b();

        } while (editor.success().isA());
    }

    public void editMessage(String bname, String key, final String attribute,
                            final String pathStr) {
        NodePath path = createNodePath(pathStr);

        JungleTreeEditor editor = null;

        do {
            JungleTree tree = jungle.getTreeByName(bname);
            editor = tree.getTreeEditor();

            Either<Error, JungleTreeEditor> either = editor.putAttribute(path, key, ByteBuffer.wrap(attribute.getBytes()));

            if (either.isA()) {
                throw new IllegalStateException();
            }
            editor = either.b();
        } while (editor.success().isA());
    }

    public String sanitize(String str) {
        if (str == null) {
            return str;
        }
        str = str.replaceAll("&", "&amp;");
        str = str.replaceAll("<", "&lt;");
        str = str.replaceAll(">", "&gt;");
        str = str.replaceAll("\"", "&quot;");
        str = str.replaceAll("'", "&#39;");
        return str;
    }

    public GetAttributeImp getAttribute(String _bname, String nodePath) {

        DefaultNodePath path = createNodePath(nodePath);
        JungleTree tree = jungle.getTreeByName(_bname);
        Either<Error, TreeNode> either = tree.getNodeOfPath(path);
        if (either.isA())
            return new GetAttributeImp(new DefaultTreeNode());
        TreeNode currentNode = either.b();
        return new GetAttributeImp(currentNode);
    }

    @Override
    public Iterator<TreeNode> getChildren(String bname, String nodePath) {
        DefaultNodePath path = createNodePath(nodePath);
        JungleTree tree = jungle.getTreeByName(bname);
        Either<Error, TreeNode> either = tree.getNodeOfPath(path);
        if (either.isA())
            return new NulIterator<TreeNode>();
        TreeNode currentNode = either.b();
        TreeNodeChildren children = currentNode.getChildren();
        return children.iterator();
    }

    @Override
    public void importLayout(String boardName, String path) {
        PropertyListParserCreateTree<Property> jp;
        PropertyListJungleNodeFactory jlf = new PropertyJungleTreeFactoryImpl();
        jp = new PropertyListParserCreateTree<Property>(jlf);
        TreeNode root = jp.parseFile(path);
        createBoards(boardName, root);
    }

    @Override
    public Iterator<TreeNode> findNode(String bname, String key, String value) {
        JungleTree tree = jungle.getTreeByName(bname);
        InterfaceTraverser traverser = tree.getTraverser(true);
        return traverser.find((TreeNode node) -> {
            return true;
        },key,value);
    }

    @Override
    public String getNodePath(String bname, TreeNode node) {
        JungleTree tree = jungle.getTreeByName(bname);
        NodePath path = tree.getNodePath(node);
        return path.toString();
    }

    @Override
    public boolean childMove(String bname, String pathString, String childNumString, String move) {
        int childNum = Integer.parseInt(childNumString);
        JungleTree tree = jungle.getTreeByName(bname);
        DefaultNodePath path = createNodePath(pathString);
        TreeNode node = tree.getNodeOfPath(path).b();
        int childrenCount = node.getChildren().size();
        TreeNode child = node.getChildren().at(childNum).b();
        TreeNodeChildren grandsons = child.getChildren();
        JungleTreeEditor editor = tree.getTreeEditor();
        editor = editor.deleteChildAt(path,childNum).b();
        if (move.equals("up") && childNum != 0)
            childNum--;
        else if (move.equals("down") && childNum != childrenCount)
            childNum++;
        editor = editor.addNewChildAt(path,childNum).b();

        Iterator<String> keys = child.getAttributes().getKeys();
        NodePath newChildPath = path.add(childNum);
        while (keys.hasNext()) {
            String key = keys.next();
            ByteBuffer value = child.getAttributes().get(key);
            editor = editor.putAttribute(newChildPath,key,value).b();
        }

        for (TreeNode grandson : grandsons) {
   
        }
        return editor.success().isB();
    }

    private DefaultNodePath createNodePath(String nodePath) {
        DefaultNodePath path = new DefaultNodePath();
        String[] nums = nodePath.split(",");
        for (String num : nums) {
            if (num.equals("-1"))
                continue;
            path = path.add(Integer.parseInt(num));
        }
        return path;
    }
}