Search in sources :

Example 6 with UploadedFile

use of com.liferay.faces.bridge.model.UploadedFile in project liferay-faces-bridge-impl by liferay.

the class ContextMapFactoryImpl method getUploadedFileMap.

@Override
public Map<String, List<UploadedFile>> getUploadedFileMap(PortletRequest portletRequest) {
    Map<String, List<UploadedFile>> bridgeUploadedFileMap = Collections.emptyMap();
    MultiPartFormData multiPartFormData = (MultiPartFormData) portletRequest.getAttribute(MULTIPART_FORM_DATA_FQCN);
    if (multiPartFormData != null) {
        Map<String, List<com.liferay.faces.util.model.UploadedFile>> uploadedFileMap = multiPartFormData.getUploadedFileMap();
        if (uploadedFileMap != null) {
            PortletSession portletSession = portletRequest.getPortletSession(true);
            PortletContext portletContext = portletSession.getPortletContext();
            Map<String, Object> applicationScopeMap = getApplicationScopeMap(portletContext, true);
            ExternalContext externalContext = new ExternalContextProductImpl(applicationScopeMap);
            ProductFactory productFactory = (ProductFactory) FactoryExtensionFinder.getFactory(externalContext, ProductFactory.class);
            Product PRIMEFACES = productFactory.getProductInfo(Product.Name.PRIMEFACES);
            boolean primeFacesDetected = PRIMEFACES.isDetected();
            int primeFacesMajorVersion = PRIMEFACES.getMajorVersion();
            bridgeUploadedFileMap = new HashMap<String, List<UploadedFile>>(uploadedFileMap.size());
            Set<Map.Entry<String, List<com.liferay.faces.util.model.UploadedFile>>> entrySet = uploadedFileMap.entrySet();
            for (Map.Entry<String, List<com.liferay.faces.util.model.UploadedFile>> mapEntry : entrySet) {
                List<com.liferay.faces.util.model.UploadedFile> uploadedFileList = mapEntry.getValue();
                if (uploadedFileList != null) {
                    List<UploadedFile> bridgeUploadedFileList = new ArrayList<UploadedFile>(uploadedFileList.size());
                    for (com.liferay.faces.util.model.UploadedFile uploadedFile : uploadedFileList) {
                        bridgeUploadedFileList.add(new UploadedFileBridgeImpl(uploadedFile, primeFacesDetected, primeFacesMajorVersion));
                    }
                    bridgeUploadedFileMap.put(mapEntry.getKey(), bridgeUploadedFileList);
                }
            }
        }
    }
    return bridgeUploadedFileMap;
}
Also used : ProductFactory(com.liferay.faces.util.product.ProductFactory) ArrayList(java.util.ArrayList) Product(com.liferay.faces.util.product.Product) UploadedFileBridgeImpl(com.liferay.faces.bridge.model.internal.UploadedFileBridgeImpl) ExternalContext(javax.faces.context.ExternalContext) ArrayList(java.util.ArrayList) List(java.util.List) MultiPartFormData(com.liferay.faces.util.context.map.MultiPartFormData) UploadedFile(com.liferay.faces.bridge.model.UploadedFile) PortletSession(javax.portlet.PortletSession) PortletContext(javax.portlet.PortletContext) HashMap(java.util.HashMap) Map(java.util.Map) FacesRequestParameterMap(com.liferay.faces.util.context.map.FacesRequestParameterMap)

Example 7 with UploadedFile

use of com.liferay.faces.bridge.model.UploadedFile in project liferay-faces-bridge-impl by liferay.

the class HtmlInputFileRenderer method decode.

@Override
public void decode(FacesContext facesContext, UIComponent uiComponent) {
    HtmlInputFile htmlInputFile = (HtmlInputFile) uiComponent;
    Map<String, List<UploadedFile>> uploadedFileMap = InputFileRenderer.getUploadedFileMap(facesContext);
    if (!uploadedFileMap.isEmpty()) {
        String clientId = uiComponent.getClientId(facesContext);
        if (uploadedFileMap.containsKey(clientId)) {
            List<UploadedFile> uploadedFiles = uploadedFileMap.get(clientId);
            if ((uploadedFiles != null) && (uploadedFiles.size() > 0)) {
                Part part = new PartFileUploadAdapterImpl(uploadedFiles.get(0), clientId);
                htmlInputFile.setTransient(true);
                htmlInputFile.setSubmittedValue(part);
            } else {
                htmlInputFile.setSubmittedValue(new PartEmptyImpl());
            }
        }
    }
    RendererUtil.decodeClientBehaviors(facesContext, uiComponent);
}
Also used : UploadedFile(com.liferay.faces.bridge.model.UploadedFile) HtmlInputFile(javax.faces.component.html.HtmlInputFile) Part(javax.servlet.http.Part) PartFileUploadAdapterImpl(com.liferay.faces.bridge.renderkit.bridge.internal.PartFileUploadAdapterImpl) List(java.util.List)

