Search in sources :

Example 1 with Attachment

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

the class ApplicantBacking method deleteUploadedFile.

public void deleteUploadedFile(ActionEvent actionEvent) {
    try {
        List<Attachment> attachments = applicant.getAttachments();
        int attachmentIndex = applicantView.getAttachmentIndex();
        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 : Attachment(com.liferay.faces.demos.applicant.richfaces.facelets.dto.Attachment) IOException(java.io.IOException)

Example 2 with Attachment

use of com.liferay.faces.demos.applicant.richfaces.facelets.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());
        logger.debug("comments=" + applicant.getComments());
        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.richfaces.facelets.dto.Attachment) IOException(java.io.IOException)

Example 3 with Attachment

use of com.liferay.faces.demos.applicant.richfaces.facelets.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.richfaces.facelets.dto.Attachment) Applicant(com.liferay.faces.demos.applicant.richfaces.facelets.dto.Applicant) File(java.io.File) UploadedFile(com.liferay.faces.bridge.model.UploadedFile) PostConstruct(javax.annotation.PostConstruct)

Example 4 with Attachment

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

the class ApplicantBacking method handleFileUpload.

public void handleFileUpload(FileUploadEvent fileUploadEvent) throws Exception {
    org.richfaces.model.UploadedFile uploadedFile = fileUploadEvent.getUploadedFile();
    FacesContext facesContext = FacesContext.getCurrentInstance();
    File attachmentDir = attachmentManager.getAttachmentDir(facesContext);
    if (!attachmentDir.exists()) {
        attachmentDir.mkdir();
    }
    File copiedFile = new File(attachmentDir, uploadedFile.getName());
    try {
        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);
    }
}
Also used : FacesContext(javax.faces.context.FacesContext) Attachment(com.liferay.faces.demos.applicant.richfaces.facelets.dto.Attachment) IOException(java.io.IOException) File(java.io.File) UploadedFile(com.liferay.faces.bridge.model.UploadedFile)

Aggregations

Attachment (com.liferay.faces.demos.applicant.richfaces.facelets.dto.Attachment)4 IOException (java.io.IOException)3 FacesContext (javax.faces.context.FacesContext)3 UploadedFile (com.liferay.faces.bridge.model.UploadedFile)2 File (java.io.File)2 Applicant (com.liferay.faces.demos.applicant.richfaces.facelets.dto.Applicant)1 PostConstruct (javax.annotation.PostConstruct)1