public void dealSiteIDTable(String tableName, DataTable dt) throws Exception {
    this.task.setCurrentInfo("正在向表" + tableName + "插入站点数据");
    if (tableName.equalsIgnoreCase("ZCSite")) {
      dealZCSite(dt.getDataRow(0));
    } else {
      SchemaSet set =
          (SchemaSet) Class.forName("com.zving.schema." + tableName + "Set").newInstance();
      if (!tableName.equalsIgnoreCase("ZDMember")) {
        for (int i = 0; i < dt.getRowCount(); ++i) {
          Schema schema =
              (Schema) Class.forName("com.zving.schema." + tableName + "Schema").newInstance();
          DataRow dr = dt.getDataRow(i);
          setSiteIDTableMaxNo(tableName, dr, true);
          schema.setValue(dt.getDataRow(i));
          set.add(schema);
        }

        this.da.insert(set);
      } else {
        Schema schema =
            (Schema) Class.forName("com.zving.schema." + tableName + "Schema").newInstance();
        SchemaColumn[] scs = SchemaUtil.getColumns(schema);
        QueryBuilder qb = new QueryBuilder("select count(1) from " + tableName + " where 1=1 ");
        for (int i = 0; i < scs.length; ++i) {
          if (scs[i].isPrimaryKey()) {
            qb.append(" and " + scs[i].getColumnName() + "=?");
          }
        }
        for (int i = 0; i < dt.getRowCount(); ++i) {
          schema = (Schema) Class.forName("com.zving.schema." + tableName + "Schema").newInstance();
          DataRow dr = dt.getDataRow(i);

          qb.getParams().clear();
          for (int j = 0; j < scs.length; ++j) {
            if (scs[j].isPrimaryKey()) {
              qb.add(dr.get(scs[j].getColumnName()));
            }
          }
          int count = qb.executeInt();
          if (count > 0) setSiteIDTableMaxNo(tableName, dr, false);
          else {
            setSiteIDTableMaxNo(tableName, dr, true);
          }
          schema.setValue(dt.getDataRow(i));
          set.add(schema);
        }

        this.da.deleteAndInsert(set);
      }
    }
  }
 private void dealCustomTable(DataTable dt, QueryBuilder qb) throws Exception {
   try {
     qb.clearBatches();
     qb.getParams().clear();
     for (int i = 0; i < dt.getRowCount(); ++i) {
       for (int j = 0; j < dt.getColCount() - 1; ++j) {
         String v = dt.getString(i, j);
         if (StringUtil.isEmpty(v)) {
           v = null;
         }
         qb.add(v);
       }
       qb.addBatch();
     }
     this.da.executeNoQuery(qb);
   } catch (Throwable t) {
     t.printStackTrace();
   }
 }
