package com.vci.web.service.impl; import com.vci.client.mw.ClientSessionUtility; import com.vci.corba.common.PLException; import com.vci.corba.omd.vrm.VersionRule; import com.vci.dto.OsRevisionRuleDTO; import com.vci.starter.web.annotation.log.VciUnLog; import com.vci.starter.web.util.VciBaseUtil; import com.vci.starter.web.util.VciDateUtil; import com.vci.pagemodel.OsRevisionRuleVO; import com.vci.starter.web.util.WebThreadLocalUtil; import com.vci.web.service.OsRevisionRuleServiceI; import com.vci.web.util.Func; import com.vci.web.util.PlatformClientUtil; import com.vci.web.util.WebUtil; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Service; import javax.swing.*; import java.awt.*; import java.util.*; import java.util.List; import java.util.stream.Collectors; /** * 版本规则的服务 * @author weidy * @date 2021-2-15 */ @Service public class OsRevisionRuleServiceImpl implements OsRevisionRuleServiceI { /** * 平台调用客户端 */ @Autowired private PlatformClientUtil platformClientUtil; /** * 加载自身 */ @Autowired(required = false) @Lazy private OsRevisionRuleServiceI self; /** * 查询所有的版本规则 * * @return 版本对象 */ @Override public List selectAllRevision() { try { return revisionRuleDO2VOs(Arrays.stream(platformClientUtil.getVersionService().getVersionRules()).collect(Collectors.toList())); } catch (PLException e) { throw WebUtil.getVciBaseException(e); } } /** * 查询所有的版本规则映射 * * @return key 是版本的英文名称 */ @Override @VciUnLog public Map selectAllRevisionMap() { return Optional.ofNullable(self.selectAllRevision()).orElseGet(()->new ArrayList<>()).stream().collect(Collectors.toMap(s->s.getId().toLowerCase(),t->t,(o1,o2)->o1)); } /** * 创建版本规则 * @param osRevisionRuleDTO * @return */ @Override public boolean addVersionRule(OsRevisionRuleDTO osRevisionRuleDTO) throws PLException { //判空 VciBaseUtil.alertNotNull(osRevisionRuleDTO,"版本规则对象",osRevisionRuleDTO.getId(),"版本规则名称"); //版本规则合规检验 this.checkVersionRule(osRevisionRuleDTO); //查重 VersionRule vr = platformClientUtil.getVersionService().getVersionRule(osRevisionRuleDTO.getName()); //name不为空 if(Func.isNotEmpty(vr) && !"".equals(vr.name)){ throw new PLException("500",new String[]{"名称重复请更换名称!"}); } return platformClientUtil.getVersionService().addVersionRule(this.dto2VersionRule(osRevisionRuleDTO)); } /** * 修改版本规则 * @param osRevisionRuleDTO * @return */ @Override public boolean updateVersionRule(OsRevisionRuleDTO osRevisionRuleDTO) throws PLException { //判空 VciBaseUtil.alertNotNull(osRevisionRuleDTO,"版本规则对象",osRevisionRuleDTO.getId(),"版本规则名称"); //判断是否在系统中存在 VersionRule vr = platformClientUtil.getVersionService().getVersionRule(osRevisionRuleDTO.getName()); //版本规则合规检验 this.checkVersionRule(osRevisionRuleDTO); //name不为空 if(Func.isEmpty(vr) && !"".equals(vr.name)){ throw new PLException("500",new String[]{"修改的版本规则在系统中不存在!"}); } return platformClientUtil.getVersionService().modifyVersionRule(this.dto2VersionRule(osRevisionRuleDTO)); } /** * 检查版本规则设置的是否合理 * @param dto */ private void checkVersionRule(OsRevisionRuleDTO dto) throws PLException { //版本规则名称只能为英文字母 String regex = "[a-z A-Z]*"; if (!dto.getId().matches(regex)) { throw new PLException("500",new String[]{"名称只能为英文!"}); } //跳跃字符只能为数字或者字母 if(Func.isNotBlank(dto.getJumpCharacter()) && (!(dto.getJumpCharacter().matches(regex)))){ throw new PLException("500",new String[]{"跳跃字符只能为数字或者字母!"}); } //初始值不能为空且只能为数字或者字母或英文状态下的符号 String regex1 = "[A-Za-z0-9!@#$%^&*()-_=+{}':|;,.?/]+$"; if(Func.isBlank(dto.getInitialValue()) || !dto.getInitialValue().matches(regex1)){ throw new PLException("500",new String[]{"初始值不能为空且只能为数字或者字母或英文状态下的符号!"}); } if(dto.getInitialValue().length() + dto.getInitialValue().length() > 32) { throw new PLException("500",new String[]{"初始值不能超过32个字符!"}); } //步长不能为空且必须为1-9的正整数 String regex2 = "[1-9]"; if(Func.isBlank(dto.getStepLength()) || (!dto.getStepLength().matches(regex2))){ throw new PLException("500",new String[]{"步长不能为空且必须为1-9的正整数"}); } //前缀相关判断 String regex3 = "^\\s+.*"; if(Func.isNotBlank(dto.getPrefixion()) && (dto.getPrefixion().matches(regex3))){ throw new PLException("500",new String[]{"前缀不能以空格开头"}); } if (dto.getPrefixion().length() + dto.getPrefixion().length() > 32) { throw new PLException("500",new String[]{"前缀不能超过32个字符"}); } //后缀相关判断 String regex4 = "^*.\\s+$"; if(Func.isNotBlank(dto.getSuffix()) && (dto.getSuffix().matches(regex4))){ throw new PLException("500",new String[]{"后缀不能以空格结尾"}); } if (dto.getSuffix().length() + dto.getSuffix().length() > 32) { throw new PLException("500",new String[]{"后缀不能超过32个字符"}); } if (dto.getId().length() > 255) { throw new PLException("500",new String[]{"名称不能超过255个字符"}); } } /** * dto对象转换为VersionRule对象 * @return */ private VersionRule dto2VersionRule(OsRevisionRuleDTO osRevisionRuleDTO){ VersionRule newVR = new VersionRule(); newVR.name = osRevisionRuleDTO.getId(); newVR.tag = osRevisionRuleDTO.getName(); newVR.description = osRevisionRuleDTO.getDescription(); newVR.jumpCharacter = osRevisionRuleDTO.getJumpCharacter(); newVR.initialValue = osRevisionRuleDTO.getInitialValue(); newVR.stepLength = osRevisionRuleDTO.getStepLength(); newVR.prefixion = osRevisionRuleDTO.getPrefixion(); newVR.suffix = osRevisionRuleDTO.getSuffix(); String userName = "developer";//WebThreadLocalUtil.getCurrentUserSessionInfoInThread().getUserId(); long timeMillis = System.currentTimeMillis(); newVR.creator = Func.isBlank(osRevisionRuleDTO.getCreator()) ? userName:osRevisionRuleDTO.getCreator(); newVR.createTime = Func.isEmpty(osRevisionRuleDTO.getCreateTime()) ? timeMillis:osRevisionRuleDTO.getCreateTime().getTime(); newVR.modifier = userName; newVR.modifyTime = timeMillis; return newVR; } /** * 数据对象转换为显示对象 * * @param versionRules 数据对象 * @return 显示对象 */ @Override public List revisionRuleDO2VOs(Collection versionRules) { List ruleVOS = new ArrayList<>(); Optional.ofNullable(versionRules).orElseGet(()->new ArrayList<>()).stream().forEach(versionRule -> { OsRevisionRuleVO ruleVO = revisionRuleDO2VO(versionRule); ruleVOS.add(ruleVO); }); return ruleVOS; } /** * 数据对象转换为显示对象 * * @param versionRule 数据对象 * @return 显示对象 */ @Override public OsRevisionRuleVO revisionRuleDO2VO(VersionRule versionRule) { OsRevisionRuleVO ruleVO = new OsRevisionRuleVO(); if(versionRule !=null){ ruleVO.setOid(versionRule.oid); ruleVO.setCreator(versionRule.creator); ruleVO.setLastModifier(versionRule.modifier); try { ruleVO.setCreateTime(VciDateUtil.long2Date(versionRule.createTime)); ruleVO.setLastModifyTime(VciDateUtil.long2Date(versionRule.modifyTime)); ruleVO.setTs(VciDateUtil.str2Date(versionRule.ts,VciDateUtil.DateTimeFormat)); } catch (Exception e) { e.printStackTrace(); } ruleVO.setDescription(versionRule.description); ruleVO.setId(versionRule.name); ruleVO.setName(versionRule.tag); ruleVO.setStepLength(WebUtil.getInt(versionRule.stepLength)); ruleVO.setJumpCharacter(versionRule.jumpCharacter); ruleVO.setPrefixion(versionRule.prefixion); ruleVO.setSuffix(versionRule.suffix); ruleVO.setInitialValue(versionRule.initialValue); //associated暂时没有使用 } return ruleVO; } /** * 使用编号获取规则的值 * * @param id 编号 * @return 显示对象 */ @Override public OsRevisionRuleVO getRevisionRuleById(String id) { if(StringUtils.isNotBlank(id)){ return self.selectAllRevisionMap().getOrDefault(id.toLowerCase().trim(),null); } return null; } /** * 查询应用范围 * @param vrName 版本规则英文名称 * @return */ @Override public List> getUsedVersionRuleList(String vrName) throws PLException { if(Func.isBlank(vrName)){ throw new PLException("500",new String[]{"请选择要查询应用范围的属性!"}); } String[] btNames = platformClientUtil.getBtmService().getBTNamesByVerName(vrName); if(Func.isEmpty(btNames)){ return new ArrayList<>(); } List> btmNameMapList = new ArrayList<>(); Arrays.stream(btNames).forEach(btName->{ Map itemMap = new HashMap<>(); itemMap.put("versionRuleName",vrName); itemMap.put("source",btName); btmNameMapList.add(itemMap); }); return btmNameMapList; } /** * 清除缓存 */ @Override public void clearCache() { } }