package com.nsharmon.iTranslate.beans;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.persistence.EntityManager;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.commons.io.IOUtils;
import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.UploadedFile;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;
import com.google.appengine.api.datastore.Blob;
import com.nsharmon.iTranslate.dao.EMF;
import com.nsharmon.iTranslate.dao.entities.TranslationJob;
import com.nsharmon.iTranslate.dao.entities.User;
import com.nsharmon.iTranslate.model.CollectionBean;
@ManagedBean(name="tjBean")
@SessionScoped
public class TranslationJobBean extends CollectionBean<TranslationJob> {
@ManagedProperty("#{identity}")
private Identity identity;
public TranslationJobBean() {
super(TranslationJob.class);
}
public void setIdentity(Identity identity) {
this.identity = identity;
if(identity != null && identity.getCurrentUser() != null) {
setParent(identity.getCurrentUser());
}
}
protected List<TranslationJob> loadCollection() {
List<TranslationJob> conventions = null;
if(identity != null) {
EntityManager entityManager = EMF.createEntityManager();
conventions = new ArrayList<TranslationJob>();
try {
User currentUser = identity.getCurrentUser();
if(currentUser != null) {
conventions = currentUser.getTranslationJobs();
}
} finally {
entityManager.close();
}
}
return conventions;
}
public void onNewAction(ActionEvent event) {
newObject();
}
public void onSaveAction(ActionEvent event) {
if(identity != null) {
if(isValid()) {
getSelected().setCreateDate(new Date());
getSelected().setLastModified(getSelected().getCreateDate());
saveSelected();
} else {
FacesContext.getCurrentInstance().validationFailed();
}
}
}
public void onEditAction(ActionEvent evt) {
}
public void onRemoveAction(ActionEvent evt) {
if(identity != null) {
removeSelected();
}
}
private boolean isValid() {
boolean isValid = true;
if(getSelected() == null) {
isValid = false;
} else if (getSelected().getTranslationFile() == null) {
isValid = false;
onFail("error.missingTranslationFile");
}
return isValid;
}
public void onFileUpload(FileUploadEvent event) {
onHandleFileUpload(event.getFile());
}
public void onHandleFileUpload(UploadedFile file) {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
Document dom = null;
boolean uploadSuccess = false;
InputStream is = null;
try {
//Using factory get an instance of document builder
DocumentBuilder db = dbf.newDocumentBuilder();
//parse using builder to get DOM representation of the XML file
is = file.getInputstream();
dom = db.parse(is);
// valid xml - now attempt to process it
processTXML(dom);
// If we're here, processing completed successfully.
// Save file information to blob.
getSelected().setTranslationFile(new Blob(IOUtils.toByteArray(is)));
uploadSuccess = true;
onSuccess("success.fileUploaded", file.getFileName());
}catch(ParserConfigurationException pce) {
pce.printStackTrace();
}catch(SAXException se) {
se.printStackTrace();
}catch(IOException ioe) {
ioe.printStackTrace();
} finally {
if(is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
if(!uploadSuccess) {
getSelected().setOriginLanguage(null);
getSelected().setDestinationLanguage(null);
onFail("error.upload", file.getFileName());
}
}
private void processTXML(Document dom) {
Element docEle = dom.getDocumentElement();
String fileVersion = docEle.getAttribute("version");
TranslationJob job = getSelected();
job.processTXML(docEle, fileVersion);
}
}