br.edu.ufsc.eventCrawler.EventByID.java Source code

Java tutorial

Introduction

Here is the source code for br.edu.ufsc.eventCrawler.EventByID.java

Source

/*
 * 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 br.edu.ufsc.eventCrawler;

import br.edu.ufsc.eventCrawler.util.FacebookEvent;
import br.edu.ufsc.eventCrawler.util.FacebookVenue;
import br.edu.ufsc.eventCrawler.util.Util;
import facebook4j.Facebook;
import facebook4j.internal.org.json.JSONArray;
import facebook4j.internal.org.json.JSONException;
import facebook4j.internal.org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

/**
 *
 * @author lucasscharf
 */
@ManagedBean
@ViewScoped
public class EventByID {

    String eventID;
    Facebook fb;
    List<FacebookEvent> events;

    public EventByID() {
        fb = Util.getFacebook(FacesContext.getCurrentInstance());
        events = new ArrayList<FacebookEvent>();
    }

    public List<FacebookEvent> getEvents() {
        return events;
    }

    public String getEventID() {
        return eventID;
    }

    public void setEventID(String eventID) {
        this.eventID = eventID;
    }

    public void inserirEvento() throws Exception {
        if (eventID == null || eventID.isEmpty()) {
            throw new Exception("Evento em branco!");
        }

        String query = "select eid, name,description, start_time, end_time," + "location, venue from event "
                + "WHERE eid =" + eventID;
        JSONArray fqlEvent = fb.executeFQL(query);
        JSONObject jsonObjectEvent = fqlEvent.getJSONObject(0);
        FacebookEvent event = new FacebookEvent(jsonObjectEvent);
        try {
            FacebookVenue venue = new FacebookVenue(jsonObjectEvent.getJSONObject("venue"));
            event.setVenue(venue);
        } catch (JSONException ex) {
            event.setVenue(new FacebookVenue(jsonObjectEvent));
        }
        events.add(event);
    }
}