Exemple #3
0
   public void writeArticle()
   {
     prepareList();
     if (this.config.getType() == 1) {
       QueryBuilder imageQB = new QueryBuilder("select id from zccatalog where type=4 and siteid=?", 
         CatalogUtil.getSiteID(this.config.getCatalogID()));
       String imageCatalogID = imageQB.executeString();
       if (StringUtil.isEmpty(CatalogUtil.getSiteID(this.config.getCatalogID()))) {
         LogUtil.getLogger().warn("文档采集的目的栏目不存在:ID=" + this.config.getCatalogID());
         return;
       }
       String sitePath = SiteUtil.getAbsolutePath(CatalogUtil.getSiteID(this.config.getCatalogID()));
       String imagePath = "upload/Image/" + CatalogUtil.getAlias(imageCatalogID) + "/";
 
       RegexParser rp = this.config.getTemplate("Ref1");
       RegexParser[] filters = this.config.getFilterBlocks();
       this.list.moveFirst();
       WebDocument doc = null;
       int cSuccess = 0;
       int cFailure = 0;
       int cLost = 0;
 
       boolean publishDateFlag = false;
       ZCArticleSet set = new ZCArticleSet();
       while ((doc = this.list.next()) != null) {
         if (this.task.checkStop()) {
           return;
         }
         if (doc.getLevel() != this.config.getUrlLevels().length - 1) {
           continue;
         }
         int percent = (100 - this.task.getPercent()) * (cSuccess + cFailure + cLost) / this.list.size();
         this.task.setPercent(this.task.getPercent() + percent);
         if ((doc.isTextContent()) && (doc.getContent() != null)) {
           String text = doc.getContentText();
           rp.setText(text);
           if (rp.match()) {
             Mapx map = rp.getMapx();
             Object[] ks = map.keyArray();
             Object[] vs = map.valueArray();
             for (int i = 0; i < map.size(); ++i) {
               String key = ks[i].toString();
               String value = vs[i].toString();
               if (!key.equalsIgnoreCase("Content")) {
                 value = this.tagPattern.matcher(value).replaceAll("");
               }
               value = StringUtil.htmlDecode(value);
               value = value.trim();
               map.put(key, value);
             }
             String title = map.getString("Title");
             String content = map.getString("Content");
             String author = map.getString("Author");
             String source = map.getString("Source");
             String strDate = map.getString("PublishDate");
             Date publishDate = doc.getLastmodifiedDate();
             if ((StringUtil.isNotEmpty(strDate)) && (StringUtil.isNotEmpty(this.config.getPublishDateFormat())))
             {
               try {
                 strDate = DateUtil.convertChineseNumber(strDate);
                 publishDate = DateUtil.parse(strDate, this.config.getPublishDateFormat());
               } catch (Exception e) {
                 this.task.addError("日期" + strDate + "不符合指定格式" + doc.getUrl());
               }
               publishDateFlag = true;
             }
             if (publishDate.getTime() > System.currentTimeMillis()) {
               publishDate = new Date();
             }
             ArticleAPI api = new ArticleAPI();
             try {
               ZCArticleSchema article = new ZCArticleSchema();
               if (StringUtil.isNotEmpty(title)) {
                 article.setTitle(title);
               } else {
                 ++cLost;
                 break label1209:
               }
               if (StringUtil.isNotEmpty(content)) {
                 content = content.trim();
                 while (rp.match()) {
                   String html = rp.getMapx().getString("Content");
                   content = content + html;
                 }
                 if (this.config.isCleanLinkFlag()) {
                   content = this.framePattern.matcher(content).replaceAll("");
                   content = this.stylePattern.matcher(content).replaceAll("");
                   content = this.scriptPattern.matcher(content).replaceAll("");
                   content = this.linkPattern.matcher(content).replaceAll("$1");
                 }
                 if (filters != null) {
                   for (int k = 0; k < filters.length; ++k) {
                     content = filters[k].replace(content, "");
                   }
                 }
 
                 String str = dealImage(content, doc.getUrl(), sitePath, imagePath, imageCatalogID);
                 article.setContent(str);
               } else {
                 ++cLost;
                 break label1209:
               }
               if (StringUtil.isNotEmpty(author)) {
                 article.setAuthor(author);
               }
               if (StringUtil.isNotEmpty(source)) {
                 article.setReferName(source);
               }
               article.setReferURL(doc.getUrl());
               article.setPublishDate(publishDate);
               article.setCatalogID(this.config.getCatalogID());
               article.setBranchInnerCode("0001");
               article.setProp2("FromWeb");
 
               if (ExtendManager.hasAction("FromWeb.BeforeSave")) {
                 ExtendManager.executeAll("FromWeb.BeforeSave", new Object[] { article });
               }
 
               Date date = (Date)new QueryBuilder(
                 "select PublishDate from ZCArticle where ReferURL=? and CatalogID=?", doc.getUrl(), 
                 this.config.getCatalogID()).executeOneValue();
               if (date != null) {
                 if (date.getTime() < doc.getLastDownloadTime()) {
                   QueryBuilder qb = new QueryBuilder(
                     "update ZCArticle set Title=?,Content=? where CatalogID=? and ReferURL=?");
                   qb.add(article.getTitle());
                   qb.add(article.getContent());
                   qb.add(this.config.getCatalogID());
                   qb.add(doc.getUrl());
                   qb.executeNoQuery();
                 }
                 ++cSuccess;
               } else {
                 api.setSchema(article);
                 set.add(article);
                 if (api.insert() > 0L)
                   ++cSuccess;
                 else
                   ++cFailure;
               }
             }
             catch (Exception e) {
               ++cFailure;
               e.printStackTrace();
             }
           } else {
             LogUtil.getLogger().info("未能匹配" + doc.getUrl());
             this.task.addError("未能匹配" + doc.getUrl());
             ++cLost;
           }
           label1209: this.task.setCurrentInfo("正在转换文档, <font class='green'>" + cSuccess + "</font> 个成功, <font class='red'>" + 
             cFailure + "</font> 个失败, <font class='green'>" + cLost + "</font> 个未匹配");
         }
       }
  public void processSubmit() {
    ZCCustomTableSchema table = new ZCCustomTableSchema();
    table.setID($V("_TableID"));
    if (!table.fill()) {
      LogUtil.warn("没有ID为" + table.getCode() + "的表!");
      return;
    }
    if ((!"Y".equals(table.getAllowModify())) && (!User.isManager())) {
      LogUtil.warn("ID为" + table.getID() + "的表不允许前台修改!");
      return;
    }

    DataAccess da = null;
    String code = table.getCode();
    if (table.getType().equals("Link")) {
      da = new DataAccess(OuterDatabase.getConnection(table.getDatabaseID()));
      code = table.getOldCode();
    } else {
      da = new DataAccess();
    }
    try {
      da.setAutoCommit(false);
      ZCCustomTableColumnSet set =
          new ZCCustomTableColumnSchema().query(new QueryBuilder("where TableID=?", table.getID()));
      StringBuffer sb = new StringBuffer("insert into " + code + "(");
      for (int j = 0; j < set.size(); ++j) {
        if (j != 0) {
          sb.append(",");
        }
        sb.append(set.get(j).getCode());
      }
      sb.append(") values (");
      for (int j = 0; j < set.size(); ++j) {
        if (j != 0) {
          sb.append(",");
        }
        sb.append("?");
      }
      sb.append(")");
      QueryBuilder qb = new QueryBuilder(sb.toString());
      StringBuffer messageSB = new StringBuffer();
      for (int j = 0; j < set.size(); ++j) {
        ZCCustomTableColumnSchema column = set.get(j);
        String v = $V("_Form_" + set.get(j).getCode());
        if (StringUtil.isEmpty(v)) {
          v = null;
          if ("Y".equals(set.get(j).getIsAutoID())) {
            v = String.valueOf(OrderUtil.getDefaultOrder());
          }
        }
        if (((("Y".equals(column.getIsMandatory())) || ("Y".equals(column.getIsPrimaryKey()))))
            && (StringUtil.isEmpty(v))) {
          messageSB.append(column.getName() + "不能为空!\n");
        }

        int dataType = Integer.parseInt(column.getDataType());
        if (v != null) {
          if ((column.getMaxLength() != 0) && (v.length() < column.getMaxLength()))
            messageSB.append(column.getName() + "数据过长,最大允许" + column.getMaxLength() + "个字符!\n");
          try {
            if (dataType == 0) {
              v = DateUtil.toDateTimeString(DateUtil.parseDateTime(v));
              if (v == null) {
                throw new SQLException("日期时间错误");
              }
            }
            if ((dataType == 8) || (dataType == 9)) {
              v = String.valueOf(new Double(Double.parseDouble(v)).intValue());
            }
            if (dataType == 7) {
              v = String.valueOf(new Double(Double.parseDouble(v)).longValue());
            }
            if (dataType == 5) {
              v = String.valueOf(new Double(Double.parseDouble(v)).floatValue());
            }
            if ((dataType == 4) || (dataType == 6) || (dataType == 3))
              v = String.valueOf(Double.parseDouble(v));
          } catch (Exception e) {
            messageSB.append(column.getName() + "数据不正确!\n");
          }
        }
        qb.add(v);
      }
      if (messageSB.length() != 0) {
        this.Response.setError(messageSB.toString());
      } else {
        da.executeNoQuery(qb);
        da.commit();
        this.Response.setMessage("提交成功!");
      }
    } catch (Exception e) {
      e.printStackTrace();
      try {
        da.rollback();
      } catch (SQLException e1) {
        e1.printStackTrace();
      }
      this.Response.setMessage("提交失败:" + e.getMessage());
    } finally {
      try {
        da.setAutoCommit(true);
        da.close();
      } catch (SQLException e) {
        e.printStackTrace();
      }
    }
  }
  public static void processSubmit(HttpServletRequest request, HttpServletResponse response) {
    ZCCustomTableSchema table = new ZCCustomTableSchema();
    table.setID(request.getParameter("_TableID"));
    if (!table.fill()) {
      LogUtil.warn("没有ID为" + table.getCode() + "的表!");
      return;
    }
    if ((!"Y".equals(table.getAllowModify())) && (!User.isManager())) {
      LogUtil.warn("ID为" + table.getID() + "的表不允许前台修改!");
      return;
    }

    DataAccess da = null;
    String code = table.getCode();
    if (table.getType().equals("Link")) {
      da = new DataAccess(OuterDatabase.getConnection(table.getDatabaseID()));
      code = table.getOldCode();
    } else {
      da = new DataAccess();
    }
    try {
      da.setAutoCommit(false);
      ZCCustomTableColumnSet set =
          new ZCCustomTableColumnSchema().query(new QueryBuilder("where TableID=?", table.getID()));
      StringBuffer insertSB = new StringBuffer("insert into " + code + "(");
      QueryBuilder deleteQB = new QueryBuilder("delete from  " + code + " where 1=1 ");
      for (int j = 0; j < set.size(); ++j) {
        if (j != 0) {
          insertSB.append(",");
        }
        insertSB.append(set.get(j).getCode());
      }
      insertSB.append(") values (");
      for (int j = 0; j < set.size(); ++j) {
        if (j != 0) {
          insertSB.append(",");
        }
        insertSB.append("?");
        if ("Y".equals(set.get(j).getIsPrimaryKey())) {
          String v = request.getParameter("_Form_" + set.get(j).getCode() + "_Old");
          deleteQB.append(" and " + set.get(j).getCode() + "=?", v);
        }
      }
      insertSB.append(")");
      QueryBuilder qb = new QueryBuilder(insertSB.toString());
      StringBuffer messageSB = new StringBuffer();
      for (int j = 0; j < set.size(); ++j) {
        ZCCustomTableColumnSchema column = set.get(j);
        String v = request.getParameter("_Form_" + set.get(j).getCode());
        if (StringUtil.isEmpty(v)) {
          v = null;
          if ("Y".equals(set.get(j).getIsAutoID())) {
            v = String.valueOf(OrderUtil.getDefaultOrder());
          }
        }
        if (((("Y".equals(column.getIsMandatory())) || ("Y".equals(column.getIsPrimaryKey()))))
            && (StringUtil.isEmpty(v))) {
          messageSB.append(column.getName() + "不能为空!\\n");
        }

        int dataType = Integer.parseInt(column.getDataType());
        if (v != null) {
          if ((column.getMaxLength() != 0) && (v.length() < column.getMaxLength()))
            messageSB.append(column.getName() + "数据过长,最大允许" + column.getMaxLength() + "个字符!\\n");
          try {
            if (dataType == 0) {
              v = DateUtil.toDateTimeString(DateUtil.parseDateTime(v));
              if (v == null) {
                throw new SQLException("日期时间错误");
              }
            }
            if ((dataType == 8) || (dataType == 9)) {
              v = String.valueOf(new Double(Double.parseDouble(v)).intValue());
            }
            if (dataType == 7) {
              v = String.valueOf(new Double(Double.parseDouble(v)).longValue());
            }
            if (dataType == 5) {
              v = String.valueOf(new Double(Double.parseDouble(v)).floatValue());
            }
            if ((dataType == 4) || (dataType == 6) || (dataType == 3))
              v = String.valueOf(Double.parseDouble(v));
          } catch (Exception e) {
            messageSB.append(column.getName() + "数据不正确!\\n");
          }
        }
        qb.add(v);
      }
      if (messageSB.length() != 0) {
        insertSB = new StringBuffer();
        insertSB.append("<script>");
        insertSB.append("alert(\"" + messageSB + "\");");
        insertSB.append("history.go(-1);");
        insertSB.append("</script>");
        response.getWriter().print(insertSB);
      } else {
        da.executeNoQuery(deleteQB);
        da.executeNoQuery(qb);
        da.commit();
        insertSB = new StringBuffer();
        insertSB.append("<script>");
        insertSB.append("alert(\"提交成功!\");");
        insertSB.append("window.location=\"" + request.getHeader("referer") + "\";");
        insertSB.append("</script>");
        response.getWriter().print(insertSB);
      }
    } catch (Exception e) {
      e.printStackTrace();
      try {
        da.rollback();
      } catch (SQLException e1) {
        e1.printStackTrace();
      }
      StringBuffer sb = new StringBuffer();
      sb.append("<script>");
      sb.append("alert(\"提交失败!\");");
      sb.append("history.go(-1);");
      sb.append("</script>");
      try {
        response.getWriter().print(sb);
      } catch (IOException e1) {
        e1.printStackTrace();
      }
    } finally {
      try {
        da.setAutoCommit(true);
        da.close();
      } catch (SQLException e) {
        e.printStackTrace();
      }
    }
  }