Java tutorial
/* * 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 com.forum.action.eder; import com.forum.beans.EstadisticasIndiv; import com.forum.beans.Perfil; import com.hp.hpl.jena.rdf.model.Model; import com.hp.hpl.jena.rdf.model.ModelFactory; import com.hp.hpl.jena.rdf.model.NodeIterator; import com.hp.hpl.jena.rdf.model.RDFNode; import com.hp.hpl.jena.rdf.model.Resource; import com.hp.hpl.jena.rdf.model.ResourceFactory; import com.hp.hpl.jena.sparql.vocabulary.FOAF; import com.hp.hpl.jena.util.FileManager; import com.opensymphony.xwork2.ActionSupport; import java.io.FileOutputStream; import java.io.IOException; import java.util.List; import javax.servlet.ServletContext; import org.apache.struts2.ServletActionContext; import org.jdom2.Document; import org.jdom2.Element; import org.jdom2.JDOMException; import org.jdom2.input.SAXBuilder; import org.jdom2.output.Format; import org.jdom2.output.XMLOutputter; /** * * @author zack */ public class PerfilACT extends ActionSupport { private String nick, solicitante, es_conocido; private Perfil detalles; private EstadisticasIndiv calificaciones; public PerfilACT() { } public String getSolicitante() { return solicitante; } public void setSolicitante(String solicitante) { this.solicitante = solicitante; } public String getEs_conocido() { return es_conocido; } public void setEs_conocido(String es_conocido) { this.es_conocido = es_conocido; } public String getNick() { return nick; } public void setNick(String nick) { this.nick = nick; } public Perfil getDetalles() { return detalles; } public void setDetalles(Perfil detalles) { this.detalles = detalles; } public EstadisticasIndiv getCalificaciones() { return calificaciones; } public void setCalificaciones(EstadisticasIndiv calificaciones) { this.calificaciones = calificaciones; } public String execute() throws Exception { int c0 = 0, c1 = 0, c2 = 0, c3 = 0, c4 = 0, c5 = 0, c6 = 0, c7 = 0, c8 = 0, c9 = 0, c10 = 0; Model modelo = ModelFactory.createDefaultModel(); FileManager.get().readModel(modelo, ServletActionContext.getServletContext().getRealPath("/") + "/usuarios.rdf"); Resource perfil = modelo.getResource("http://comunipn.multiaportes.com/rdf/" + nick); NodeIterator amigos = modelo.listObjectsOfProperty( modelo.getResource("http://comunipn.multiaportes.com/rdf/" + solicitante), FOAF.knows); while (amigos.hasNext()) { RDFNode amigo = amigos.next(); Resource amigo1 = (Resource) amigo; // Resource es clase hija de RDFNode if (amigo1.getProperty(FOAF.nick).getObject().toString().equals(nick)) es_conocido = "1"; else es_conocido = "0"; } ServletContext context = ServletActionContext.getServletContext(); String url = context.getRealPath("/"); Document document = getDocument(url + "/database.xml"); Element root = document.getRootElement(); Element temas = root.getChild("respuestas"); List<Element> child = temas.getChildren(); for (Element tema : child) { for (Element respuesta : tema.getChildren()) { if (respuesta.getAttributeValue("usuario").equals(nick)) { switch (Integer.parseInt(respuesta.getAttributeValue("calificacion"))) { case 0: c0++; break; case 1: c1++; break; case 2: c2++; break; case 3: c3++; break; case 4: c4++; break; case 5: c5++; break; case 6: c6++; break; case 7: c7++; break; case 8: c8++; break; case 9: c9++; break; case 10: c10++; break; } } } } detalles = new Perfil(perfil.getProperty(FOAF.givenname).getObject().toString(), perfil.getProperty(FOAF.family_name).getObject().toString(), "Alumno", perfil.getProperty(FOAF.schoolHomepage).getObject().toString()); calificaciones = new EstadisticasIndiv(c0, c1, c2, c3, c4, c5, c6, c7, c8, c9, c10); return SUCCESS; } public void write(Document document, String url) { try { new XMLOutputter(Format.getPrettyFormat()).output(document, new FileOutputStream(url, false)); } catch (IOException ex) { ex.printStackTrace(); } } public Document getDocument(String url) { SAXBuilder builder = new SAXBuilder(); Document document = null; try { document = builder.build(url); return document; } catch (IOException | JDOMException ex) { ex.printStackTrace(); } return null; } }