You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在BLC及BLC/Graph扩展中运行导入场景?请提供代码示例

在BLC及BLC/Graph扩展中运行导入场景的实现

一、BLC内部运行导入场景

Broadleaf Commerce(BLC)支持直接在内部运行导入场景,核心是利用其内置的ImportManager组件完成导入流程,只需配置导入文件路径、导入模式等参数即可触发执行。

代码示例

import org.broadleafcommerce.common.exception.ServiceException;
import org.broadleafcommerce.core.catalog.service.CatalogService;
import org.broadleafcommerce.core.importservice.service.ImportManager;
import org.broadleafcommerce.core.importservice.type.ImportBatchMode;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class BlcInternalImportRunner {
    public static void main(String[] args) throws ServiceException {
        // 初始化Spring上下文(需匹配项目实际配置文件路径)
        ApplicationContext appContext = new ClassPathXmlApplicationContext("classpath:blc-application-context.xml");
        
        // 获取核心导入服务Bean
        ImportManager importManager = appContext.getBean(ImportManager.class);
        CatalogService catalogService = appContext.getBean(CatalogService.class);
        
        // 配置导入参数
        String importFileLocation = "classpath:data/initial-catalog-import.csv"; // 导入文件路径
        ImportBatchMode importMode = ImportBatchMode.INSERT_UPDATE; // 导入模式:插入或更新已有数据
        
        // 执行导入,关联目标目录(此处使用ID为1的默认目录,可按需调整)
        importManager.executeImport(importFileLocation, importMode, catalogService.getCatalogById(1L));
        
        System.out.println("内部导入场景执行完成");
    }
}

二、BLC/Graph扩展中执行导入场景

在BLC的GraphQL扩展中,可以通过自定义GraphQL突变(Mutation)暴露导入能力,将导入逻辑封装在Resolver中,通过GraphQL接口触发执行。

1. 定义GraphQL突变类型

在项目的.graphqls schema文件中添加突变定义:

type Mutation {
    # 执行目录数据导入
    runCatalogImport(filePath: String!, batchMode: String!): Boolean
}

2. 实现GraphQL Resolver

import org.broadleafcommerce.core.importservice.service.ImportManager;
import org.broadleafcommerce.core.importservice.type.ImportBatchMode;
import org.springframework.stereotype.Component;
import graphql.kickstart.tools.GraphQLMutationResolver;

@Component
public class ImportOperationResolver implements GraphQLMutationResolver {

    private final ImportManager importManager;

    // 构造注入ImportManager
    public ImportOperationResolver(ImportManager importManager) {
        this.importManager = importManager;
    }

    public Boolean runCatalogImport(String filePath, String batchMode) {
        try {
            // 转换导入模式参数
            ImportBatchMode mode = ImportBatchMode.valueOf(batchMode.trim().toUpperCase());
            // 执行导入,关联目标目录
            importManager.executeImport(filePath, mode, importManager.getCatalogService().getCatalogById(1L));
            return true;
        } catch (IllegalArgumentException e) {
            System.err.println("无效的导入模式:" + batchMode);
            return false;
        } catch (Exception e) {
            System.err.println("导入执行失败:" + e.getMessage());
            return false;
        }
    }
}

注意事项

  • 导入文件路径需保证BLC应用有权限访问,可使用绝对路径或classpath路径。
  • ImportBatchMode支持三种模式:INSERT(仅新增)、UPDATE(仅更新)、INSERT_UPDATE(新增或更新)。
  • 需确保Spring上下文已正确加载BLC导入相关的Bean,避免依赖注入失败。

内容的提问来源于stack exchange,提问作者pmfith

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 14:20:23