Skip to content

Commit

Permalink
Merge pull request #1 from carlosvasquez/master
Browse files Browse the repository at this point in the history
merge de nuevos cambios para el manejo de información de universidades
  • Loading branch information
carlosvasquez committed Jan 18, 2014
2 parents ef7b95f + c977b1d commit 9d81d00
Show file tree
Hide file tree
Showing 3 changed files with 175 additions and 5 deletions.
4 changes: 4 additions & 0 deletions src/main/java/org/devdom/fbclient/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ public static void main( String[] args )
fbConnect.setQuestionID("562574913759358"); //Cuales son tus skillls? (Plataforma para la que desarrollas)
fbConnect.updateSkills();
fbConnect.setQuestionID("558308817519301"); //Cuales son tus skillls? (Manejadores de versión)
fbConnect.updateSkills();
fbConnect.setQuestionID("606824229334426"); //Universidades
fbConnect.updateSkills();
fbConnect.updateUniversities();
fbConnect.updateDevelopers();
}
}
58 changes: 53 additions & 5 deletions src/main/java/org/devdom/fbclient/FBConnect.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import org.devdom.fbclient.model.dto.Skillset;
import org.devdom.fbclient.model.dto.University;
import org.devdom.fbclient.model.dto.Users;
import org.devdom.fbclient.model.dto.Votes;
import org.devdom.fbclient.model.dto.VotesPK;
Expand Down Expand Up @@ -68,7 +69,7 @@ private JSONArray getFBVoters(Long skillID) {
}
}

public JSONArray getSkills(){
public JSONArray getOptions(){
try {
String query = FQL.QUESTION_OPTIONS;
query = query.replace(":question_id",questionID);
Expand All @@ -81,7 +82,7 @@ public JSONArray getSkills(){

public int totalFBVotes(){
int count = 0;
JSONArray result = getSkills();
JSONArray result = getOptions();
try {
for (int i = 0; i < result.length(); i++){
JSONObject json = result.getJSONObject(i);
Expand Down Expand Up @@ -175,19 +176,66 @@ public void updateDevelopers(){
em.getTransaction().commit();
}

public void updateUniversities() {
EntityManager em = getEntityManager();
List<University> universities = em.createNamedQuery("University.findAll").getResultList();
int added = 0;
int updated = 0;
JSONArray result = getOptions();
boolean exists;
em.getTransaction().begin();
for(int i=0;i<result.length();i++){
try {
exists = false;
JSONObject json = result.getJSONObject(i);
Long fbId = Long.valueOf( json.getString("id") );
for(University university : universities){
if(fbId == university.getFbId()){
exists=true;
university.setVotes(Short.valueOf(json.getString("votes")));
Logger.getLogger(FBConnect.class.getName()).log(Level.INFO, "{0} es la nueva cantida de votos para la universidad {1} ", new Object[]{json.getString("votes"), json.getString("name")});
updated++;
Logger.getLogger(FBConnect.class.getName()).log(Level.INFO, "{0} = {1} ", new Object[]{fbId, university.getFbId()});
break;
}
}
if(!exists){
University newUni = new University();
newUni.setFbId( fbId );
newUni.setName(json.getString("name"));
newUni.setVotes(Short.valueOf( json.getString("votes") ));
em.persist(newUni);
Logger.getLogger(FBConnect.class.getName()).log(Level.INFO, "fue agregada la universidad {0} con {1} votos", new Object[]{json.getString("name"), json.getString("votes")});
added++;
}

int count = updateVotes(json.getLong("id"));
if(count>0){
Logger.getLogger(FBConnect.class.getName()).log(Level.INFO, "fueron añadidas {0} universidades", count );
}
} catch (JSONException ex) {
Logger.getLogger(FBConnect.class.getName()).log(Level.SEVERE, null, ex);
em.getTransaction().rollback();
}
}
em.getTransaction().commit();
//em.getTransaction().rollback();
}

public void updateSkills(){

EntityManager em = getEntityManager();
List<Skillset> skills = em.createNamedQuery("Skillset.findAll").getResultList();

int updated = 0;
JSONArray result = getSkills();
JSONArray result = getOptions();
for(Skillset skill : skills){
for (int i = 0; i < result.length(); i++){
try {
em.getTransaction().begin();
JSONObject json = result.getJSONObject(i);
if( json.get("id").equals(skill.getOptionId()) ){
Long skillID = Long.valueOf(json.getString("id"));
if( skillID == skill.getOptionId() ){
if(!json.get("votes").equals(skill.getVotes())){
updated++;
Logger.getLogger(FBConnect.class.getName()).log(Level.INFO, "skill : {0}", json.getString("name"));
Expand All @@ -212,7 +260,7 @@ public void updateSkills(){
}
Logger.getLogger(FBConnect.class.getName()).log(Level.INFO, "skill actualizados: {0}", updated);
}

/**
* @return the questionID
*/
Expand Down
118 changes: 118 additions & 0 deletions src/main/java/org/devdom/fbclient/model/dto/University.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

package org.devdom.fbclient.model.dto;

import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlRootElement;

/**
*
* @author Ronny Placencia
*/
@Entity
@Table(name = "dev_university")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "University.findAll", query = "SELECT u FROM University u"),
@NamedQuery(name = "University.findById", query = "SELECT u FROM University u WHERE u.id = :id"),
@NamedQuery(name = "University.findByFbId", query = "SELECT u FROM University u WHERE u.fbId = :fbId"),
@NamedQuery(name = "University.findByName", query = "SELECT u FROM University u WHERE u.name = :name"),
@NamedQuery(name = "University.findByVotes", query = "SELECT u FROM University u WHERE u.votes = :votes")})
public class University implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "id")
private Integer id;
@Column(name = "fb_id")
private long fbId;
@Column(name = "name")
private String name;
@Column(name = "votes")
private Short votes;

public University() {
}

public University(long fbId, String name, Short votes){
this.fbId = fbId;
this.name = name;
this.votes = votes;
}

public University(Integer id) {
this.id = id;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public long getFbId() {
return fbId;
}

public void setFbId(long fbId) {
this.fbId = fbId;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Short getVotes() {
return votes;
}

public void setVotes(Short votes) {
this.votes = votes;
}

@Override
public int hashCode() {
int hash = 0;
hash += (id != null ? id.hashCode() : 0);
return hash;
}

@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof University)) {
return false;
}
University other = (University) object;
if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
return false;
}
return true;
}

@Override
public String toString() {
return "org.devdom.fbclient.model.dto.University[ id=" + id + " ]";
}

}

0 comments on commit 9d81d00

Please sign in to comment.