List of usage examples for com.google.gson JsonObject getAsJsonArray
public JsonArray getAsJsonArray(String memberName)
From source file:csh.vctt.datautils.DataManager.java
License:Apache License
/** * Parses CertTree.json file into Tiers. * @param certTreeFile The contents of the cert tree file. * @return ArrayList containing all of the tiers in the tree. *//* www.j a v a 2 s .c om*/ public static ArrayList<Tier> getTreeFromFile(String certTreeFile) { ArrayList<Tier> tiers = new ArrayList<Tier>(); JsonElement root = new JsonParser().parse(certTreeFile); JsonObject topObj = root.getAsJsonObject(); JsonArray topArr = topObj.getAsJsonArray(M_JSONMEM_TREE_TOP); JsonElement tierTop = topArr.get(0); JsonObject tierTopObj = tierTop.getAsJsonObject(); int tierCt = 1; while (tierTopObj.has("tier" + tierCt)) { String tierName = "tier" + tierCt; Tier curTier = new Tier(tierName); tiers.add(curTier); JsonArray curTierArr = tierTopObj.getAsJsonArray(tierName); JsonElement classTop = curTierArr.get(0); JsonObject classTopObject = classTop.getAsJsonObject(); JsonArray classArr = classTopObject.getAsJsonArray(M_JSONMEM_TREE_TOP_CLASS); for (JsonElement curClassElem : classArr) { JsonObject curClassTopObj = curClassElem.getAsJsonObject(); String className = getMemberAsStr(curClassTopObj, M_JSONMEM_TREE_CLASS_NAME); ClassType curClass = new ClassType(className); curTier.addClass(curClass); JsonArray curClassArr = curClassTopObj.getAsJsonArray(className); for (JsonElement curReqElem : curClassArr) { JsonObject curReqObj = curReqElem.getAsJsonObject(); String reqId = getMemberAsStr(curReqObj, M_JSONMEM_TREE_REQ_ID); String reqName = getMemberAsStr(curReqObj, M_JSONMEM_TREE_REQ_NAME); String reqCost = getMemberAsStr(curReqObj, M_JSONMEM_TREE_REQ_COST); String reqReqd = getMemberAsStr(curReqObj, M_JSONMEM_TREE_REQ_REQD); String reqIsItem = getMemberAsStr(curReqObj, M_JSONMEM_TREE_REQ_ISITEM); String reqLineId = getMemberAsStr(curReqObj, M_JSONMEM_TREE_REQ_LINEID); Requirement curReq = new Requirement(reqId, reqName, reqCost, reqReqd, reqIsItem, reqLineId); curClass.addReq(curReq); } } tierCt++; } return tiers; }
From source file:csh.vctt.datautils.DataManager.java
License:Apache License
/** * Parses JSON into Players./*from w w w . j a v a2 s .c om*/ * @param outfitMembersJson The JSON result of the outfit member query to the API. * @return ArrayList containing Player objects for all outfit members. */ public static ArrayList<Player> getOutfitMembers(String outfitMembersJson) { ArrayList<Player> outfitMembers = new ArrayList<Player>(); JsonElement root = new JsonParser().parse(outfitMembersJson); JsonObject obj = root.getAsJsonObject(); JsonArray outfitMemArr = obj.getAsJsonArray(M_JSONMEM_OUTF_MEMLIST); for (JsonElement curCharE : outfitMemArr) { JsonObject curChar = curCharE.getAsJsonObject(); if (curChar.has(M_JSONMEM_OUTF_TOP_CHAR)) { JsonObject charObj = curChar.getAsJsonObject(M_JSONMEM_OUTF_TOP_CHAR); JsonObject charNameObj = charObj.getAsJsonObject(M_JSONMEM_OUTF_TOP_CHAR_NAME); String id = getMemberAsStr(curChar, M_JSONMEM_OUTF_CHAR_ID); String name = getMemberAsStr(charNameObj, M_JSONMEM_OUTF_CHAR_NAME); outfitMembers.add(new Player(id, name)); } } Collections.sort(outfitMembers); return outfitMembers; }
From source file:cz.pchmelar.ddw.homework1.MainServlet.java
/** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> * methods.// w w w. ja va 2s . c o m * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); try (PrintWriter out = response.getWriter()) { /* TODO output your page here. You may use following sample code. */ out.println("<!DOCTYPE html>"); out.println("<html>"); out.println("<head>"); out.println("<title>Simpsons Episode Analyzer</title>"); out.println( "<link rel=\"stylesheet\" href=\"http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css\">"); out.println( "<script src=\"https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js\"></script>"); out.println( "<script src=\"http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js\"></script>"); out.println("</head>"); out.println("<body style=\"background-color: #A0D1F5\">"); out.println("<div class=\"container\">"); out.println("<br>"); out.println("<br>"); out.println( "<a href=\"/ddw-homework1/MainServlet\"><img src=\"http://www.returndates.com/backgrounds/simpsons.logo.png\" class=\"img-responsive center-block\" style=\"width: 50%; height: 50%\"></a>"); out.println("<br>"); out.println("<br>"); //GET JSON of all simpsons episodes String urlString = "http://api.tvmaze.com/singlesearch/shows?q=simpsons&embed=episodes"; URL url = new URL(urlString); URLConnection conn = url.openConnection(); InputStream is = conn.getInputStream(); String JSON = IOUtils.toString(is, "UTF-8"); //print season/episode/name for every episode in JSON JsonElement jelement = new JsonParser().parse(JSON); JsonObject jobject = jelement.getAsJsonObject(); jobject = jobject.getAsJsonObject("_embedded"); JsonArray jarray = jobject.getAsJsonArray("episodes"); out.println("<table class=\"table table-bordered table-hover\" style=\"background-color: #FFFFFF\">"); out.println("<thead><tr><th>Season</th><th>Episode</th><th>Name</th></tr></thead><tbody>"); for (int i = 0; i < jarray.size(); i++) { jobject = jarray.get(i).getAsJsonObject(); String season = jobject.get("season").toString(); if (season.length() == 1) season = "0" + season; String episode = jobject.get("number").toString(); if (episode.length() == 1) episode = "0" + episode; out.println("<tr>"); out.println("<td>" + season + "</td>"); out.println("<td>" + episode + "</td>"); out.println("<td><a href=\"/ddw-homework1/DetailServlet?season=" + season + "&episode=" + episode + "\">" + jobject.get("name").toString() + "</a></td>"); out.println("</tr>"); } out.println("</tbody></table>"); out.println("</container>"); out.println("</body>"); out.println("</html>"); } }
From source file:dao.ChatDAO.java
public HashMap<Integer, Chat> getChatList(int staffId, String token) throws UnsupportedEncodingException, IOException, ParseException { HashMap<Integer, Chat> chatList = new HashMap<Integer, Chat>(); //Add URL here String url = "http://119.81.43.85/chat/retrive_chat_list_history"; HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost(url); // add header post.setHeader("User-Agent", USER_AGENT); //Add parameters here List<NameValuePair> urlParameters = new ArrayList<NameValuePair>(); urlParameters.add(new BasicNameValuePair("staff_id", staffId + "")); urlParameters.add(new BasicNameValuePair("token", token)); post.setEntity(new UrlEncodedFormEntity(urlParameters)); HttpResponse response = client.execute(post); BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); StringBuffer result = new StringBuffer(); String line = ""; while ((line = rd.readLine()) != null) { result.append(line);/*from w w w . j a v a 2s . c o m*/ } String str = result.toString(); JsonParser jsonParser = new JsonParser(); JsonElement element = jsonParser.parse(str); JsonObject jobj = element.getAsJsonObject(); JsonObject jobj2 = jobj.getAsJsonObject("payload"); JsonObject jobj3 = jobj2.getAsJsonObject("chat_history_list_result"); JsonArray arr = jobj3.getAsJsonArray("generalList"); int arrSize = arr.size(); for (int i = 0; i < arrSize; i++) { // for (int i = 0; i < arr.size(); i++) { JsonElement qrElement = arr.get(i); JsonObject qrObj = qrElement.getAsJsonObject(); JsonElement attElement = qrObj.get("id"); int id = 0; if (!attElement.isJsonNull()) { id = attElement.getAsInt(); } attElement = qrObj.get("service_id"); int service_id = 0; if (!attElement.isJsonNull()) { service_id = attElement.getAsInt(); } attElement = qrObj.get("user_id"); int user_id = 0; if (!attElement.isJsonNull()) { user_id = attElement.getAsInt(); } attElement = qrObj.get("shop_id"); int shop_id = 0; if (!attElement.isJsonNull()) { shop_id = attElement.getAsInt(); } attElement = qrObj.get("last_message"); String last_message = ""; if (!attElement.isJsonNull()) { last_message = attElement.getAsString(); } attElement = qrObj.get("modified"); Timestamp modified = null; String dateTimeString = "1990-01-01 00:00:00"; SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); Date parsedDate = dateFormat.parse(dateTimeString); modified = new java.sql.Timestamp(parsedDate.getTime()); if (attElement != null && !attElement.isJsonNull()) { dateTimeString = attElement.getAsString(); dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); parsedDate = dateFormat.parse(dateTimeString); modified = new java.sql.Timestamp(parsedDate.getTime()); } attElement = qrObj.get("created"); Timestamp created = null; dateTimeString = "1990-01-01 00:00:00"; dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); parsedDate = dateFormat.parse(dateTimeString); created = new java.sql.Timestamp(parsedDate.getTime()); if (attElement != null && !attElement.isJsonNull()) { dateTimeString = attElement.getAsString(); dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); parsedDate = dateFormat.parse(dateTimeString); created = new java.sql.Timestamp(parsedDate.getTime()); } attElement = qrObj.get("deleted_at"); Timestamp deleted_at = null; dateTimeString = "1990-01-01 00:00:00"; dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); parsedDate = dateFormat.parse(dateTimeString); deleted_at = new java.sql.Timestamp(parsedDate.getTime()); if (attElement != null && !attElement.isJsonNull()) { dateTimeString = attElement.getAsString(); dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); parsedDate = dateFormat.parse(dateTimeString); deleted_at = new java.sql.Timestamp(parsedDate.getTime()); } attElement = qrObj.get("user_name"); String user_name = ""; if (!attElement.isJsonNull()) { user_name = attElement.getAsString(); } Chat chat = new Chat(id, service_id, user_id, shop_id, last_message, modified, created, deleted_at, user_name); chatList.put(i, chat); } return chatList; }
From source file:dao.WorkshopDAO.java
public ArrayList<String> retrieveAllCarBrands(int staffId, String token) throws UnsupportedEncodingException, IOException { ArrayList<String> carBrands = new ArrayList<String>(); String url = "http://119.81.43.85/erp/avalible_car/get_all_avalible_car"; HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost(url); // add header post.setHeader("User-Agent", USER_AGENT); List<NameValuePair> urlParameters = new ArrayList<NameValuePair>(); urlParameters.add(new BasicNameValuePair("staff_id", staffId + "")); urlParameters.add(new BasicNameValuePair("token", token)); post.setEntity(new UrlEncodedFormEntity(urlParameters)); HttpResponse response = client.execute(post); BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); StringBuffer result = new StringBuffer(); String line = ""; while ((line = rd.readLine()) != null) { result.append(line);/*from w w w .java2 s . co m*/ } String str = result.toString(); JsonParser jsonParser = new JsonParser(); JsonElement element = jsonParser.parse(str); JsonObject jobj = element.getAsJsonObject(); JsonElement brandsElement = jobj.get("payload"); JsonObject brands = null; if (brandsElement.isJsonNull()) { return carBrands; } else { brands = brandsElement.getAsJsonObject(); JsonArray brandsArr = brands.getAsJsonArray("available_cars"); for (int i = 0; i < brandsArr.size(); i++) { JsonElement brandArr = brandsArr.get(i); JsonObject brandObj = brandArr.getAsJsonObject(); JsonElement attElement = brandObj.get("car_brand"); String brand = ""; if (!attElement.isJsonNull()) { brand = attElement.getAsString(); if (!carBrands.contains(brand)) { carBrands.add(brand); } } } } return carBrands; }
From source file:datalayer.DBFacade.java
@Override public String flightReservation(String JSONReservationPayload, String flightId) { em.getTransaction().begin();//ww w. j a va 2 s . c om JsonObject jo = new JsonParser().parse(JSONReservationPayload).getAsJsonObject(); JsonArray passengers = jo.getAsJsonArray("Passengers"); List<Customer> cList = new ArrayList<>(); for (JsonElement passenger : passengers) { JsonObject tempJo = passenger.getAsJsonObject(); Customer tempCustomer = new Customer(); tempCustomer.setfName(tempJo.get("firstName").getAsString()); tempCustomer.setlName(tempJo.get("lastName").getAsString()); tempCustomer.setCity(tempJo.get("city").getAsString()); tempCustomer.setCountry(tempJo.get("country").getAsString()); tempCustomer.setStreet(tempJo.get("street").getAsString()); cList.add(tempCustomer); em.persist(tempCustomer); } FlightInstance fInstance = em.find(FlightInstance.class, flightId); Reservation reservation = fInstance.addReservation(cList.get(0), cList); em.persist(reservation);//should work without this? em.merge(fInstance); em.getTransaction().commit(); //Setting up the reservationobject to return as JSON JsonObject reservationJo = new JsonObject(); reservationJo.addProperty("reservationID", reservation.getId()); reservationJo.addProperty("flightID", fInstance.getId());//FlightID or flightinstanceID? reservationJo.add("Passengers", passengers); reservationJo.addProperty("totalPrice", fInstance.getPrice() * passengers.size()); return reservationJo.toString(); }
From source file:de.behrfried.wikianalyzer.wawebapp.server.service.JsonWikiAccess.java
License:Apache License
@Override public ArticleInfo getArticleInfo(String title) throws ArticleNotExistException { try {// w w w. ja v a 2 s. co m final int pageid = this.getPageId(title); if (pageid == -1) { /* article does not exist */ throw new ArticleNotExistException("Artikel \"" + title + "\" existiert nicht!"); } final List<ArticleInfo.AuthorAndCommits> authorsAndCommits = new ArrayList<ArticleInfo.AuthorAndCommits>(); final List<ArticleInfo.Revision> revisions = new ArrayList<ArticleInfo.Revision>(); final List<ArticleInfo.RevsPerDate> revsPerDates = new ArrayList<ArticleInfo.RevsPerDate>(); int lastRev = 0; Date creationDate = null; String initialAuthor = null; /* get revisions of an article (max 500 are allowed) */ // http://de.wikipedia.org/w/api.php?action=query&format=xml&prop=revisions&pageids=88112&rvprop=user|ids|timestamp|sha1&rvlimit=10000&rvdiffto=next&rvdir=older final Map<String, Integer> authorsAndCommitsTmp = new HashMap<String, Integer>(); final Map<Long, Integer> revsPerDatesTmp = new HashMap<Long, Integer>(); while (lastRev != -1) { final String response1 = this.requester .getResult(this.convertRequest("action=query&format=json&prop=revisions&rvprop=user|ids" + "|timestamp|comment|size&rvlimit=500&rvdir=newer&rvexcludeuser=127.0.0.1&pageids=" + pageid + "&rvstartid=" + lastRev + "&continue=")); final JsonObject root = this.parser.parse(response1).getAsJsonObject(); final JsonObject page = root.getAsJsonObject("query").getAsJsonObject("pages") .getAsJsonObject(pageid + ""); if (!page.has("revisions")) { break; } final JsonArray w = page.getAsJsonArray("revisions"); /* * iterate the revisions */ for (JsonElement obj : w) { final JsonObject jsonObj = obj.getAsJsonObject(); final String author = jsonObj.getAsJsonPrimitive("user").getAsString(); if (!authorsAndCommitsTmp.containsKey(author)) { authorsAndCommitsTmp.put(author, 1); } else { authorsAndCommitsTmp.put(author, authorsAndCommitsTmp.get(author) + 1); } try { revisions.add(new ArticleInfo.Revision(jsonObj.getAsJsonPrimitive("revid").getAsInt(), jsonObj.getAsJsonPrimitive("parentid").getAsInt(), this.formatter.parse( jsonObj.getAsJsonPrimitive("timestamp").getAsString().replace('T', '_')), author, jsonObj.getAsJsonPrimitive("comment").getAsString(), jsonObj.getAsJsonPrimitive("size").getAsInt(), 0, false)); } catch (ParseException e) { this.logger.error(e.getMessage(), e); } } if (lastRev == 0) { try { creationDate = this.formatter.parse(w.get(0).getAsJsonObject() .getAsJsonPrimitive("timestamp").getAsString().replace('T', '_')); } catch (ParseException e) { this.logger.error(e.getMessage(), e); } initialAuthor = w.get(0).getAsJsonObject().getAsJsonPrimitive("user").getAsString(); } // lastRev = w.get(w.size() - // 1).getAsJsonObject().getAsJsonPrimitive("revid").getAsInt() + // 1; lastRev = root.has("continue") ? root.getAsJsonObject("continue").getAsJsonPrimitive("rvcontinue").getAsInt() : -1; } for (Map.Entry<String, Integer> entry : authorsAndCommitsTmp.entrySet()) { authorsAndCommits.add(new ArticleInfo.AuthorAndCommits(entry.getKey(), entry.getValue())); } Collections.sort(authorsAndCommits, new Comparator<ArticleInfo.AuthorAndCommits>() { @Override public int compare(ArticleInfo.AuthorAndCommits authorAndCommits, ArticleInfo.AuthorAndCommits authorAndCommits2) { return authorAndCommits2.getNumOfCommits() - authorAndCommits.getNumOfCommits(); } }); /* set diffs in revisions and look for edit wars */ for (int i = 1; i < revisions.size(); i++) { revisions.get(i).setDiff(revisions.get(i).getBytes() - revisions.get(i - 1).getBytes()); } Date currentDate = new Date(creationDate.getYear(), creationDate.getMonth(), creationDate.getDate()); while (currentDate.before(revisions.get(revisions.size() - 1).getTimestamp())) { revsPerDatesTmp.put(currentDate.getTime(), 0); currentDate = new Date(currentDate.getTime() + 86400000); } for (final ArticleInfo.Revision revision : revisions) { final Date key = new Date(revision.getTimestamp().getYear(), revision.getTimestamp().getMonth(), revision.getTimestamp().getDate()); long time = key.getTime(); try { if (!revsPerDatesTmp.containsKey(time)) { time -= 3600000; } if (!revsPerDatesTmp.containsKey(time)) { time += 7200000; } revsPerDatesTmp.put(time, revsPerDatesTmp.get(time) + 1); } catch (Exception e) { this.logger.error(e.getMessage()); } } for (Map.Entry<Long, Integer> entry : revsPerDatesTmp.entrySet()) { revsPerDates.add(new ArticleInfo.RevsPerDate(new Date(entry.getKey()), entry.getValue())); } Collections.sort(revsPerDates, new Comparator<ArticleInfo.RevsPerDate>() { @Override public int compare(ArticleInfo.RevsPerDate revsPerDate, ArticleInfo.RevsPerDate revsPerDate2) { return revsPerDate.getDate().compareTo(revsPerDate2.getDate()); } }); /* * find edit wars */ final List<ArticleInfo.EditWar> editWars = new ArrayList<ArticleInfo.EditWar>(); final List<ArticleInfo.Revision> revertedRevs = this.getRevertedRevisions(revisions); for (int i = 0; i < revertedRevs.size() - 4; i++) { final ArticleInfo.Revision revision = revertedRevs.get(i); int startI = i; while (i < revertedRevs.size() - 4 && revertedRevs.get(i + 4).getTimestamp().getTime() - revertedRevs.get(i).getTimestamp().getTime() < 100000000) { i += 4; } if (i != startI) { final StringBuilder usersStrBldr = new StringBuilder(); final Map<String, Integer> usersMap = new HashMap<String, Integer>(); for (int j = startI; j <= i; j++) { final String author = revertedRevs.get(j).getAuthor(); if (!usersMap.containsKey(author)) { usersMap.put(author, 0); } usersMap.put(author, usersMap.get(author) + 1); } for (final Map.Entry<String, Integer> entry : usersMap.entrySet()) { usersStrBldr.append(entry.getKey()); usersStrBldr.append(" ("); usersStrBldr.append(entry.getValue()); usersStrBldr.append("); "); } editWars.add(new ArticleInfo.EditWar(revertedRevs.get(startI).getTimestamp(), revertedRevs.get(i).getTimestamp(), usersStrBldr.toString().substring(0, usersStrBldr.length() - 2))); } } /* get similiar articles */ // http://de.wikipedia.org/w/api.php?action=query&format=xml&list=search&srsearch=Maus&srlimit=500 final String similar = this.requester.getResult( this.convertRequest("action=query&format=json&list=search&srlimit=500&srsearch=" + title)); final JsonArray search = this.parser.parse(similar).getAsJsonObject().getAsJsonObject("query") .getAsJsonArray("search"); final List<ArticleInfo.SimilarArticle> similarArticles = new ArrayList<ArticleInfo.SimilarArticle>( search.size()); for (final JsonElement obj : search) { final JsonObject jsonObj = obj.getAsJsonObject(); final String simTitle = jsonObj.getAsJsonPrimitive("title").getAsString(); final int simPageid = this.getPageId(simTitle); if (simPageid == pageid) { continue; } /* get categories */ final String categories = this.getCategories(simPageid); /* get creation date */ Date simCreationDate = null; final String creationDateStr = this.requester.getResult(this.convertRequest( "action=query&format=json&prop=revisions&rvprop=timestamp&rvlimit=1&rvdir=newer&pageids=" + simPageid)); try { simCreationDate = this.formatter.parse(this.parser.parse(creationDateStr).getAsJsonObject() .getAsJsonObject("query").getAsJsonObject("pages").getAsJsonObject(simPageid + "") .getAsJsonArray("revisions").get(0).getAsJsonObject().getAsJsonPrimitive("timestamp") .getAsString().replace('T', '_')); } catch (ParseException e) { this.logger.error(e.getMessage(), e); } similarArticles.add(new ArticleInfo.SimilarArticle(simTitle, categories, simCreationDate)); } /* get number of images */ final String imageStr = this.requester .getResult(this.convertRequest("action=query&format=json&prop=images&pageids=" + pageid)); final JsonObject images = this.parser.parse(imageStr).getAsJsonObject().getAsJsonObject("query") .getAsJsonObject("pages").getAsJsonObject(pageid + ""); int numOfImages = 0; if (images.has("images")) { numOfImages = images.getAsJsonArray("images").size(); } /* * get categories */ final String categoriesStr = this.requester.getResult(this.convertRequest( "action=query&format=json&prop=categories&clprop=timestamp&cllimit=500&pageids=" + pageid)); final JsonObject categoriesJsonObj = this.parser.parse(categoriesStr).getAsJsonObject() .getAsJsonObject("query").getAsJsonObject("pages").getAsJsonObject(pageid + ""); final List<ArticleInfo.Category> categoryList = new ArrayList<ArticleInfo.Category>(); if (categoriesJsonObj.has("categories")) { final JsonArray categoriesJsonArr = categoriesJsonObj.getAsJsonArray("categories"); for (final JsonElement catElem : categoriesJsonArr) { final JsonObject catJson = catElem.getAsJsonObject(); try { categoryList.add(new ArticleInfo.Category( catJson.getAsJsonPrimitive("title").getAsString().replaceAll("Kategorie:", "") .replaceAll("Wikipedia:", ""), this.formatter.parse( catJson.getAsJsonPrimitive("timestamp").getAsString().replace('T', '_')))); } catch (ParseException e) { e.printStackTrace(); } } } return new ArticleInfo(pageid, title, initialAuthor, creationDate, "http://de.wikipedia.org/wiki/" + title.replaceAll(" ", "_"), "http://de.wikipedia.org/wiki/Benutzer:" + initialAuthor, numOfImages, this.getCategories(pageid), revisions.get(revisions.size() - 1).getBytes(), authorsAndCommits, revisions, revsPerDates, editWars, similarArticles, categoryList); } catch (Exception e) { this.logger.error(e.getMessage(), e); } return null; }
From source file:de.behrfried.wikianalyzer.wawebapp.server.service.JsonWikiAccess.java
License:Apache License
private String getCategories(int pageid) { /* get categories */ final String categories = this.requester .getResult(this.convertRequest("action=query&format=json&prop=categories&pageids=" + pageid)); final JsonObject jsonObj = this.parser.parse(categories).getAsJsonObject().getAsJsonObject("query") .getAsJsonObject("pages").getAsJsonObject(pageid + ""); if (!jsonObj.has("categories")) { return "\"nicht kategorisiert\""; }// w w w. j a v a2 s. c o m final JsonArray cats = jsonObj.getAsJsonArray("categories"); final StringBuilder stringBuilder = new StringBuilder(); this.logger.info("Pageid: " + pageid); for (JsonElement inner : cats) { stringBuilder.append(inner.getAsJsonObject().getAsJsonPrimitive("title").getAsString()); stringBuilder.append("; "); } return stringBuilder.toString().substring(0, stringBuilder.length() - 2).replaceAll("Kategorie:", "") .replaceAll("Wikipedia:", ""); }
From source file:de.behrfried.wikianalyzer.wawebapp.server.service.JsonWikiAccess.java
License:Apache License
@Override public UserInfo getUserInfo(String userName) throws UserNotExistException { try {/*from w ww .j a va 2s . c o m*/ final int userid = this.getUserID(userName); if (userid == -1) { /* article does not exist */ throw new UserNotExistException("Nutzer \"" + userid + "\" existiert nicht!"); } final List<UserInfo.ArticleEdited> categoryEdited = new ArrayList<UserInfo.ArticleEdited>(); boolean isBot = false; Date signInDate = null; int lastUserCommits = 0; final String response1 = this.requester .getResult(this.convertRequest("action=query&format=json&list=users&ususers=" + userName + "&usprop=editcount|registration|blockinfo|groups")); final JsonObject root = this.parser.parse(response1).getAsJsonObject(); final JsonObject user = root.getAsJsonObject("query").getAsJsonArray("users").get(0).getAsJsonObject(); if (user.has("groups")) { final JsonArray groupsJsonArr = user.getAsJsonArray("groups"); for (JsonElement jsonElement : groupsJsonArr) { if ("bot".equals(jsonElement.getAsJsonPrimitive().getAsString())) { isBot = true; break; } } } final int editcount = user.getAsJsonPrimitive("editcount").getAsInt(); final boolean blocked = user.has("blockid"); Map<Tuple2<Integer, String>, Tuple2<Integer, Integer>> comparableRevisions = new HashMap<Tuple2<Integer, String>, Tuple2<Integer, Integer>>(); final Map<String, Integer> commitsPerCategory = new HashMap<String, Integer>(); final List<ArticleEdited> articleEditeds = new ArrayList<UserInfo.ArticleEdited>(); int numOfComments = 0; int numOfReverts = 0; int numOfUserDiscussion = 0; int numofSelfDiscussion = 0; final String userArticles = this.requester .getResult(this.convertRequest("action=query&format=json&list=usercontribs&ucdir=older&ucuser=" + userName + "&uclimit=500&ucprop=ids|sizediff|title|flags|comment|timestamp")); final JsonObject rootObj = this.parser.parse(userArticles).getAsJsonObject(); final JsonArray articlesArticle = rootObj.getAsJsonObject("query").getAsJsonArray("usercontribs"); lastUserCommits += articlesArticle.size(); for (JsonElement elm : articlesArticle) { final JsonObject tmpObj = elm.getAsJsonObject(); final Tuple2<Integer, String> key = Tuple.create(tmpObj.getAsJsonPrimitive("pageid").getAsInt(), tmpObj.getAsJsonPrimitive("title").getAsString()); if (key.getItem2().startsWith("Benutzer Diskussion:")) { if (key.getItem2().endsWith(userName)) { numofSelfDiscussion++; } else { numOfUserDiscussion++; } } final int sizediff = tmpObj.has("sizediff") ? tmpObj.getAsJsonPrimitive("sizediff").getAsInt() : 0; final String comment = tmpObj.has("comment") ? tmpObj.getAsJsonPrimitive("comment").getAsString() : ""; if (!comment.isEmpty()) { numOfComments++; if (this.isRevert(comment)) { numOfReverts++; } } if (!comparableRevisions.containsKey(key)) { comparableRevisions.put(key, Tuple.create(0, 0)); } comparableRevisions.put(key, Tuple.create(comparableRevisions.get(key).getItem1() + 1, comparableRevisions.get(key).getItem2() + sizediff)); } final Set<String> categorySet = new HashSet<String>(); for (final Map.Entry<Tuple2<Integer, String>, Tuple2<Integer, Integer>> entry : comparableRevisions .entrySet()) { final String categories = this.getCategories(entry.getKey().getItem1()); categoryEdited.add(new ArticleEdited(entry.getKey().getItem2(), entry.getValue().getItem1(), entry.getValue().getItem2(), categories)); final String[] catArr = categories.split(";"); for (final String cat : catArr) { categorySet.add(cat.trim()); if (!commitsPerCategory.containsKey(cat.trim())) { commitsPerCategory.put(cat.trim(), entry.getValue().getItem1()); } else { commitsPerCategory.put(cat.trim(), (commitsPerCategory.get(cat.trim()) + entry.getValue().getItem1())); } } } try { signInDate = this.formatter .parse(user.getAsJsonPrimitive("registration").getAsString().replace('T', '_')); } catch (Exception e) { this.logger.warn(userName + " has no sign in date!"); final String signInRsp = this.requester.getResult( this.convertRequest("action=query&format=json&list=usercontribs&ucdir=newer&ucuser=" + userName + "&uclimit=1&ucprop=timestamp")); final JsonObject aa = this.parser.parse(userArticles).getAsJsonObject(); final JsonArray aaa = aa.getAsJsonObject("query").getAsJsonArray("usercontribs"); if (aaa.size() > 0) { // get the date of the first user contrib signInDate = this.formatter.parse(aaa.get(aaa.size() - 1).getAsJsonObject() .getAsJsonPrimitive("timestamp").getAsString().replace('T', '_')); } } final StringBuilder categoriesStrBuilder = new StringBuilder(); categoriesStrBuilder.append("("); categoriesStrBuilder.append(categorySet.size()); categoriesStrBuilder.append(") = "); for (final String cat : categorySet) { categoriesStrBuilder.append(cat); categoriesStrBuilder.append("; "); } /* * get categories */ final String categoryCommits = categoriesStrBuilder.toString().substring(0, categoriesStrBuilder.length() - 2); /* * get reputation */ final Object[] reps = this.calcReputation(userName, lastUserCommits, blocked); final double reputation = (Double) reps[0]; final String abuses = (String) reps[1]; final int abuseCount = (Integer) reps[2]; /* * user classes */ String userclassNumOfCommits = "Gott"; if (lastUserCommits < 100) { userclassNumOfCommits = "Rookie"; } else if (lastUserCommits < 1000) { userclassNumOfCommits = "Fortgeschrittener"; } else if (lastUserCommits < 10000) { userclassNumOfCommits = "Profi"; } else if (lastUserCommits < 100000) { userclassNumOfCommits = "Master"; } String userclassAvgCommits = "\"nicht bewertbar\""; double avgCommits = 0; if (signInDate != null) { avgCommits = editcount / ((System.currentTimeMillis() - signInDate.getTime()) / 86400000); if (avgCommits == 0.0) { userclassAvgCommits = "Schlafmtze"; } else if (avgCommits < 1.0d) { userclassAvgCommits = "Gelegenheitssurfer"; } else if (avgCommits < 5.0d) { userclassAvgCommits = "Durchschnittsuser"; } else if (avgCommits < 15.0d) { userclassAvgCommits = "Vielschreiber"; } else { userclassAvgCommits = "Dauergast"; } } String userclassRevert = "\"nicht bewertbar\""; String userclassComment = "\"nicht bewertbar\""; if (lastUserCommits > 0) { final double revertCommitRelation = (double) numOfReverts / lastUserCommits; userclassRevert = "Spielverderber"; if (revertCommitRelation < 0.01) { userclassRevert = "Ein-Auge-Zudrcker"; } else if (revertCommitRelation < 0.1) { userclassRevert = "Gelegenheitsspieer"; } else if (revertCommitRelation < 0.2) { userclassRevert = "Sturrkopf"; } else if (revertCommitRelation < 0.5) { userclassRevert = "Kontrolleur"; } final double commentCommitRelation = (double) numOfComments / lastUserCommits; userclassComment = "Saubermann"; if (commentCommitRelation < 0.1) { userclassComment = "Dokuhasser"; } else if (commentCommitRelation < 0.5) { userclassComment = "Ab und zu vergesslich"; } else if (commentCommitRelation < 0.8) { userclassComment = "Ordnungshter"; } } String userDiscussion = "Sehr oft"; if (numOfUserDiscussion == 0) { userDiscussion = "Nie"; } else if (numOfUserDiscussion < 3) { userDiscussion = "Selten"; } else if (numOfUserDiscussion < 10) { userDiscussion = "Gelegentlich"; } else if (numOfUserDiscussion < 20) { userDiscussion = "Oft"; } String selfDiscussion = "Sehr oft"; if (numofSelfDiscussion == 0) { selfDiscussion = "Nie"; } else if (numofSelfDiscussion < 10) { selfDiscussion = "Selten"; } else if (numofSelfDiscussion < 30) { selfDiscussion = "Gelegentlich"; } else if (numofSelfDiscussion < 100) { selfDiscussion = "Oft"; } return new UserInfo(userid, userName, editcount, categoryCommits, avgCommits, signInDate, reputation, categoryEdited, blocked, userclassNumOfCommits, userclassAvgCommits, userclassRevert, userclassComment, userDiscussion, selfDiscussion, commitsPerCategory, abuses, abuseCount, numOfReverts, numOfComments, numOfUserDiscussion, numofSelfDiscussion, isBot); } catch (Exception e) { this.logger.error(e.getMessage(), e); } return null; }
From source file:de.behrfried.wikianalyzer.wawebapp.server.service.JsonWikiAccess.java
License:Apache License
@Override public CriterionInfo getCriterionInfo(List<TitleOrCategory> titlesOrCategories) throws CriterionNotFoundException { final Map<String, Map<String, Integer>> users = new HashMap<String, Map<String, Integer>>(); final Map<String, List<Integer>> pages = new HashMap<String, List<Integer>>(); for (final TitleOrCategory toc : titlesOrCategories) { if (toc.isCategory()) { String nextpage = ""; do {/*w w w . j av a 2 s. c om*/ String resp = this.requester.getResult(this.convertRequest( "action=query&format=json&list=categorymembers&cmlimit=500&cmtitle=Kategorie:" + toc.getTitle() + "&cmstartsortkey=" + nextpage)); final JsonObject root = this.parser.parse(resp).getAsJsonObject(); final JsonArray jsonPagesArr = root.getAsJsonObject("query").getAsJsonArray("categorymembers"); final List<Integer> pageids = new ArrayList<Integer>(jsonPagesArr.size()); for (final JsonElement jsonElem : jsonPagesArr) { pageids.add(jsonElem.getAsJsonObject().getAsJsonPrimitive("pageid").getAsInt()); } pages.put(toc.getTitle(), pageids); nextpage = ""; if (root.has("query-continue")) { nextpage = root.getAsJsonObject("query-continue").getAsJsonObject("categorymembers") .getAsJsonPrimitive("cmcontinue").getAsString(); } } while (!nextpage.isEmpty()); } else { final List<Integer> pageids = new ArrayList<Integer>(1); pageids.add(this.getPageId(toc.getTitle())); pages.put(toc.getTitle(), pageids); this.getPageId(toc.getTitle()); } } /* * iterate all criterions */ // User, Title, Commits for (final Map.Entry<String, List<Integer>> entry : pages.entrySet()) { for (final int pageid : entry.getValue()) { /* * iterate all revisions */ int lastRev = 0; while (lastRev != -1) { final String response1 = this.requester .getResult(this.convertRequest("action=query&format=json&prop=revisions&rvprop=user" + "&rvlimit=500&rvdir=newer&rvexcludeuser=127.0.0.1&pageids=" + pageid + "&rvstartid=" + lastRev + "&continue=")); final JsonObject root = this.parser.parse(response1).getAsJsonObject(); final JsonObject page = root.getAsJsonObject("query").getAsJsonObject("pages") .getAsJsonObject(pageid + ""); if (!page.has("revisions")) { break; } final JsonArray jsonRevArr = page.getAsJsonArray("revisions"); for (final JsonElement jsonElem : jsonRevArr) { final String user = jsonElem.getAsJsonObject().getAsJsonPrimitive("user").getAsString(); if (!users.containsKey(user)) { final Map<String, Integer> title = new HashMap<String, Integer>(); users.put(user, title); } final Map<String, Integer> title = users.get(user); if (!title.containsKey(entry.getKey())) { title.put(entry.getKey(), 0); } title.put(entry.getKey(), title.get(entry.getKey()) + 1); } lastRev = root.has("continue") ? root.getAsJsonObject("continue").getAsJsonPrimitive("rvcontinue").getAsInt() : -1; } } } final List<CriterionInfo.User> userList = new ArrayList<CriterionInfo.User>(users.size()); outer: for (final Map.Entry<String, Map<String, Integer>> entry : users.entrySet()) { final CriterionInfo.User user = new CriterionInfo.User(entry.getKey(), 1); for (final TitleOrCategory toc : titlesOrCategories) { if (!entry.getValue().containsKey(toc.getTitle())) { continue outer; } user.setMatch(user.getMatch() * entry.getValue().get(toc.getTitle())); } userList.add(user); } outer: for (int i = 0; i < userList.size(); i++) { final CriterionInfo.User us = userList.get(i); final String userBotRsp = this.requester.getResult(this.convertRequest( "action=query&format=json&list=users&usprop=groups|blockinfo&ususers=" + us.getUserName())); final JsonObject usersJsonObj = this.parser.parse(userBotRsp).getAsJsonObject().getAsJsonObject("query") .getAsJsonArray("users").get(0).getAsJsonObject(); if (usersJsonObj.has("blockid")) { userList.remove(i); continue; } if (!usersJsonObj.has("groups")) { userList.remove(i); continue; } final JsonArray groupsJsonArr = usersJsonObj.getAsJsonArray("groups"); for (JsonElement jsonElement : groupsJsonArr) { if ("bot".equals(jsonElement.getAsJsonPrimitive().getAsString())) { userList.remove(i); continue outer; } } us.setMatch(us.getMatch() * (Double) this.calcReputation(us.getUserName(), users.get(us.getUserName()).size(), false)[0]); } Collections.sort(userList, new Comparator<CriterionInfo.User>() { @Override public int compare(CriterionInfo.User user, CriterionInfo.User user2) { return (int) Math.signum(user2.getMatch() - user.getMatch()); } }); return new CriterionInfo(userList); }