Sei sulla pagina 1di 14

package it.unisannio.community.web.

jsf; import import import import import import import import import import import import import import it.unisannio.community.core.searchEngine.Engine; it.unisannio.community.core.searchEngine.Indexer; it.unisannio.community.core.searchEngine.util.IndexedDoc; it.unisannio.community.core.searchEngine.util.MicrosoftExtractor; it.unisannio.community.core.searchEngine.util.Pdf2txt; it.unisannio.community.core.searchEngine.util.Text2txt; it.unisannio.community.dao.CommentoDAO; it.unisannio.community.dao.FileDAO; it.unisannio.community.dao.GruppoCorsoDAO; it.unisannio.community.dao.UtenteDAO; it.unisannio.community.domain.Commento; it.unisannio.community.domain.File; it.unisannio.community.domain.GruppoCorso; it.unisannio.community.domain.Utente;

import it.unisannio.community.service.CommentoService; import it.unisannio.community.service.FileService; import import import import import import import import import import import java.io.FileInputStream; java.io.FileNotFoundException; java.io.FileOutputStream; java.io.IOException; java.io.InputStream; java.io.Serializable; java.util.ArrayList; java.util.Calendar; java.util.HashSet; java.util.List; java.util.Set;

import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Transactional; import import import import import import javax.faces.application.FacesMessage; javax.faces.context.ExternalContext; javax.faces.context.FacesContext; org.primefaces.event.FileUploadEvent; org.primefaces.model.DefaultStreamedContent; org.primefaces.model.StreamedContent;

/** * Spring Component that handles CRUD requests for File entities * */ @Scope("session") @Component("FileComponent") public class FileComponentImpl implements FileComponent, Serializable { /** * Data type variable that provides CRUD operations for File entities * */

private File file; private File upFile; private List<File> listaFile; private List<Commento> listaCommenti; private StreamedContent downloadableFile; private String tag; /** * DAO injected by Spring that manages Utente entities * */ private Utente utente; /** * DAO injected by Spring that manages GruppoCorso entities * */ private GruppoCorso gruppocorso; /** * DAO injected by Spring that manages Commento entities * */ private Commento commentos; /** * DAO injected by Spring that manages Commento entities * */ @Autowired private CommentoDAO commentoDAO; /** * DAO injected by Spring that manages File entities * */ @Autowired private FileDAO fileDAO; /** * DAO injected by Spring that manages GruppoCorso entities * */ @Autowired private GruppoCorsoDAO gruppoCorsoDAO; /** * DAO injected by Spring that manages Utente entities * */ @Autowired private UtenteDAO utenteDAO; /** * Service injected by Spring that provides CRUD operations for File ent ities

* */ @Autowired private FileService fileService; @SuppressWarnings("unused") @Autowired private CommentoService commentoService; private String descrizione; private FileUploadEvent event; /** */ public FileComponentImpl() { } /** * Select the child Utente entity for display allowing the user to confi rm that they would like to delete the entity * */ @Transactional public String confirmDeleteFileUtente(Integer file_id, Integer related_u tente_id) { utente = utenteDAO.findUtenteByPrimaryKey(related_utente_id); return "/jsf/file/utente/deleteUtente.xhtml"; } /** * Show all File entities * */ @Transactional public List<File> listFiles() { return new java.util.ArrayList<File>(fileService.loadFiles()); }

/** * Create a new File entity * */ @Transactional public String newFile() { file = new File(); return "/jsf/file/createFile.xhtml"; } /** * Edit an existing GruppoCorso entity * */ @Transactional public String editFileGruppoCorso(Integer file_id, Integer related_grupp

ocorso_id) { gruppocorso = gruppoCorsoDAO.findGruppoCorsoByPrimaryKey(related _gruppocorso_id, -1, -1); return "/jsf/file/gruppocorso/editGruppoCorso.xhtml"; } /** * Select the child Commento entity for display allowing the user to con firm that they would like to delete the entity * */ @Transactional public String confirmDeleteFileCommentos(Integer file_id, Integer relate d_commentos_id) { commentos = commentoDAO.findCommentoByPrimaryKey(related_comment os_id); return "/jsf/file/commentos/deleteCommentos.xhtml"; } /** * Get Utente entity by File * */ @Transactional public Utente getFileUtente(Integer idKey) { return fileDAO.findFileByPrimaryKey(idKey).getUtente(); } /** * Save an existing GruppoCorso entity * */ @Transactional public String saveFileGruppoCorso(Integer file_id, GruppoCorso gruppocor so) { file = fileService.saveFileGruppoCorso(file_id, gruppocorso); return "/jsf/file/viewFile.xhtml"; } /** * Create a new Commento entity * */ @Transactional public String newFileCommentos(Integer file_id) { commentos = new Commento(); return "/jsf/file/commentos/createCommentos.xhtml"; } /** * View an existing Commento entity * */ @Transactional public String selectFileCommentos(Integer file_id, Integer related_comme ntos_id) { commentos = commentoDAO.findCommentoByPrimaryKey(related_comment

os_id, -1, -1); return "/jsf/file/commentos/viewCommentos.xhtml"; } /** * Getter for variable File * */ @Transactional public GruppoCorso getGruppocorso() { return gruppocorso; } /** * Delete an existing Utente entity * */ @Transactional public String deleteFileUtente(Integer file_id, Integer related_utente_i d) { file = fileService.deleteFileUtente(file_id, related_utente_id); return "/jsf/file/viewFile.xhtml"; } /** * Show all Commento entities by File * */ @Transactional public List<Commento> listFileCommentos(Integer idKey) { return new java.util.ArrayList<Commento>(fileDAO.findFileByPrima ryKey(idKey).getCommentos()); } /** * Getter for variable File * */ @Transactional public File getFile() { return file; } /** * Create a new GruppoCorso entity * */ @Transactional public String newFileGruppoCorso(Integer file_id) { gruppocorso = new GruppoCorso(); return "/jsf/file/gruppocorso/createGruppoCorso.xhtml"; } /** * Edit an existing Utente entity * */ @Transactional

public String editFileUtente(Integer file_id, Integer related_utente_id) { utente = utenteDAO.findUtenteByPrimaryKey(related_utente_id, -1, -1); return "/jsf/file/utente/editUtente.xhtml"; } /** * Get GruppoCorso entity by File * */ @Transactional public GruppoCorso getFileGruppoCorso(Integer idKey) { return fileDAO.findFileByPrimaryKey(idKey).getGruppoCorso(); } /** * Select the File entity for display allowing the user to confirm that they would like to delete the entity * */ @Transactional public String confirmDeleteFile(Integer idKey) { file = fileDAO.findFileByPrimaryKey(idKey); return "/jsf/file/deleteFile.xhtml"; } /** * Edit an existing Commento entity * */ @Transactional public String editFileCommentos(Integer file_id, Integer related_comment os_id) { commentos = commentoDAO.findCommentoByPrimaryKey(related_comment os_id, -1, -1); return "/jsf/file/commentos/editCommentos.xhtml"; } /** * Save an existing Commento entity * */ public void saveFileCommentos(Utente utente,String commento) { if(!commento.equals("")){ Commento comm= new Commento(); comm.setCorpo(commento); comm.setDataCommento(Calendar.getInstance()); comm.setUtente(utente); System.out.println("Sto stampando all'interno di FileComponent i l valore di file: "+file); listaFile.remove(file); this.file=fileService.saveFileCommentos(file.getId(), comm); listaFile.add(this.file); listaCommenti=new ArrayList<Commento>(commentoDAO.findCommentoBy IdFile(file.getId())); } }

/** * Delete an existing File entity * */ @Transactional public void deleteFile() { if(file!=null){ listaFile.remove(file); fileService.deleteFile(file); } } /** * Create a new Utente entity * */ @Transactional public String newFileUtente(Integer file_id) { utente = new Utente(); return "/jsf/file/utente/createUtente.xhtml"; } /** * Getter for variable File * */ @Transactional public Commento getCommentos() { return commentos; } /** * Delete an existing GruppoCorso entity * */ @Transactional public String deleteFileGruppoCorso(Integer file_id, Integer related_gru ppocorso_id) { file = fileService.deleteFileGruppoCorso(file_id, related_gruppo corso_id); return "/jsf/file/viewFile.xhtml"; } /** * Select the child GruppoCorso entity for display allowing the user to confirm that they would like to delete the entity * */ @Transactional public String confirmDeleteFileGruppoCorso(Integer file_id, Integer rela ted_gruppocorso_id) { gruppocorso = gruppoCorsoDAO.findGruppoCorsoByPrimaryKey(related _gruppocorso_id); return "/jsf/file/gruppocorso/deleteGruppoCorso.xhtml"; } /**

* Save an existing File entity * @throws IOException * */ @Transactional public void saveFile() throws IOException { commentos=new Commento(); FacesContext fc = FacesContext.getCurrentInstance(); ExternalContext ec = fc.getExternalContext(); String path=ec.getRealPath(""); String sep=System.getProperty("file.separator"); int i; String pathFin=""; String dir=""; if(path.contains(".metadata"+sep+".plugins"+sep+"org.eclipse.wst .server.core")){ i=path.indexOf("."); pathFin=path.substring(0, i); dir=pathFin+"community"+sep; } System.out.println("Stampo il path prima di gruppoCorso: "+dir+" WebContent"+sep+"FileRepository"+sep); pathFin=dir+"WebContent"+sep+"FileRepository"+sep+gruppocorso.ge tCorsoDiLaurea().getCorsoLaurea()+sep+gruppocorso.getCorso().getDenominazione()+ sep+gruppocorso.getAnno(); System.out.println("PATH= "+pathFin); try { FileOutputStream fileOutputStream = new FileOutputStream(pathFin+sep +event.getFile().getFileName()); byte[] buffer = new byte[1000000]; int bulk; InputStream inputStream = event.getFile().getInputstream(); while (true) { bulk = inputStream.read(buffer); if (bulk < 0) { break; } fileOutputStream.write(buffer, 0, bulk); fileOutputStream.flush(); } fileOutputStream.close(); inputStream.close(); FacesMessage msg = new FacesMessage("Il file", event.getFile().g etFileName() + " stato aggiunto con successo."); FacesContext.getCurrentInstance().addMessage(null, msg); } catch (IOException e) { e.printStackTrace(); FacesMessage error = new FacesMessage("Errore nel salvataggio del Fi le!"); FacesContext.getCurrentInstance().addMessage(null, error); } //SCRITTURA DEL FILE NEL DB upFile = new it.unisannio.community.domain.File(); upFile.setNome(event.getFile().getFileName()); upFile.setDataFile(Calendar.getInstance()); upFile.setGruppoCorso(gruppocorso);

upFile.setPath("FileRepository"+sep+gruppocorso.getCorsoDiLaurea().getCo rsoLaurea()+sep+gruppocorso.getCorso().getDenominazione()+sep+gruppocorso.getAnn o()+sep+event.getFile().getFileName()); upFile.setDescrizione(descrizione); upFile.setTipo(event.getFile().getFileName().substring((event.getFile(). getFileName().lastIndexOf('.')+1), event.getFile().getFileName().length())); //Da INSERIRE L'ID DELL'UTENTE CHE EFFETTUA L'UPLOAD utente = utenteDAO.findUtenteById(1); upFile.setUtente(utente); upFile=fileDAO.merge(upFile); IndexedDoc id=new IndexedDoc(); id.setName(event.getFile().getFileName()); id.setTag(upFile.getDescrizione()); id.setGroup(gruppocorso.getNome()); id.setIdDoc(upFile.getId()); id.setType(upFile.getTipo()); if (id.getType().equalsIgnoreCase("pdf")){ Pdf2txt extractor = new Pdf2txt(dir+"WebContent"+sep+upF ile.getPath()); String testo = extractor.pdf2txt(); id.setContent(testo); } else if (id.getType().equalsIgnoreCase("txt")) { Text2txt txt = new Text2txt(dir+"WebContent"+sep+upFile. getPath()); String testo = txt.text2text(); id.setContent(testo); } else if (id.getType().equalsIgnoreCase("ppt")) { MicrosoftExtractor me = new MicrosoftExtractor(dir+"WebC ontent"+sep+upFile.getPath()); String testo = me.ppt2text(); id.setContent(testo); } else if (id.getType().equalsIgnoreCase("doc")) { MicrosoftExtractor me = new MicrosoftExtractor(dir+"WebC ontent"+sep+upFile.getPath()); String testo = me.doc2text(); id.setContent(testo); } else id.setContent(""); Engine eng=new Engine(dir); Indexer inx=new Indexer(eng); inx.indexDoc(id); Set<Commento> listComm= new HashSet<Commento>(); upFile.setCommentos(listComm); listaFile.add(upFile); } /** * Edit an existing File entity * */ @Transactional public String editFile(Integer idKey) { file = fileDAO.findFileByPrimaryKey(idKey); return "/jsf/file/editFile.xhtml";

} /** * Getter for variable File * */ @Transactional public Utente getUtente() { return utente; } /** * Save an existing Utente entity * */ @Transactional public String saveFileUtente(Integer file_id, Utente utente) { file = fileService.saveFileUtente(file_id, utente); return "/jsf/file/viewFile.xhtml"; } /** * Select an existing File entity * @throws FileNotFoundException * */ @Transactional public String selectFile(Integer idKey) throws FileNotFoundException { file = fileDAO.findFileByPrimaryKey(idKey); InputStream stream = new FileInputStream(file.getPath()); String mimeType=""; if(file.getTipo().equals("jpg")) { mimeType="image/jpg"; } else if(file.getTipo().equals("jpeg")) { mimeType="image/jpeg"; } else if(file.getTipo().equals("gif")) { mimeType="image/gif"; } else if(file.getTipo().equals("doc")) { mimeType="application/msword"; } else if(file.getTipo().equals("ppt")) { mimeType="application/vnd.ms-powerpoin"; } else if(file.getTipo().equals("pdf")) { mimeType="application/pdf"; } else if(file.getTipo().equals("txt")) { mimeType="text/plain"; } downloadableFile = new DefaultStreamedContent(stream, mimeType, file.getNome()); System.out.println(downloadableFile); return "/jsf/file/viewFile.xhtml"; }

/** * View an existing Utente entity * */ @Transactional public String selectFileUtente(Integer file_id, Integer related_utente_i d) { utente = utenteDAO.findUtenteByPrimaryKey(related_utente_id, -1, -1); return "/jsf/file/utente/viewUtente.xhtml"; } /** * Delete an existing Commento entity * */ @Transactional public String deleteFileCommentos(Integer file_id, Integer related_comme ntos_id) { file = fileService.deleteFileCommentos(file_id, related_commento s_id); return "/jsf/file/viewFile.xhtml"; } /** * View an existing GruppoCorso entity * */ @Transactional public String selectFileGruppoCorso(Integer file_id, Integer related_gru ppocorso_id) { gruppocorso = gruppoCorsoDAO.findGruppoCorsoByPrimaryKey(related _gruppocorso_id, -1, -1); return "/jsf/file/gruppocorso/viewGruppoCorso.xhtml"; } @Override public void handleFileUpload(FileUploadEvent event) throws IOException { // TODO Auto-generated method stub FacesContext fc = FacesContext.getCurrentInstance(); ExternalContext ec = fc.getExternalContext(); String path=ec.getRealPath(""); String sep=System.getProperty("file.separator"); int i; String pathFin=""; String dir=""; if(path.contains(".metadata"+sep+".plugins"+sep+"org.eclipse.wst .server.core")){ i=path.indexOf("."); pathFin=path.substring(0, i); dir=pathFin+"community"+sep; } pathFin=dir+"WebContent"+sep+"FileRepository"+sep+gruppocorso.ge tCorsoDiLaurea().getCorsoLaurea()+sep+gruppocorso.getCorso().getDenominazione()+ sep+gruppocorso.getAnno(); try {

FileOutputStream fileOutputStream = new FileOutputStream(pathFin+sep +event.getFile().getFileName()); byte[] buffer = new byte[100000000]; int bulk; InputStream inputStream = event.getFile().getInputstream(); while (true) { bulk = inputStream.read(buffer); if (bulk < 0) { break; } fileOutputStream.write(buffer, 0, bulk); fileOutputStream.flush(); } fileOutputStream.close(); inputStream.close(); FacesMessage msg = new FacesMessage("Il file", event.getFile().g etFileName() + " stato aggiunto con successo."); FacesContext.getCurrentInstance().addMessage(null, msg); } catch (IOException e) { e.printStackTrace(); FacesMessage error = new FacesMessage("Errore nel salvataggio del Fi le!"); FacesContext.getCurrentInstance().addMessage(null, error); } //SCRITTURA DEL FILE NEL DB upFile = new it.unisannio.community.domain.File(); upFile.setNome(event.getFile().getFileName()); upFile.setDataFile(Calendar.getInstance()); upFile.setGruppoCorso(gruppocorso); upFile.setPath(pathFin+sep+event.getFile().getFileName()); upFile.setDescrizione(gruppocorso.getNome() + " " + gruppocorso.getAnno( )+event.getFile().getFileName()); upFile.setTipo(event.getFile().getFileName().substring((event.getFile(). getFileName().lastIndexOf('.')+1), event.getFile().getFileName().length())); //Da INSERIRE L'ID DELL'UTENTE CHE EFFETTUA L'UPLOAD utente = utenteDAO.findUtenteById(1); upFile.setUtente(utente); fileService.saveFile(upFile); IndexedDoc id=new IndexedDoc(); id.setName(event.getFile().getFileName()); id.setTag(upFile.getDescrizione()); id.setGroup(gruppocorso.getNome()); id.setIdDoc(upFile.getId()); id.setType(upFile.getTipo()); if (id.getType().equalsIgnoreCase("pdf")){ Pdf2txt extractor = new Pdf2txt(upFile.getPath()); String testo = extractor.pdf2txt(); id.setContent(testo); } else if (id.getType().equalsIgnoreCase("txt")) { Text2txt txt = new Text2txt(upFile.getPath()); String testo = txt.text2text(); id.setContent(testo);

} else if (id.getType().equalsIgnoreCase("ppt")) { MicrosoftExtractor me = new MicrosoftExtractor(upFile.ge tPath()); String testo = me.ppt2text(); id.setContent(testo); } else if (id.getType().equalsIgnoreCase("doc")) { MicrosoftExtractor me = new MicrosoftExtractor(upFile.ge tPath()); String testo = me.doc2text(); id.setContent(testo); } else id.setContent(""); Engine eng=new Engine(dir); Indexer inx=new Indexer(eng); inx.indexDoc(id); //System.out.println("TUTTO OK FILE UPLOAD TAG="+this.tag+"Content type= "+event.getFile().getContentType()); } public StreamedContent getDownloadableFile() { return downloadableFile; } public void setDownloadableFile(StreamedContent file) { this.downloadableFile = file; } @Override public void setTag(String tag) { // TODO Auto-generated method stub this.tag=tag; } @Override public String getTag() { // TODO Auto-generated method stub return this.tag; } @Override public void setDescrizione(String descrizione) { // TODO Auto-generated method stub this.descrizione=descrizione; } @Override public String getDescrizione() { // TODO Auto-generated method stub return descrizione; } @Transactional public String selectFiles(Integer idGruppoCorso) { commentos=new Commento(); file=new File(); gruppocorso = gruppoCorsoDAO.findGruppoCorsoByPrimaryKey(idGrupp oCorso);

listaFile=new java.util.ArrayList<File>(fileDAO.findFileByGruppo (this.gruppocorso.getId())); return "/jsf/gruppocorso/listFiles.xhtml"; } public void setListaFile(List<File> listaFile) { this.listaFile = listaFile; } public List<File> getListaFile() { return listaFile; } public void setCommentos(Commento commentos) { this.commentos = commentos; } public void setFile(File file) { this.file = file; } public List<Commento> getListaCommenti() { return listaCommenti; } public void setListaCommenti(List<Commento> listaCommenti) { this.listaCommenti = listaCommenti; } public void listaCommenti(){ System.out.println("Controllo se effettuo questa chiamata, sto i n listaCommenti!!!"); System.out.println("Stampo il valore di idFile: "+file.getId()); listaCommenti=new ArrayList<Commento>(commentoDAO.findCommentoBy IdFile(file.getId())); System.out.println("Stampo il valore di listaCommenti: "+listaCo mmenti); } }

Potrebbero piacerti anche