Ejemplo n.º 1
0
 // 删除
 public String delete() throws Exception {
   Json _json = new Json();
   try {
     if (this.getId() != null) { // 删除一条
       this.getCrudService().delete(this.getId());
     } else { // 删除一批
       if (this.getIds() != null && this.getIds().length() > 0) {
         Long[] ids = cn.bc.core.util.StringUtils.stringArray2LongArray(this.getIds().split(","));
         this.getCrudService().delete(ids);
       } else {
         throw new CoreException("must set property id or ids");
       }
     }
     _json.put("success", true);
     _json.put("msg", getText("form.delete.success"));
     json = _json.toString();
     return "json";
   } catch (PermissionDeniedException e) {
     // 执行没有权限的操作
     _json.put("msg", getDeleteExceptionMsg(e));
     _json.put("e", e.getClass().getSimpleName());
   } catch (InnerLimitedException e) {
     // 删除内置对象
     _json.put("msg", getDeleteExceptionMsg(e));
     _json.put("e", e.getClass().getSimpleName());
   } catch (NotExistsException e) {
     // 执行没有权限的操作
     _json.put("msg", getDeleteExceptionMsg(e));
     _json.put("e", e.getClass().getSimpleName());
   } catch (ConstraintViolationException e) {
     // 违反约束关联引发的异常
     _json.put("msg", getDeleteExceptionMsg(e));
     _json.put("e", e.getClass().getSimpleName());
   } catch (Exception e) {
     // 其他异常
     dealOtherDeleteException(_json, e);
   }
   _json.put("success", false);
   json = _json.toString();
   return "json";
 }
  public void testRequiredAsBeanValidationAnnotation() throws Exception {
    DrivingLicence dl = new DrivingLicence();
    dl.setType("X");
    dl.setLevel(1);
    dl.setDescription(""); // This is annotated with @Required

    XPersistence.getManager().persist(dl);
    try {
      XPersistence.commit();
    } catch (RollbackException ex) {
      if (ex.getCause() instanceof ConstraintViolationException) {
        ConstraintViolationException cex = (ConstraintViolationException) ex.getCause();
        assertEquals("1 constraint violation expected", 1, cex.getConstraintViolations().size());
        ConstraintViolation v = cex.getConstraintViolations().iterator().next();
        assertEquals("Property", "description", v.getPropertyPath().toString());
        assertEquals("Message text", "{required}", v.getMessage());
        return;
      }
    }
    fail("A constraint violation exception should be thrown");
  }
  public void testPropertyValidatorsAsBeanValidationAnnotation() throws Exception {
    Product p = new Product();
    p.setNumber(66);
    p.setDescription("MOTO");
    p.setFamilyNumber(1);
    p.setSubfamilyNumber(1);
    p.setWarehouseKey(new Warehouse());
    p.setUnitPrice(new BigDecimal("900"));

    XPersistence.getManager().persist(p);
    try {
      XPersistence.commit();
    } catch (RollbackException ex) {
      if (ex.getCause() instanceof ConstraintViolationException) {
        ConstraintViolationException cex = (ConstraintViolationException) ex.getCause();
        assertEquals("1 constraint violation expected", 1, cex.getConstraintViolations().size());
        ConstraintViolation v = cex.getConstraintViolations().iterator().next();
        assertEquals("Property", "description", v.getPropertyPath().toString());
        assertEquals("Message text", "", v.getMessage());
        return;
      }
    }
    fail("A constraint violation exception should be thrown");
  }
  public void testEntityValidatorsAsHibernateAnnotation() throws Exception {
    Product p = new Product();
    p.setNumber(66);
    p.setDescription("BUENO, BONITO, BARATO"); // It's cheap ('BARATO') thus...
    p.setFamilyNumber(1);
    p.setSubfamilyNumber(1);
    p.setWarehouseKey(new Warehouse());
    p.setUnitPrice(new BigDecimal("900")); // ... it cannot cost 900 (max 100)

    XPersistence.getManager().persist(p);
    try {
      XPersistence.commit();
    } catch (RollbackException ex) {
      if (ex.getCause() instanceof ConstraintViolationException) {
        ConstraintViolationException cex = (ConstraintViolationException) ex.getCause();
        assertEquals("1 constraint violation expected", 1, cex.getConstraintViolations().size());
        ConstraintViolation v = cex.getConstraintViolations().iterator().next();
        assertEquals("Bean", "Product", v.getRootBean().getClass().getSimpleName());
        assertEquals("Message text", "", v.getMessage());
        return;
      }
    }
    fail("A constraint violation exception should be thrown");
  }