// Copyright 2016 Yahoo Inc.
// Licensed under the terms of the Apache license. Please see LICENSE.md file distributed with this work for terms.
package com.yahoo.bard.webservice.util;

import com.yahoo.bard.webservice.config.BardFeatureFlag;

import java.util.Locale;

/**
 * Provider for magic strings used in dimension keystore key generation.
 */
public class DimensionStoreKeyUtils {

    public static String getLastUpdatedKey() {
        return "last_updated_key";
    }

    /**
     * Returns a key for accessing all the dimension values in a
     * {@link com.yahoo.bard.webservice.data.dimension.KeyValueStore}.
     * <p>
     * When this key is passed into a {@link com.yahoo.bard.webservice.data.dimension.KeyValueStore}, the
     * KeyValueStore will return a String representation of a list of all the dimension values stored in the key value
     * dimension store.
     *
     * @return A key for accessing a list of all the values in a KeyValueStore as a String.
     */
    public static String getAllValuesKey() {
        return "all_values_key";
    }

    /**
     * Returns a key that allows access to the dimension rows of a given dimension.
     * <p>
     * Given a dimension field name (such as "id"), and the id of the dimension desired, returns a key. When
     * this key is passed into the appropriate {@link com.yahoo.bard.webservice.data.dimension.KeyValueStore},
     * the KeyValueStore will return the metadata of the associated dimension.
     *
     * @param rowName  The dimension field name to be appended to the beginning of the key
     * @param rowValue  The id of the dimension whose data is desired
     *
     * @return A key that, when passed into the appropriate KeyValueStore, will return the associated dimension
     * metadata.
     */
    public static String getRowKey(String rowName, String rowValue) {
        String key = "";
        if (rowName != null) {
            key = BardFeatureFlag.CASE_SENSITIVE_KEYS.isOn() ? rowName : rowName.toLowerCase(Locale.ENGLISH);
        }
        if (rowValue != null) {
            key += "_" + (
                    BardFeatureFlag.CASE_SENSITIVE_KEYS.isOn() ? rowValue : rowValue.toLowerCase(Locale.ENGLISH)
            );
        }
        return key + "_row_key";
    }

    /**
     *  Returns a key that, when fed into a {@link com.yahoo.bard.webservice.data.dimension.KeyValueStore} returns the
     *  field name used by Lucene.
     *
     * @param columnName  The name of the column to extract from Lucene.
     *
     * @return A key that, when fed into a KeyValueStore, returns the ID used to query a dimension in Lucene.
     */
    public static String getColumnKey(String columnName) {
        String key = "";
        if (columnName != null) {
            key = BardFeatureFlag.CASE_SENSITIVE_KEYS.isOn()
                    ? columnName : columnName.toLowerCase(Locale.ENGLISH);
        }
        return key + "_column_key";
    }

    /**
     * Returns a key that, when fed into a {@link com.yahoo.bard.webservice.data.dimension.KeyValueStore} returns the
     * cardinality of the dimensions.
     *
     * @return  A key that, when fed into a KeyValueStore returns the cardinality of the dimensions.
     */
    public static String getCardinalityKey() {
        return "cardinality_key";
    }
}

@@ -263,16 +263,62 @@
 ocale;%0A%0A
+import javax.validation.constraints.NotNull;%0A%0A
 /**%0A * P
@@ -429,16 +429,129 @@
 tils %7B%0A%0A
+    public static final String ROW_KEY_SUFFIX = %22_row_key%22;%0A    public static final String KEY_SEPARATOR = %22_%22;%0A%0A
     publ
@@ -1650,19 +1650,21 @@
  @param 
-row
+field
 Name  Th
@@ -1743,19 +1743,21 @@
  @param 
-row
+field
 Value  T
@@ -1759,18 +1759,19 @@
 ue  The 
-id
+key
  of the 
@@ -1924,24 +1924,14 @@
 sion
-%0A     * metadata
+ value
 .%0A  
@@ -1971,26 +1971,37 @@
 tRowKey(
+@NotNull 
 String 
-row
+field
 Name, St
@@ -2005,19 +2005,21 @@
  String 
-row
+field
 Value) %7B
@@ -2031,132 +2031,241 @@
     
-String key = %22%22;%0A        if (rowName != null) %7B%0A            key = BardFeatureFlag.CASE_SENSITIVE_KEYS.isOn() ? row
+boolean caseSensitive = BardFeatureFlag.CASE_SENSITIVE_KEYS.isOn();%0A        String lookupFieldValue = fieldValue == null ? %22%22 : fieldValue;%0A%0A        return new StringBuilder()%0A                .append(caseSensitive ? field
 Name : 
-row
+field
 Name
@@ -2284,33 +2284,33 @@
 (Locale.ENGLISH)
-;
+)
 %0A        %7D%0A     
@@ -2302,26 +2302,24 @@
         
-%7D%0A
         
 if (rowV
@@ -2314,138 +2314,101 @@
     
-if (rowValue != null) %7B%0A            key += %22_%22 + (%0A                    BardFeatureFlag.CASE_SENSITIVE_KEYS.isOn() ? rowValue : row
+.append(KEY_SEPARATOR)%0A                .append(caseSensitive ? lookupFieldValue : lookupField
 Valu
@@ -2436,16 +2436,17 @@
 ENGLISH)
+)
 %0A       
@@ -2450,18 +2450,43 @@
         
+    .append(ROW_KEY_SUFFIX
 )
-;
 %0A       
@@ -2482,34 +2482,32 @@
 IX)%0A        
-%7D%0A
         
 return key +
@@ -2498,31 +2498,19 @@
     
-return key + %22_row_key%22
+.toString()
 ;%0A  

Simplify string processing on mbdm keys (#480)