/*-
 * ~~~~~~licensing~~~~~~
 * atlasmap-entaxy-services
 * ==========
 * Copyright (C) 2020 - 2025 EmDev LLC
 * ==========
 * You may not use this file except in accordance with the License Terms of the Copyright
 * Holder located at: https://entaxy.ru/eula . All copyrights, all intellectual property
 * rights to the Software and any copies are the property of the Copyright Holder. Unless
 * it is explicitly allowed the Copyright Holder, the User is prohibited from using the
 * Software for commercial purposes to provide services to third parties.
 * 
 * The Copyright Holder hereby declares that the Software is provided on an "AS IS".
 * Under no circumstances does the Copyright Holder guarantee or promise that the
 * Software provided by him will be suitable or not suitable for the specific purposes
 * of the User, that the Software will meet all commercial and personal subjective
 * expectations of the User, that the Software will work properly, without technical
 * errors, quickly and uninterruptedly.
 * 
 * Under no circumstances shall the Copyright Holder or its Affiliates is not liable
 * to the User for any direct or indirect losses of the User, his expenses or actual
 * damage, including, downtime; loss of bussines; lost profit; lost earnings; loss
 * or damage to data, property, etc.
 * ~~~~~~/licensing~~~~~~
 */
/*
 * Copyright (C) 2017 Red Hat, Inc.
 *
 * 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
 *
 *         http://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 io.atlasmap.builder;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.atlasmap.api.AtlasException;
import io.atlasmap.api.AtlasMappingBuilder;
import io.atlasmap.api.AtlasSession;
import io.atlasmap.core.AtlasUtil;
import io.atlasmap.core.DefaultAtlasSession;
import io.atlasmap.spi.AtlasConversionService;
import io.atlasmap.spi.AtlasFieldActionService;
import io.atlasmap.v2.AuditStatus;
import io.atlasmap.v2.SimpleField;

/**
 * A base {@code AtlasMappingBuilder} with some common utility methods.
 * In most cases user can extend this class and just implement {@link #processMapping()}.
 * @see AtlasField
 */
public abstract class DefaultAtlasMappingBuilder implements AtlasMappingBuilder {

    private static final Logger LOG = LoggerFactory.getLogger(DefaultAtlasMappingBuilder.class);
    private DefaultAtlasSession session;
    private AtlasConversionService conversionService;
    private AtlasFieldActionService fieldActionService;

    public AtlasField read(String docId, String path) throws AtlasException {
        return new AtlasField(session).read(docId, path);
    }

    public AtlasField readConstant(String name) throws AtlasException {
        return new AtlasField(session).readConstant(name);
    }

    public AtlasField readProperty(String scope, String name) throws AtlasException {
        return new AtlasField(session).readProperty(scope, name);
    }

    public void write(String docId, String path, Object value) throws AtlasException {
        SimpleField source = new SimpleField();
        if (value != null) {
            source.setValue(value);
            source.setFieldType(this.conversionService.fieldTypeFromClass(value.getClass()));
        }
        new AtlasField(session).setRawField(source).write(docId, path);
    }

    @Override
    public void process() {
        try {
            processMapping();
        } catch (Exception e) {
            addAudit(e);
        }
    }

    /**
     * Define custom mapping logic. User can extend this class and implement
     * custom mapping logic in this method. The thrown Exception will be catched
     * in {@link #process()} and added as an Audit.
     * @throws Exception Indicate mapping error to be recorded as an Audit
     */
    public abstract void processMapping() throws Exception;

    @Override
    public void setAtlasSession(AtlasSession session) throws AtlasException {
        if (!(session instanceof DefaultAtlasSession)) {
            throw new IllegalArgumentException(String.format(
                    "This version of MappingBuilder doesn't support %s",
                    session.getClass().getName()));
        }
        this.session = (DefaultAtlasSession) session;
        this.conversionService = session.getAtlasContext().getContextFactory().getConversionService();
        this.fieldActionService = session.getAtlasContext().getContextFactory().getFieldActionService();
    };

    /**
     * Get {@code DefaultAtlasSession}.
     * @return {@code AtlasSession}.
     */
    public AtlasSession getAtlasSession() {
        return this.session;
    };

    public void addAudit(Exception e) {
        AtlasUtil.addAudit(this.session, this.getClass().getName(),
                e.getMessage(), AuditStatus.ERROR, null);
        if (LOG.isDebugEnabled()) {
            LOG.error("", e);
        }
    }

}
