<%#
 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.CommonTableRelationship;
import <%= packageName %>.domain.enumeration.RelationshipType;
import <%= packageName %>.domain.enumeration.SourceType;
import <%= packageName %>.domain.enumeration.EndUsedType;

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.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 = "006")
public class InitialCommonTableRelationshipMigration {

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

    private  static List<CommonTableRelationship> commonTableRelationships() {
        List<CommonTableRelationship> result = new ArrayList<>();
        final CellProcessor[] processors = new CellProcessor[] {
                new ParseLong(),              // id
                new StrNULLTonull(new Optional(new ParseEnum(RelationshipType.class))), // relationshipType
                new StrNULLTonull(new Optional(new ParseEnum(SourceType.class))), // sourceType
                new Optional(),               // name
                new Optional(),               // otherEntityField
                new Optional(),               // relationshipName
                new Optional(),               // otherEntityRelationshipName
                new Optional(), // columnWidth
                new Optional(), // order
                new Optional(), // hideInList
                new Optional(),                // fontColor
                new Optional(),                // backgroundColor
                new Optional(),                // help
                new StrNULLTonull(new Optional()), // ownerSide
                new Optional(),                // commonTable.id
                new Optional(),                // dataName
                new Optional(),  // webComponentType
                new Optional(),                // otherEntityIsTree
                new Optional(),                // otherEntityName
                new StrNULLTonull(new Optional(new ParseEnum(EndUsedType.class))),  // endUsed
                new Optional(),  // editInList
                new Optional(),  // hideInForm
                new Optional(),  // system
                new Optional(),  // searchForm
        };
        final String[] FIELD_MAPPING = new String[]{
                "id",
                "relationshipType",
                "sourceType",
                "name",
                "otherEntityField",
                "relationshipName",
                "otherEntityRelationshipName",
                "columnWidth",
                "order",
                "hideInList",
                "fontColor",
                "backgroundColor",
                "help",
                "ownerSide",
                "commonTable.id",
                "dataName",
                "webComponentType",
                "otherEntityIsTree",
                "otherEntityName",
                "endUsed",
                "editInList",
                "hideInForm",
                "system",
                "searchForm",
        };

        ICsvDozerBeanReader beanReader = null;
        try {
            InputStream csvIn = InitialSetupMigration.class.getClassLoader().getResourceAsStream("config/liquibase/data/common_table_relationship.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(CommonTableRelationship.class, FIELD_MAPPING);

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

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

}
