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

org.nuiton.topia.service.csv.TopiaCsvCommons 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.collect.Lists;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.nuiton.topia.persistence.TopiaException;
import org.nuiton.topia.persistence.TopiaEntity;
import org.nuiton.util.StringUtil;
import org.nuiton.csv.Common;
import org.nuiton.csv.ValueFormatter;
import org.nuiton.csv.ValueParser;
import org.nuiton.csv.ValueParserFormatter;
import org.nuiton.decorator.Decorator;

import java.sql.Timestamp;
import java.text.ParseException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.Map;

/**
 * More useful method added to {@link Common}.
 *
 * @author Tony Chemit - [email protected]
 * @since 2.6.12
 */
public class TopiaCsvCommons extends Common {

    protected TopiaCsvCommons() {
        // no instance of this helper
    }

    public static final ValueParserFormatter DAY_TIME_SECOND_WITH_TIMESTAMP =
            new DateValue("dd/MM/yyyy HH:mm:ss") {

                @Override
                public Date parse(String value) throws ParseException {

                    Date parse = super.parse(value);
                    if (parse != null) {
                        parse = new Timestamp(parse.getTime());
                    }
                    return parse;
                }
            };

    public static final ValueParserFormatter DAY_TIME_SECOND_MILI_WITH_TIMESTAMP =
            new DateValue("dd/MM/yyyy HH:mm:ss.SSSS") {

                @Override
                public Date parse(String value) throws ParseException {

                    Date parse = super.parse(value);
                    if (parse != null) {
                        parse = new Timestamp(parse.getTime());
                    }
                    return parse;
                }
            };
    public static final AssociationValueParser ASSOCIATION_VALUE_PARSER = new AssociationValueParser();

    public static  ForeignKeyValue newForeignKeyValue(Class type, String propertyName, Map universe) {
        return new ForeignKeyValue(type, propertyName, universe);
    }

    public static  ForeignKeyValueForAssociation newForeignKeyValueAssociation(Class type, String propertyName, Map universe) {
        return new ForeignKeyValueForAssociation(type, propertyName, universe);
    }

    public static  ValueFormatter> newAssociationValueFormatter() {
        return new AssociationValueParserFormatter(null, null);
    }

    public static  ForeignKeyDecoratedValue newForeignKeyDecoratedValue(Decorator decorator) {
        return new ForeignKeyDecoratedValue(decorator);
    }

    /**
     * @author Tony Chemit - [email protected]
     * @since 2.6.12
     */
    public static class AssociationValueParser implements ValueParser {

        @Override
        public String[] parse(String value) throws ParseException {
            String[] ids = value.split("\\|");
            return ids;
        }
    }

    /**
     * @param 
     * @author Tony Chemit - [email protected]
     * @since 2.6.12
     */
    public static class AssociationValueParserFormatter implements ValueParserFormatter> {

        protected final Class entityType;

        protected final Map universe;

        public AssociationValueParserFormatter(
                Class entityType,
                Map universe) {
            this.entityType = entityType;
            this.universe = universe;
        }

        @Override
        public Collection parse(String value) throws ParseException {
            Collection result = Lists.newArrayList();
            if (StringUtils.isNotBlank(value)) {

                String[] ids = value.split("\\|");
                for (String id : ids) {
                    E association = universe.get(id);
                    association.setTopiaId(id);
                    result.add(association);
                }
            }
            return result;
        }

        @Override
        public String format(Collection e) {

            String value;
            if (CollectionUtils.isEmpty(e)) {
                value = "";
            } else {
                Collection ids = Lists.newArrayList();
                for (E e1 : e) {
                    ids.add(e1.getTopiaId());
                }
                value = StringUtil.join(ids, "|", true);
            }
            return value;
        }
    }

    /**
     * TODO
     *
     * @author Tony Chemit - [email protected]
     * @since 2.6.12
     */
    public static class ForeignKeyDecoratedValue implements ValueFormatter {

        protected final Decorator decorator;

        public ForeignKeyDecoratedValue(Decorator decorator) {
            this.decorator = decorator;
        }

        @Override
        public String format(E e) {
            String value = "";
            if (e != null) {
                value = decorator.toString(e);
            }
            return value;
        }
    }

    /**
     * @param 
     * @author Tony Chemit - [email protected]
     * @since 2.6.12
     */
    public static class ForeignKeyValue implements ValueParserFormatter {

        protected final String propertyName;

        protected final Class entityType;

        protected final Map universe;

        public ForeignKeyValue(Class entityType,
                               String propertyName,
                               Map universe) {
            this.entityType = entityType;
            this.propertyName = propertyName;
            this.universe = universe;
        }


        @Override
        public E parse(String value) throws ParseException {
            E result = null;
            if (StringUtils.isNotBlank(value)) {

                // get entity from universe
                result = universe.get(value);

                if (result == null) {

                    // can not find entity this is a big problem for us...
                    throw new TopiaException(
                            "Could not find entity of type " +
                            entityType.getSimpleName() + " with '" +
                            propertyName + "' = " + value);
                }
            }
            return result;
        }

        @Override
        public String format(E e) {
            String value = "";
            if (e != null) {
                value = e.getTopiaId();
            }
            return value;
        }
    }

    public static class ForeignKeyValueForAssociation implements ValueParser> {

        protected final String propertyName;

        protected final Class entityType;

        protected final Map universe;

        public ForeignKeyValueForAssociation(Class entityType,
                                             String propertyName,
                                             Map universe) {
            this.entityType = entityType;
            this.propertyName = propertyName;
            this.universe = universe;
        }

        @Override
        public Collection parse(String value) throws ParseException {
            E result = null;
            if (StringUtils.isNotBlank(value)) {

                // get entity from universe
                result = universe.get(value);

                if (result == null) {

                    // can not find entity this is a big problem for us...
                    throw new TopiaException(
                            "Could not find entity with '" + propertyName + "' = " + value);
                }
            }
            return Arrays.asList(result);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy