/** @param ArrayList<Term> terms 该方法是对查询扩展的存储过程,在对每一个词计算出对应的查询扩展以后,将其以一定的形式存入数据库中,以方便客户端的查询请求 */
  public void insertExpansion(ArrayList<Expansion> expansions) {

    init();
    if (expansions.isEmpty() || expansions == null) {
      System.out.println("terms is null");
      return;
    } else {
      Connection conn = null;
      PreparedStatement pstmt = null;
      ResultSet rs = null;
      try {
        conn = dataSource.getConnection();
        pstmt = conn.prepareStatement(SQL_INSERT_EXPANSION, Statement.RETURN_GENERATED_KEYS);
        for (Expansion expansion : expansions) {
          // 如果数据库中已存在,则只进行更新操作
          if (getExpansionByKeywords(expansion.getTerm()) != null) updateExpansion(expansion);
          // 若不存在,则进行插入操作
          else {
            pstmt.setString(1, expansion.getTerm());

            /*将与查询词相关的事件id以字符串的形式存储,中间用‘,’隔开,以防止过度冗余,存储形式如"1,2,3",说明与该词相关的事件是事件1、事件2和事件3,
             * 并且对于事件只存id号,如果需要其他详细信息,可以根据id号进行进一步的查询 */
            String EventsIdString = intArrayToString(expansion.getEventId());
            pstmt.setString(2, EventsIdString);

            /*对于新闻的存储方法同事件的存储方式*/
            String NewsIdString = intArrayToString(expansion.getNewsId());
            pstmt.setString(3, NewsIdString);

            String ExpansionTermString = stringArrayToString(expansion.getExpansionTerms());
            pstmt.setString(4, ExpansionTermString);

            pstmt.executeUpdate();
          } // else
        } // for

      } catch (SQLException e) {
        e.printStackTrace();
      } finally {
        try {
          if (rs != null) rs.close();
          if (pstmt != null) pstmt.close();
          if (conn != null) conn.close();
        } catch (SQLException e) {
          e.printStackTrace();
        }
      } // finally
    } // else
  }
  /**
   * @param Term term
   *     此方法实现对数据库原有的扩展进行更新,即如果数据库中已经存在关于某个查询关键词的扩展,并且要插入的expansion的关键词与该词相同,则将插入操作改为更新操作
   */
  public void updateExpansion(Expansion expansion) {
    init();
    Expansion existExpansion = getExpansionByKeywords(expansion.getTerm());
    // 将数据库中已有的扩展和该扩展合并
    if (existExpansion.getEventId() == null || existExpansion.getEventId().isEmpty()) {
      existExpansion.addEventId(expansion.getEventId());
    } else {
      for (int eid : expansion.getEventId()) {
        if (!existExpansion.getEventId().contains(eid)) existExpansion.addEventId(eid);
      }
    }

    for (int nid : expansion.getNewsId()) {
      if (!existExpansion.getNewsId().contains(nid)) existExpansion.addNewsId(nid);
    }
    for (String expansionTerm : expansion.getExpansionTerms()) {
      if (!existExpansion.getExpansionTerms().contains(expansionTerm))
        existExpansion.addExpansionTerms(expansionTerm);
    }
    Connection conn = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
      conn = dataSource.getConnection();
      pstmt = conn.prepareStatement(SQL_UPDATE_EXPANSION);
      pstmt.setString(1, intArrayToString(existExpansion.getEventId()));
      pstmt.setString(2, intArrayToString(existExpansion.getNewsId()));
      pstmt.setString(3, stringArrayToString(existExpansion.getExpansionTerms()));
      pstmt.setString(4, existExpansion.getTerm());
      pstmt.executeUpdate();
    } catch (SQLException e) {
      e.printStackTrace();
    } finally {
      try {
        if (rs != null) rs.close();
        if (pstmt != null) pstmt.close();
        if (conn != null) conn.close();
      } catch (SQLException e) {
        e.printStackTrace();
      }
    } // finally
  }