<%#
 Copyright 2013-2021 the original author or authors from the BegCode project.

 This file is part of the JHipster project, see https://www.jhipster.tech/
 for more information.

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

      https://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-%>
package <%= packageName %>.config.dbmigrations;

import <%= packageName %>.domain.CommonTable;
import <%= packageName %>.domain.<%= user.persistClass %>;
import <%= packageName %>.security.AuthoritiesConstants;
import <%= packageName %>.config.Constants;

import com.github.cloudyrock.mongock.ChangeLog;
import com.github.cloudyrock.mongock.ChangeSet;
import com.github.cloudyrock.mongock.driver.mongodb.springdata.v3.decorator.impl.MongockTemplate;

import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.io.InputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import org.supercsv.cellprocessor.Optional;
import org.supercsv.cellprocessor.ParseEnum;
import org.supercsv.cellprocessor.ParseLong;
import org.supercsv.cellprocessor.ift.CellProcessor;
import org.supercsv.io.dozer.CsvDozerBeanReader;
import org.supercsv.io.dozer.ICsvDozerBeanReader;
import org.supercsv.prefs.CsvPreference;

/**
 * Creates the initial database setup.
 */
@ChangeLog(order = "004")
public class InitialCommonTableMigration {

    @ChangeSet(order = "01", author = "initiator", id = "01-addCommonTables")
    public void addCommonTables(MongockTemplate mongoTemplate) {
        mongoTemplate.insertAll(commonTables());
    }

    private  static List<CommonTable> commonTables() {
        List<CommonTable> result = new ArrayList<>();
        final CellProcessor[] processors = new CellProcessor[] {
                new ParseLong(),              // id
                new Optional(),               // name
                new Optional(),               // entityName
                new Optional(),               // tableName
                new Optional(),               // clazzName
                new Optional(),               // system
                new Optional(),               // generated
                null,
                null,
                null,
                new Optional(),                // description
                new Optional(),                // creator.id
                new Optional(),                // businessType.id
                new Optional(),                // treeTable
                new Optional(),                // editInModal
                new Optional(),                // searchForm
        };
        final String[] FIELD_MAPPING = new String[] {
                "id",                   // simple field mapping (like CsvBeanReader)
                "name",          // as above
                "entityName", // indexed (first element) + deep mapping
                "tableName",
                "clazzName", // indexed (second element) + deep mapping
                "system",
                "generated",
                null,
                null,
                null,
                "description",
                "creator.id",
                "businessType.id",
                "treeTable",
                "editInContainer",
                "searchForm"
        };

        ICsvDozerBeanReader beanReader = null;
        try {
            InputStream csvIn = InitialSetupMigration.class.getClassLoader().getResourceAsStream("config/liquibase/data/common_table.csv");
            beanReader = new CsvDozerBeanReader(new InputStreamReader(csvIn, StandardCharsets.UTF_8), (new CsvPreference.Builder('"', 59, "\r\n")).build());
            beanReader.getHeader(true); // ignore the header
            beanReader.configureBeanMapping(CommonTable.class, FIELD_MAPPING);

            CommonTable commonTable;
            while( (commonTable = beanReader.read(CommonTable.class, processors)) != null ) {
                result.add(commonTable);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if( beanReader != null ) {
                try {
                    beanReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }

}
