All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.primefaces.component.export.DataExporters Maven / Gradle / Ivy

There is a newer version: 14.0.0
Show newest version
/*
 * The MIT License
 *
 * Copyright (c) 2009-2023 PrimeTek Informatics
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.primefaces.component.export;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.FacesException;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;

import org.primefaces.context.PrimeApplicationContext;

public final class DataExporters {

    private static final Logger LOGGER = Logger.getLogger(DataExporters.class.getName());

    private DataExporters() {
        // NOOP
    }

    public static  Exporter get(Class targetClass, String type) {
        PrimeApplicationContext primeAppContext = PrimeApplicationContext.getCurrentInstance(FacesContext.getCurrentInstance());

        // since users might defined their own impl of components (e.g MyDataTable, MyTreeTable etc.)
        // retrieve the most eligible built-in component class first (e.g DataTable, TreeTable) to get corresponding exporters
        Class resolvedTargetClass = primeAppContext.getExporters().keySet().stream()
                .filter(k -> k.isAssignableFrom(targetClass))
                .findFirst()
                .orElseThrow(() -> new UnsupportedOperationException(
                        "Component " + targetClass.getName() + " not supported. Use DataExporters#register()"));

        Map>> supportedExporters = primeAppContext.getExporters().get(resolvedTargetClass);

        String newType = type.toLowerCase();
        Class> exportClass = Optional.ofNullable(supportedExporters.get(newType))
                .orElseThrow(() -> new UnsupportedOperationException(
                        "Exporter for " + targetClass.getName() + " of type '" + newType + "' is not supported. Use DataExporters#register()"));

        try {
            return (Exporter) exportClass.getConstructor().newInstance();
        }
        catch (ReflectiveOperationException e) {
            throw new FacesException(e);
        }
    }

    public static > void register(Class targetClass, Class exporter, String type) {
        PrimeApplicationContext primeAppContext = PrimeApplicationContext.getCurrentInstance(FacesContext.getCurrentInstance());
        Map>> supportedExporters = primeAppContext.getExporters().computeIfAbsent(targetClass, o -> new HashMap<>());

        String newType = type.toLowerCase();
        Class> old = supportedExporters.put(newType, exporter);
        if (old != null) {
            LOGGER.log(Level.INFO, "Exporter {0} of type {1} has been replaced by {2}", new Object[]{old.getName(), newType, exporter.getName()});
        }
        else {
            LOGGER.log(Level.INFO, "New exporter {0} of type {1} has been registered", new Object[]{exporter.getName(), newType});
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy