コード例 #1
0
ファイル: GenericDAOImpl.java プロジェクト: qza/qaf
  @SuppressWarnings("unchecked")
  public List<T> findByText2(String searchText, String sortColumn, boolean ascending) {
    List<Column> cols = HibernateUtil.getColumns(persistentClass.getName(), excludeDefault);
    String query = "select * from " + schemaName + "." + tableName + " where ";
    Iterator<Column> it = cols.iterator();
    while (it.hasNext()) {
      Column col = it.next();
      String colName = col.getName();
      String propName = HibernateUtil.convertToHibernatePropertyName(colName);
      String type = HibernateUtil.getPropertyType(persistentClass, propName);
      String condition = null;
      if (type != null) {
        if (type.equals("integer") || type.equals("smallint")) {
          try {
            Integer.valueOf(searchText);
            condition = colName + "=" + searchText;
          } catch (NumberFormatException ex) {
          }
        } else if (type.equals("timestamp")) {
          try {
            Integer.valueOf(searchText);
            condition =
                "( YEAR("
                    + colName
                    + ") = "
                    + searchText
                    + "  OR "
                    + "  MONTH("
                    + colName
                    + ") = "
                    + searchText
                    + "  OR "
                    + "  DAY("
                    + colName
                    + ") = "
                    + searchText
                    + ")";
          } catch (NumberFormatException ex) {
          }
        } else if (type.equals("string") || type.equals("timestamp") || type.equals("date"))
          condition = colName + " ilike '%" + searchText + "%'";

        if (condition != null)
          if (query.trim().endsWith("where")) query = query + condition;
          else query = query + " OR " + condition;
      }
      System.out.println("\n TYPE ZA KOLONU: " + colName + " JE: " + type + "\n");
    }
    if (sortColumn != null && !sortColumn.equals("")) {
      if (ascending) query = query + " order by " + sortColumn + " asc";
      else query = query + " order by " + sortColumn + " desc";
    }
    System.out.println("\n\n\n FINAL QUERY = " + query + " \n\n\n");
    return getSession().createSQLQuery(query).list();
  }
コード例 #2
0
ファイル: GenericDAOImpl.java プロジェクト: qza/qaf
  @SuppressWarnings("unchecked")
  public List<T> findByText(String searchText, String sortProp, boolean ascending) {
    List<Column> cols = HibernateUtil.getColumns(persistentClass.getName(), excludeDefault);
    Iterator<Column> it = cols.iterator();
    Criteria crit = getSession().createCriteria(persistentClass);
    Disjunction disjunction = Restrictions.disjunction();
    while (it.hasNext()) {
      Column col = it.next();
      String colName = col.getName();
      String propName = HibernateUtil.convertToHibernatePropertyName(colName);
      String type = HibernateUtil.getPropertyType(persistentClass, propName);
      if (type != null) {
        if (type.equals("integer") || type.equals("smallint")) {
          try {
            Integer.valueOf(searchText);
            Criterion criterium = Restrictions.eq(propName, Integer.valueOf(searchText));
            disjunction.add(criterium);
          } catch (NumberFormatException ex) {
          }
        } else if (type.equals("string")) {
          Criterion criterium = Restrictions.ilike(propName, searchText, MatchMode.ANYWHERE);
          disjunction.add(criterium);
        }
        /*else
            	if(type.equals("timestamp")){
            		Disjunction dis = Restrictions.disjunction();
           			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
           			try {
        	dis.add(Restrictions.eq(propName, format.parse(searchText)));
        } catch (ParseException e) {
        }
        try{
            			Integer.valueOf(searchText);
            			dis.add(Restrictions.sqlRestriction(
            					"YEAR(DATE("+colName+"))="+searchText+ " OR " +
               		"MONTH(DATE("+colName+"))="+searchText+ " OR " +
               		"DAY(DATE("+colName+"))="+searchText));
            		} catch(NumberFormatException ex) {
            		}
            		disjunction.add(dis);
            	}*/
      }
    }
    crit.add(disjunction);
    if (sortProp != null && !sortProp.equals("")) {
      if (ascending) crit.addOrder(Order.asc(sortProp));
      else crit.addOrder(Order.desc(sortProp));
    }

    return crit.list();
  }