From 398a8a3ec3227dc95baf7f91ebc9f5907859348d Mon Sep 17 00:00:00 2001 From: OTTO <731554297@qq.com> Date: Mon, 19 Aug 2024 08:38:46 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E5=88=B6=E5=93=81=E5=BA=93=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=E5=BC=80=E5=8F=91):=20=E4=BA=A7=E5=93=81=E5=BA=93?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=89=93=E5=8C=85=E4=B8=8B=E8=BD=BD=E4=BA=A7?= =?UTF-8?q?=E5=93=81=E5=BA=93=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. 调整产品库服务层命名 2. 新增产品库打包接口 --- .../PmsProductLibraryController.java | 34 +- .../ProductLibraryProxyController.java | 4 +- .../IPmsProductLibraryAsyncService.java | 7 + .../IPmsProductLibraryProductRepoService.java | 81 ----- .../IPmsProductLibraryProxyService.java | 14 + .../service/IPmsProductLibraryService.java | 77 ++++- .../PmsProductLibraryAsyncServiceImpl.java | 12 + ...PmsProductLibraryComponentServiceImpl.java | 10 +- ...sProductLibraryProductRepoServiceImpl.java | 326 ------------------ ...msProductLibraryProxyProxyServiceImpl.java | 25 ++ .../impl/PmsProductLibraryServiceImpl.java | 323 ++++++++++++++++- 11 files changed, 479 insertions(+), 434 deletions(-) delete mode 100644 microservices-modules/microservices-modules-pms/src/main/java/com/microservices/pms/productLibrary/service/IPmsProductLibraryProductRepoService.java create mode 100644 microservices-modules/microservices-modules-pms/src/main/java/com/microservices/pms/productLibrary/service/IPmsProductLibraryProxyService.java delete mode 100644 microservices-modules/microservices-modules-pms/src/main/java/com/microservices/pms/productLibrary/service/impl/PmsProductLibraryProductRepoServiceImpl.java create mode 100644 microservices-modules/microservices-modules-pms/src/main/java/com/microservices/pms/productLibrary/service/impl/PmsProductLibraryProxyProxyServiceImpl.java diff --git a/microservices-modules/microservices-modules-pms/src/main/java/com/microservices/pms/productLibrary/controller/PmsProductLibraryController.java b/microservices-modules/microservices-modules-pms/src/main/java/com/microservices/pms/productLibrary/controller/PmsProductLibraryController.java index c342820fe..3b7bf4f62 100644 --- a/microservices-modules/microservices-modules-pms/src/main/java/com/microservices/pms/productLibrary/controller/PmsProductLibraryController.java +++ b/microservices-modules/microservices-modules-pms/src/main/java/com/microservices/pms/productLibrary/controller/PmsProductLibraryController.java @@ -9,7 +9,7 @@ import com.microservices.common.log.enums.BusinessType; import com.microservices.pms.productLibrary.domain.ProductLibraryRepositoriesFormatEnum; import com.microservices.pms.productLibrary.domain.vo.*; import com.microservices.pms.productLibrary.service.IPmsProductLibraryComponentService; -import com.microservices.pms.productLibrary.service.IPmsProductLibraryProductRepoService; +import com.microservices.pms.productLibrary.service.IPmsProductLibraryService; import io.swagger.annotations.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; @@ -31,7 +31,7 @@ import java.util.List; }) public class PmsProductLibraryController extends BaseController { @Autowired - private IPmsProductLibraryProductRepoService pmsProductLibraryProductRepoService; + private IPmsProductLibraryService pmsProductLibraryService; @Autowired private IPmsProductLibraryComponentService pmsProductLibraryComponentService; @@ -46,7 +46,7 @@ public class PmsProductLibraryController extends BaseController { @ApiParam(value = "产品库制品查询条件") PmsProductLibraryComponentSearchVo search) { search.setPmsEnterpriseIdentifier(enterpriseIdentifier); startPage(); - return pmsProductLibraryProductRepoService.selectComponentList(search); + return pmsProductLibraryService.selectComponentList(search); } /** @@ -57,7 +57,7 @@ public class PmsProductLibraryController extends BaseController { @ApiOperation(value = "获取产品库详情") public GenericsAjaxResult getByRepoName(@PathVariable String enterpriseIdentifier, @ApiParam(value = "产品库标识", required = true) @PathVariable String repoName) { - return GenericsAjaxResult.success(pmsProductLibraryProductRepoService.selectPmsProductLibraryDataByName(enterpriseIdentifier, repoName)); + return GenericsAjaxResult.success(pmsProductLibraryService.selectPmsProductLibraryDataByName(enterpriseIdentifier, repoName)); } /** @@ -70,7 +70,7 @@ public class PmsProductLibraryController extends BaseController { @PathVariable String enterpriseIdentifier, @ApiParam("模糊搜索产品库标识") String name) { startPage(); - return pmsProductLibraryProductRepoService.selectPmsProductLibraryPageList(enterpriseIdentifier, name); + return pmsProductLibraryService.selectPmsProductLibraryPageList(enterpriseIdentifier, name); } /** @@ -83,7 +83,7 @@ public class PmsProductLibraryController extends BaseController { public AjaxResult addProduct(@PathVariable String enterpriseIdentifier, @Valid @RequestBody ProductRepositoriesInputVo productRepositoriesInputVo) { productRepositoriesInputVo.setEnterpriseIdentifier(enterpriseIdentifier); - return toAjax(pmsProductLibraryProductRepoService.insertPmsProductLibrary(productRepositoriesInputVo)); + return toAjax(pmsProductLibraryService.insertPmsProductLibrary(productRepositoriesInputVo)); } /** @@ -98,7 +98,7 @@ public class PmsProductLibraryController extends BaseController { @Valid @RequestBody ProductRepositoriesOperateVo productRepositoriesOperateVo) { productRepositoriesOperateVo.setEnterpriseIdentifier(enterpriseIdentifier); productRepositoriesOperateVo.setName(name); - return toAjax(pmsProductLibraryProductRepoService.updatePmsProductLibrary(productRepositoriesOperateVo)); + return toAjax(pmsProductLibraryService.updatePmsProductLibrary(productRepositoriesOperateVo)); } /** @@ -110,7 +110,7 @@ public class PmsProductLibraryController extends BaseController { @ApiOperation(value = "删除产品库") public AjaxResult remove(@PathVariable String enterpriseIdentifier, @ApiParam(value = "产品库标识", required = true) @PathVariable String name) { - return toAjax(pmsProductLibraryProductRepoService.deletePmsProductLibraryByName(enterpriseIdentifier, name)); + return toAjax(pmsProductLibraryService.deletePmsProductLibraryByName(enterpriseIdentifier, name)); } @@ -266,7 +266,7 @@ public class PmsProductLibraryController extends BaseController { componentSearchVo.setRepositoryName(repoName); componentSearchVo.setProductRepoName(productRepoName); NexusComponentResultVo> result = - pmsProductLibraryProductRepoService.selectPmsProductLibraryKeyComponentList(enterpriseIdentifier, componentSearchVo); + pmsProductLibraryService.selectPmsProductLibraryKeyComponentList(enterpriseIdentifier, componentSearchVo); return success(result); } @@ -284,7 +284,7 @@ public class PmsProductLibraryController extends BaseController { commonAssetAddToProductRepoVo.setEnterpriseIdentifier(enterpriseIdentifier); commonAssetAddToProductRepoVo.setProductRepoName(productRepoName); commonAssetAddToProductRepoVo.setRepoName(repoName); - return toAjax(pmsProductLibraryProductRepoService.addCommonAssetToProductRepo(commonAssetAddToProductRepoVo)); + return toAjax(pmsProductLibraryService.addCommonAssetToProductRepo(commonAssetAddToProductRepoVo)); } /** @@ -301,6 +301,18 @@ public class PmsProductLibraryController extends BaseController { dockerComponentAddToProductRepoVo.setEnterpriseIdentifier(enterpriseIdentifier); dockerComponentAddToProductRepoVo.setProductRepoName(productRepoName); dockerComponentAddToProductRepoVo.setRepoName(repoName); - return toAjax(pmsProductLibraryProductRepoService.addDockerComponentToProductRepo(dockerComponentAddToProductRepoVo)); + return toAjax(pmsProductLibraryService.addDockerComponentToProductRepo(dockerComponentAddToProductRepoVo)); + } + + /** + * 产品库打包 + */ +// @RequiresPermissions("pms:pmsProductLibraryComponent:add") + @Log(title = "产品库打包", businessType = BusinessType.INSERT) + @PostMapping("/packaged/{productRepoName}") + @ApiOperation(value = "产品库打包") + public AjaxResult packagedProduct(@PathVariable String enterpriseIdentifier, + @ApiParam(value = "产品库标识", required = true) @PathVariable String productRepoName) { + return toAjax(pmsProductLibraryService.packagedProduct(enterpriseIdentifier, productRepoName)); } } diff --git a/microservices-modules/microservices-modules-pms/src/main/java/com/microservices/pms/productLibrary/controller/ProductLibraryProxyController.java b/microservices-modules/microservices-modules-pms/src/main/java/com/microservices/pms/productLibrary/controller/ProductLibraryProxyController.java index ae7e437cc..6cd90e08e 100644 --- a/microservices-modules/microservices-modules-pms/src/main/java/com/microservices/pms/productLibrary/controller/ProductLibraryProxyController.java +++ b/microservices-modules/microservices-modules-pms/src/main/java/com/microservices/pms/productLibrary/controller/ProductLibraryProxyController.java @@ -2,7 +2,7 @@ package com.microservices.pms.productLibrary.controller; import com.microservices.common.core.web.controller.BaseController; import com.microservices.common.core.web.domain.AjaxResult; -import com.microservices.pms.productLibrary.service.IPmsProductLibraryService; +import com.microservices.pms.productLibrary.service.IPmsProductLibraryProxyService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; @@ -16,7 +16,7 @@ import javax.servlet.http.HttpServletRequest; @Controller public class ProductLibraryProxyController extends BaseController { @Autowired - private IPmsProductLibraryService pmsProductLibraryService; + private IPmsProductLibraryProxyService pmsProductLibraryService; @ResponseBody diff --git a/microservices-modules/microservices-modules-pms/src/main/java/com/microservices/pms/productLibrary/service/IPmsProductLibraryAsyncService.java b/microservices-modules/microservices-modules-pms/src/main/java/com/microservices/pms/productLibrary/service/IPmsProductLibraryAsyncService.java index 5bccfea7a..5a213cfd1 100644 --- a/microservices-modules/microservices-modules-pms/src/main/java/com/microservices/pms/productLibrary/service/IPmsProductLibraryAsyncService.java +++ b/microservices-modules/microservices-modules-pms/src/main/java/com/microservices/pms/productLibrary/service/IPmsProductLibraryAsyncService.java @@ -47,4 +47,11 @@ public interface IPmsProductLibraryAsyncService { * @param directory */ void asyncAddDockerComponentToProductRepo(PmsProductLibraryRepositories productRepo, PmsProductLibraryRepositories repo, List nexusComponentResultDataVoList, String directory); + + /** + * 打包产品库 + * + * @param productRepo 产品库 + */ + void packagedProduct(PmsProductLibraryRepositories productRepo); } diff --git a/microservices-modules/microservices-modules-pms/src/main/java/com/microservices/pms/productLibrary/service/IPmsProductLibraryProductRepoService.java b/microservices-modules/microservices-modules-pms/src/main/java/com/microservices/pms/productLibrary/service/IPmsProductLibraryProductRepoService.java deleted file mode 100644 index 2958f0766..000000000 --- a/microservices-modules/microservices-modules-pms/src/main/java/com/microservices/pms/productLibrary/service/IPmsProductLibraryProductRepoService.java +++ /dev/null @@ -1,81 +0,0 @@ -package com.microservices.pms.productLibrary.service; - -import com.microservices.common.core.web.page.GenericsTableDataInfo; -import com.microservices.pms.productLibrary.domain.PmsProductLibraryRepositories; -import com.microservices.pms.productLibrary.domain.vo.*; - -import java.util.List; - -/** - * @author OTTO - */ -public interface IPmsProductLibraryProductRepoService { - - PmsProductLibraryRepositoriesDataVo selectPmsProductLibraryDataByName(String enterpriseIdentifier, String repoName); - - /** - * 根据组织标识和名称查找产品库 - * - * @param enterpriseIdentifier 组织标识 - * @param repoName 产品库标识 - * @return 产品库 - */ - PmsProductLibraryRepositories selectPmsProductLibraryByName(String enterpriseIdentifier, String repoName); - - - /** - * 查询产品库列表 - * - * @param enterpriseIdentifier 组织标识 - * @param name 产品库标识 - * @return 产品库列表 - */ - GenericsTableDataInfo selectPmsProductLibraryPageList(String enterpriseIdentifier, String name); - - - /** - * 创建产品库 - * - * @param productRepositoriesInputVo 产品库输入对象 - * @return 结果 - */ - int insertPmsProductLibrary(ProductRepositoriesInputVo productRepositoriesInputVo); - - /** - * 更新产品库 - * - * @param productRepositoriesOperateVo 产品库操作对象 - * @return 结果 - */ - int updatePmsProductLibrary(ProductRepositoriesOperateVo productRepositoriesOperateVo); - - /** - * 删除产品库 - * - * @param enterpriseIdentifier 组织标识 - * @param name 产品库标识 - * @return 产品库 - */ - int deletePmsProductLibraryByName(String enterpriseIdentifier, String name); - - /** - * 选择普通制品移入产品库 - * - * @param commonAssetAddToProductRepoVo 普通制品移入产品库对象 - * @return 移入状态 - */ - boolean addCommonAssetToProductRepo(CommonAssetAddToProductRepoVo commonAssetAddToProductRepoVo); - - /** - * 查询关键制品列表 - * - * @param enterpriseIdentifier 组织标识 - * @param componentSearchVo 查询条件 - * @return 制品列表 - */ - NexusComponentResultVo> selectPmsProductLibraryKeyComponentList(String enterpriseIdentifier, ComponentSearchVo componentSearchVo); - - GenericsTableDataInfo selectComponentList(PmsProductLibraryComponentSearchVo search); - - boolean addDockerComponentToProductRepo(DockerComponentAddToProductRepoVo dockerComponentAddToProductRepoVo); -} diff --git a/microservices-modules/microservices-modules-pms/src/main/java/com/microservices/pms/productLibrary/service/IPmsProductLibraryProxyService.java b/microservices-modules/microservices-modules-pms/src/main/java/com/microservices/pms/productLibrary/service/IPmsProductLibraryProxyService.java new file mode 100644 index 000000000..e0d7bec20 --- /dev/null +++ b/microservices-modules/microservices-modules-pms/src/main/java/com/microservices/pms/productLibrary/service/IPmsProductLibraryProxyService.java @@ -0,0 +1,14 @@ +package com.microservices.pms.productLibrary.service; + + +import com.microservices.common.core.web.domain.AjaxResult; + +import javax.servlet.http.HttpServletRequest; + +/** + * @author OTTO + */ +public interface IPmsProductLibraryProxyService { + + AjaxResult proxyNexusJson(HttpServletRequest request); +} diff --git a/microservices-modules/microservices-modules-pms/src/main/java/com/microservices/pms/productLibrary/service/IPmsProductLibraryService.java b/microservices-modules/microservices-modules-pms/src/main/java/com/microservices/pms/productLibrary/service/IPmsProductLibraryService.java index d658cc83a..b350b1d5d 100644 --- a/microservices-modules/microservices-modules-pms/src/main/java/com/microservices/pms/productLibrary/service/IPmsProductLibraryService.java +++ b/microservices-modules/microservices-modules-pms/src/main/java/com/microservices/pms/productLibrary/service/IPmsProductLibraryService.java @@ -1,14 +1,83 @@ package com.microservices.pms.productLibrary.service; +import com.microservices.common.core.web.page.GenericsTableDataInfo; +import com.microservices.pms.productLibrary.domain.PmsProductLibraryRepositories; +import com.microservices.pms.productLibrary.domain.vo.*; -import com.microservices.common.core.web.domain.AjaxResult; - -import javax.servlet.http.HttpServletRequest; +import java.util.List; /** * @author OTTO */ public interface IPmsProductLibraryService { - AjaxResult proxyNexusJson(HttpServletRequest request); + GenericsTableDataInfo selectComponentList(PmsProductLibraryComponentSearchVo search); + + PmsProductLibraryRepositoriesDataVo selectPmsProductLibraryDataByName(String enterpriseIdentifier, String repoName); + + /** + * 根据组织标识和名称查找产品库 + * + * @param enterpriseIdentifier 组织标识 + * @param repoName 产品库标识 + * @return 产品库 + */ + PmsProductLibraryRepositories selectPmsProductLibraryByName(String enterpriseIdentifier, String repoName); + + + /** + * 查询产品库列表 + * + * @param enterpriseIdentifier 组织标识 + * @param name 产品库标识 + * @return 产品库列表 + */ + GenericsTableDataInfo selectPmsProductLibraryPageList(String enterpriseIdentifier, String name); + + + /** + * 创建产品库 + * + * @param productRepositoriesInputVo 产品库输入对象 + * @return 结果 + */ + int insertPmsProductLibrary(ProductRepositoriesInputVo productRepositoriesInputVo); + + /** + * 更新产品库 + * + * @param productRepositoriesOperateVo 产品库操作对象 + * @return 结果 + */ + int updatePmsProductLibrary(ProductRepositoriesOperateVo productRepositoriesOperateVo); + + /** + * 删除产品库 + * + * @param enterpriseIdentifier 组织标识 + * @param name 产品库标识 + * @return 产品库 + */ + int deletePmsProductLibraryByName(String enterpriseIdentifier, String name); + + /** + * 选择普通制品移入产品库 + * + * @param commonAssetAddToProductRepoVo 普通制品移入产品库对象 + * @return 移入状态 + */ + boolean addCommonAssetToProductRepo(CommonAssetAddToProductRepoVo commonAssetAddToProductRepoVo); + + /** + * 查询关键制品列表 + * + * @param enterpriseIdentifier 组织标识 + * @param componentSearchVo 查询条件 + * @return 制品列表 + */ + NexusComponentResultVo> selectPmsProductLibraryKeyComponentList(String enterpriseIdentifier, ComponentSearchVo componentSearchVo); + + boolean addDockerComponentToProductRepo(DockerComponentAddToProductRepoVo dockerComponentAddToProductRepoVo); + + boolean packagedProduct(String enterpriseIdentifier, String productRepoName); } diff --git a/microservices-modules/microservices-modules-pms/src/main/java/com/microservices/pms/productLibrary/service/impl/PmsProductLibraryAsyncServiceImpl.java b/microservices-modules/microservices-modules-pms/src/main/java/com/microservices/pms/productLibrary/service/impl/PmsProductLibraryAsyncServiceImpl.java index c8890aa82..d4237e301 100644 --- a/microservices-modules/microservices-modules-pms/src/main/java/com/microservices/pms/productLibrary/service/impl/PmsProductLibraryAsyncServiceImpl.java +++ b/microservices-modules/microservices-modules-pms/src/main/java/com/microservices/pms/productLibrary/service/impl/PmsProductLibraryAsyncServiceImpl.java @@ -187,6 +187,18 @@ public class PmsProductLibraryAsyncServiceImpl implements IPmsProductLibraryAsyn }); } + @Override + public void packagedProduct(PmsProductLibraryRepositories productRepo) { + CustomExecutorFactory.threadPoolExecutor.execute(() -> { + pmsProductLibraryRepositoriesService.lockRepository(productRepo.getPmsEnterpriseIdentifier(), productRepo.getId()); + try { + + } finally { + pmsProductLibraryRepositoriesService.unlockRepository(productRepo.getPmsEnterpriseIdentifier(), productRepo.getId()); + } + }); + } + private void syncMavenRepoToSnapshotRepo(PmsProductLibraryRepositories repo, PmsProductLibraryRepositories snapshotRepo) { List pmsProductLibraryComponentList = pmsProductLibraryComponentService.selectPmsProductLibraryComponentListByDatabase(repo.getId()); for (PmsProductLibraryComponent pmsProductLibraryComponent : pmsProductLibraryComponentList) { diff --git a/microservices-modules/microservices-modules-pms/src/main/java/com/microservices/pms/productLibrary/service/impl/PmsProductLibraryComponentServiceImpl.java b/microservices-modules/microservices-modules-pms/src/main/java/com/microservices/pms/productLibrary/service/impl/PmsProductLibraryComponentServiceImpl.java index b62d3af1c..fcd95ae91 100644 --- a/microservices-modules/microservices-modules-pms/src/main/java/com/microservices/pms/productLibrary/service/impl/PmsProductLibraryComponentServiceImpl.java +++ b/microservices-modules/microservices-modules-pms/src/main/java/com/microservices/pms/productLibrary/service/impl/PmsProductLibraryComponentServiceImpl.java @@ -49,7 +49,7 @@ public class PmsProductLibraryComponentServiceImpl implements IPmsProductLibrary private IPmsProductLibraryRepositoriesService pmsProductLibraryRepositoriesService; @Autowired @Lazy - private IPmsProductLibraryProductRepoService pmsProductLibraryProductRepoService; + private IPmsProductLibraryService pmsProductLibraryProductRepoService; @Autowired private IPmsProductLibrarySnapshotRepoService pmsProductLibrarySnapshotRepoService; @Autowired @@ -427,7 +427,7 @@ public class PmsProductLibraryComponentServiceImpl implements IPmsProductLibrary } else { fileBytes = PmsUtils.getFeignFile(fileService.getFileEntityByIdentifier(assetMavenInputVo.getFileIdentifier(), SecurityConstants.INNER)); } - if (fileBytes == null || fileBytes.getFileInputStream() == null) { + if (fileBytes.getFileInputStream() == null) { throw new ServiceException("制品文件读取失败"); } builder.addBinaryBody(String.format("maven2.asset%d", i + 1), fileBytes.getFileInputStream(), ContentType.DEFAULT_BINARY, fileBytes.getFileName()); @@ -487,7 +487,7 @@ public class PmsProductLibraryComponentServiceImpl implements IPmsProductLibrary } else { fileBytes = PmsUtils.getFeignFile(fileService.getFileEntityByIdentifier(assetRawInputVo.getFileIdentifier(), SecurityConstants.INNER)); } - if (fileBytes == null || fileBytes.getFileInputStream() == null) { + if (fileBytes.getFileInputStream() == null) { throw new ServiceException("制品文件读取失败"); } builder.addBinaryBody(String.format("raw.asset%d", i + 1), fileBytes.getFileInputStream(), ContentType.DEFAULT_BINARY, fileBytes.getFileName()); @@ -527,7 +527,7 @@ public class PmsProductLibraryComponentServiceImpl implements IPmsProductLibrary } else { fileBytes = PmsUtils.getFeignFile(fileService.getFileEntityByIdentifier(componentInputVo.getFileIdentifier(), SecurityConstants.INNER)); } - if (fileBytes == null || fileBytes.getFileInputStream() == null) { + if (fileBytes.getFileInputStream() == null) { throw new ServiceException("制品文件读取失败"); } @@ -564,7 +564,7 @@ public class PmsProductLibraryComponentServiceImpl implements IPmsProductLibrary } else { fileBytes = PmsUtils.getFeignFile(fileService.getFileEntityByIdentifier(componentInputVo.getFileIdentifier(), SecurityConstants.INNER)); } - if (fileBytes == null || fileBytes.getFileInputStream() == null) { + if (fileBytes.getFileInputStream() == null) { throw new ServiceException("制品文件读取失败"); } builder.addBinaryBody("pypi.asset1", fileBytes.getFileInputStream(), ContentType.DEFAULT_BINARY, fileBytes.getFileName()); diff --git a/microservices-modules/microservices-modules-pms/src/main/java/com/microservices/pms/productLibrary/service/impl/PmsProductLibraryProductRepoServiceImpl.java b/microservices-modules/microservices-modules-pms/src/main/java/com/microservices/pms/productLibrary/service/impl/PmsProductLibraryProductRepoServiceImpl.java deleted file mode 100644 index e79127c22..000000000 --- a/microservices-modules/microservices-modules-pms/src/main/java/com/microservices/pms/productLibrary/service/impl/PmsProductLibraryProductRepoServiceImpl.java +++ /dev/null @@ -1,326 +0,0 @@ -package com.microservices.pms.productLibrary.service.impl; - -import com.alibaba.fastjson2.JSONObject; -import com.microservices.common.core.exception.ServiceException; -import com.microservices.common.core.utils.DateUtils; -import com.microservices.common.core.utils.PageUtils; -import com.microservices.common.core.utils.StringUtils; -import com.microservices.common.core.utils.html.EscapeUtil; -import com.microservices.common.core.web.page.GenericsTableDataInfo; -import com.microservices.common.security.utils.SecurityUtils; -import com.microservices.pms.enterprise.domain.PmsEnterprise; -import com.microservices.pms.enterprise.service.IPmsEnterpriseService; -import com.microservices.pms.productLibrary.domain.PmsProductLibraryComponent; -import com.microservices.pms.productLibrary.domain.PmsProductLibraryRepositories; -import com.microservices.pms.productLibrary.domain.ProductLibraryRepositoriesFormatEnum; -import com.microservices.pms.productLibrary.domain.vo.*; -import com.microservices.pms.productLibrary.mapper.PmsProductLibraryComponentMapper; -import com.microservices.pms.productLibrary.mapper.PmsProductLibraryRepositoriesMapper; -import com.microservices.pms.productLibrary.service.IPmsProductLibraryAsyncService; -import com.microservices.pms.productLibrary.service.IPmsProductLibraryComponentService; -import com.microservices.pms.productLibrary.service.IPmsProductLibraryProductRepoService; -import com.microservices.pms.productLibrary.service.IPmsProductLibraryRepositoriesService; -import com.microservices.pms.utils.NexusRequestHelper; -import com.microservices.pms.utils.NexusRequestUrl; -import com.microservices.pms.utils.PmsUtils; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; - -import java.util.ArrayList; -import java.util.List; -import java.util.stream.Collectors; - -/** - * @author OTTO - */ -@Service -public class PmsProductLibraryProductRepoServiceImpl implements IPmsProductLibraryProductRepoService { - - @Autowired - private PmsProductLibraryRepositoriesMapper pmsProductLibraryRepositoriesMapper; - @Autowired - private PmsProductLibraryComponentMapper pmsProductLibraryComponentMapper; - @Autowired - private NexusRequestHelper nexusRequestHelper; - @Autowired - private IPmsEnterpriseService pmsEnterpriseService; - @Autowired - private IPmsProductLibraryAsyncService pmsProductLibraryAsyncService; - @Autowired - private IPmsProductLibraryComponentService pmsProductLibraryComponentService; - @Autowired - private IPmsProductLibraryRepositoriesService pmsProductLibraryRepositoriesService; - - @Override - public PmsProductLibraryRepositoriesDataVo selectPmsProductLibraryDataByName(String enterpriseIdentifier, String repoName) { - PmsProductLibraryRepositories pmsProductLibraryRepositories = selectPmsProductLibraryByName(enterpriseIdentifier, repoName); - return pmsProductLibraryRepositories.toPmsProductLibraryRepositoriesDataVo(); - } - - - @Override - public PmsProductLibraryRepositories selectPmsProductLibraryByName(String enterpriseIdentifier, String name) { - PmsProductLibraryRepositories pmsProductLibraryRepositories = pmsProductLibraryRepositoriesMapper.selectPmsProductLibraryByName(enterpriseIdentifier, name); - if (pmsProductLibraryRepositories == null) { - throw new ServiceException("组织下不存在该产品库(组织标识[%s],产品库标识[%s])", enterpriseIdentifier, name); - } - return pmsProductLibraryRepositories; - } - - private PmsProductLibraryRepositories selectPmsProductLibraryRepositoriesById(Long repoId) { - PmsProductLibraryRepositories pmsProductLibraryRepositories = - pmsProductLibraryRepositoriesMapper.selectProductRepoById(repoId); - if (pmsProductLibraryRepositories == null) { - throw new ServiceException("组织下不存在该产品库(产品库id[%d])", repoId); - } - return pmsProductLibraryRepositories; - } - - @Override - public GenericsTableDataInfo selectPmsProductLibraryPageList(String enterpriseIdentifier, String name) { - return PageUtils.toPage(pmsProductLibraryRepositoriesMapper.selectPmsProductLibraryList(enterpriseIdentifier, name), PmsProductLibraryRepositories::toPmsProductLibrarySimpleVo); - } - - @Override - public int updatePmsProductLibrary(ProductRepositoriesOperateVo productRepositoriesOperateVo) { - PmsProductLibraryRepositories oldRepo = selectPmsProductLibraryByName(productRepositoriesOperateVo.getEnterpriseIdentifier(), productRepositoriesOperateVo.getName()); - - // 校验制品库别名 - PmsProductLibraryRepositories aliasRepo = pmsProductLibraryRepositoriesMapper.selectPmsProductLibraryRepositoriesByAlias(productRepositoriesOperateVo.getAlias(), productRepositoriesOperateVo.getEnterpriseIdentifier()); - if (aliasRepo != null && !aliasRepo.getId().equals(oldRepo.getId())) { - throw new ServiceException("该产品库名称在当前组织下已存在(产品库名称[%s])", productRepositoriesOperateVo.getAlias()); - } - // 构建更新对象 - PmsProductLibraryRepositories pmsProductLibraryRepositories = productRepositoriesOperateVo.toPmsProductLibraryRepositories(); - pmsProductLibraryRepositories.setId(oldRepo.getId()); - pmsProductLibraryRepositories.setUpdateTime(DateUtils.getNowDate()); - pmsProductLibraryRepositories.setUpdateBy(SecurityUtils.getUsername()); - NexusHostedRepoInputVo requestBody = productRepositoriesOperateVo.toNexusRepoDataVo(); - - // 调用制品库更新接口更新制品库 - nexusRequestHelper.doRequest(NexusRequestUrl.UPDATE_REPOSITORIES(requestBody.getType(), requestBody.getFormat(), requestBody.getName()), JSONObject.from(requestBody)); - - return pmsProductLibraryRepositoriesMapper.updatePmsProductLibraryRepositories(pmsProductLibraryRepositories); - } - - - @Override - public int insertPmsProductLibrary(ProductRepositoriesInputVo productRepositoriesInputVo) { - PmsEnterprise pmsEnterprise = pmsEnterpriseService.selectPmsEnterpriseByIdentifier(productRepositoriesInputVo.getEnterpriseIdentifier()); - // 校验制品库标识 - Integer count = pmsProductLibraryRepositoriesMapper.selectPmsProductLibraryRepositoriesByNameCount(productRepositoriesInputVo.getName(), productRepositoriesInputVo.getEnterpriseIdentifier(), productRepositoriesInputVo.getFormat()); - if (count > 0) { - throw new ServiceException("该产品库标识在当前组织下已存在(产品库标识[%s])", productRepositoriesInputVo.getName()); - } - // 校验制品库别名 - PmsProductLibraryRepositories aliasRepo = pmsProductLibraryRepositoriesMapper.selectPmsProductLibraryRepositoriesByAlias(productRepositoriesInputVo.getAlias(), productRepositoriesInputVo.getEnterpriseIdentifier()); - if (aliasRepo != null) { - throw new ServiceException("该产品库名称在当前组织下已存在(产品库名称[%s])", productRepositoriesInputVo.getAlias()); - } - NexusHostedRepoInputVo requestBody = productRepositoriesInputVo.toNexusRepoDataVo(); - - // 调用制品库创建接口创建产品库 - nexusRequestHelper.doRequest(NexusRequestUrl.ADD_REPOSITORIES(requestBody.getType(), requestBody.getFormat()), JSONObject.from(requestBody)); - // 调用Nexus制品库详情接口获取制品库详情 - NexusHostedRepoInputVo nexusDataVo = pmsProductLibraryRepositoriesService.getNexusRepo( - productRepositoriesInputVo.getType(), - productRepositoriesInputVo.getFormat(), - PmsProductLibraryRepositories.getNexusProductRepositoriesName( - productRepositoriesInputVo.getEnterpriseIdentifier(), - productRepositoriesInputVo.getName() - )); - // 数据库添加该制品库 - PmsProductLibraryRepositories pmsProductLibraryRepositories = productRepositoriesInputVo.toPmsProductLibraryRepositories(); - pmsProductLibraryRepositories.setUrl(nexusDataVo.getUrl()); - pmsProductLibraryRepositories.setPmsEnterpriseId(pmsEnterprise.getId()); - pmsProductLibraryRepositories.setDeptId(pmsEnterprise.getDeptId()); - pmsProductLibraryRepositories.setCreateTime(DateUtils.getNowDate()); - pmsProductLibraryRepositories.setCreateBy(SecurityUtils.getUsername()); - return pmsProductLibraryRepositoriesMapper.insertPmsProductLibraryRepositories(pmsProductLibraryRepositories); - } - - @Override - public int deletePmsProductLibraryByName(String enterpriseIdentifier, String name) { - PmsProductLibraryRepositories oldRepo = selectPmsProductLibraryByName(enterpriseIdentifier, name); - // 调用制品库删除接口删除制品库 - nexusRequestHelper.doRequest(NexusRequestUrl.DELETE_REPOSITORIES(PmsProductLibraryRepositories.getNexusProductRepositoriesName(enterpriseIdentifier, name))); - return pmsProductLibraryRepositoriesMapper.deletePmsProductLibraryRepositoriesById(oldRepo.getId()); - } - - @Override - public boolean addCommonAssetToProductRepo(CommonAssetAddToProductRepoVo commonAssetAddToProductRepoVo) { - // 先校验产品库、制品库、制品库下制品Id列表是否存在,再进行异步处理 - PmsProductLibraryRepositories productRepo = selectPmsProductLibraryByName(commonAssetAddToProductRepoVo.getEnterpriseIdentifier(), commonAssetAddToProductRepoVo.getProductRepoName()); - PmsProductLibraryRepositories repo = pmsProductLibraryRepositoriesService.selectPmsProductLibraryRepositoriesByName(commonAssetAddToProductRepoVo.getEnterpriseIdentifier(), commonAssetAddToProductRepoVo.getRepoName()); - if (ProductLibraryRepositoriesFormatEnum.DOCKER.getKey().equals(repo.getFormat())) { - throw new ServiceException("Docker制品移入不能使用该接口"); - } - List nexusAssetResultDataVoList = new ArrayList<>(); - for (String assetId : commonAssetAddToProductRepoVo.getAssetIdList()) { - NexusComponentResultVo nexusAssetResultVo = pmsProductLibraryComponentService.getAssetDetailByAssetId(commonAssetAddToProductRepoVo.getEnterpriseIdentifier(), commonAssetAddToProductRepoVo.getRepoName(), null, assetId, false); - if (nexusAssetResultVo == null || nexusAssetResultVo.getResult() == null || !nexusAssetResultVo.getResult().getSuccess()) { - throw new ServiceException("该制品不存在(制品库[%s],制品Id[%s])", commonAssetAddToProductRepoVo.getRepoName(), assetId); - } else { - NexusAssetResultDataVo data = nexusAssetResultVo.getResult().getData(); - if (pmsProductLibraryComponentService.getLockComponentStatus(productRepo.getId(), assetId)) { - throw new ServiceException("制品锁定中,无法被移入产品库(制品名称[%s],产品库名称[%s])", data.getName(), productRepo.getName()); - } - PmsProductLibraryComponent pmsProductLibraryComponent = pmsProductLibraryComponentMapper.selectPmsProductLibraryComponentByIdentifier(productRepo.getId(), assetId); - if (pmsProductLibraryComponent != null) { - throw new ServiceException("制品已被移入产品库中,无法重复移入(制品名称[%s],产品库名称[%s])", data.getName(), productRepo.getName()); - } - nexusAssetResultDataVoList.add(data); - } - } - pmsProductLibraryAsyncService.asyncAddCommonAssetToProductRepo(productRepo, repo, nexusAssetResultDataVoList, commonAssetAddToProductRepoVo.getDirectory()); - return true; - } - - @Override - public NexusComponentResultVo> selectPmsProductLibraryKeyComponentList(String enterpriseIdentifier, ComponentSearchVo componentSearchVo) { - PmsProductLibraryRepositories productRepo = pmsProductLibraryComponentService.getPmsProductLibraryRepositories(true, enterpriseIdentifier, componentSearchVo.getProductRepoName(), null); - PmsProductLibraryComponent productComponentSearch = new PmsProductLibraryComponent(); - productComponentSearch.setRepoId(productRepo.getId()); - List productComponentList = pmsProductLibraryComponentMapper.selectPmsProductLibraryComponentList(productComponentSearch); - PmsProductLibraryRepositories repo = pmsProductLibraryComponentService.getPmsProductLibraryRepositories(false, enterpriseIdentifier, componentSearchVo.getRepositoryName(), null); - componentSearchVo.setRepositoryName(repo.getNexusRepositoriesName()); - // Docker类型调用Nexus获取制品列表接口获取 - if (ProductLibraryRepositoriesFormatEnum.DOCKER.getKey().equals(repo.getFormat())) { - List keyNexusResultDataVoList = new ArrayList<>(); - // 需循环遍历获取所有镜像列表 - boolean isAll = false; - // Nexus分页Token - String continuationToken = null; - while (!isAll) { - NexusComponentAndAssetListVo assetListVo = nexusRequestHelper.doRequestToJavaObject(NexusRequestUrl.GET_COMPONENTS(repo.getNexusRepositoriesName(), continuationToken), NexusComponentAndAssetListVo.class, NexusDockerComponentVo.class); - if (assetListVo != null) { - if (StringUtils.isEmpty(assetListVo.getContinuationToken())) { - isAll = true; - } else { - continuationToken = assetListVo.getContinuationToken(); - } - for (NexusDockerComponentVo nexusDockerComponentVo : assetListVo.getItems()) { - NexusResultDataVo nexusResultDataVo = nexusDockerComponentVo.toNexusResultDataVo(); - keyNexusResultDataVoList.add(nexusResultDataVo); - } - } else { - isAll = true; - } - - } - setNexusResultDataVoChoose(productRepo, productComponentList, keyNexusResultDataVoList, true); - setNexusResultDataVoPath(repo, keyNexusResultDataVoList); - return buildDockerResultVo(keyNexusResultDataVoList); - } else { - NexusComponentSearchVo nexusComponentSearchVo = NexusComponentSearchVo.toRead(componentSearchVo, nexusRequestHelper.getNexusTid()); - NexusComponentResultVo> assetResultDataVo = nexusRequestHelper.doRequestToJavaObjectList(NexusRequestUrl.DO_OPERATION(), JSONObject.from(nexusComponentSearchVo), NexusComponentResultVo.class, NexusResultDataVo.class); - if (assetResultDataVo != null && assetResultDataVo.getResult() != null) { - NexusComponentResultStateVo> nexusComponentResultStateVo = assetResultDataVo.getResult(); - if (nexusComponentResultStateVo.getSuccess()) { - List nexusResultDataVoList = nexusComponentResultStateVo.getData(); - setNexusResultDataVoChoose(productRepo, productComponentList, nexusResultDataVoList, false); - setNexusResultDataVoPath(repo, nexusResultDataVoList); - // 当制品库为Maven时,需排除自动生成的制品数据 - if (ProductLibraryRepositoriesFormatEnum.MAVEN.getKey().equals(repo.getFormat())) { - List keyNexusResultDataVoList = new ArrayList<>(); - for (NexusResultDataVo nexusResultDataVo : nexusResultDataVoList) { - // 当类型为asset时,需检查数据库是否存在记录,若存在则返回 - if ("asset".equals(nexusResultDataVo.getType())) { - PmsProductLibraryComponent pmsProductLibraryComponent = pmsProductLibraryComponentMapper.selectPmsProductLibraryComponentByPath(nexusResultDataVo.getId(), repo.getId(), repo.getPmsEnterpriseId()); - if (pmsProductLibraryComponent != null) { - keyNexusResultDataVoList.add(nexusResultDataVo); - } - } else { - keyNexusResultDataVoList.add(nexusResultDataVo); - } - } - nexusComponentResultStateVo.setData(keyNexusResultDataVoList); - assetResultDataVo.setResult(nexusComponentResultStateVo); - } - } - } - return assetResultDataVo; - } - } - - @Override - public boolean addDockerComponentToProductRepo(DockerComponentAddToProductRepoVo dockerComponentAddToProductRepoVo) { - // 先校验产品库、制品库、制品库下制品Id列表是否存在,再进行异步处理 - PmsProductLibraryRepositories productRepo = selectPmsProductLibraryByName(dockerComponentAddToProductRepoVo.getEnterpriseIdentifier(), dockerComponentAddToProductRepoVo.getProductRepoName()); - PmsProductLibraryRepositories repo = pmsProductLibraryRepositoriesService.selectPmsProductLibraryRepositoriesByName(dockerComponentAddToProductRepoVo.getEnterpriseIdentifier(), dockerComponentAddToProductRepoVo.getRepoName()); - if (!ProductLibraryRepositoriesFormatEnum.DOCKER.getKey().equals(repo.getFormat())) { - throw new ServiceException("当前接口仅允许进行Docker移入产品库操作"); - } - List nexusComponentResultDataVoList = new ArrayList<>(); - for (String componentId : dockerComponentAddToProductRepoVo.getComponentIdList()) { - NexusComponentResultVo nexusAssetResultVo = pmsProductLibraryComponentService.getComponentDetailByComponentId(dockerComponentAddToProductRepoVo.getEnterpriseIdentifier(), dockerComponentAddToProductRepoVo.getRepoName(), null, componentId, false); - if (nexusAssetResultVo == null || nexusAssetResultVo.getResult() == null || !nexusAssetResultVo.getResult().getSuccess()) { - throw new ServiceException("该制品不存在(制品库[%s],制品Id[%s])", dockerComponentAddToProductRepoVo.getRepoName(), componentId); - } else { - NexusComponentResultDataVo data = nexusAssetResultVo.getResult().getData(); - if (pmsProductLibraryComponentService.getLockComponentStatus(productRepo.getId(), componentId)) { - throw new ServiceException("制品锁定中,无法被移入产品库(制品名称[%s],产品库名称[%s])", data.getName(), productRepo.getName()); - } - PmsProductLibraryComponent pmsProductLibraryComponent = pmsProductLibraryComponentMapper.selectPmsProductLibraryComponentByIdentifier(productRepo.getId(), componentId); - if (pmsProductLibraryComponent != null) { - throw new ServiceException("制品已被移入产品库中,无法重复移入(制品名称[%s],产品库名称[%s])", data.getName(), productRepo.getName()); - } - nexusComponentResultDataVoList.add(data); - } - } - pmsProductLibraryAsyncService.asyncAddDockerComponentToProductRepo(productRepo, repo, nexusComponentResultDataVoList, dockerComponentAddToProductRepoVo.getDirectory()); - return true; - } - @Override - public GenericsTableDataInfo selectComponentList(PmsProductLibraryComponentSearchVo search) { - List pmsProductLibraryComponentList = - pmsProductLibraryComponentMapper.selectProductRepoComponentList(search); - List repoIdList = pmsProductLibraryComponentList.stream().map(PmsProductLibraryComponent::getRepoId).distinct().collect(Collectors.toList()); - List repoList = repoIdList.stream().map(this::selectPmsProductLibraryRepositoriesById).collect(Collectors.toList()); - return PageUtils.toPage(pmsProductLibraryComponentList, x -> toPmsProductLibraryComponentDataVo(x, repoList)); - } - - private PmsProductLibraryComponentDataVo toPmsProductLibraryComponentDataVo(PmsProductLibraryComponent component, List repoList) { - PmsProductLibraryComponentDataVo pmsProductLibraryComponentDataVo = component.toPmsProductLibraryComponentDataVo(); - repoList.stream().filter(x -> x.getId().equals(component.getRepoId())).findFirst().ifPresent(repo -> pmsProductLibraryComponentDataVo.setDownloadUrl( - EscapeUtil.removeExtraSlashOfUrl( - String.format("%s/%s", repo.getUrl(), component.getPath())))); - return pmsProductLibraryComponentDataVo; - } - private void setNexusResultDataVoChoose(PmsProductLibraryRepositories productRepo, List productComponentList, List nexusResultDataVoList, boolean isDocker) { - for (PmsProductLibraryComponent component : productComponentList) { - nexusResultDataVoList.forEach(x -> { - if (pmsProductLibraryComponentService.getLockComponentStatus(productRepo.getId(), x.getComponentId())) { - x.setIsChoose(true); - } else if (isDocker && StringUtils.isNotEmpty(x.getComponentId())) { - if (x.getComponentId().equals(component.getIdentifier())) { - x.setIsChoose(true); - } - } else if (StringUtils.isNotEmpty(x.getAssetId()) && x.getAssetId().equals(component.getIdentifier())) { - x.setIsChoose(true); - } - }); - } - } - private void setNexusResultDataVoPath(PmsProductLibraryRepositories repo, List nexusResultDataVoList) { - nexusResultDataVoList.forEach(x -> { - if (ProductLibraryRepositoriesFormatEnum.DOCKER.getKey().equals(repo.getFormat())) { - x.setDefaultDir(EscapeUtil.removeExtraSlashOfUrl(String.format("/%s/", repo.getAlias()))); - } else { - x.setDefaultDir(EscapeUtil.removeExtraSlashOfUrl(String.format("/%s/%s", repo.getAlias(), PmsUtils.parseDirectory(x.getId())))); - } - }); - } - private NexusComponentResultVo> buildDockerResultVo(List data) { - NexusComponentResultVo> target = new NexusComponentResultVo<>(); - target.setAction("coreui_Browse"); - target.setMethod("read"); - target.setTid(1); - target.setType("rpc"); - NexusComponentResultStateVo> nexusComponentResultStateVo = new NexusComponentResultStateVo<>(); - nexusComponentResultStateVo.setData(data); - nexusComponentResultStateVo.setSuccess(true); - target.setResult(nexusComponentResultStateVo); - return target; - } -} diff --git a/microservices-modules/microservices-modules-pms/src/main/java/com/microservices/pms/productLibrary/service/impl/PmsProductLibraryProxyProxyServiceImpl.java b/microservices-modules/microservices-modules-pms/src/main/java/com/microservices/pms/productLibrary/service/impl/PmsProductLibraryProxyProxyServiceImpl.java new file mode 100644 index 000000000..087e23177 --- /dev/null +++ b/microservices-modules/microservices-modules-pms/src/main/java/com/microservices/pms/productLibrary/service/impl/PmsProductLibraryProxyProxyServiceImpl.java @@ -0,0 +1,25 @@ +package com.microservices.pms.productLibrary.service.impl; + +import com.microservices.common.core.web.domain.AjaxResult; +import com.microservices.pms.productLibrary.service.IPmsProductLibraryProxyService; +import com.microservices.pms.utils.NexusRequestHelper; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import javax.servlet.http.HttpServletRequest; + + +/** + * @author OTTO + */ +@Service +public class PmsProductLibraryProxyProxyServiceImpl implements IPmsProductLibraryProxyService { + + @Autowired + private NexusRequestHelper nexusRequestHelper; + + @Override + public AjaxResult proxyNexusJson(HttpServletRequest request) { + return AjaxResult.success(nexusRequestHelper.doRequest(request)); + } +} diff --git a/microservices-modules/microservices-modules-pms/src/main/java/com/microservices/pms/productLibrary/service/impl/PmsProductLibraryServiceImpl.java b/microservices-modules/microservices-modules-pms/src/main/java/com/microservices/pms/productLibrary/service/impl/PmsProductLibraryServiceImpl.java index d038f0e31..75f87717b 100644 --- a/microservices-modules/microservices-modules-pms/src/main/java/com/microservices/pms/productLibrary/service/impl/PmsProductLibraryServiceImpl.java +++ b/microservices-modules/microservices-modules-pms/src/main/java/com/microservices/pms/productLibrary/service/impl/PmsProductLibraryServiceImpl.java @@ -1,13 +1,34 @@ package com.microservices.pms.productLibrary.service.impl; -import com.microservices.common.core.web.domain.AjaxResult; +import com.alibaba.fastjson2.JSONObject; +import com.microservices.common.core.exception.ServiceException; +import com.microservices.common.core.utils.DateUtils; +import com.microservices.common.core.utils.PageUtils; +import com.microservices.common.core.utils.StringUtils; +import com.microservices.common.core.utils.html.EscapeUtil; +import com.microservices.common.core.web.page.GenericsTableDataInfo; +import com.microservices.common.security.utils.SecurityUtils; +import com.microservices.pms.enterprise.domain.PmsEnterprise; +import com.microservices.pms.enterprise.service.IPmsEnterpriseService; +import com.microservices.pms.productLibrary.domain.PmsProductLibraryComponent; +import com.microservices.pms.productLibrary.domain.PmsProductLibraryRepositories; +import com.microservices.pms.productLibrary.domain.ProductLibraryRepositoriesFormatEnum; +import com.microservices.pms.productLibrary.domain.vo.*; +import com.microservices.pms.productLibrary.mapper.PmsProductLibraryComponentMapper; +import com.microservices.pms.productLibrary.mapper.PmsProductLibraryRepositoriesMapper; +import com.microservices.pms.productLibrary.service.IPmsProductLibraryAsyncService; +import com.microservices.pms.productLibrary.service.IPmsProductLibraryComponentService; +import com.microservices.pms.productLibrary.service.IPmsProductLibraryRepositoriesService; import com.microservices.pms.productLibrary.service.IPmsProductLibraryService; import com.microservices.pms.utils.NexusRequestHelper; +import com.microservices.pms.utils.NexusRequestUrl; +import com.microservices.pms.utils.PmsUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; -import javax.servlet.http.HttpServletRequest; - +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; /** * @author OTTO @@ -15,11 +36,303 @@ import javax.servlet.http.HttpServletRequest; @Service public class PmsProductLibraryServiceImpl implements IPmsProductLibraryService { + @Autowired + private PmsProductLibraryRepositoriesMapper pmsProductLibraryRepositoriesMapper; + @Autowired + private PmsProductLibraryComponentMapper pmsProductLibraryComponentMapper; @Autowired private NexusRequestHelper nexusRequestHelper; + @Autowired + private IPmsEnterpriseService pmsEnterpriseService; + @Autowired + private IPmsProductLibraryAsyncService pmsProductLibraryAsyncService; + @Autowired + private IPmsProductLibraryComponentService pmsProductLibraryComponentService; + @Autowired + private IPmsProductLibraryRepositoriesService pmsProductLibraryRepositoriesService; @Override - public AjaxResult proxyNexusJson(HttpServletRequest request) { - return AjaxResult.success(nexusRequestHelper.doRequest(request)); + public GenericsTableDataInfo selectComponentList(PmsProductLibraryComponentSearchVo search) { + List pmsProductLibraryComponentList = + pmsProductLibraryComponentMapper.selectProductRepoComponentList(search); + List repoIdList = pmsProductLibraryComponentList.stream().map(PmsProductLibraryComponent::getRepoId).distinct().collect(Collectors.toList()); + List repoList = repoIdList.stream().map(this::selectPmsProductLibraryRepositoriesById).collect(Collectors.toList()); + return PageUtils.toPage(pmsProductLibraryComponentList, x -> toPmsProductLibraryComponentDataVo(x, repoList)); + } + + private PmsProductLibraryRepositories selectPmsProductLibraryRepositoriesById(Long repoId) { + PmsProductLibraryRepositories pmsProductLibraryRepositories = + pmsProductLibraryRepositoriesMapper.selectProductRepoById(repoId); + if (pmsProductLibraryRepositories == null) { + throw new ServiceException("组织下不存在该产品库(产品库id[%d])", repoId); + } + return pmsProductLibraryRepositories; + } + + private PmsProductLibraryComponentDataVo toPmsProductLibraryComponentDataVo(PmsProductLibraryComponent component, List repoList) { + PmsProductLibraryComponentDataVo pmsProductLibraryComponentDataVo = component.toPmsProductLibraryComponentDataVo(); + repoList.stream().filter(x -> x.getId().equals(component.getRepoId())).findFirst().ifPresent(repo -> pmsProductLibraryComponentDataVo.setDownloadUrl( + EscapeUtil.removeExtraSlashOfUrl( + String.format("%s/%s", repo.getUrl(), component.getPath())))); + return pmsProductLibraryComponentDataVo; + } + + @Override + public PmsProductLibraryRepositoriesDataVo selectPmsProductLibraryDataByName(String enterpriseIdentifier, String repoName) { + PmsProductLibraryRepositories pmsProductLibraryRepositories = selectPmsProductLibraryByName(enterpriseIdentifier, repoName); + return pmsProductLibraryRepositories.toPmsProductLibraryRepositoriesDataVo(); + } + + + @Override + public PmsProductLibraryRepositories selectPmsProductLibraryByName(String enterpriseIdentifier, String name) { + PmsProductLibraryRepositories pmsProductLibraryRepositories = pmsProductLibraryRepositoriesMapper.selectPmsProductLibraryByName(enterpriseIdentifier, name); + if (pmsProductLibraryRepositories == null) { + throw new ServiceException("组织下不存在该产品库(组织标识[%s],产品库标识[%s])", enterpriseIdentifier, name); + } + return pmsProductLibraryRepositories; + } + + @Override + public GenericsTableDataInfo selectPmsProductLibraryPageList(String enterpriseIdentifier, String name) { + return PageUtils.toPage(pmsProductLibraryRepositoriesMapper.selectPmsProductLibraryList(enterpriseIdentifier, name), PmsProductLibraryRepositories::toPmsProductLibrarySimpleVo); + } + + @Override + public int updatePmsProductLibrary(ProductRepositoriesOperateVo productRepositoriesOperateVo) { + PmsProductLibraryRepositories oldRepo = selectPmsProductLibraryByName(productRepositoriesOperateVo.getEnterpriseIdentifier(), productRepositoriesOperateVo.getName()); + + // 校验制品库别名 + PmsProductLibraryRepositories aliasRepo = pmsProductLibraryRepositoriesMapper.selectPmsProductLibraryRepositoriesByAlias(productRepositoriesOperateVo.getAlias(), productRepositoriesOperateVo.getEnterpriseIdentifier()); + if (aliasRepo != null && !aliasRepo.getId().equals(oldRepo.getId())) { + throw new ServiceException("该产品库名称在当前组织下已存在(产品库名称[%s])", productRepositoriesOperateVo.getAlias()); + } + // 构建更新对象 + PmsProductLibraryRepositories pmsProductLibraryRepositories = productRepositoriesOperateVo.toPmsProductLibraryRepositories(); + pmsProductLibraryRepositories.setId(oldRepo.getId()); + pmsProductLibraryRepositories.setUpdateTime(DateUtils.getNowDate()); + pmsProductLibraryRepositories.setUpdateBy(SecurityUtils.getUsername()); + NexusHostedRepoInputVo requestBody = productRepositoriesOperateVo.toNexusRepoDataVo(); + + // 调用制品库更新接口更新制品库 + nexusRequestHelper.doRequest(NexusRequestUrl.UPDATE_REPOSITORIES(requestBody.getType(), requestBody.getFormat(), requestBody.getName()), JSONObject.from(requestBody)); + + return pmsProductLibraryRepositoriesMapper.updatePmsProductLibraryRepositories(pmsProductLibraryRepositories); + } + + + @Override + public int insertPmsProductLibrary(ProductRepositoriesInputVo productRepositoriesInputVo) { + PmsEnterprise pmsEnterprise = pmsEnterpriseService.selectPmsEnterpriseByIdentifier(productRepositoriesInputVo.getEnterpriseIdentifier()); + // 校验制品库标识 + Integer count = pmsProductLibraryRepositoriesMapper.selectPmsProductLibraryRepositoriesByNameCount(productRepositoriesInputVo.getName(), productRepositoriesInputVo.getEnterpriseIdentifier(), productRepositoriesInputVo.getFormat()); + if (count > 0) { + throw new ServiceException("该产品库标识在当前组织下已存在(产品库标识[%s])", productRepositoriesInputVo.getName()); + } + // 校验制品库别名 + PmsProductLibraryRepositories aliasRepo = pmsProductLibraryRepositoriesMapper.selectPmsProductLibraryRepositoriesByAlias(productRepositoriesInputVo.getAlias(), productRepositoriesInputVo.getEnterpriseIdentifier()); + if (aliasRepo != null) { + throw new ServiceException("该产品库名称在当前组织下已存在(产品库名称[%s])", productRepositoriesInputVo.getAlias()); + } + NexusHostedRepoInputVo requestBody = productRepositoriesInputVo.toNexusRepoDataVo(); + + // 调用制品库创建接口创建产品库 + nexusRequestHelper.doRequest(NexusRequestUrl.ADD_REPOSITORIES(requestBody.getType(), requestBody.getFormat()), JSONObject.from(requestBody)); + // 调用Nexus制品库详情接口获取制品库详情 + NexusHostedRepoInputVo nexusDataVo = pmsProductLibraryRepositoriesService.getNexusRepo( + productRepositoriesInputVo.getType(), + productRepositoriesInputVo.getFormat(), + PmsProductLibraryRepositories.getNexusProductRepositoriesName( + productRepositoriesInputVo.getEnterpriseIdentifier(), + productRepositoriesInputVo.getName() + )); + // 数据库添加该制品库 + PmsProductLibraryRepositories pmsProductLibraryRepositories = productRepositoriesInputVo.toPmsProductLibraryRepositories(); + pmsProductLibraryRepositories.setUrl(nexusDataVo.getUrl()); + pmsProductLibraryRepositories.setPmsEnterpriseId(pmsEnterprise.getId()); + pmsProductLibraryRepositories.setDeptId(pmsEnterprise.getDeptId()); + pmsProductLibraryRepositories.setCreateTime(DateUtils.getNowDate()); + pmsProductLibraryRepositories.setCreateBy(SecurityUtils.getUsername()); + return pmsProductLibraryRepositoriesMapper.insertPmsProductLibraryRepositories(pmsProductLibraryRepositories); + } + + @Override + public int deletePmsProductLibraryByName(String enterpriseIdentifier, String name) { + PmsProductLibraryRepositories oldRepo = selectPmsProductLibraryByName(enterpriseIdentifier, name); + // 调用制品库删除接口删除制品库 + nexusRequestHelper.doRequest(NexusRequestUrl.DELETE_REPOSITORIES(PmsProductLibraryRepositories.getNexusProductRepositoriesName(enterpriseIdentifier, name))); + return pmsProductLibraryRepositoriesMapper.deletePmsProductLibraryRepositoriesById(oldRepo.getId()); + } + + @Override + public boolean addCommonAssetToProductRepo(CommonAssetAddToProductRepoVo commonAssetAddToProductRepoVo) { + // 先校验产品库、制品库、制品库下制品Id列表是否存在,再进行异步处理 + PmsProductLibraryRepositories productRepo = selectPmsProductLibraryByName(commonAssetAddToProductRepoVo.getEnterpriseIdentifier(), commonAssetAddToProductRepoVo.getProductRepoName()); + PmsProductLibraryRepositories repo = pmsProductLibraryRepositoriesService.selectPmsProductLibraryRepositoriesByName(commonAssetAddToProductRepoVo.getEnterpriseIdentifier(), commonAssetAddToProductRepoVo.getRepoName()); + if (ProductLibraryRepositoriesFormatEnum.DOCKER.getKey().equals(repo.getFormat())) { + throw new ServiceException("Docker制品移入不能使用该接口"); + } + List nexusAssetResultDataVoList = new ArrayList<>(); + for (String assetId : commonAssetAddToProductRepoVo.getAssetIdList()) { + NexusComponentResultVo nexusAssetResultVo = pmsProductLibraryComponentService.getAssetDetailByAssetId(commonAssetAddToProductRepoVo.getEnterpriseIdentifier(), commonAssetAddToProductRepoVo.getRepoName(), null, assetId, false); + if (nexusAssetResultVo == null || nexusAssetResultVo.getResult() == null || !nexusAssetResultVo.getResult().getSuccess()) { + throw new ServiceException("该制品不存在(制品库[%s],制品Id[%s])", commonAssetAddToProductRepoVo.getRepoName(), assetId); + } else { + NexusAssetResultDataVo data = nexusAssetResultVo.getResult().getData(); + if (pmsProductLibraryComponentService.getLockComponentStatus(productRepo.getId(), assetId)) { + throw new ServiceException("制品锁定中,无法被移入产品库(制品名称[%s],产品库名称[%s])", data.getName(), productRepo.getName()); + } + PmsProductLibraryComponent pmsProductLibraryComponent = pmsProductLibraryComponentMapper.selectPmsProductLibraryComponentByIdentifier(productRepo.getId(), assetId); + if (pmsProductLibraryComponent != null) { + throw new ServiceException("制品已被移入产品库中,无法重复移入(制品名称[%s],产品库名称[%s])", data.getName(), productRepo.getName()); + } + nexusAssetResultDataVoList.add(data); + } + } + pmsProductLibraryAsyncService.asyncAddCommonAssetToProductRepo(productRepo, repo, nexusAssetResultDataVoList, commonAssetAddToProductRepoVo.getDirectory()); + return true; + } + + @Override + public NexusComponentResultVo> selectPmsProductLibraryKeyComponentList(String enterpriseIdentifier, ComponentSearchVo componentSearchVo) { + PmsProductLibraryRepositories productRepo = pmsProductLibraryComponentService.getPmsProductLibraryRepositories(true, enterpriseIdentifier, componentSearchVo.getProductRepoName(), null); + PmsProductLibraryComponent productComponentSearch = new PmsProductLibraryComponent(); + productComponentSearch.setRepoId(productRepo.getId()); + List productComponentList = pmsProductLibraryComponentMapper.selectPmsProductLibraryComponentList(productComponentSearch); + PmsProductLibraryRepositories repo = pmsProductLibraryComponentService.getPmsProductLibraryRepositories(false, enterpriseIdentifier, componentSearchVo.getRepositoryName(), null); + componentSearchVo.setRepositoryName(repo.getNexusRepositoriesName()); + // Docker类型调用Nexus获取制品列表接口获取 + if (ProductLibraryRepositoriesFormatEnum.DOCKER.getKey().equals(repo.getFormat())) { + List keyNexusResultDataVoList = new ArrayList<>(); + // 需循环遍历获取所有镜像列表 + boolean isAll = false; + // Nexus分页Token + String continuationToken = null; + while (!isAll) { + NexusComponentAndAssetListVo assetListVo = nexusRequestHelper.doRequestToJavaObject(NexusRequestUrl.GET_COMPONENTS(repo.getNexusRepositoriesName(), continuationToken), NexusComponentAndAssetListVo.class, NexusDockerComponentVo.class); + if (assetListVo != null) { + if (StringUtils.isEmpty(assetListVo.getContinuationToken())) { + isAll = true; + } else { + continuationToken = assetListVo.getContinuationToken(); + } + for (NexusDockerComponentVo nexusDockerComponentVo : assetListVo.getItems()) { + NexusResultDataVo nexusResultDataVo = nexusDockerComponentVo.toNexusResultDataVo(); + keyNexusResultDataVoList.add(nexusResultDataVo); + } + } else { + isAll = true; + } + + } + setNexusResultDataVoChoose(productRepo, productComponentList, keyNexusResultDataVoList, true); + setNexusResultDataVoPath(repo, keyNexusResultDataVoList); + return buildDockerResultVo(keyNexusResultDataVoList); + } else { + NexusComponentSearchVo nexusComponentSearchVo = NexusComponentSearchVo.toRead(componentSearchVo, nexusRequestHelper.getNexusTid()); + NexusComponentResultVo> assetResultDataVo = nexusRequestHelper.doRequestToJavaObjectList(NexusRequestUrl.DO_OPERATION(), JSONObject.from(nexusComponentSearchVo), NexusComponentResultVo.class, NexusResultDataVo.class); + if (assetResultDataVo != null && assetResultDataVo.getResult() != null) { + NexusComponentResultStateVo> nexusComponentResultStateVo = assetResultDataVo.getResult(); + if (nexusComponentResultStateVo.getSuccess()) { + List nexusResultDataVoList = nexusComponentResultStateVo.getData(); + setNexusResultDataVoChoose(productRepo, productComponentList, nexusResultDataVoList, false); + setNexusResultDataVoPath(repo, nexusResultDataVoList); + // 当制品库为Maven时,需排除自动生成的制品数据 + if (ProductLibraryRepositoriesFormatEnum.MAVEN.getKey().equals(repo.getFormat())) { + List keyNexusResultDataVoList = new ArrayList<>(); + for (NexusResultDataVo nexusResultDataVo : nexusResultDataVoList) { + // 当类型为asset时,需检查数据库是否存在记录,若存在则返回 + if ("asset".equals(nexusResultDataVo.getType())) { + PmsProductLibraryComponent pmsProductLibraryComponent = pmsProductLibraryComponentMapper.selectPmsProductLibraryComponentByPath(nexusResultDataVo.getId(), repo.getId(), repo.getPmsEnterpriseId()); + if (pmsProductLibraryComponent != null) { + keyNexusResultDataVoList.add(nexusResultDataVo); + } + } else { + keyNexusResultDataVoList.add(nexusResultDataVo); + } + } + nexusComponentResultStateVo.setData(keyNexusResultDataVoList); + assetResultDataVo.setResult(nexusComponentResultStateVo); + } + } + } + return assetResultDataVo; + } + } + + @Override + public boolean addDockerComponentToProductRepo(DockerComponentAddToProductRepoVo dockerComponentAddToProductRepoVo) { + // 先校验产品库、制品库、制品库下制品Id列表是否存在,再进行异步处理 + PmsProductLibraryRepositories productRepo = selectPmsProductLibraryByName(dockerComponentAddToProductRepoVo.getEnterpriseIdentifier(), dockerComponentAddToProductRepoVo.getProductRepoName()); + PmsProductLibraryRepositories repo = pmsProductLibraryRepositoriesService.selectPmsProductLibraryRepositoriesByName(dockerComponentAddToProductRepoVo.getEnterpriseIdentifier(), dockerComponentAddToProductRepoVo.getRepoName()); + if (!ProductLibraryRepositoriesFormatEnum.DOCKER.getKey().equals(repo.getFormat())) { + throw new ServiceException("当前接口仅允许进行Docker移入产品库操作"); + } + List nexusComponentResultDataVoList = new ArrayList<>(); + for (String componentId : dockerComponentAddToProductRepoVo.getComponentIdList()) { + NexusComponentResultVo nexusAssetResultVo = pmsProductLibraryComponentService.getComponentDetailByComponentId(dockerComponentAddToProductRepoVo.getEnterpriseIdentifier(), dockerComponentAddToProductRepoVo.getRepoName(), null, componentId, false); + if (nexusAssetResultVo == null || nexusAssetResultVo.getResult() == null || !nexusAssetResultVo.getResult().getSuccess()) { + throw new ServiceException("该制品不存在(制品库[%s],制品Id[%s])", dockerComponentAddToProductRepoVo.getRepoName(), componentId); + } else { + NexusComponentResultDataVo data = nexusAssetResultVo.getResult().getData(); + if (pmsProductLibraryComponentService.getLockComponentStatus(productRepo.getId(), componentId)) { + throw new ServiceException("制品锁定中,无法被移入产品库(制品名称[%s],产品库名称[%s])", data.getName(), productRepo.getName()); + } + PmsProductLibraryComponent pmsProductLibraryComponent = pmsProductLibraryComponentMapper.selectPmsProductLibraryComponentByIdentifier(productRepo.getId(), componentId); + if (pmsProductLibraryComponent != null) { + throw new ServiceException("制品已被移入产品库中,无法重复移入(制品名称[%s],产品库名称[%s])", data.getName(), productRepo.getName()); + } + nexusComponentResultDataVoList.add(data); + } + } + pmsProductLibraryAsyncService.asyncAddDockerComponentToProductRepo(productRepo, repo, nexusComponentResultDataVoList, dockerComponentAddToProductRepoVo.getDirectory()); + return true; + } + + private void setNexusResultDataVoChoose(PmsProductLibraryRepositories productRepo, List productComponentList, List nexusResultDataVoList, boolean isDocker) { + for (PmsProductLibraryComponent component : productComponentList) { + nexusResultDataVoList.forEach(x -> { + if (pmsProductLibraryComponentService.getLockComponentStatus(productRepo.getId(), x.getComponentId())) { + x.setIsChoose(true); + } else if (isDocker && StringUtils.isNotEmpty(x.getComponentId())) { + if (x.getComponentId().equals(component.getIdentifier())) { + x.setIsChoose(true); + } + } else if (StringUtils.isNotEmpty(x.getAssetId()) && x.getAssetId().equals(component.getIdentifier())) { + x.setIsChoose(true); + } + }); + } + } + + private void setNexusResultDataVoPath(PmsProductLibraryRepositories repo, List nexusResultDataVoList) { + nexusResultDataVoList.forEach(x -> { + if (ProductLibraryRepositoriesFormatEnum.DOCKER.getKey().equals(repo.getFormat())) { + x.setDefaultDir(EscapeUtil.removeExtraSlashOfUrl(String.format("/%s/", repo.getAlias()))); + } else { + x.setDefaultDir(EscapeUtil.removeExtraSlashOfUrl(String.format("/%s/%s", repo.getAlias(), PmsUtils.parseDirectory(x.getId())))); + } + }); + } + + private NexusComponentResultVo> buildDockerResultVo(List data) { + NexusComponentResultVo> target = new NexusComponentResultVo<>(); + target.setAction("coreui_Browse"); + target.setMethod("read"); + target.setTid(1); + target.setType("rpc"); + NexusComponentResultStateVo> nexusComponentResultStateVo = new NexusComponentResultStateVo<>(); + nexusComponentResultStateVo.setData(data); + nexusComponentResultStateVo.setSuccess(true); + target.setResult(nexusComponentResultStateVo); + return target; + } + + + @Override + public boolean packagedProduct(String enterpriseIdentifier, String productRepoName) { + PmsProductLibraryRepositories productRepo = selectPmsProductLibraryByName(enterpriseIdentifier, productRepoName); + pmsProductLibraryAsyncService.packagedProduct(productRepo); + return true; } }