Created
March 15, 2017 01:27
-
-
Save Jatapiaro/07e0803b738c0ab832136bc0ce2a497b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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 edu.itesm.Controllers; | |
import edu.itesm.Dao.ReactivoDAO; | |
import edu.itesm.Ejbs.OpcionFacadeLocal; | |
import edu.itesm.Ejbs.ReactivoFacadeLocal; | |
import edu.itesm.Helpers.UploadImageHelper; | |
import edu.itesm.Models.Modulo; | |
import edu.itesm.Models.Opcion; | |
import edu.itesm.Models.Reactivo; | |
import edu.itesm.Models.Usuario; | |
import java.io.File; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.Serializable; | |
import java.nio.file.Files; | |
import java.nio.file.Path; | |
import java.nio.file.Paths; | |
import java.nio.file.StandardCopyOption; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.Map; | |
import javax.annotation.PostConstruct; | |
import javax.ejb.EJB; | |
import javax.faces.application.FacesMessage; | |
import javax.faces.context.ExternalContext; | |
import javax.faces.context.FacesContext; | |
import javax.faces.view.ViewScoped; | |
import javax.inject.Named; | |
import org.apache.commons.io.FilenameUtils; | |
import org.primefaces.event.FileUploadEvent; | |
/** | |
* | |
* @author jacobotapia | |
*/ | |
@Named | |
@ViewScoped | |
public class ReactivoController implements Serializable{ | |
@EJB | |
ReactivoFacadeLocal reactivoEJB; | |
OpcionFacadeLocal opcionEJB; | |
private Reactivo reactivo; | |
private List<Reactivo> reactivos; | |
private Opcion opcion; | |
private boolean isEditing,isModifying; | |
@PostConstruct | |
public void init(){ | |
this.reactivo=new Reactivo(); | |
this.reactivos=new ArrayList<Reactivo>(); | |
this.opcion=new Opcion(); | |
this.isEditing=false; | |
this.isModifying=false; | |
} | |
public void registrar(Usuario u) throws Exception{ | |
try{ | |
this.reactivo.setCreadoPor(u); | |
for(int i=0;i<this.reactivo.getOpcionList().size();i++){ | |
this.reactivo.getOpcionList().get(i).setIdReactivo(reactivo); | |
} | |
this.reactivoEJB.create(reactivo); | |
String contextPath = FacesContext.getCurrentInstance().getExternalContext().getApplicationContextPath(); | |
FacesContext.getCurrentInstance().getExternalContext().redirect(contextPath+"/faces/views/reactivo/List.xhtml"); | |
}catch(Exception e){ | |
throw e; | |
} | |
} | |
public void actualizar() throws Exception{ | |
try{ | |
for (int i = 0; i < this.reactivo.getOpcionList().size(); i++) { | |
this.reactivo.getOpcionList().get(i).setIdReactivo(reactivo); | |
} | |
this.reactivoEJB.edit(reactivo); | |
String contextPath = FacesContext.getCurrentInstance().getExternalContext().getApplicationContextPath(); | |
FacesContext.getCurrentInstance().getExternalContext().redirect(contextPath+"/faces/views/reactivo/List.xhtml"); | |
}catch(Exception e){ | |
throw e; | |
} | |
} | |
public void eliminar(Reactivo r) throws Exception{ | |
try{ | |
this.reactivoEJB.remove(r); | |
}catch(Exception e){ | |
throw e; | |
} | |
} | |
public void find() throws Exception{ | |
if(this.isModifying==false){ | |
ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext(); | |
Map<String, Object> sessionMap = externalContext.getSessionMap(); | |
Object id = sessionMap.get("id"); | |
try { | |
this.reactivo = this.reactivoEJB.find(id); | |
} catch (Exception e) { | |
throw e; | |
} | |
this.isModifying=true; | |
} | |
} | |
public void listar() throws Exception{ | |
try{ | |
this.reactivos=this.reactivoEJB.findAll(); | |
}catch(Exception e){ | |
throw e; | |
} | |
} | |
/** | |
* @return the reactivo | |
*/ | |
public Reactivo getReactivo() { | |
return reactivo; | |
} | |
/** | |
* @param reactivo the reactivo to set | |
*/ | |
public void setReactivo(Reactivo reactivo) { | |
this.reactivo = reactivo; | |
} | |
/** | |
* @return the reactivos | |
*/ | |
public List<Reactivo> getReactivos() { | |
return reactivos; | |
} | |
/** | |
* @param reactivos the reactivos to set | |
*/ | |
public void setReactivos(List<Reactivo> reactivos) { | |
this.reactivos = reactivos; | |
} | |
/** | |
* @return the opcion | |
*/ | |
public Opcion getOpcion() { | |
return opcion; | |
} | |
/** | |
* @param opcion the opcion to set | |
*/ | |
public void setOpcion(Opcion opcion) { | |
this.opcion = opcion; | |
} | |
public void addOpcion() throws Exception{ | |
if(this.opcion.getCorrecto() && this.reactivo.hasCorrectAnswer()){ | |
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "Ey! La pregunta ya tiene una respuesta correcta.", "La pregunta ya tiene una respuesta correcta.")); | |
}else{ | |
System.out.println(this.opcion.getFile()+" El archivo"); | |
if(this.opcion.getFile()!=null){ | |
this.subirImagenOpcion(); | |
} | |
this.reactivo.getOpcionList().add(0, opcion); | |
this.opcion=null; | |
this.opcion=new Opcion(); | |
} | |
} | |
/** | |
* Edita una opción durante la creación de un reactivo | |
*/ | |
public void editOpcion() { | |
if (this.opcion.getCorrecto() && this.reactivo.hasCorrectAnswer()) { | |
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "Ey! La pregunta ya tiene una respuesta correcta.", "La pregunta ya tiene una respuesta correcta.")); | |
} else { | |
this.reactivo.getOpcionList().get( | |
this.opcion.getNumeroOpcion()).setCorrecto(this.opcion.getCorrecto()); | |
this.reactivo.getOpcionList().get( | |
this.opcion.getNumeroOpcion()).setTexto(this.opcion.getTexto()); | |
this.opcion = null; | |
this.opcion = new Opcion(); | |
this.isEditing=false; | |
} | |
} | |
/** | |
* Permite editar una opción durante la modificación total de un reactivo | |
*/ | |
public void editOpcionTwo() throws Exception { | |
if (this.opcion.getCorrecto() && this.reactivo.hasCorrectAnswer() && this.opcion.getNumeroOpcion()!=this.reactivo.indexOfCorrectAnswer()) { | |
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "Ey! La pregunta ya tiene una respuesta correcta.", "La pregunta ya tiene una respuesta correcta.")); | |
} else { | |
this.reactivo.getOpcionList().get( | |
this.opcion.getNumeroOpcion()).setCorrecto(this.opcion.getCorrecto()); | |
this.reactivo.getOpcionList().get( | |
this.opcion.getNumeroOpcion()).setTexto(this.opcion.getTexto()); | |
if(this.opcion.getFile()!=null){ | |
UploadImageHelper ui = new UploadImageHelper(); | |
if(this.opcion.hasImagen()){ | |
ui.deleteFile(this.opcion.getImagen()); | |
String fileName = ui.processUpload(this.opcion.getFile()); | |
this.opcion.setImagen(fileName); | |
}else{ | |
String fileName = ui.processUpload(this.opcion.getFile()); | |
this.opcion.setImagen(fileName); | |
} | |
} | |
this.reactivo.getOpcionList().get( | |
this.opcion.getNumeroOpcion()).setImagen(this.opcion.getImagen()); | |
this.opcion = null; | |
this.opcion = new Opcion(); | |
this.isEditing = false; | |
} | |
} | |
public void removeOpcion(int index){ | |
this.reactivo.getOpcionList().remove(index); | |
} | |
/** | |
* @return the isEditing | |
*/ | |
public boolean isIsEditing() { | |
return isEditing; | |
} | |
/** | |
* @param isEditing the isEditing to set | |
*/ | |
public void setIsEditing(boolean isEditing) { | |
this.isEditing = isEditing; | |
} | |
public void editMode(int i){ | |
this.isEditing=true; | |
this.editOption(i); | |
} | |
/* | |
*Guarda los cambios | |
*/ | |
public void editOption(int i){ | |
Opcion o=this.reactivo.getOpcionList().get(i); | |
this.opcion=new Opcion(); | |
this.opcion.setCorrecto(o.getCorrecto()); | |
this.opcion.setNumeroOpcion(i); | |
this.opcion.setTexto(o.getTexto()); | |
this.opcion.setImagen(o.getImagen()); | |
} | |
public void editPage(Object id) throws IOException{ | |
ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext(); | |
Map<String, Object> sessionMap = externalContext.getSessionMap(); | |
sessionMap.put("id", id); | |
String contextPath = FacesContext.getCurrentInstance().getExternalContext().getApplicationContextPath(); | |
FacesContext.getCurrentInstance().getExternalContext().redirect(contextPath+"/faces/views/reactivo/Edit.xhtml"); | |
} | |
public void viewPage(Object id) throws IOException { | |
ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext(); | |
Map<String, Object> sessionMap = externalContext.getSessionMap(); | |
sessionMap.put("id", id); | |
String contextPath = FacesContext.getCurrentInstance().getExternalContext().getApplicationContextPath(); | |
FacesContext.getCurrentInstance().getExternalContext().redirect(contextPath+"/faces/views/reactivo/View.xhtml"); | |
} | |
public void newPage() throws IOException{ | |
String contextPath = FacesContext.getCurrentInstance().getExternalContext().getApplicationContextPath(); | |
FacesContext.getCurrentInstance().getExternalContext().redirect(contextPath+"/faces/views/reactivo/Create.xhtml"); | |
} | |
public void subirImagen() throws IOException, Exception{ | |
if(this.reactivo.getFile()!=null){ | |
UploadImageHelper im = new UploadImageHelper(); | |
if (this.reactivo.hasImagen()) { | |
im.deleteFile(this.reactivo.getImagen()); | |
} | |
this.reactivo.setImagen(im.processUpload(this.reactivo.getFile())); | |
}else{ | |
System.out.println("Nope"); | |
} | |
} | |
public void subirImagenOpcion() throws IOException, Exception { | |
if(this.opcion.getFile()!=null){ | |
UploadImageHelper im = new UploadImageHelper(); | |
if (this.opcion.hasImagen()) { | |
im.deleteFile(this.opcion.getImagen()); | |
} | |
this.opcion.setImagen(im.processUpload(this.opcion.getFile())); | |
}else{ | |
System.out.println("Nope"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment