<commit_before>/*
 * Copyright (C) 2012-2014 Soomla 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 com.soomla.store.data;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.soomla.BusProvider;
import com.soomla.SoomlaConfig;
import com.soomla.data.KeyValueStorage;
import com.soomla.store.IStoreAssets;
import com.soomla.SoomlaApp;
import com.soomla.store.StoreConfig;
import com.soomla.SoomlaUtils;
import com.soomla.store.domain.PurchasableVirtualItem;
import com.soomla.store.domain.VirtualCategory;
import com.soomla.store.domain.VirtualItem;
import com.soomla.store.domain.virtualCurrencies.VirtualCurrency;
import com.soomla.store.domain.virtualCurrencies.VirtualCurrencyPack;
import com.soomla.store.domain.virtualGoods.*;
import com.soomla.store.events.UnexpectedStoreErrorEvent;
import com.soomla.store.exceptions.VirtualItemNotFoundException;
import com.soomla.store.purchaseTypes.PurchaseType;
import com.soomla.store.purchaseTypes.PurchaseWithMarket;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.*;

/**
 * This class holds the store's metadata including:
 * virtual currencies,
 * virtual currency packs,
 * all kinds of virtual goods,
 * virtual categories, and
 * non-consumable items
 */
public class StoreInfo {

    /**
     * Initializes <code>StoreInfo</code>.
     * On first initialization, when the database doesn't have any previous version of the store
     * metadata, <code>StoreInfo</code> gets loaded from the given <code>IStoreAssets</code>.
     * After the first initialization, <code>StoreInfo</code> will be initialized from the database.
     *
     * IMPORTANT: If you want to override the current <code>StoreInfo</code>, you'll have to bump
     * the version of your implementation of <code>IStoreAssets</code> in order to remove the
     * metadata when the application loads. Bumping the version is done by returning a higher number
     * in {@link com.soomla.store.IStoreAssets#getVersion()}.
     */
    public static void setStoreAssets(IStoreAssets storeAssets){
        if (storeAssets == null){
            SoomlaUtils.LogError(TAG, "The given store assets can't be null!");
            return;
        }

        mCurrentAssetsVersion = storeAssets.getVersion();

        checkAndResetMetadata();

        // we always initialize from the database, unless this is the first time the game is
        // loaded - in that case we initialize with setStoreAssets.
        if (!loadFromDB()){
            initializeWithStoreAssets(storeAssets);
        }
    }

    /**
     * Another option to set the data of StoreInfo. This will usually be used from game engine
     * wrappers like Unity or Cocos2dx.
     * @param version the version of IStoreAssets.
     * @param storeMetaJSON the store metadata as JSON.
     */
    public static void setStoreAssets(int version, String storeMetaJSON){
        if (TextUtils.isEmpty(storeMetaJSON)){
            SoomlaUtils.LogError(TAG, "The given store assets JSON can't be empty or null!");
            return;
        }

        mCurrentAssetsVersion = version;

        checkAndResetMetadata();

        // we always initialize from the database, unless this is the first time the game is
        // loaded - in that case we initialize with setStoreAssets.
        if (!loadFromDB()){
            try {
                fromJSONObject(new JSONObject(storeMetaJSON));
            } catch (JSONException e) {
                String err = "Can't parse store metadata json. That's a major issue." + storeMetaJSON;
                SoomlaUtils.LogError(TAG, err);
                BusProvider.getInstance().post(new UnexpectedStoreErrorEvent(err));
            }
        }
    }

    /**
     * Initializes <code>StoreInfo</code> from the database. This action should be performed only
     * once during the lifetime of a session of the game. <code>SoomlaStore</code> automatically
     * initializes <code>StoreInfo</code>. Don't do it if you don't know what you're doing.
     *
     * @return success
     */
    public static boolean loadFromDB() {
        checkAndResetMetadata();

        //if migration process is required we do not initialize from DB.
        //remove this code when migration process becomes obsolete.
        if(isMigrationRequired())
            return false;

        String key = keyMetaStoreInfo();
        String val = KeyValueStorage.getValue(key);

        if (val == null && TextUtils.isEmpty(val)){
            SoomlaUtils.LogDebug(TAG, "store json is not in DB yet.");
            return false;
        }

        // This is done in case old versions of the DB exist (especially from
        // Cocos2dx) which used jsonType instead of className
        if (val.contains("jsonType")) {<commit_msg>Logging debug message and small issue with checking for empty state.<commit_after>/*
 * Copyright (C) 2012-2014 Soomla 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 com.soomla.store.data;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.soomla.BusProvider;
import com.soomla.Soomla;
import com.soomla.SoomlaConfig;
import com.soomla.data.KeyValueStorage;
import com.soomla.store.IStoreAssets;
import com.soomla.SoomlaApp;
import com.soomla.store.StoreConfig;
import com.soomla.SoomlaUtils;
import com.soomla.store.domain.PurchasableVirtualItem;
import com.soomla.store.domain.VirtualCategory;
import com.soomla.store.domain.VirtualItem;
import com.soomla.store.domain.virtualCurrencies.VirtualCurrency;
import com.soomla.store.domain.virtualCurrencies.VirtualCurrencyPack;
import com.soomla.store.domain.virtualGoods.*;
import com.soomla.store.events.UnexpectedStoreErrorEvent;
import com.soomla.store.exceptions.VirtualItemNotFoundException;
import com.soomla.store.purchaseTypes.PurchaseType;
import com.soomla.store.purchaseTypes.PurchaseWithMarket;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.*;

/**
 * This class holds the store's metadata including:
 * virtual currencies,
 * virtual currency packs,
 * all kinds of virtual goods,
 * virtual categories, and
 * non-consumable items
 */
public class StoreInfo {

    /**
     * Initializes <code>StoreInfo</code>.
     * On first initialization, when the database doesn't have any previous version of the store
     * metadata, <code>StoreInfo</code> gets loaded from the given <code>IStoreAssets</code>.
     * After the first initialization, <code>StoreInfo</code> will be initialized from the database.
     *
     * IMPORTANT: If you want to override the current <code>StoreInfo</code>, you'll have to bump
     * the version of your implementation of <code>IStoreAssets</code> in order to remove the
     * metadata when the application loads. Bumping the version is done by returning a higher number
     * in {@link com.soomla.store.IStoreAssets#getVersion()}.
     */
    public static void setStoreAssets(IStoreAssets storeAssets){
        if (storeAssets == null){
            SoomlaUtils.LogError(TAG, "The given store assets can't be null!");
            return;
        }

        mCurrentAssetsVersion = storeAssets.getVersion();

//        checkAndResetMetadata();

        // we always initialize from the database, unless this is the first time the game is
        // loaded - in that case we initialize with setStoreAssets.
        if (!loadFromDB()){
            initializeWithStoreAssets(storeAssets);
        }
    }

    /**
     * Another option to set the data of StoreInfo. This will usually be used from game engine
     * wrappers like Unity or Cocos2dx.
     * @param version the version of IStoreAssets.
     * @param storeMetaJSON the store metadata as JSON.
     */
    public static void setStoreAssets(int version, String storeMetaJSON){
        SoomlaUtils.LogDebug(TAG, "trying to set json: " + storeMetaJSON);

        if (TextUtils.isEmpty(storeMetaJSON)){
            SoomlaUtils.LogError(TAG, "The given store assets JSON can't be empty or null!");
            return;
        }

        mCurrentAssetsVersion = version;

//        checkAndResetMetadata();

        // we always initialize from the database, unless this is the first time the game is
        // loaded - in that case we initialize with setStoreAssets.
        if (!loadFromDB()){
            try {
                fromJSONObject(new JSONObject(storeMetaJSON));
            } catch (JSONException e) {
                String err = "Can't parse store metadata json. That's a major issue." + storeMetaJSON;
                SoomlaUtils.LogError(TAG, err);
                BusProvider.getInstance().post(new UnexpectedStoreErrorEvent(err));
            }
        }
    }

    /**
     * Initializes <code>StoreInfo</code> from the database. This action should be performed only
     * once during the lifetime of a session of the game. <code>SoomlaStore</code> automatically
     * initializes <code>StoreInfo</code>. Don't do it if you don't know what you're doing.
     *
     * @return success
     */
    public static boolean loadFromDB() {
        checkAndResetMetadata();

        //if migration process is required we do not initialize from DB.
        //remove this code when migration process becomes obsolete.
        if(isMigrationRequired())
            return false;

        String key = keyMetaStoreInfo();
        String val = KeyValueStorage.getValue(key);

        if (val == null || TextUtils.isEmpty(val)){
            SoomlaUtils.LogDebug(TAG, "store json is not in DB yet.");
            return false;
        }

        // This is done in case old versions of the DB exist (especially from
        // Cocos2dx) which used jsonType instead of className
        if (val.contains("jsonType")) {
{'fraction_non_alphanumeric': 0.08667339264422635, 'fraction_numerical': 0.002568100522791892, 'mean_word_length': 3.053531598513011, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2401563', 'n_tokens_mistral': 2990, 'n_tokens_neox': 2904, 'n_words': 1006}