diff --git a/WikiRating/src/main/java/computations/CreditSystem.java b/WikiRating/src/main/java/computations/CreditSystem.java index 701dbb6..7af4999 100644 --- a/WikiRating/src/main/java/computations/CreditSystem.java +++ b/WikiRating/src/main/java/computations/CreditSystem.java @@ -1,153 +1,201 @@ package main.java.computations; - -import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; - import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; - -import org.codehaus.jettison.json.JSONArray; -import org.codehaus.jettison.json.JSONException; +import org.json.JSONArray; import org.json.JSONObject; +import com.orientechnologies.orient.core.Orient; import com.tinkerpop.blueprints.Direction; import com.tinkerpop.blueprints.Edge; import com.tinkerpop.blueprints.Vertex; import com.tinkerpop.blueprints.impls.orient.OrientGraph; -import com.tinkerpop.blueprints.impls.orient.OrientVertex; - import main.java.utilities.Connections; + +/** + * This class is used to return User Contributions for the passed pages collectively or individually. + * Only major contributions done by registered users are considered. + */ @Path("credit") @Produces("application/json") public class CreditSystem { @GET @Path("users") @Produces("application/json") - public static String generateCredits(@QueryParam("name") List pageList,@QueryParam("join") boolean join){ + + /** + * This is the chief method that will return a JSON string having all the contributions + * @param pageList List having all the Page Titles + * @param join If set true collective user contributions for all the pages will be returned + * @return JSON string having all the contributions and current version sizes. + */ + public static String generateCredits(@QueryParam("titles") List pageList,@QueryParam("join") boolean join){ + OrientGraph graph = Connections.getInstance().getDbGraph(); + if(!join){ + //This part will be executed when individual contributions are asked for Vertex pageNode=null; + + JSONArray responseJson=new JSONArray(); + for(int i=0;i userContributions=getUserContributions(graph,pageNode); + JSONArray authorList=new JSONArray(); + JSONObject pageNodeObject=new JSONObject(); + + Iterator it = userContributions.entrySet().iterator(); + while (it.hasNext()) { + Map.Entry pair = (Map.Entry) it.next(); + JSONObject authorNode=new JSONObject(); + authorNode.put("username", (String)pair.getKey()); + authorNode.put("bytes_changed", (int)pair.getValue()); + authorList.put(authorNode); + + } + + + pageNodeObject.put("title",pageList.get(i) ); + pageNodeObject.put("authors", authorList); + pageNodeObject.put("bytes", pageNode.getEdges(Direction.OUT, "@class", "PreviousVersionOfPage").iterator().next().getVertex(Direction.IN).getProperty("size")); + + responseJson.put(pageNodeObject); } - + JSONObject creditObject=new JSONObject(); + creditObject.put("credits", responseJson); graph.shutdown(); - return "Success"; + return creditObject.toString(); } else{ + //This part will be executed when collective contributions are asked for Vertex pageNode=null; - int contributionSize=0; + int contributionSize=0;int totalBytes=0; String username=""; HashMap totalUserContributions=new HashMap(); for(int i=0;i userContributions=getUserContributions(graph,pageNode); Iterator it = userContributions.entrySet().iterator(); while (it.hasNext()) { Map.Entry pair = (Map.Entry) it.next(); username=(String)pair.getKey(); contributionSize=(int)pair.getValue(); if(totalUserContributions.containsKey(username)){ totalUserContributions.put(username, totalUserContributions.get(username)+contributionSize); } else{ totalUserContributions.put(username, contributionSize); } } + totalBytes+=(int)pageNode.getEdges(Direction.OUT, "@class", "PreviousVersionOfPage").iterator().next().getVertex(Direction.IN).getProperty("size"); } - System.out.println("===Printing bulk contributions==="); + JSONArray authorList=new JSONArray(); Iterator it = totalUserContributions.entrySet().iterator(); + while (it.hasNext()) { + Map.Entry pair = (Map.Entry) it.next(); - System.out.println(pair.getKey()+" "+pair.getValue()); + JSONObject authorNode=new JSONObject(); + authorNode.put("username", (String)pair.getKey()); + authorNode.put("bytes_changed", (int)pair.getValue()); + authorList.put(authorNode); + } - + JSONObject authorListObject=new JSONObject(); + authorListObject.put("totalBytes", totalBytes); + authorListObject.put("authors", authorList); + graph.shutdown(); - return "Success"; + + return authorListObject.toString(); } - + + } /** - * @param graph - * @param userContributions - * @param pageNode + * This method returns a HashMap filled with the user contributions of the passed pageNode + * @param graph OrientGraph + * @param pageNode The Page Node of whose contribution you want + * @return HashMap having username as key and contributions as corresponding value */ public static HashMap getUserContributions(OrientGraph graph,Vertex pageNode){ Vertex revisionNode=null,userNode=null; revisionNode=pageNode.getEdges(Direction.OUT, "@class", "PreviousVersionOfPage").iterator().next().getVertex(Direction.IN); Edge contribute=null; int contributionSize=0; String username=""; HashMap userContributions=new HashMap(); try{ while(true){ - //System.out.println("Hello"); + if(revisionNode.getEdges(Direction.IN, "@class", "Contribute").iterator().hasNext()){ - //System.out.println("insider"); + contribute=revisionNode.getEdges(Direction.IN, "@class", "Contribute").iterator().next(); userNode=contribute.getVertex(Direction.OUT); //Get the required bytes and the corresponding username contributionSize=contribute.getProperty("contributionSize"); - username=userNode.getProperty("username"); //Putting the data into the HashMap + username=userNode.getProperty("username"); + //Putting the data into the HashMap if(userContributions.containsKey(username)){ userContributions.put(username, userContributions.get(username)+contributionSize); } else{ userContributions.put(username, contributionSize); } } if((int)revisionNode.getProperty("parentid")==0) break; revisionNode=graph.getVertices("revid", (int)revisionNode.getProperty("parentid")).iterator().next(); } }catch(Exception e){ e.printStackTrace(); } System.out.println(pageNode.getProperty("title")); Iterator it = userContributions.entrySet().iterator(); while (it.hasNext()) { Map.Entry pair = (Map.Entry) it.next(); System.out.println(pair.getKey()+" "+pair.getValue()); } return userContributions; } } diff --git a/WikiRating/src/main/java/models/Revision.java b/WikiRating/src/main/java/models/Revision.java index 8ad5169..b99287c 100644 --- a/WikiRating/src/main/java/models/Revision.java +++ b/WikiRating/src/main/java/models/Revision.java @@ -1,129 +1,129 @@ package main.java.models; import java.io.InputStream; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import org.wikidata.wdtk.wikibaseapi.ApiConnection; 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.Connections; /** * This class will link the revisions to the corresponding pages */ public class Revision { - //This + /** * This method will compute the revisions for all the pages and link them * @param key Name of the key here '@class' * @param value Value of the key here 'Revision' */ public static void getAllRevisions(String key,String value){ String result=""; OrientGraph graph=Connections.getInstance().getDbGraph(); System.out.println("=====Checkpoint for Revisions=========="); for (Vertex pageNode : graph.getVertices(key,value)) { //Fetching the revision string for a particular page. result=getRevision(pageNode.getProperty("pid").toString()); try { //JSON interpretation try { JSONObject js=new JSONObject(result); JSONObject js2=js.getJSONObject("query").getJSONObject("pages").getJSONObject(pageNode.getProperty("pid").toString()); JSONArray arr=js2.getJSONArray("revisions"); JSONObject currentJsonObject; for(int i=0;i