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

org.nuiton.topia.service.csv.EntityCsvModel Maven / Gradle / Ivy

The newest version!
package org.nuiton.topia.service.csv;

/*
 * #%L
 * ToPIA :: Service Csv
 * $Id$
 * $HeadURL$
 * %%
 * Copyright (C) 2004 - 2014 CodeLutin
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as 
 * published by the Free Software Foundation, either version 3 of the 
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Lesser Public License for more details.
 * 
 * You should have received a copy of the GNU General Lesser Public 
 * License along with this program.  If not, see
 * .
 * #L%
 */

import com.google.common.base.Function;
import com.google.common.collect.Maps;
import org.nuiton.topia.persistence.TopiaEntities;
import org.nuiton.topia.persistence.TopiaEntity;
import org.nuiton.topia.persistence.TopiaEntityEnum;
import org.nuiton.topia.persistence.metadata.TableMeta;
import org.nuiton.csv.ValueParserFormatter;
import org.nuiton.csv.ext.AbstractImportExportModel;
import org.nuiton.decorator.Decorator;

import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.Map;

/**
 * A model to import / export entities into csv files.
 *
 * @author Tony Chemit - [email protected]
 * @since 0.2
 */
public class EntityCsvModel extends AbstractImportExportModel {

    protected final TableMeta tableMeta;

    protected boolean useOrdinalForEnum;

    public static  EntityCsvModel newModel(
            char separator,
            TableMeta tableMeta) {
        return new EntityCsvModel(separator, tableMeta);
    }

    public static  EntityCsvModel newModel(
            char separator,
            TableMeta tableMeta,
            String idHeader) {
        return new EntityCsvModel(separator, tableMeta, idHeader);
    }

    @Override
    public E newEmptyInstance() {
        return (E) tableMeta.newEntity();
    }

    public void setUseOrdinalForEnum(boolean useOrdinalForEnum) {
        this.useOrdinalForEnum = useOrdinalForEnum;
    }

    public void addForeignKeyForExport(String propertyName,
                                       Class entityType) {

        Map universe = Collections.emptyMap();

        newColumnForExport(propertyName,
                           TopiaCsvCommons.newForeignKeyValue(entityType,
                                                              propertyName,
                                                              universe)
        );
    }

    public  void addDecoratedForeignKeyForExport(String headerName,
                                                    String propertyName,
                                                    Decorator decorator) {
        modelBuilder.newColumnForExport(
                headerName,
                propertyName,
                TopiaCsvCommons.newForeignKeyDecoratedValue(decorator));
    }

    public  void addForeignKeyForImport(String headerName,
                                                               String propertyName,
                                                               Class entityType,
                                                               Collection entities,
                                                               Function transform) {

        Map universe = Maps.uniqueIndex(entities, transform);

        newMandatoryColumn(headerName,
                           propertyName,
                           TopiaCsvCommons.newForeignKeyValue(entityType,
                                                              propertyName,
                                                              universe)
        );
    }

    public  void addForeignKeyForAssociationForImport(String headerName,
                                                                             String propertyName,
                                                                             Class entityType,
                                                                             Collection entities,
                                                                             Function transform) {

        Map universe = Maps.uniqueIndex(entities, transform);

        newMandatoryColumn(
                headerName,
                propertyName,
                TopiaCsvCommons.newForeignKeyValueAssociation(entityType,
                                                              propertyName,
                                                              universe)
        );
    }

    public  void addForeignKeyForImport(String propertyName,
                                                               Class entityType,
                                                               Collection entities) {

        Map universe = Maps.uniqueIndex(entities,
                                                   TopiaEntities.getTopiaIdFunction());

        newMandatoryColumn(propertyName,
                           TopiaCsvCommons.newForeignKeyValue(entityType,
                                                              propertyName,
                                                              universe)
        );
    }

    public  void addForeignKeyForImport(String propertyName,
                                                               Class entityType,
                                                               Map universe) {

        newMandatoryColumn(propertyName,
                           TopiaCsvCommons.newForeignKeyValue(entityType,
                                                              propertyName,
                                                              universe)
        );
    }
    public void addDefaultColumn(String propertyName, Class type) {
        addDefaultColumn(propertyName, propertyName, type);
    }

    public void addDefaultColumn(String headerName,
                                 String propertyName,
                                 Class type) {

        if (Date.class.equals(type)) {
            newColumnForImportExport(
                    headerName,
                    propertyName,
                    TopiaCsvCommons.DAY_TIME_SECOND_WITH_TIMESTAMP);
        } else if (double.class.equals(type)) {
            newColumnForImportExport(
                    headerName,
                    propertyName,
                    TopiaCsvCommons.DOUBLE_PRIMITIVE);
        } else if (Double.class.equals(type)) {
            newColumnForImportExport(
                    headerName,
                    propertyName,
                    TopiaCsvCommons.DOUBLE);
        } else if (long.class.equals(type)) {
            newColumnForImportExport(
                    headerName,
                    propertyName,
                    TopiaCsvCommons.PRIMITIVE_LONG);
        } else if (Long.class.equals(type)) {
            newColumnForImportExport(
                    headerName,
                    propertyName,
                    TopiaCsvCommons.LONG);
        } else if (float.class.equals(type)) {
            newColumnForImportExport(
                    headerName,
                    propertyName,
                    TopiaCsvCommons.PRIMITIVE_FLOAT);
        } else if (Float.class.equals(type)) {
            newColumnForImportExport(
                    headerName,
                    propertyName,
                    TopiaCsvCommons.FLOAT);
        } else if (int.class.equals(type)) {
            newColumnForImportExport(
                    headerName,
                    propertyName,
                    TopiaCsvCommons.PRIMITIVE_INTEGER);
        } else if (Integer.class.equals(type)) {
            newColumnForImportExport(
                    headerName,
                    propertyName,
                    TopiaCsvCommons.INTEGER);
        } else if (boolean.class.equals(type)) {
            newColumnForImportExport(
                    headerName,
                    propertyName,
                    TopiaCsvCommons.PRIMITIVE_BOOLEAN);
        } else if (Boolean.class.equals(type)) {
            newColumnForImportExport(
                    headerName,
                    propertyName,
                    TopiaCsvCommons.BOOLEAN);
        } else if (String.class.equals(type)) {
            newColumnForImportExport(
                    headerName,
                    propertyName);
        } else if (type.isEnum()) {

            Class enumType = (Class) type;
            ValueParserFormatter valueParserFormatter;
            if (useOrdinalForEnum) {
                valueParserFormatter =
                        TopiaCsvCommons.newEnumByOrdinalParserFormatter(enumType);
            } else {
                valueParserFormatter =
                        TopiaCsvCommons.newEnumByNameParserFormatter(enumType);
            }

            newColumnForImportExport(headerName,
                                     propertyName,
                                     valueParserFormatter);

        } else {

            throw new IllegalStateException(String.format(
                    "For header %s, property %s, no specific handler " +
                    "found for type %s", headerName, propertyName, type));
        }
    }

    protected EntityCsvModel(char separator, TableMeta tableMeta) {
        super(separator);
        this.tableMeta = tableMeta;
    }

    protected EntityCsvModel(char separator, TableMeta tableMeta,
                             String idHeader) {
        this(separator, tableMeta);
        newColumnForImportExport(idHeader, TopiaEntity.PROPERTY_TOPIA_ID);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy