<commit_before>package ua.com.fielden.platform.domaintree.impl;

import java.util.Map.Entry;

import org.apache.log4j.Logger;

import ua.com.fielden.platform.domaintree.centre.impl.CentreDomainTreeManager;
import ua.com.fielden.platform.domaintree.centre.impl.CentreDomainTreeManager.AddToCriteriaTickManager;
import ua.com.fielden.platform.domaintree.centre.impl.CentreDomainTreeManager.AddToResultTickManager;
import ua.com.fielden.platform.domaintree.centre.impl.CentreDomainTreeManager0.AddToCriteriaTickManager0;
import ua.com.fielden.platform.domaintree.centre.impl.CentreDomainTreeManagerAndEnhancer;
import ua.com.fielden.platform.domaintree.centre.impl.CentreDomainTreeManagerAndEnhancer0;
import ua.com.fielden.platform.domaintree.centre.impl.CentreDomainTreeRepresentation;
import ua.com.fielden.platform.domaintree.centre.impl.LocatorDomainTreeManager;
import ua.com.fielden.platform.domaintree.centre.impl.LocatorDomainTreeManager.AddToCriteriaTickManagerForLocator;
import ua.com.fielden.platform.domaintree.centre.impl.LocatorDomainTreeManager0.AddToCriteriaTickManagerForLocator0;
import ua.com.fielden.platform.domaintree.centre.impl.LocatorDomainTreeManagerAndEnhancer;
import ua.com.fielden.platform.domaintree.centre.impl.LocatorDomainTreeManagerAndEnhancer0;
import ua.com.fielden.platform.domaintree.centre.impl.LocatorDomainTreeRepresentation;
import ua.com.fielden.platform.domaintree.master.impl.MasterDomainTreeManager;
import ua.com.fielden.platform.domaintree.master.impl.MasterDomainTreeManager0;
import ua.com.fielden.platform.serialisation.api.ISerialiser;
import ua.com.fielden.platform.serialisation.api.ISerialiser0;
import ua.com.fielden.platform.ui.config.EntityCentreConfig;
import ua.com.fielden.platform.ui.config.EntityLocatorConfig;
import ua.com.fielden.platform.ui.config.EntityMasterConfig;
import ua.com.fielden.platform.ui.config.api.IEntityCentreConfigController;
import ua.com.fielden.platform.ui.config.api.IEntityLocatorConfigController;
import ua.com.fielden.platform.ui.config.api.IEntityMasterConfigController;
import ua.com.fielden.platform.utils.Pair;

/**
 * This utility class is responsible in maintaining configuration versions.
 * <p>
 * 1. Version of Domain Tree serialisation classes.<br>
 * a) 0 Version -- DomainTreeEnhanser with byte arrays serialised<br>
 * b) CURRENT Version -- DomainTreeEnhanser with {@link CalculatedPropertyInfo} serialised<br>
 * <br>
 * 2. Version of Original types<br>
 * a) TODO property adding<br>
 * b) TODO property removal<br>
 *
 * @author TG Team
 *
 */
public class DomainTreeVersionMaintainer extends AbstractDomainTree {
    private final static Logger logger = Logger.getLogger(DomainTreeVersionMaintainer.class);

    // private final ISerialiser serialiser = getSerialiser();
    private final ISerialiser0 serialiser0;
    private final IEntityLocatorConfigController elcController;
    private final IEntityCentreConfigController eccController;
    private final IEntityMasterConfigController emcController;

    public DomainTreeVersionMaintainer(final ISerialiser serialiser, final ISerialiser0 serialiser0, final IEntityLocatorConfigController entityLocatorConfigController, final IEntityCentreConfigController entityCentreConfigController, final IEntityMasterConfigController entityMasterConfigController) {
        super(serialiser);
        this.serialiser0 = serialiser0;

        //	System.err.println("\tserialiser0 == " + serialiser0);
        //	System.err.println("\tserialiser == " + serialiser);

        this.elcController = entityLocatorConfigController;
        this.eccController = entityCentreConfigController;
        this.emcController = entityMasterConfigController;
    }

    public static Pair<LocatorDomainTreeManagerAndEnhancer, Boolean> retrieveLocator(final String elcKey, final byte[] array, final ISerialiser serialiser, final ISerialiser serialiser0)
            throws Exception {
        logger.debug("Started maintaining the version of default locator instance for [" + elcKey + "].");
        try {
            // try to deserialise with a current version of serialiser
            final LocatorDomainTreeManagerAndEnhancer ldtmae = serialiser.deserialise(array, LocatorDomainTreeManagerAndEnhancer.class);
            logger.debug("\tA default locator instance for [" + elcKey + "] is of CURRENT (1) version and has been succesfully deserialised.");
            // all is okay -- the version is current
            logger.debug("Ended maintaining the version of default locator instance for [" + elcKey + "].");
            return new Pair<LocatorDomainTreeManagerAndEnhancer, Boolean>(ldtmae, false);
        } catch (final Exception e) {
            System.out.println("EXCEPTION:" + e.getMessage());
            //e.printStackTrace();
            // smth is wrong -- try previous versions
            final LocatorDomainTreeManagerAndEnhancer0 ldtmae0 = serialiser0.deserialise(array, LocatorDomainTreeManagerAndEnhancer0.class);
            logger.warn("\tA default locator instance for [" + elcKey + "] is of OLD (0) version -- trying to convert it to CURRENT (1) version.");

            // all is okay -- the version is 0 -- convert it to current and return
            final LocatorDomainTreeManagerAndEnhancer ldtmae = convert(ldtmae0, serialiser);
            logger.warn("\tA default locator instance for [" + elcKey + "] has been converted succesfully to CURRENT (1) version from OLD (0) version.");

            return new Pair<LocatorDomainTreeManagerAndEnhancer, Boolean>(ldtmae, true);
        }
    }

    public LocatorDomainTreeManagerAndEnhancer maintainLocatorVersion(final EntityLocatorConfig downloadedElc) throws Exception {
        final Pair<LocatorDomainTreeManagerAndEnhancer, Boolean> ldtmaeAndShouldBePromoted = retrieveLocator(downloadedElc.toString(), downloadedElc.getConfigBody(), getSerialiser(), serialiser0);
        if (ldtmaeAndShouldBePromoted.getValue()) {
            // the converted version should be promoted to the cloud
            downloadedElc.setConfigBody(getSerialiser().serialise(ldtmaeAndShouldBePromoted.getKey())); // serialise with CURRENT version of serialiser
            final EntityLocatorConfig updatedElc = elcController.save(downloadedElc);
            logger.warn("\tA default locator instance, converted to CURRENT (1) version, for [" + downloadedElc + "] has been succesfully saved (promoted to the cloud).");

            logger.debug("Ended maintaining the version of default locator instance for [" + downloadedElc + "].");
            return maintainLocatorVersion(updatedElc);
        } else {
            logger.debug("Ended maintaining the version of default locator instance for [" + downloadedElc + "].");
            return ldtmaeAndShouldBePromoted.getKey();
        }
    }

    private static LocatorDomainTreeManagerAndEnhancer convert(final LocatorDomainTreeManagerAndEnhancer0 ldtmae0, final ISerialiser serialiser) {
        final AddToCriteriaTickManagerForLocator0 atctmfl0 = (AddToCriteriaTickManagerForLocator0) ldtmae0.base().getFirstTick();
        return //
        new LocatorDomainTreeManagerAndEnhancer(//
        serialiser, //
        new LocatorDomainTreeManager(serialiser, (LocatorDomainTreeRepresentation) ldtmae0.base().getRepresentation(), new AddToCriteriaTickManagerForLocator(atctmfl0.checkedProperties(), serialiser, atctmfl0.propertiesValues1(), atctmfl0.propertiesValues2(), atctmfl0.propertiesExclusive1(), atctmfl0.propertiesExclusive2(), atctmfl0.propertiesDatePrefixes(), atctmfl0.propertiesDateMnemonics(), atctmfl0.propertiesAndBefore(), atctmfl0.propertiesOrNulls(), atctmfl0.propertiesNots(), atctmfl0.columnsNumber(), new LocatorManager(serialiser, ldtmae0.base().getRepresentation().rootTypes(), convert(atctmfl0.locatorManager().persistentLocators(), serialiser)), atctmfl0.propertiesMetaValuePresences()), (AddToResultTickManager) ldtmae0.base().getSecondTick(), ldtmae0.base().isRunAutomatically(), ldtmae0.base().isUseForAutocompletion(), ldtmae0.base().getSearchBy()), new DomainTreeEnhancer(serialiser, ldtmae0.getEnhancer().originalAndEnhancedRootTypesAndArrays(), ldtmae0.getEnhancer().calculatedProperties(), ldtmae0.getEnhancer().customProperties()), ldtmae0.persistentAnalyses(), ldtmae0.currentAnalyses(), ldtmae0.freezedAnalyses());
    }

    public static Pair<CentreDomainTreeManagerAndEnhancer, Boolean> retrieveCentre(final String eccKey, final byte[] array, final ISerialiser serialiser, final ISerialiser0 serialiser0)
            throws Exception {
        logger.debug("Started maintaining the version of centre instance for [" + eccKey + "].");
        try {
            // try to deserialise with a current version of serialiser
            final CentreDomainTreeManagerAndEnhancer cdtmae = serialiser.deserialise(array, CentreDomainTreeManagerAndEnhancer.class);
            logger.debug("\tA centre instance for [" + eccKey + "] is of CURRENT (1) version and has been succesfully deserialised.");
            // all is okay -- the version is current
            logger.debug("Ended maintaining the version of centre instance for [" + eccKey + "].");
            return new Pair<CentreDomainTreeManagerAndEnhancer, Boolean>(cdtmae, false);
        } catch (final Exception e) {
            System.out.println("EXCEPTION:" + e.getMessage());
            //e.printStackTrace();
            // smth is wrong -- try previous versions
            final CentreDomainTreeManagerAndEnhancer0 cdtmae0 = serialiser0.deserialise(array, CentreDomainTreeManagerAndEnhancer0.class);
            logger.warn("\tA centre instance for [" + eccKey + "] is of OLD (0) version -- trying to convert it to CURRENT (1) version.");

            // all is okay -- the version is 0 -- convert it to current and return
            final CentreDomainTreeManagerAndEnhancer cdtmae = convert(cdtmae0, serialiser);
            logger.warn("\tA centre instance for [" + eccKey + "] has been converted succesfully to CURRENT (1) version from OLD (0) version.");

            return new Pair<CentreDomainTreeManagerAndEnhancer, Boolean>(cdtmae, true);
        }
    }

    public CentreDomainTreeManagerAndEnhancer maintainCentreVersion(final EntityCentreConfig downloadedEcc) throws Exception {
        final Pair<CentreDomainTreeManagerAndEnhancer, Boolean> cdtmaeAndShouldBePromoted = retrieveCentre(downloadedEcc.toString(), downloadedEcc.getConfigBody(), getSerialiser(), serialiser0);
        if (cdtmaeAndShouldBePromoted.getValue()) {
            // the converted version should be promoted to the cloud
            downloadedEcc.setConfigBody(getSerialiser().serialise(cdtmaeAndShouldBePromoted.getKey())); // serialise with CURRENT version of serialiser
            final EntityCentreConfig updatedEcc = eccController.save(downloadedEcc);<commit_msg>Commented out the logic of trying to deserialise centre with previous 0 version serialiser in case of deserialisation failure (0 version is very old and should not be used at all). In this case appropriate exception of 'original deserialisation failure' bubbles up.<commit_after>package ua.com.fielden.platform.domaintree.impl;

import java.util.Map.Entry;

import org.apache.log4j.Logger;

import ua.com.fielden.platform.domaintree.centre.impl.CentreDomainTreeManager;
import ua.com.fielden.platform.domaintree.centre.impl.CentreDomainTreeManager.AddToCriteriaTickManager;
import ua.com.fielden.platform.domaintree.centre.impl.CentreDomainTreeManager.AddToResultTickManager;
import ua.com.fielden.platform.domaintree.centre.impl.CentreDomainTreeManager0.AddToCriteriaTickManager0;
import ua.com.fielden.platform.domaintree.centre.impl.CentreDomainTreeManagerAndEnhancer;
import ua.com.fielden.platform.domaintree.centre.impl.CentreDomainTreeManagerAndEnhancer0;
import ua.com.fielden.platform.domaintree.centre.impl.CentreDomainTreeRepresentation;
import ua.com.fielden.platform.domaintree.centre.impl.LocatorDomainTreeManager;
import ua.com.fielden.platform.domaintree.centre.impl.LocatorDomainTreeManager.AddToCriteriaTickManagerForLocator;
import ua.com.fielden.platform.domaintree.centre.impl.LocatorDomainTreeManager0.AddToCriteriaTickManagerForLocator0;
import ua.com.fielden.platform.domaintree.centre.impl.LocatorDomainTreeManagerAndEnhancer;
import ua.com.fielden.platform.domaintree.centre.impl.LocatorDomainTreeManagerAndEnhancer0;
import ua.com.fielden.platform.domaintree.centre.impl.LocatorDomainTreeRepresentation;
import ua.com.fielden.platform.domaintree.master.impl.MasterDomainTreeManager;
import ua.com.fielden.platform.domaintree.master.impl.MasterDomainTreeManager0;
import ua.com.fielden.platform.serialisation.api.ISerialiser;
import ua.com.fielden.platform.serialisation.api.ISerialiser0;
import ua.com.fielden.platform.ui.config.EntityCentreConfig;
import ua.com.fielden.platform.ui.config.EntityLocatorConfig;
import ua.com.fielden.platform.ui.config.EntityMasterConfig;
import ua.com.fielden.platform.ui.config.api.IEntityCentreConfigController;
import ua.com.fielden.platform.ui.config.api.IEntityLocatorConfigController;
import ua.com.fielden.platform.ui.config.api.IEntityMasterConfigController;
import ua.com.fielden.platform.utils.Pair;

/**
 * This utility class is responsible in maintaining configuration versions.
 * <p>
 * 1. Version of Domain Tree serialisation classes.<br>
 * a) 0 Version -- DomainTreeEnhanser with byte arrays serialised<br>
 * b) CURRENT Version -- DomainTreeEnhanser with {@link CalculatedPropertyInfo} serialised<br>
 * <br>
 * 2. Version of Original types<br>
 * a) TODO property adding<br>
 * b) TODO property removal<br>
 *
 * @author TG Team
 *
 */
public class DomainTreeVersionMaintainer extends AbstractDomainTree {
    private final static Logger logger = Logger.getLogger(DomainTreeVersionMaintainer.class);

    // private final ISerialiser serialiser = getSerialiser();
    private final ISerialiser0 serialiser0;
    private final IEntityLocatorConfigController elcController;
    private final IEntityCentreConfigController eccController;
    private final IEntityMasterConfigController emcController;

    public DomainTreeVersionMaintainer(final ISerialiser serialiser, final ISerialiser0 serialiser0, final IEntityLocatorConfigController entityLocatorConfigController, final IEntityCentreConfigController entityCentreConfigController, final IEntityMasterConfigController entityMasterConfigController) {
        super(serialiser);
        this.serialiser0 = serialiser0;

        //	System.err.println("\tserialiser0 == " + serialiser0);
        //	System.err.println("\tserialiser == " + serialiser);

        this.elcController = entityLocatorConfigController;
        this.eccController = entityCentreConfigController;
        this.emcController = entityMasterConfigController;
    }

    public static Pair<LocatorDomainTreeManagerAndEnhancer, Boolean> retrieveLocator(final String elcKey, final byte[] array, final ISerialiser serialiser, final ISerialiser serialiser0)
            throws Exception {
        logger.debug("Started maintaining the version of default locator instance for [" + elcKey + "].");
        try {
            // try to deserialise with a current version of serialiser
            final LocatorDomainTreeManagerAndEnhancer ldtmae = serialiser.deserialise(array, LocatorDomainTreeManagerAndEnhancer.class);
            logger.debug("\tA default locator instance for [" + elcKey + "] is of CURRENT (1) version and has been succesfully deserialised.");
            // all is okay -- the version is current
            logger.debug("Ended maintaining the version of default locator instance for [" + elcKey + "].");
            return new Pair<LocatorDomainTreeManagerAndEnhancer, Boolean>(ldtmae, false);
        } catch (final Exception e) {
            System.out.println("EXCEPTION:" + e.getMessage());
            //e.printStackTrace();
            // smth is wrong -- try previous versions
            final LocatorDomainTreeManagerAndEnhancer0 ldtmae0 = serialiser0.deserialise(array, LocatorDomainTreeManagerAndEnhancer0.class);
            logger.warn("\tA default locator instance for [" + elcKey + "] is of OLD (0) version -- trying to convert it to CURRENT (1) version.");

            // all is okay -- the version is 0 -- convert it to current and return
            final LocatorDomainTreeManagerAndEnhancer ldtmae = convert(ldtmae0, serialiser);
            logger.warn("\tA default locator instance for [" + elcKey + "] has been converted succesfully to CURRENT (1) version from OLD (0) version.");

            return new Pair<LocatorDomainTreeManagerAndEnhancer, Boolean>(ldtmae, true);
        }
    }

    public LocatorDomainTreeManagerAndEnhancer maintainLocatorVersion(final EntityLocatorConfig downloadedElc) throws Exception {
        final Pair<LocatorDomainTreeManagerAndEnhancer, Boolean> ldtmaeAndShouldBePromoted = retrieveLocator(downloadedElc.toString(), downloadedElc.getConfigBody(), getSerialiser(), serialiser0);
        if (ldtmaeAndShouldBePromoted.getValue()) {
            // the converted version should be promoted to the cloud
            downloadedElc.setConfigBody(getSerialiser().serialise(ldtmaeAndShouldBePromoted.getKey())); // serialise with CURRENT version of serialiser
            final EntityLocatorConfig updatedElc = elcController.save(downloadedElc);
            logger.warn("\tA default locator instance, converted to CURRENT (1) version, for [" + downloadedElc + "] has been succesfully saved (promoted to the cloud).");

            logger.debug("Ended maintaining the version of default locator instance for [" + downloadedElc + "].");
            return maintainLocatorVersion(updatedElc);
        } else {
            logger.debug("Ended maintaining the version of default locator instance for [" + downloadedElc + "].");
            return ldtmaeAndShouldBePromoted.getKey();
        }
    }

    private static LocatorDomainTreeManagerAndEnhancer convert(final LocatorDomainTreeManagerAndEnhancer0 ldtmae0, final ISerialiser serialiser) {
        final AddToCriteriaTickManagerForLocator0 atctmfl0 = (AddToCriteriaTickManagerForLocator0) ldtmae0.base().getFirstTick();
        return //
        new LocatorDomainTreeManagerAndEnhancer(//
        serialiser, //
        new LocatorDomainTreeManager(serialiser, (LocatorDomainTreeRepresentation) ldtmae0.base().getRepresentation(), new AddToCriteriaTickManagerForLocator(atctmfl0.checkedProperties(), serialiser, atctmfl0.propertiesValues1(), atctmfl0.propertiesValues2(), atctmfl0.propertiesExclusive1(), atctmfl0.propertiesExclusive2(), atctmfl0.propertiesDatePrefixes(), atctmfl0.propertiesDateMnemonics(), atctmfl0.propertiesAndBefore(), atctmfl0.propertiesOrNulls(), atctmfl0.propertiesNots(), atctmfl0.columnsNumber(), new LocatorManager(serialiser, ldtmae0.base().getRepresentation().rootTypes(), convert(atctmfl0.locatorManager().persistentLocators(), serialiser)), atctmfl0.propertiesMetaValuePresences()), (AddToResultTickManager) ldtmae0.base().getSecondTick(), ldtmae0.base().isRunAutomatically(), ldtmae0.base().isUseForAutocompletion(), ldtmae0.base().getSearchBy()), new DomainTreeEnhancer(serialiser, ldtmae0.getEnhancer().originalAndEnhancedRootTypesAndArrays(), ldtmae0.getEnhancer().calculatedProperties(), ldtmae0.getEnhancer().customProperties()), ldtmae0.persistentAnalyses(), ldtmae0.currentAnalyses(), ldtmae0.freezedAnalyses());
    }

    public static Pair<CentreDomainTreeManagerAndEnhancer, Boolean> retrieveCentre(final String eccKey, final byte[] array, final ISerialiser serialiser, final ISerialiser0 serialiser0)
            throws Exception {
        logger.debug("Started maintaining the version of centre instance for [" + eccKey + "].");
        try {
            // try to deserialise with a current version of serialiser
            final CentreDomainTreeManagerAndEnhancer cdtmae = serialiser.deserialise(array, CentreDomainTreeManagerAndEnhancer.class);
            logger.debug("\tA centre instance for [" + eccKey + "] is of CURRENT (1) version and has been succesfully deserialised.");
            // all is okay -- the version is current
            logger.debug("Ended maintaining the version of centre instance for [" + eccKey + "].");
            return new Pair<CentreDomainTreeManagerAndEnhancer, Boolean>(cdtmae, false);
        } catch (final Exception e) {
            logger.debug("Ended maintaining the version of centre instance for [" + eccKey + "] -- the exception has occured.");
            throw e;
//            // smth is wrong -- try previous versions
//            final CentreDomainTreeManagerAndEnhancer0 cdtmae0 = serialiser0.deserialise(array, CentreDomainTreeManagerAndEnhancer0.class);
//            logger.warn("\tA centre instance for [" + eccKey + "] is of OLD (0) version -- trying to convert it to CURRENT (1) version.");
//
//            // all is okay -- the version is 0 -- convert it to current and return
//            final CentreDomainTreeManagerAndEnhancer cdtmae = convert(cdtmae0, serialiser);
//            logger.warn("\tA centre instance for [" + eccKey + "] has been converted succesfully to CURRENT (1) version from OLD (0) version.");
//
//            return new Pair<CentreDomainTreeManagerAndEnhancer, Boolean>(cdtmae, true);
        }
    }

    public CentreDomainTreeManagerAndEnhancer maintainCentreVersion(final EntityCentreConfig downloadedEcc) throws Exception {
        final Pair<CentreDomainTreeManagerAndEnhancer, Boolean> cdtmaeAndShouldBePromoted = retrieveCentre(downloadedEcc.toString(), downloadedEcc.getConfigBody(), getSerialiser(), serialiser0);
        if (cdtmaeAndShouldBePromoted.getValue()) {
            // the converted version should be promoted to the cloud
            downloadedEcc.setConfigBody(getSerialiser().serialise(cdtmaeAndShouldBePromoted.getKey())); // serialise with CURRENT version of serialiser
            final EntityCentreConfig updatedEcc = eccController.save(downloadedEcc);
{'fraction_non_alphanumeric': 0.09040371821744281, 'fraction_numerical': 0.007655153558735077, 'mean_word_length': 4.443204365079365, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 26, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4338263', 'n_tokens_mistral': 6207, 'n_tokens_neox': 6104, 'n_words': 1352}