首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >根据角度下拉值更改按钮的功能

根据角度下拉值更改按钮的功能
EN

Stack Overflow用户
提问于 2019-12-11 18:17:29
回答 1查看 42关注 0票数 0

我目前正在根据来自用户的输入生成一个许可证密钥,并将其传递给我的Java代码。生成密钥的代码为:

代码语言:javascript
复制
@PostMapping("/generate")
    public License generateLicense(@RequestBody License lic) {

        SecurityHashService service = new SecurityHashService();
        StringBuffer combinedKey = new StringBuffer(); 

        combinedKey.append("hardwareKey:").append(lic.getHardwareKey()).append(",").append("customerId:").append(lic.getCustomerId()).append(",")
        .append("product:").append(lic.getProduct()).append(",").append("msan:").append(lic.getMsan()).append(",")
        .append("msan:").append(lic.getCpe()).append(",")
        .append("connections:").append(lic.getConnections()).append(",").append("concurrent:").append(lic.getConcurrent()).append(",")
        .append("zmsVersion:").append(lic.getZmsVersion()).append(",").append("expiryDate:").append(lic.getExpiryDate().getTime());

        final String encryptedkey = service.generateKey(combinedKey.toString(), lic.getHardwareKey());
        lic.setLicenseKey(encryptedkey);
        return licenseDao.saveLicense(lic);
    }

@EntityListeners(AuditingEntityListener.class)类指定了所有参数:@Entity @Table(name=“License”)License

代码语言:javascript
复制
public class License {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id;

    @NotBlank
    private String product;

    @NotBlank
    private String hardwareKey;

    @NotBlank
    private String licenseKey;

    @NotNull
    private Date expiryDate;

    @NotNull
    private Integer concurrent;

    @NotNull
    private Integer connections;

    @NotNull
    private Integer msan;

    @NotNull
    private Integer cpe;

    @Temporal(TemporalType.TIMESTAMP)
    @LastModifiedDate
    private Date creationDate;

    @NotNull
    private String customerId;

    @NotNull
    private String zmsVersion;

在Angular部分:它接受所有的值并生成许可证。组件部分为:

代码语言:javascript
复制
export class GeneratelicenseComponent implements OnInit {
  editable: boolean = false;
  licenseKey: string = "";
  submitted: boolean = false;
  form: FormGroup;
  minDate = new Date();
  allProducts:any=[];
  products: any=[];
  constructor(
    private generateLicenseService: GeneratelicenseService,
    private builder: FormBuilder, public dialog: MatDialog) { }

  ngOnInit() {
    this.createForm();
    this.generateLicenseService.getProducts()
    .subscribe((data) => {
      console.log(data)
      this.allProducts = data;
      this.allProducts.forEach(element => {
        this.products.push({
          "value" : element["productId"],
          "viewValue" : element["productId"]
        })

      });
    });
    this.getDefaultConfig();
  }

  createForm() {
    this.form = this.builder.group({
        hardwareKey: ['', Validators.required],
        customerID: ['', Validators.required],
        product: ['', Validators.required],
        zmsVersion: ['', Validators.required],
        expiryDate: ['', Validators.required],
        concurrent: ['', Validators.required],
        connections: ['', Validators.required],
        msan: ['', Validators.required],
        cpe: ['', Validators.required]
    });



  }

  zmsVersions = [
    { value: '03.00.00', viewValue: '03.00.00' },
    { value: '03.01.00', viewValue: '03.01.00' },
    { value: '03.02.00', viewValue: '03.02.00' },
    { value: '03.03.00', viewValue: '03.03.00' },
    { value: '03.03.20', viewValue: '03.03.20' },
    { value: '03.04.00', viewValue: '03.04.00' }
  ]

  getDefaultValues() {
    let product = this.form.get('product').value;
    if(product=="MXK"){
      const dialogRef = this.dialog.open(GenerateMxkLicenseDialog, {
       width: '450px',
       disableClose: true,
       autoFocus: true
     });
    }

    if(product="OSSXML"){
      debugger
      this.form.get('customerID').disable();
      this.form.get('zmsVersion').disable();
      this.form.get('concurrent').disable();
      this.form.get('connections').disable();
      this.form.get('msan').disable();
      this.form.get('cpe').disable();
    }
    if (product !=null) {
      //if (product == 'ZMS-VA' || product == 'ZMS-TIER 1' || product == 'ZMS-TIER 2' || product == 'ZMS-TIER 3') {
        let defaultArray = this.getDefaultConfig().filter((item) => {
        return item.id === product;
        });
        debugger
        this.form.get('concurrent').setValue(defaultArray[0].concurrent)
        this.form.get('connections').setValue(defaultArray[0].connections)
        this.form.get('msan').setValue(defaultArray[0].msan);
        this.form.get('cpe').setValue(defaultArray[0].cpe);
        this.form.get('expiryDate').setValue('');
      } else {
      debugger
      this.form.get('concurrent').setValue('')
      this.form.get('connections').setValue('')
      this.form.get('msan').setValue('')
      this.form.get('cpe').setValue('');
      var targetDate = new Date();
      targetDate.setDate(targetDate.getDate() + 365);
      this.form.get('expiryDate').setValue(targetDate);
    }
}

  getDefaultConfig() {

    let defaultProductConfig: any=[];
    this.allProducts.forEach(element => {

      defaultProductConfig.push
        ({"id": element["productId"],
        "concurrent" : element["defaultConcurrent"],
        "connections": element["defaultConnections"],
        "msan": element["msan"],
        "cpe": element["cpe"]});
    });
    console.log("----->"+JSON.stringify(defaultProductConfig));
    return defaultProductConfig;
  }

  onSubmit() {
    console.log(this.allProducts[0]);
    this.submitted = true;
    if (this.form.invalid) {
      return;
    }
    let product = this.form.value.product;
    let hardwareKey = this.form.value.hardwareKey;  
    let zmsVersion = this.form.value.zmsVersion;
    let customerID = this.form.value.customerID;
    let concurrent = this.form.value.concurrent;
    let connections = this.form.value.connections;
    let expiryDate = this.form.value.expiryDate;
    let msan =  this.form.value.msan;
    let cpe =  this.form.value.cpe;

    console.log("hardwareKey=====> "+hardwareKey);

    let licenseValuesArr = [
      {
        "hardwareKey" : hardwareKey,
        "product" : product,
        "zmsVersion" : zmsVersion,
        "customerId" : customerID,
        "concurrent" : concurrent,
        "connections" : connections,
        "expiryDate" : expiryDate,
        "msan" : msan,
        "cpe" : cpe
      }
    ];


    this.generateLicenseService.generateLicense(licenseValuesArr)
      .subscribe((data) => {
        debugger
        console.log(data["licenseKey"])
        this.licenseKey = data["licenseKey"];
      },
      error => {
        console.log(error)
      });
  }

在表单中,我有一个产品字段,它是一个下拉列表。

代码语言:javascript
复制
<td colspan="2" width="100%">
                        <mat-form-field class="generate-full-width">
                            <mat-select placeholder="Product" formControlName="product" name="product" (selectionChange)="getDefaultValues()">
                                <mat-option *ngFor="let product of products" [value]="product.value">
                                    {{product.viewValue}}
                                </mat-option>
                            </mat-select>
                            <mat-error *ngIf="submitted && hasError('product', 'required')">Product is required</mat-error>

现在,假设产品名称是"OSSXML",我想禁用除硬件键和过期日期之外的所有输入表单字段。我不想使用弹出窗口和单独的按钮来为此生成许可证。我尝试禁用这些字段,将值设置为"“,并应用了多个if-else条件,但每次它检查其他字段的值时,在我的应用程序中,我都会收到错误:

代码语言:javascript
复制
List of constraint violations:[
    ConstraintViolationImpl{interpolatedMessage='must not be null', propertyPath=connections, rootBeanClass=class com.licensegeneration.licenseGenerator.model.License, messageTemplate='{javax.validation.constraints.NotNull.message}'}
    ConstraintViolationImpl{interpolatedMessage='must not be null', propertyPath=cpe, rootBeanClass=class com.licensegeneration.licenseGenerator.model.License, messageTemplate='{javax.validation.constraints.NotNull.message}'}
    ConstraintViolationImpl{interpolatedMessage='must not be null', propertyPath=zmsVersion, rootBeanClass=class com.licensegeneration.licenseGenerator.model.License, messageTemplate='{javax.validation.constraints.NotNull.message}'}
    ConstraintViolationImpl{interpolatedMessage='must not be null', propertyPath=msan, rootBeanClass=class com.licensegeneration.licenseGenerator.model.License, messageTemplate='{javax.validation.constraints.NotNull.message}'}
    ConstraintViolationImpl{interpolatedMessage='must not be null', propertyPath=concurrent, rootBeanClass=class com.licensegeneration.licenseGenerator.model.License, messageTemplate='{javax.validation.constraints.NotNull.message}'}
    ConstraintViolationImpl{interpolatedMessage='must not be null', propertyPath=customerId, rootBeanClass=class com.licensegeneration.licenseGenerator.model.License, messageTemplate='{javax.validation.constraints.NotNull.message}'}

请指教我应该做什么。我希望避免在我的应用程序中添加新类,或者创建单独的服务。此外,此代码是否需要更改,因为生成的许可证存储在一个名为license的表中。我想将OSSXML的许可证存储在同一个表中。

代码语言:javascript
复制
@RestController
@CrossOrigin(origins = "http://localhost:4200")
@RequestMapping("/licenseGenerator")
public class LicenseController {
EN

回答 1

Stack Overflow用户

发布于 2019-12-11 18:38:43

出现这个错误是因为您已经在实体类中使用@NotBlank注释了这些字段-这意味着JPA不允许您使用这些包含null的字段保存新的实体。

您可以做的是从某些情况下可能为空的字段中删除@NotBlank。或者,如果不能删除注释,您可以确保angular应用程序将发送{"connections": ""}而不是{"connections": null}。为此,如果选择的产品是OSSXML,则可以使用patchValue方法使用""填充字段,如下所示:

代码语言:javascript
复制
this.form.patchValue({
  connections: "",
  cpe: "",
  zmsVersion: "",
  msan: "",
  concurrent: "",
  customerId: ""
});

如果要指定其他字段,请使用在createForm()方法中使用的值

onSubmit()方法中调用它,只是在检查表单是否有效之后。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59283473

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档