Java Code Examples for com.tinkerpop.blueprints.impls.orient.OrientVertex#addEdge()

The following examples show how to use com.tinkerpop.blueprints.impls.orient.OrientVertex#addEdge() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractProductRule.java    From light with Apache License 2.0 6 votes vote down vote up
protected void upVoteProduct(Map<String, Object> data) {
    OrientGraph graph = ServiceLocator.getInstance().getGraph();
    try {
        graph.begin();
        OrientVertex updateUser = (OrientVertex) graph.getVertexByKey("User.userId", data.remove("updateUserId"));
        OrientVertex product = (OrientVertex) graph.getVertexByKey("Product.entityId", data.get("entityId"));
        if (product != null && updateUser != null) {
            // remove DownVote edge if there is.
            for (Edge edge : updateUser.getEdges(product, Direction.OUT, "DownVote")) {
                if (edge.getVertex(Direction.IN).equals(product)) graph.removeEdge(edge);
            }
            updateUser.addEdge("UpVote", product);
        }
        graph.commit();
    } catch (Exception e) {
        logger.error("Exception:", e);
        graph.rollback();
    } finally {
        graph.shutdown();
    }
}
 
Example 2
Source File: AbstractProductRule.java    From light with Apache License 2.0 6 votes vote down vote up
protected void downVoteProduct(Map<String, Object> data) {
    OrientGraph graph = ServiceLocator.getInstance().getGraph();
    try {
        graph.begin();
        OrientVertex updateUser = (OrientVertex) graph.getVertexByKey("User.userId", data.remove("updateUserId"));
        OrientVertex product = (OrientVertex) graph.getVertexByKey("Product.entityId", data.get("entityId"));
        if (product != null && updateUser != null) {
            // remove UpVote edge if there is.
            for (Edge edge : updateUser.getEdges(product, Direction.OUT, "UpVote")) {
                if (edge.getVertex(Direction.IN).equals(product)) graph.removeEdge(edge);
            }
            updateUser.addEdge("DownVote", product);
        }
        graph.commit();
    } catch (Exception e) {
        logger.error("Exception:", e);
        graph.rollback();
    } finally {
        graph.shutdown();
    }
}
 
Example 3
Source File: AbstractPostRule.java    From light with Apache License 2.0 6 votes vote down vote up
protected void upVotePost(Map<String, Object> data) {
    OrientGraph graph = ServiceLocator.getInstance().getGraph();
    try {
        graph.begin();
        OrientVertex updateUser = (OrientVertex) graph.getVertexByKey("User.userId", data.remove("updateUserId"));
        OrientVertex post = (OrientVertex) graph.getVertexByKey("Post.entityId", data.get("entityId"));
        if (post != null && updateUser != null) {
            // remove DownVote edge if there is.
            for (Edge edge : updateUser.getEdges(post, Direction.OUT, "DownVote")) {
                if (edge.getVertex(Direction.IN).equals(post)) graph.removeEdge(edge);
            }
            updateUser.addEdge("UpVote", post);
        }
        graph.commit();
    } catch (Exception e) {
        logger.error("Exception:", e);
        graph.rollback();
    } finally {
        graph.shutdown();
    }
}
 
Example 4
Source File: AbstractPostRule.java    From light with Apache License 2.0 6 votes vote down vote up
protected void downVotePost(Map<String, Object> data) {
    OrientGraph graph = ServiceLocator.getInstance().getGraph();
    try {
        graph.begin();
        OrientVertex updateUser = (OrientVertex) graph.getVertexByKey("User.userId", data.remove("updateUserId"));
        OrientVertex post = (OrientVertex) graph.getVertexByKey("Post.entityId", data.get("entityId"));
        if (post != null && updateUser != null) {
            // remove UpVote edge if there is.
            for (Edge edge : updateUser.getEdges(post, Direction.OUT, "UpVote")) {
                if (edge.getVertex(Direction.IN).equals(post)) graph.removeEdge(edge);
            }
            updateUser.addEdge("DownVote", post);
        }
        graph.commit();
    } catch (Exception e) {
        logger.error("Exception:", e);
        graph.rollback();
    } finally {
        graph.shutdown();
    }
}
 
