Subversion Repositories SmartDukaan

Rev

Blame | Last modification | View Log | RSS feed

package com.spice.profitmandi.service.catalog;

import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
import com.spice.profitmandi.dao.entity.catalog.CategorySpecDefinition;
import com.spice.profitmandi.dao.model.CategorySpecPojo;

import java.util.ArrayList;
import java.util.List;

/**
 * Pairs positional SpecValue<N> upload columns with a category's active
 * spec definitions (external catalogMaster categorySpecs). Pure logic, no
 * Spring wiring — the fofo content upload flow calls it per CSV row.
 */
public final class CategorySpecUtil {

    private CategorySpecUtil() {
    }

    public static List<CategorySpecPojo> pairValues(List<CategorySpecDefinition> defs, List<String> values)
            throws ProfitMandiBusinessException {
        List<CategorySpecPojo> specs = new ArrayList<>();
        for (int idx = 0; idx < values.size(); idx++) {
            String value = values.get(idx) == null ? "" : values.get(idx).trim();
            if (value.isEmpty()) {
                continue;
            }
            int position = idx + 1;
            CategorySpecDefinition def = defAt(defs, position);
            if (def == null) {
                throw new ProfitMandiBusinessException("SpecValue" + position, value,
                        "No spec defined at position " + position + " for this category");
            }
            specs.add(new CategorySpecPojo(def.getSpecKey(), def.getDisplayLabel(), value));
        }
        return specs;
    }

    private static CategorySpecDefinition defAt(List<CategorySpecDefinition> defs, int position) {
        for (CategorySpecDefinition def : defs) {
            if (def.getPosition() == position) {
                return def;
            }
        }
        return null;
    }
}