Exemplo n.º 1
0
  /**
   * 字段值重复性校验 唯一性验证URL示例:id=1&element=masterId&masterId=ABC&additional=referenceId &referenceId=XYZ
   * 处理额外补充参数,有些数据是通过两个字段共同决定唯一性,可以通过additional参数补充提供
   */
  public HttpHeaders checkUnique() {
    String element = this.getParameter("element");
    Assert.notNull(element);
    GroupPropertyFilter groupPropertyFilter = new GroupPropertyFilter();

    String value = getRequest().getParameter(element);
    if (!ExtStringUtils.hasChinese(value)) {
      value = ExtStringUtils.encodeUTF8(value);
    }

    groupPropertyFilter.and(new PropertyFilter(entityClass, "EQ_" + element, value));

    // 处理额外补充参数,有些数据是通过两个字段共同决定唯一性,可以通过additional参数补充提供
    String additionalName = getRequest().getParameter("additional");
    if (StringUtils.isNotBlank(additionalName)) {
      String additionalValue = getRequest().getParameter(additionalName);
      if (!ExtStringUtils.hasChinese(additionalValue)) {
        additionalValue = ExtStringUtils.encodeUTF8(additionalValue);
      }
      groupPropertyFilter.and(new PropertyFilter(entityClass, additionalName, additionalValue));
    }
    String additionalName2 = getRequest().getParameter("additional2");
    if (StringUtils.isNotBlank(additionalName2)) {
      String additionalValue2 = getRequest().getParameter(additionalName2);
      if (!ExtStringUtils.hasChinese(additionalValue2)) {
        additionalValue2 = ExtStringUtils.encodeUTF8(additionalValue2);
      }
      groupPropertyFilter.and(new PropertyFilter(entityClass, additionalName2, additionalValue2));
    }

    List<T> entities = getEntityService().findByFilters(groupPropertyFilter);
    if (entities == null || entities.size() == 0) { // 未查到重复数据
      this.setModel(Boolean.TRUE);
    } else {
      if (entities.size() == 1) { // 查询到一条重复数据
        String id = getRequest().getParameter("id");
        if (StringUtils.isNotBlank(id)) {
          Serializable entityId = entities.get(0).getId();
          logger.debug("Check Unique Entity ID = {}", entityId);
          if (id.equals(entityId.toString())) { // 查询到数据是当前更新数据,不算已存在
            this.setModel(Boolean.TRUE);
          } else { // 查询到数据不是当前更新数据,算已存在
            this.setModel(Boolean.FALSE);
          }
        } else { // 没有提供Sid主键,说明是创建记录,则算已存在
          this.setModel(Boolean.FALSE);
        }
      } else { // 查询到多余一条重复数据,说明数据库数据本身有问题
        this.setModel(Boolean.FALSE);
        throw new WebException("error.check.unique.duplicate: " + element + "=" + value);
      }
    }
    return buildDefaultHttpHeaders();
  }
  @Override
  public DBObject filterInternal(String url, WebPage webPage, DBObject parsedDBObject) {
    String pageText = webPage.getPageText();
    DocumentFragment doc = parse(pageText);

    // 获取公司公司简介
    Node node = selectSingleNode(doc, "//div[@class='introduct']/p");
    if (node != null) {
      String introduct = getXPathValue(node, "./");
      introduct = ExtStringUtils.cutRedundanceStr(introduct.trim(), 3000);
      putKeyValue(parsedDBObject, "公司简介", introduct);
    }

    // 获取公司联系方式
    NodeList nodes = selectNodeList(doc, "//div[@class='Contact']//tr");
    if (nodes != null && nodes.getLength() > 0) {
      for (int i = 0; i < nodes.getLength(); i++) {
        Node n = nodes.item(i);
        String content = n.getTextContent();
        // 中文冒号分隔
        String[] nodeTexts = StringUtils.split(content, ":");
        // 容错处理,英文冒号分隔
        if (nodeTexts.length <= 1) {
          nodeTexts = StringUtils.split(content, ":");
        }
        if (nodeTexts.length > 1) {
          putKeyValue(parsedDBObject, nodeTexts[0].trim(), nodeTexts[1].trim());
        }
      }
    }
    if (node == null && (nodes == null || nodes.getLength() < 1)) {
      return null;
    } else {
      return parsedDBObject;
    }
  }