import org.apache.commons.lang3.StringUtils;
import com.broadleafcommerce.dataimport.domain.ImportFieldConfig;
import com.broadleafcommerce.dataimport.service.normalizer.ImportDataNormalizer;
import com.broadleafcommerce.dataimport.service.validation.BooleanValidator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import lombok.Getter;
public class UpdateProductNameAndOnlineSpecification extends DefaultSpecification
implements GlobalImportSpecification {
private static String UPDATE_PRODUCT_NAME_AND_ONLINE_IMPORT_TYPE =
"UPDATE_PRODUCT_NAME_AND_ONLINE";
private static String UPDATE_PRODUCT_NAME_AND_ONLINE_SPECIFICATION_NAME =
"Update Product Name and Online";
private static String PRODUCT_ROW_TYPE = "PRODUCT";
@Getter(onMethod_ = @Override)
private final List<ImportDataNormalizer> importDataNormalizers;
public UpdateProductNameAndOnlineSpecification(List<ImportDataNormalizer> normalizers,
List<String> requiredAuthorities,
List<String> requiredScopes) {
super(UPDATE_PRODUCT_NAME_AND_ONLINE_IMPORT_TYPE,
requiredAuthorities,
requiredScopes,
UPDATE_PRODUCT_NAME_AND_ONLINE_SPECIFICATION_NAME);
this.importDataNormalizers = normalizers;
}
@Override
public boolean canHandle(String importType) {
return StringUtils.equals(importType, UPDATE_PRODUCT_NAME_AND_ONLINE_IMPORT_TYPE);
}
@Override
public String getMainRecordType() {
/*
* This defaults to match the import type, but in our case, the row type is different from
* the import type, so we must override this value.
*/
return PRODUCT_ROW_TYPE;
}
@Override
public boolean isCatalogDiscriminated() {
/*
* The entities we're dealing with are catalog-discriminated, so we indicate this to ensure
* correct context information is available.
*/
return true;
}
@Override
public boolean isSandboxDiscriminated() {
/*
* The entities we're dealing with are sandbox-discriminated, so we indicate this to ensure
* correct context information is available.
*/
return true;
}
@Override
public boolean shouldAutoGenerateOperationTypeForEachRecord(String rowType) {
/*
* Since we're requiring an external ID to be provided, the resource tier handler will be
* able to check for each record's existence in the datastore and determine whether it needs
* to be created or updated. The import service doesn't have to do anything eagerly.
*/
return false;
}
@Override
public boolean shouldAutoGenerateResourceTierIdForEachRecord(String rowType) {
/*
* Since we're requiring an external ID to be provided, 'resource tier ID' becomes
* irrelevant and unnecessary to deal with. We already have a mechanism to uniquely identify
* a record, so the import service does not need to eagerly generate or deal with resource
* tier IDs.
*/
return false;
}
@Override
public boolean shouldAllowUnmappedHeaders(String rowType) {
/*
* We only want to honor headers that we've explicitly defined mappings for, and ignore any
* columns we don't recognize.
*/
return false;
}
@Override
protected void populateHeaderFieldConfigsByRowType(
Map<String, Map<String, ImportFieldConfig>> headerFieldConfigsByRowType) {
/*
* Since we don't have more than one row type in this import, we just place all field
* configurations under the 'main' record type, which in this case is product. This is
* particularly important in the case where no row type column is provided in the input
* file.
*/
headerFieldConfigsByRowType.put(getMainRecordType(), fieldConfigurationsForProductRow());
}
private Map<String, ImportFieldConfig> fieldConfigurationsForProductRow() {
/*
* Using a LinkedHashMap provides some consistency in ordering semantics, which can be
* convenient in some cases.
*/
Map<String, ImportFieldConfig> fieldConfigurationByHeader = new LinkedHashMap<>();
fieldConfigurationByHeader.put("External ID", new ImportFieldConfig("externalId", true));
fieldConfigurationByHeader.put("Name", new ImportFieldConfig("name", false));
fieldConfigurationByHeader.put("Is Online", new ImportFieldConfig("online", false)
.withValidator(new BooleanValidator()));
return fieldConfigurationByHeader;
}
}