Search in sources :

Example 1 with Attachment

use of com.liferay.faces.demos.applicant.jsf.jsp.dto.Attachment in project liferay-faces-bridge-impl by liferay.

the class ApplicantBacking method uploadAttachments.

@SuppressWarnings("unchecked")
public void uploadAttachments(ActionEvent actionEvent) {
    FacesContext facesContext = FacesContext.getCurrentInstance();
    File attachmentDir = attachmentManager.getAttachmentDir(facesContext);
    if (!attachmentDir.exists()) {
        attachmentDir.mkdir();
    }
    try {
        List<UploadedFile> attachments1 = (List<UploadedFile>) attachment1.getValue();
        if (attachments1 != null) {
            for (UploadedFile uploadedFile : attachments1) {
                File copiedFile = new File(attachmentDir, uploadedFile.getName());
                uploadedFile.write(copiedFile.getAbsolutePath());
                uploadedFile.delete();
                logger.debug("Received fileName=[{0}] absolutePath=[{1}]", copiedFile.getName(), copiedFile.getAbsolutePath());
            }
        }
        List<UploadedFile> attachments2 = (List<UploadedFile>) attachment2.getValue();
        if (attachments2 != null) {
            for (UploadedFile uploadedFile : attachments2) {
                File copiedFile = new File(attachmentDir, uploadedFile.getName());
                uploadedFile.write(copiedFile.getAbsolutePath());
                uploadedFile.delete();
                logger.debug("Received fileName=[{0}] absolutePath=[{1}]", copiedFile.getName(), copiedFile.getAbsolutePath());
            }
        }
        List<UploadedFile> attachments3 = (List<UploadedFile>) attachment3.getValue();
        if (attachments3 != null) {
            for (UploadedFile uploadedFile : attachments3) {
                File copiedFile = new File(attachmentDir, uploadedFile.getName());
                uploadedFile.write(copiedFile.getAbsolutePath());
                uploadedFile.delete();
                logger.debug("Received fileName=[{0}] absolutePath=[{1}]", copiedFile.getName(), copiedFile.getAbsolutePath());
            }
        }
        List<Attachment> attachments = attachmentManager.getAttachments(attachmentDir);
        applicant.setAttachments(attachments);
    } catch (IOException e) {
        logger.error(e);
    }
    applicantView.setFileUploaderRendered(false);
}
Also used : FacesContext(javax.faces.context.FacesContext) UploadedFile(com.liferay.faces.bridge.model.UploadedFile) List(java.util.List) Attachment(com.liferay.faces.demos.applicant.jsf.jsp.dto.Attachment) IOException(java.io.IOException) File(java.io.File) UploadedFile(com.liferay.faces.bridge.model.UploadedFile) InputFile(com.liferay.faces.bridge.component.inputfile.InputFile)

Example 2 with Attachment

use of com.liferay.faces.demos.applicant.jsf.jsp.dto.Attachment in project liferay-faces-bridge-impl by liferay.

the class ApplicantBacking method postConstruct.

@PostConstruct
public void postConstruct() {
    applicant = new Applicant();
    FacesContext facesContext = FacesContext.getCurrentInstance();
    File attachmentDir = attachmentManager.getAttachmentDir(facesContext);
    List<Attachment> attachments = attachmentManager.getAttachments(attachmentDir);
    applicant.setAttachments(attachments);
}
Also used : FacesContext(javax.faces.context.FacesContext) Attachment(com.liferay.faces.demos.applicant.jsf.jsp.dto.Attachment) Applicant(com.liferay.faces.demos.applicant.jsf.jsp.dto.Applicant) File(java.io.File) UploadedFile(com.liferay.faces.bridge.model.UploadedFile) InputFile(com.liferay.faces.bridge.component.inputfile.InputFile) PostConstruct(javax.annotation.PostConstruct)

Example 3 with Attachment

use of com.liferay.faces.demos.applicant.jsf.jsp.dto.Attachment in project liferay-faces-bridge-impl by liferay.

the class ApplicantBacking method submit.

public String submit() {
    if (logger.isDebugEnabled()) {
        logger.debug("firstName=" + applicant.getFirstName());
        logger.debug("lastName=" + applicant.getLastName());
        logger.debug("emailAddress=" + applicant.getEmailAddress());
        logger.debug("phoneNumber=" + applicant.getPhoneNumber());
        logger.debug("dateOfBirth=" + applicant.getDateOfBirth());
        logger.debug("city=" + applicant.getCity());
        logger.debug("provinceId=" + applicant.getProvinceId());
        logger.debug("postalCode=" + applicant.getPostalCode());
        List<Attachment> attachments = applicant.getAttachments();
        for (Attachment attachment : attachments) {
            logger.debug("attachment=[{0}]", attachment.getName());
        }
    }
    // Delete the uploaded files.
    try {
        List<Attachment> attachments = applicant.getAttachments();
        for (Attachment attachment : attachments) {
            attachment.getFile().delete();
            logger.debug("Deleted file=[{0}]", attachment.getName());
        }
        // Store the applicant's first name in JSF 2 Flash Scope so that it can be picked up
        // for use inside of confirmation.xhtml
        FacesContext facesContext = FacesContext.getCurrentInstance();
        facesContext.getExternalContext().getFlash().put("firstName", applicant.getFirstName());
        applicant.clearProperties();
        return "success";
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
        FacesContextHelperUtil.addGlobalUnexpectedErrorMessage();
        return "failure";
    }
}
Also used : FacesContext(javax.faces.context.FacesContext) Attachment(com.liferay.faces.demos.applicant.jsf.jsp.dto.Attachment) IOException(java.io.IOException)

Example 4 with Attachment

use of com.liferay.faces.demos.applicant.jsf.jsp.dto.Attachment in project liferay-faces-bridge-impl by liferay.

the class ApplicantBacking method deleteUploadedFile.

public void deleteUploadedFile(ActionEvent actionEvent) {
    FacesContext facesContext = FacesContext.getCurrentInstance();
    ExternalContext externalContext = facesContext.getExternalContext();
    Map<String, String> requestParameterMap = externalContext.getRequestParameterMap();
    int attachmentIndex = Integer.valueOf(requestParameterMap.get("fileId"));
    try {
        List<Attachment> attachments = applicant.getAttachments();
        Attachment attachmentToDelete = null;
        for (Attachment attachment : attachments) {
            if (attachment.getIndex() == attachmentIndex) {
                attachmentToDelete = attachment;
                break;
            }
        }
        if (attachmentToDelete != null) {
            attachmentToDelete.getFile().delete();
            attachments.remove(attachmentToDelete);
            logger.debug("Deleted file=[{0}]", attachmentToDelete.getName());
        }
    } catch (Exception e) {
        logger.error(e);
    }
}
Also used : FacesContext(javax.faces.context.FacesContext) ExternalContext(javax.faces.context.ExternalContext) Attachment(com.liferay.faces.demos.applicant.jsf.jsp.dto.Attachment) IOException(java.io.IOException)

Aggregations

Attachment (com.liferay.faces.demos.applicant.jsf.jsp.dto.Attachment)4 FacesContext (javax.faces.context.FacesContext)4 IOException (java.io.IOException)3 InputFile (com.liferay.faces.bridge.component.inputfile.InputFile)2 UploadedFile (com.liferay.faces.bridge.model.UploadedFile)2 File (java.io.File)2 Applicant (com.liferay.faces.demos.applicant.jsf.jsp.dto.Applicant)1 List (java.util.List)1 PostConstruct (javax.annotation.PostConstruct)1 ExternalContext (javax.faces.context.ExternalContext)1