Example 5
Source File: AbstractUserRule.java    From light with Apache License 2.0 6 votes vote down vote up
protected void upVoteUser(Map<String, Object> data) {
    OrientGraph graph = ServiceLocator.getInstance().getGraph();
    try {
        graph.begin();
        OrientVertex user = (OrientVertex)graph.getVertexByKey("User.userId", data.get("userId"));
        OrientVertex voteUser = (OrientVertex)graph.getVertexByKey("User.userId", data.get("voteUserId"));
        if(user != null && voteUser != null) {
            for (Edge edge : voteUser.getEdges(user, Direction.OUT, "DownVote")) {
                if(edge.getVertex(Direction.IN).equals(user)) graph.removeEdge(edge);
            }
            voteUser.addEdge("UpVote", user);
        }
        graph.commit();
    } catch (Exception e) {
        logger.error("Exception:", e);
        graph.rollback();
        throw e;
    } finally {
        graph.shutdown();
    }
}
 
Example 6
Source File: AbstractUserRule.java    From light with Apache License 2.0 6 votes vote down vote up
protected void downVoteUser(Map<String, Object> data) {
    OrientGraph graph = ServiceLocator.getInstance().getGraph();
    try {
        graph.begin();
        OrientVertex user = (OrientVertex)graph.getVertexByKey("User.userId", data.get("userId"));
        OrientVertex voteUser = (OrientVertex)graph.getVertexByKey("User.userId", data.get("voteUserId"));
        if(user != null && voteUser != null) {
            for (Edge edge : voteUser.getEdges(user, Direction.OUT, "UpVote")) {
                if(edge.getVertex(Direction.IN).equals(user)) graph.removeEdge(edge);
            }
            voteUser.addEdge("DownVote", user);
        }
        graph.commit();
    } catch (Exception e) {
        logger.error("Exception:", e);
        graph.rollback();
        throw e;
    } finally {
        graph.shutdown();
    }
}
 
Example 7
Source File: BranchRule.java    From light with Apache License 2.0 6 votes vote down vote up
protected void downBranchDb(String branchType, Map<String, Object> data) throws Exception {
    String className = branchType.substring(0, 1).toUpperCase() + branchType.substring(1);
    String index = className + ".categoryId";
    OrientGraph graph = ServiceLocator.getInstance().getGraph();
    try{
        graph.begin();
        OrientVertex updateUser = (OrientVertex)graph.getVertexByKey("User.userId", data.remove("updateUserId"));
        OrientVertex branch = (OrientVertex)graph.getVertexByKey(index, data.get("categoryId"));
        if(branch != null && updateUser != null) {
            // remove UpVote edge if there is.
            for (Edge edge : updateUser.getEdges(branch, Direction.OUT, "UpVote")) {
                if(edge.getVertex(Direction.IN).equals(branch)) graph.removeEdge(edge);
            }
            updateUser.addEdge("DownVote", branch);
        }
        graph.commit();
    } catch (Exception e) {
        logger.error("Exception:", e);
        graph.rollback();
    } finally {
        graph.shutdown();
    }
}
 
Example 8
Source File: BranchRule.java    From light with Apache License 2.0 6 votes vote down vote up
protected void upBranchDb(String branchType, Map<String, Object> data) throws Exception {
    String className = branchType.substring(0, 1).toUpperCase() + branchType.substring(1);
    String index = className + ".categoryId";
    OrientGraph graph = ServiceLocator.getInstance().getGraph();
    try{
        graph.begin();
        OrientVertex updateUser = (OrientVertex)graph.getVertexByKey("User.userId", data.remove("updateUserId"));
        OrientVertex branch = (OrientVertex)graph.getVertexByKey(index, data.get("categoryId"));
        if(branch != null && updateUser != null) {
            // remove DownVote edge if there is.
            for (Edge edge : updateUser.getEdges(branch, Direction.OUT, "DownVote")) {
                if(edge.getVertex(Direction.IN).equals(branch)) graph.removeEdge(edge);
            }
            updateUser.addEdge("UpVote", branch);
        }
        graph.commit();
    } catch (Exception e) {
        logger.error("Exception:", e);
        graph.rollback();
    } finally {
        graph.shutdown();
    }
}
 
