diff --git a/WikiRating/src/main/java/computations/BadgeGenerator.java b/WikiRating/src/main/java/computations/BadgeGenerator.java index 10c0ae6..ab298de 100644 --- a/WikiRating/src/main/java/computations/BadgeGenerator.java +++ b/WikiRating/src/main/java/computations/BadgeGenerator.java @@ -1,169 +1,186 @@ package main.java.computations; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import com.tinkerpop.blueprints.Vertex; import com.tinkerpop.blueprints.impls.orient.OrientGraph; import main.java.utilities.Connections; +import main.java.utilities.PropertiesAccess; /** * This class will calculate the Badges that will be assigned * to the Pages based on the analysis of Page Rating distribution */ public class BadgeGenerator { //These varibales will store the computed cutoffs for the various badges static double platinumBadgeRatingCutoff; static double goldBadgeRatingCutoff; static double silverBadgeRatingCutoff; static double bronzeBadgeRatingCutoff; static double stoneBadgeRatingCutoff; /** * This enum has the percentile ranges for various badges */ public enum Badges { - + PLATINUM(1), + GOLD(2), + SILVER(3), + BRONZE(4), + STONE(5), PLATINUM_BADGE_START_PERCENTILE(80), GOLD_BADGE_START_PERCENTILE(60), SILVER_BADGE_START_PERCENTILE(40), BRONZE_BADGE_START_PERCENTILE(20), STONE_BADGE_START_PERCENTILE(0); private int value; Badges(int value) { this.value = value; } public int getValue() { return value; } } /** * This class will store our PageObjects to insert into Arraylist for * percentile calculations */ public class PageRatingData{ int pid; double pageRating; String pageName; public PageRatingData(int pid,double pageRating,String pageName) { this.pid=pid; this.pageRating=pageRating; this.pageName=pageName; } } /** * This is the custom comparator to sort the pageList in the ascending * order of PageRatings */ class PageRatingComparator implements Comparator{ @Override public int compare(PageRatingData pageRating1, PageRatingData pageRating2) { if(pageRating1.pageRating>pageRating2.pageRating) return 1; else return -1; } } /** * This method will assign badges based on the percentile */ public void generateBadges(){ ArrayList pageList=new ArrayList(); OrientGraph graph = Connections.getInstance().getDbGraph(); - - String badgeName=""; - int currentPageID=0; - double currentPageRating=0; + Vertex currentPageNode=null; + int badgeNumber=4; + int currentPageID=0,noOfPages=0; + double currentPageRating=0,maxPageRating=0; String currentPageName=""; for(Vertex pageNode:graph.getVertices("@class","Page")){ currentPageID=pageNode.getProperty("pid"); currentPageRating=pageNode.getProperty("PageRating"); currentPageName=pageNode.getProperty("title"); pageList.add(new PageRatingData(currentPageID, currentPageRating,currentPageName)); } Collections.sort(pageList,new PageRatingComparator()); calculateBadgeCutoff(pageList); + noOfPages=pageList.size(); + int noOfPagesCounter=0; for(PageRatingData currentPage:pageList){ - badgeName=getBadgeName(currentPage.pageRating); - System.out.println(currentPage.pageName + " ------with ratings= "+currentPage.pageRating+" earned "+badgeName); + badgeNumber=getBadgeNumber(currentPage.pageRating); + System.out.println(currentPage.pageName + " ------with ratings= "+currentPage.pageRating+" earned "+badgeNumber); + + currentPageNode=graph.getVertices("pid",currentPage.pid).iterator().next(); + currentPageNode.setProperty("badgeNumber",badgeNumber); + graph.commit(); + noOfPagesCounter++; + if(noOfPagesCounter==noOfPages) + maxPageRating=currentPage.pageRating; } - + //Adding the max value to the Preferences for later access + PropertiesAccess.putParameter("maxRating", maxPageRating); + + graph.shutdown(); } /** * This method will calculate the cutoff for the various badges * @param pageList The Arraylist containg Page Objects */ public static void calculateBadgeCutoff(ArrayList pageList){ int noOfPages=pageList.size(); int platinumPageIndex; int goldPageIndex; int silverPageIndex; int bronzePageIndex; int stonePageIndex; //Storing index where the cutoff of badges start to get the respective cutoffs platinumPageIndex=(int)(noOfPages*(Badges.PLATINUM_BADGE_START_PERCENTILE.value/100.00)); goldPageIndex=(int)(noOfPages*(Badges.GOLD_BADGE_START_PERCENTILE.value/100.00)); silverPageIndex=(int)(noOfPages*(Badges.SILVER_BADGE_START_PERCENTILE.value/100.00)); bronzePageIndex=(int)(noOfPages*(Badges.BRONZE_BADGE_START_PERCENTILE.value/100.00)); stonePageIndex=(int)(noOfPages*(Badges.STONE_BADGE_START_PERCENTILE.value/100.00)); //Storing cutoffs platinumBadgeRatingCutoff=pageList.get(platinumPageIndex).pageRating; goldBadgeRatingCutoff=pageList.get(goldPageIndex).pageRating; silverBadgeRatingCutoff=pageList.get(silverPageIndex).pageRating; bronzeBadgeRatingCutoff=pageList.get(bronzePageIndex).pageRating; stoneBadgeRatingCutoff=pageList.get(stonePageIndex).pageRating; System.out.println("Index "+platinumPageIndex+"marks platinum cutoff -------"+platinumBadgeRatingCutoff); System.out.println("Index "+goldPageIndex+"marks gold cutoff------"+goldBadgeRatingCutoff); System.out.println("Index "+silverPageIndex+"marks silver cutoff------"+silverBadgeRatingCutoff); System.out.println("Index "+bronzePageIndex+"marks bronze cutoff------"+bronzeBadgeRatingCutoff); System.out.println("Index "+stonePageIndex+"marks stone cutoff------"+stoneBadgeRatingCutoff); } /** * This method will pick the badge according to the passed pageRating * @param pageRating PageRating of the page under consideration * @return The name of the Badge earned */ - public static String getBadgeName(double pageRating){ + public static int getBadgeNumber(double pageRating){ if(pageRating>=platinumBadgeRatingCutoff) - return "PLATINUM"; + return Badges.PLATINUM.value; else if(pageRating>=goldBadgeRatingCutoff) - return "GOLD"; + return Badges.GOLD.value; else if(pageRating>=silverBadgeRatingCutoff) - return "SILVER"; + return Badges.SILVER.value; else if(pageRating>=bronzeBadgeRatingCutoff) - return "BRONZE"; + return Badges.BRONZE.value; else - return "STONE"; + return Badges.STONE.value; } } diff --git a/WikiRating/src/main/java/controllers/PageInfo.java b/WikiRating/src/main/java/controllers/PageInfo.java index 4d5f92c..b0fb3d6 100644 --- a/WikiRating/src/main/java/controllers/PageInfo.java +++ b/WikiRating/src/main/java/controllers/PageInfo.java @@ -1,51 +1,55 @@ 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 javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; import org.glassfish.jersey.server.JSONP; +import com.tinkerpop.blueprints.Vertex; +import com.tinkerpop.blueprints.impls.orient.OrientGraph; -@Path("employee") +import main.java.utilities.Connections; +import main.java.utilities.PropertiesAccess; + +@Path("display") public class PageInfo { @GET - @Path("search1") - @Produces("application/json") - - public Response getEmployeeDetails(@QueryParam("param1") int employeeId,@QueryParam("param2") String employeeName) - { - System.out.println(employeeName+" is a good person having an ID of "+employeeId); - - String m=employeeName+" is a good person having an ID of "+employeeId; - - - return Response.status(200).entity(m).build(); - - } - - @GET - @Path("search") - @JSONP(queryParam="callback") - @Produces({"application/x-javascript"}) - public String getAllTestData(@QueryParam("callback") String callback,@QueryParam("param1") int PageId,@QueryParam("param2") String PageName) { - - - System.out.println("I got executed!"); - System.out.println(callback); - System.out.println(PageId); - System.out.println(PageName); - //String sJson="{\"data\":{\"id\":1}}"; - //String sJson="{\"id\":1}"; - String sJson="{\"PageName\":78,\"Ratings\":1.0}"; - String result=callback+"("+sJson+");"; - //return Response.status(200).entity("{ foo: 'bar' }").build(); - return result; - //return Response.ok("mycallback({status: 'success'})").header("Access-Control-Allow-Origin", true).build(); - } - + @Path("pageRating") + @JSONP(queryParam = "callback") + @Produces({ "application/x-javascript" }) + 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; + + } + } diff --git a/WikiRating/src/main/java/controllers/Secondrun.java b/WikiRating/src/main/java/controllers/Secondrun.java index e12a192..72ae515 100644 --- a/WikiRating/src/main/java/controllers/Secondrun.java +++ b/WikiRating/src/main/java/controllers/Secondrun.java @@ -1,78 +1,79 @@ package main.java.controllers; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.Response; import main.java.computations.BadgeGenerator; import main.java.computations.NormalisedVotes; import main.java.computations.PageRating; import main.java.computations.Pagerank; import main.java.computations.Reliability; import main.java.computations.UserCredibility; import main.java.models.AddNewPages; import main.java.models.User; +import main.java.utilities.PropertiesAccess; /** * This class will be used for detecting and storing any new changes like * Page addition, Page modification and User Addition to the platform. */ @Path("secondRun") public class Secondrun { @GET @Produces("application/json") /** * This method will call suitable methods to insert all the new users and * update the database for any new changes * @return Response object showing time taken to run the computation */ public Response pCompute() { long startTime = System.currentTimeMillis(); /*Now we will check for new pages and add revisions to them too. *Make links to the user contributions too *Calculate the user votes and then calculate the recursive votes too. *We will calculate the backlinks too *Drop backlinks and then create the new ones again *Calculate the votes *Calculate the user reliability */ - - User.insertAllUsers(); + //PropertiesAccess.putParameter("maxPageRainting", 20.0); + /*User.insertAllUsers(); System.out.println("==================Checked for new User's insertion====================="); AddNewPages.checkForPages(); System.out.println("==================Checked for any new pages,revisions and linked the user contributions and made backlinks====================="); NormalisedVotes.calculatePageVotes(); System.out.println("==================Calculated new page votes====================="); Reliability.calculateReliability(); System.out.println("==================Calculated new reliabilities====================="); Pagerank.pageRankCompute(); System.out.println("==================Page rank over====================="); UserCredibility.getUserCredibility(); System.out.println("==================User Credibility computed====================="); PageRating.computePageRatings(); System.out.println("==================Page Ratings computed====================="); new BadgeGenerator().generateBadges(); - System.out.println("==================Badges given====================="); + System.out.println("==================Badges given=====================");*/ long estimatedTime = System.currentTimeMillis() - startTime; estimatedTime = estimatedTime / 60000; return Response.status(200).entity("Successful and took" + estimatedTime + "Minutes").build(); } } diff --git a/WikiRating/src/main/java/models/AddNewPages.java b/WikiRating/src/main/java/models/AddNewPages.java index 05ab716..f5abe72 100644 --- a/WikiRating/src/main/java/models/AddNewPages.java +++ b/WikiRating/src/main/java/models/AddNewPages.java @@ -1,286 +1,287 @@ package main.java.models; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; 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.controllers.WikiUtil; import main.java.utilities.AllowedNamespaces; import main.java.utilities.Connections; import main.java.utilities.PropertiesAccess; /** * This class will check for new pages, revisions made on previous pages and add them. * Further it will link them to the corresponding User contributions. * It will also link a page to other pages that has a back link towards it. */ public class AddNewPages { /** * This method will check for all the changes and then call suitable methods to handle them. */ public static void checkForPages(){ OrientGraph graph = Connections.getInstance().getDbGraph(); String allPages=""; try { //Now we will be iterating over all the namespaces to get all the pages in each og them. for(AllowedNamespaces namespace:AllowedNamespaces.values()){ //JSON interpretation try { //Getting the JSON formatted String to process. allPages =Page.getAllPages(namespace.getValue()); JSONObject js=new JSONObject(allPages); JSONObject js2=js.getJSONObject("query"); JSONArray arr=js2.getJSONArray("allpages"); JSONObject currentJsonObject; //Storing all the pages in a particular namespace for(int i=0;i