diff --git a/WikiRating/src/main/java/computations/UserVoteFetch.java b/WikiRating/src/main/java/computations/UserVoteFetch.java index 8316564..b0b0566 100644 --- a/WikiRating/src/main/java/computations/UserVoteFetch.java +++ b/WikiRating/src/main/java/computations/UserVoteFetch.java @@ -1,75 +1,104 @@ package main.java.computations; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import org.glassfish.jersey.server.JSONP; import com.tinkerpop.blueprints.Direction; import com.tinkerpop.blueprints.Edge; import com.tinkerpop.blueprints.Vertex; import com.tinkerpop.blueprints.impls.orient.OrientGraph; import main.java.utilities.Connections; - +/** + * This class is used to fetch the votes given by the user and add that to the database. + * Duplicate votes aren't added but previous votes are updated during a revote + */ @Path("votePage") public class UserVoteFetch { @GET @Path("userVote") @JSONP(queryParam = "callback") @Produces({ "application/x-javascript" }) + /** + * This method adds the user vote to the the latest version of the page + * @param callback The function name for bypassing SOP + * @param pageTitle The title of the page whose latest revision is being voted + * @param userName The name of the user who is voting + * @param userVote The value of the vote + * @return A string stating the process is successful + */ public String getAllTestData(@QueryParam("callback") String callback, @QueryParam("pageTitle") String pageTitle,@QueryParam("userName") String userName,@QueryParam("userVote") int userVote) { try{ OrientGraph graph = Connections.getInstance().getDbGraph(); Vertex userNode=graph.getVertices("username",userName).iterator().next(); Vertex pageNode=graph.getVertices("title",pageTitle).iterator().next(); Vertex revisionNode = pageNode.getEdges(Direction.OUT, "@class", "PreviousVersionOfPage").iterator().next().getVertex(Direction.IN); - if(IsNotDuplicateVote(userNode, revisionNode)){ - + //Removes the old vote if exists + if(IsNotDuplicateVote(userNode, revisionNode)==false){ + Vertex votedRevisionNode=null; + for(Edge votedRevisionEdge:userNode.getEdges(Direction.OUT, "@class", "Review")){ + votedRevisionNode=votedRevisionEdge.getVertex(Direction.IN); + if(votedRevisionNode.getId()==revisionNode.getId()){ + System.out.println("Vote removed with value = "+votedRevisionEdge.getProperty("vote")+" having id = "+votedRevisionEdge.getProperty("@RID")); + graph.removeEdge(votedRevisionEdge); + + } + } + + } + //Creates the new vote Edge review = graph.addEdge("review", userNode, revisionNode, "Review"); review.setProperty("vote", userVote/10.0); review.setProperty("voteCredibility",userNode.getProperty("credibility")); graph.commit(); System.out.println(pageNode.getProperty("title")); System.out.println(userNode.getProperty("username")); System.out.println(userVote); System.out.println("New Vote added successfully"); - } + graph.shutdown(); }catch(Exception e){ e.printStackTrace(); } - String sJson="{\"pageTitle\":\"dd\",\"currentPageRating\":2,\"maxPageRating\":55,\"badgeNumber\":4}"; + String sJson="{\"pageTitle\":\"Successful\"}"; String result = callback + "(" + sJson + ");"; return result; } + /** + * This method checks whether the user already voted for the current version or not + * @param userNode The user Vertex + * @param revisionNode The latest revision of the Page being voted + * @return Either true of false indicating the presence of a duplicate edge + */ public boolean IsNotDuplicateVote(Vertex userNode,Vertex revisionNode){ Vertex votedRevisionNode=null; for(Edge votedRevisionEdge:userNode.getEdges(Direction.OUT, "@class", "Review")){ votedRevisionNode=votedRevisionEdge.getVertex(Direction.IN); if(votedRevisionNode.getId()==revisionNode.getId()){ System.out.println("Already voted"); return false; } } return true; } } diff --git a/WikiRating/src/main/java/controllers/PageInfo.java b/WikiRating/src/main/java/controllers/PageInfo.java index b0fb3d6..2597b2c 100644 --- a/WikiRating/src/main/java/controllers/PageInfo.java +++ b/WikiRating/src/main/java/controllers/PageInfo.java @@ -1,55 +1,59 @@ package main.java.controllers; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import org.glassfish.jersey.server.JSONP; import com.tinkerpop.blueprints.Vertex; import com.tinkerpop.blueprints.impls.orient.OrientGraph; import main.java.utilities.Connections; import main.java.utilities.PropertiesAccess; - +/** + * This class deals with the basic info requested by the platform's pages + */ @Path("display") public class PageInfo { @GET @Path("pageRating") @JSONP(queryParam = "callback") @Produces({ "application/x-javascript" }) + /** + * This method returns all the parameters requested by the Wiki page during the page load. + * @param callback The function name for bypassing SOP + * @param pageTitle Page title for which the information is being requested + * @return JSON string consisting of requesting parameters. + */ public String getAllTestData(@QueryParam("callback") String callback, @QueryParam("pageTitle") String pageTitle) { double currentPageRating=0,maxPageRating=0; int badgeNumber=0; OrientGraph graph = Connections.getInstance().getDbGraph(); try{ Vertex currentPage=graph.getVertices("title",pageTitle).iterator().next(); currentPageRating=currentPage.getProperty("PageRating"); maxPageRating=PropertiesAccess.getParameter("maxRating"); badgeNumber=currentPage.getProperty("badgeNumber"); }catch(Exception e){ e.printStackTrace(); } graph.shutdown(); System.out.println(pageTitle); System.out.println(currentPageRating); System.out.println(maxPageRating); System.out.println(badgeNumber); - - //{\"pageTitle\":ccc,\"currentPageRating\":xxc,\"maxPageRating\":vvv} - // String sJson="{\"data\":{\"id\":1}}"; - // String sJson="{\"id\":1}"; - //String sJson = "{\"PageName\":78,\"Ratings\":2.02}"; + String sJson="{\"pageTitle\":\""+pageTitle+"\",\"currentPageRating\":"+currentPageRating+",\"maxPageRating\":"+maxPageRating+",\"badgeNumber\":"+badgeNumber+"}"; - //String sJson="{\"pageTitle\":22,\"currentPageRating\":12.0,\"maxPageRating\":20.4}"; + String result = callback + "(" + sJson + ");"; return result; } }