Example 9
Source File: AbstractMenuRule.java    From light with Apache License 2.0 6 votes vote down vote up
protected void addMenuItem(Map<String, Object> data) throws Exception {
    OrientGraph graph = ServiceLocator.getInstance().getGraph();
    try {
        graph.begin();
        Vertex user = graph.getVertexByKey("User.userId", data.remove("createUserId"));
        List<String> addMenuItems = (List<String>)data.remove("addMenuItems");
        OrientVertex menuItem = graph.addVertex("class:MenuItem", data);
        if(addMenuItems != null && addMenuItems.size() > 0) {
            // find vertex for each menuItem id and create edge to it.
            for(String menuItemId: addMenuItems) {
                Vertex childMenuItem = graph.getVertexByKey("MenuItem.menuItemId", menuItemId);
                menuItem.addEdge("Own", childMenuItem);
            }
        }
        user.addEdge("Create", menuItem);
        graph.commit();
    } catch (Exception e) {
        logger.error("Exception:", e);
        graph.rollback();
        throw e;
    } finally {
        graph.shutdown();
    }
}
 
Example 10
Source File: BranchRule.java    From light with Apache License 2.0 5 votes vote down vote up
protected void addBranchDb(String branchType, Map<String, Object> data) throws Exception {
    String className = branchType.substring(0, 1).toUpperCase() + branchType.substring(1);
    String host = (String)data.get("host");
    OrientGraph graph = ServiceLocator.getInstance().getGraph();
    try{
        graph.begin();
        Vertex createUser = graph.getVertexByKey("User.userId", data.remove("createUserId"));
        List<String> parentIds = (List<String>)data.remove("in_Own");
        List<String> childrenIds = (List<String>)data.remove("out_Own");
        OrientVertex branch = graph.addVertex("class:" + className, data);
        createUser.addEdge("Create", branch);
        // parent
        if(parentIds != null && parentIds.size() == 1) {
            OrientVertex parent = getBranchByHostId(graph, branchType, host, parentIds.get(0));
            if(parent != null) {
                parent.addEdge("Own", branch);
            }
        }
        // children
        if(childrenIds != null) {
            for(String childId: childrenIds) {
                OrientVertex child = getBranchByHostId(graph, branchType, host, childId);
                if(child != null) {
                    branch.addEdge("Own", child);
                }
            }
        }
        graph.commit();
    } catch (Exception e) {
        logger.error("Exception:", e);
        graph.rollback();
    } finally {
        graph.shutdown();
    }
}
 
Example 11
Source File: AbstractCommentRule.java    From light with Apache License 2.0 5 votes vote down vote up
protected void spmComment(Map<String, Object> data) throws Exception {
    OrientGraph graph = ServiceLocator.getInstance().getGraph();
    try{
        graph.begin();
        String commentId = (String)data.get("commentId");
        OrientVertex comment = (OrientVertex)graph.getVertexByKey("Comment.commentId", commentId);
        if(comment != null) {
            String userId = (String)data.get("userId");
            OrientVertex user = (OrientVertex)graph.getVertexByKey("User.userId", userId);
            if(user != null) {
                // check if this user has reported spam for this comment.
                boolean reported = false;
                for (Edge edge : user.getEdges(comment, Direction.OUT, "ReportSpam")) {
                    if(edge.getVertex(Direction.IN).equals(comment)) {
                        reported = true;
                        graph.removeEdge(edge);
                    }
                }
                if(!reported) {
                    user.addEdge("ReportSpam", comment);
                }
            }
        }
        graph.commit();
    } catch (Exception e) {
        logger.error("Exception:", e);
        graph.rollback();
    } finally {
        graph.shutdown();
    }
}
 
Example 12
Source File: AbstractMenuRule.java    From light with Apache License 2.0 5 votes vote down vote up
protected String addMenu( Map<String, Object> data) throws Exception {
    String json = null;
    OrientGraph graph = ServiceLocator.getInstance().getGraph();
    try {
        graph.begin();
        OrientVertex menu = graph.addVertex("class:Menu", "host", data.get("host"), "createDate", data.get("createDate"));
        List<String> addMenuItems = (List<String>)data.get("addMenuItems");
        if(addMenuItems != null && addMenuItems.size() > 0) {
            // find vertex for each menuItem id and create edge to it.
            for(String menuItemId: addMenuItems) {
                Vertex menuItem = graph.getVertexByKey("MenuItem.menuItemId", menuItemId);
                menu.addEdge("Own", menuItem);
            }
        }
        Vertex user = graph.getVertexByKey("User.userId", data.get("createUserId"));
        user.addEdge("Create", menu);
        graph.commit();
        json = menu.getRecord().toJSON("fetchPlan:menuItems:2");
    } catch (Exception e) {
        logger.error("Exception:", e);
        graph.rollback();
        throw e;
    } finally {
        graph.shutdown();
    }
    Map<String, Object> menuMap = (Map<String, Object>)ServiceLocator.getInstance().getMemoryImage("menuMap");
    ConcurrentMap<Object, Object> cache = (ConcurrentMap<Object, Object>)menuMap.get("cache");
    if(cache == null) {
        cache = new ConcurrentLinkedHashMap.Builder<Object, Object>()
                .maximumWeightedCapacity(100)
                .build();
        menuMap.put("cache", cache);
    }
    cache.put(data.get("host"), json);
    return json;
}
 