Example 8 with UploadedFile

use of com.liferay.faces.bridge.model.UploadedFile 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)

Example 9 with UploadedFile

use of com.liferay.faces.bridge.model.UploadedFile in project liferay-faces-bridge-impl by liferay.

the class BridgeInputFileBackingBean method handleFileUpload.

public void handleFileUpload(FileUploadEvent fileUploadEvent) {
    List<UploadedFile> uploadedFiles = bridgeInputFileModelBean.getUploadedFiles();
    UploadedFile uploadedFile = fileUploadEvent.getUploadedFile();
    if (uploadedFile.getStatus() == UploadedFile.Status.FILE_SAVED) {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        FacesMessage facesMessage = new FacesMessage("Received fileUploadEvent for file named '" + uploadedFile.getName() + "' in the " + fileUploadEvent.getPhaseId().toString() + " phase.");
        facesContext.addMessage(null, facesMessage);
        uploadedFiles.add(uploadedFile);
        logger.debug("Received fileName=[{0}] absolutePath=[{1}]", uploadedFile.getName(), uploadedFile.getAbsolutePath());
    } else {
        logger.error("Failed to receive uploaded file due to error status=[{0}] message=[{1}]", uploadedFile.getStatus(), uploadedFile.getMessage());
    }
}
Also used : FacesContext(javax.faces.context.FacesContext) UploadedFile(com.liferay.faces.bridge.model.UploadedFile) FacesMessage(javax.faces.application.FacesMessage)

Example 10 with UploadedFile

use of com.liferay.faces.bridge.model.UploadedFile in project liferay-faces-bridge-impl by liferay.

the class BridgeInputFileBackingBean method deleteUploadedFile.

public void deleteUploadedFile(ActionEvent actionEvent) {
    UICommand uiCommand = (UICommand) actionEvent.getComponent();
    String fileId = (String) uiCommand.getValue();
    try {
        List<UploadedFile> uploadedFiles = bridgeInputFileModelBean.getUploadedFiles();
        UploadedFile uploadedFileToDelete = null;
        for (UploadedFile uploadedFile : uploadedFiles) {
            if (uploadedFile.getId().equals(fileId)) {
                uploadedFileToDelete = uploadedFile;
                break;
            }
        }
        if (uploadedFileToDelete != null) {
            uploadedFileToDelete.delete();
            uploadedFiles.remove(uploadedFileToDelete);
            logger.debug("Deleted file=[{0}]", uploadedFileToDelete.getName());
        }
    } catch (Exception e) {
        logger.error(e);
    }
}
Also used : UploadedFile(com.liferay.faces.bridge.model.UploadedFile) UICommand(javax.faces.component.UICommand)

Aggregations

UploadedFile (com.liferay.faces.bridge.model.UploadedFile)10 List (java.util.List)6 ArrayList (java.util.ArrayList)3 Map (java.util.Map)3 FacesContext (javax.faces.context.FacesContext)3 Part (javax.servlet.http.Part)3 InputFile (com.liferay.faces.bridge.component.inputfile.InputFile)2 File (java.io.File)2 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 ExternalContext (javax.faces.context.ExternalContext)2 ContextMapFactory (com.liferay.faces.bridge.context.map.internal.ContextMapFactory)1 FileUploadEvent (com.liferay.faces.bridge.event.FileUploadEvent)1 UploadedFileBridgeImpl (com.liferay.faces.bridge.model.internal.UploadedFileBridgeImpl)1 PartFileUploadAdapterImpl (com.liferay.faces.bridge.renderkit.bridge.internal.PartFileUploadAdapterImpl)1 Attachment (com.liferay.faces.demos.applicant.jsf.jsp.dto.Attachment)1 Attachment (com.liferay.faces.demos.applicant.richfaces.facelets.dto.Attachment)1 FacesRequestParameterMap (com.liferay.faces.util.context.map.FacesRequestParameterMap)1 MultiPartFormData (com.liferay.faces.util.context.map.MultiPartFormData)1 Product (com.liferay.faces.util.product.Product)1