Example 13
Source File: AbstractCatalogRule.java    From light with Apache License 2.0 4 votes vote down vote up
protected void addProductDb(Map<String, Object> data) throws Exception {
    String host = (String)data.get("host");
    OrientGraph graph = ServiceLocator.getInstance().getGraph();
    try{
        graph.begin();
        OrientVertex createUser = (OrientVertex)graph.getVertexByKey("User.userId", data.remove("createUserId"));
        String parentId = (String)data.remove("parentId");
        List<String> tags = (List<String>)data.remove("tags");
        OrientVertex product = graph.addVertex("class:Product", data);
        createUser.addEdge("Create", product);
        // parent
        OrientVertex parent = getBranchByHostId(graph, categoryType, host, parentId);
        if(parent != null) {
            parent.addEdge("HasProduct", product);
        }
        // tag
        if(tags != null && tags.size() > 0) {
            for(String tagId: tags) {
                Vertex tag = null;
                // get the tag is it exists
                OIndex<?> tagHostIdIdx = graph.getRawGraph().getMetadata().getIndexManager().getIndex("tagHostIdIdx");
                OCompositeKey tagKey = new OCompositeKey(host, tagId);
                OIdentifiable tagOid = (OIdentifiable) tagHostIdIdx.get(tagKey);
                if (tagOid != null) {
                    tag = graph.getVertex(tagOid.getRecord());
                    product.addEdge("HasTag", tag);
                } else {
                    tag = graph.addVertex("class:Tag", "host", host, "tagId", tagId, "createDate", data.get("createDate"));
                    createUser.addEdge("Create", tag);
                    product.addEdge("HasTag", tag);
                }
            }
        }
        graph.commit();
    } catch (Exception e) {
        logger.error("Exception:", e);
        graph.rollback();
    } finally {
        graph.shutdown();
    }
}
 
Example 14
Source File: AbstractBfnRule.java    From light with Apache License 2.0 4 votes vote down vote up
protected void addPostDb(String bfnType, Map<String, Object> data) throws Exception {
    String className = bfnType.substring(0, 1).toUpperCase() + bfnType.substring(1);
    String host = (String)data.get("host");
    OrientGraph graph = ServiceLocator.getInstance().getGraph();
    try{
        graph.begin();
        Vertex createUser = graph.getVertexByKey("User.userId", data.remove("createUserId"));
        List<String> tags = (List<String>)data.remove("tags");
        
        OrientVertex post = graph.addVertex("class:Post", data);
        createUser.addEdge("Create", post);
        // parent
        OrientVertex parent = getBranchByHostId(graph, bfnType, host, (String) data.get("parentId"));
        if(parent != null) {
            parent.addEdge("HasPost", post);
        }
        // tag
        if(tags != null && tags.size() > 0) {
            for(String tagId: tags) {
                Vertex tag = null;
                // get the tag is it exists
                OIndex<?> tagHostIdIdx = graph.getRawGraph().getMetadata().getIndexManager().getIndex("tagHostIdIdx");
                OCompositeKey tagKey = new OCompositeKey(host, tagId);
                OIdentifiable tagOid = (OIdentifiable) tagHostIdIdx.get(tagKey);
                if (tagOid != null) {
                    tag = graph.getVertex(tagOid.getRecord());
                    post.addEdge("HasTag", tag);
                } else {
                    tag = graph.addVertex("class:Tag", "host", host, "tagId", tagId, "createDate", data.get("createDate"));
                    createUser.addEdge("Create", tag);
                    post.addEdge("HasTag", tag);
                }
            }
        }
        graph.commit();
    } catch (Exception e) {
        logger.error("Exception:", e);
        graph.rollback();
    } finally {
        graph.shutdown();
    }
}