package com.ccid.str;
import java.text.SimpleDateFormat;
import java.util.Date;
public class TestHqlString {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Date date1 = new Date();
Date date2 = new Date();
String[] baseArray = {"BusinessType","Methodology","CaseBase"};
String str1 = "参数1";
//getKnowledgeBase(str1,date1,date2,baseArray);
String str2 = "参数1 参数2 ";
getKnowledgeBase(str2,date1,date2,baseArray);
String str3 = "参数1 参数2 参数3 ";
//getKnowledgeBase(str3,date1,date2,baseArray);
String str4 = "参数1 参数2 参数3 参数4 ";
//getKnowledgeBase(str4,date1,date2,baseArray);
}
public static String getKnowledgeBase(String searchStr, Date startDate, Date endDate,String[] baseArray){
String[] searchArray = searchStr.trim().split(" ");
String hqlStr = "from ";
String tableStr = "";
String whereStr = "";
//加入时间条件
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
for (int m = 0; m < baseArray.length; m++) {
tableStr = tableStr + baseArray[m] + ", ";
String whereDateStr = "";
if (!baseArray[m].equals("CaseBase")) {
whereDateStr = baseArray[m] + ".createDate between " + f.format(startDate) + " and " + f.format(endDate) + " and ";
}
String whereLikeStr = "";
for (int i = 0; i <searchArray.length; i++) {
whereLikeStr = whereLikeStr + baseArray[m] + ".name like '%" + searchArray[i] + "%' or ";
}
whereStr = whereStr + "(" + whereDateStr + "(" + whereLikeStr.substring(0, whereLikeStr.lastIndexOf("or") - 1) + ")) or ";
}
tableStr = tableStr.substring(0, tableStr.lastIndexOf(",") - 1);
whereStr = whereStr.substring(0, whereStr.lastIndexOf("or") - 1);
System.out.println(hqlStr + tableStr + " where " + whereStr);
// if (baseArray[m] == "BusinessType") {
// tableStr = tableStr + " BusinessType b";
// whereStr = whereStr + "((b.createDate between " + f.format(startDate) + " and " + f.format(endDate) + ") and ( ";
// for (int i = 0; i <searchArray.length; i++) {
// whereStr = whereStr + "b.name like '%"+searchArray[i]+"%'";
// if (i != searchArray.length-1) {
// whereStr = whereStr + " or ";
// }
// }
// whereStr = whereStr + " )) ";
// }
// if (baseArray[m] == "Methodology") {
// tableStr = tableStr + " Methodology m";
// whereStr = whereStr + "((m.createDate between " + f.format(startDate) + " and " + f.format(endDate) + ") and (";
// for (int i = 0; i <searchArray.length; i++) {
// whereStr = whereStr + " m.name like '%"+searchArray[i]+"%' ";
// if (i != searchArray.length-1) {
// whereStr = whereStr + " or ";
// }
// }
// whereStr = whereStr + " )) ";
// }
// if (baseArray[m] == "CaseBase") {
// tableStr = tableStr + " CaseBase c";
// whereStr = whereStr + " ( ";
// for (int i = 0; i <searchArray.length; i++) {
// whereStr = whereStr + " c.name like '%"+searchArray[i]+"%' ";
// if (i != searchArray.length-1) {
// whereStr = whereStr + " or ";
// }
// }
// whereStr = whereStr + " )";
// }
// if (m != baseArray.length-1) {
// tableStr = tableStr + ",";
// whereStr = whereStr + " or ";
// }
// }
// hqlStr = hqlStr + tableStr + whereStr;
// System.out.println(hqlStr);
// System.out.println("################");
return hqlStr;
}
}
import java.text.SimpleDateFormat;
import java.util.Date;
public class TestHqlString {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Date date1 = new Date();
Date date2 = new Date();
String[] baseArray = {"BusinessType","Methodology","CaseBase"};
String str1 = "参数1";
//getKnowledgeBase(str1,date1,date2,baseArray);
String str2 = "参数1 参数2 ";
getKnowledgeBase(str2,date1,date2,baseArray);
String str3 = "参数1 参数2 参数3 ";
//getKnowledgeBase(str3,date1,date2,baseArray);
String str4 = "参数1 参数2 参数3 参数4 ";
//getKnowledgeBase(str4,date1,date2,baseArray);
}
public static String getKnowledgeBase(String searchStr, Date startDate, Date endDate,String[] baseArray){
String[] searchArray = searchStr.trim().split(" ");
String hqlStr = "from ";
String tableStr = "";
String whereStr = "";
//加入时间条件
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
for (int m = 0; m < baseArray.length; m++) {
tableStr = tableStr + baseArray[m] + ", ";
String whereDateStr = "";
if (!baseArray[m].equals("CaseBase")) {
whereDateStr = baseArray[m] + ".createDate between " + f.format(startDate) + " and " + f.format(endDate) + " and ";
}
String whereLikeStr = "";
for (int i = 0; i <searchArray.length; i++) {
whereLikeStr = whereLikeStr + baseArray[m] + ".name like '%" + searchArray[i] + "%' or ";
}
whereStr = whereStr + "(" + whereDateStr + "(" + whereLikeStr.substring(0, whereLikeStr.lastIndexOf("or") - 1) + ")) or ";
}
tableStr = tableStr.substring(0, tableStr.lastIndexOf(",") - 1);
whereStr = whereStr.substring(0, whereStr.lastIndexOf("or") - 1);
System.out.println(hqlStr + tableStr + " where " + whereStr);
// if (baseArray[m] == "BusinessType") {
// tableStr = tableStr + " BusinessType b";
// whereStr = whereStr + "((b.createDate between " + f.format(startDate) + " and " + f.format(endDate) + ") and ( ";
// for (int i = 0; i <searchArray.length; i++) {
// whereStr = whereStr + "b.name like '%"+searchArray[i]+"%'";
// if (i != searchArray.length-1) {
// whereStr = whereStr + " or ";
// }
// }
// whereStr = whereStr + " )) ";
// }
// if (baseArray[m] == "Methodology") {
// tableStr = tableStr + " Methodology m";
// whereStr = whereStr + "((m.createDate between " + f.format(startDate) + " and " + f.format(endDate) + ") and (";
// for (int i = 0; i <searchArray.length; i++) {
// whereStr = whereStr + " m.name like '%"+searchArray[i]+"%' ";
// if (i != searchArray.length-1) {
// whereStr = whereStr + " or ";
// }
// }
// whereStr = whereStr + " )) ";
// }
// if (baseArray[m] == "CaseBase") {
// tableStr = tableStr + " CaseBase c";
// whereStr = whereStr + " ( ";
// for (int i = 0; i <searchArray.length; i++) {
// whereStr = whereStr + " c.name like '%"+searchArray[i]+"%' ";
// if (i != searchArray.length-1) {
// whereStr = whereStr + " or ";
// }
// }
// whereStr = whereStr + " )";
// }
// if (m != baseArray.length-1) {
// tableStr = tableStr + ",";
// whereStr = whereStr + " or ";
// }
// }
// hqlStr = hqlStr + tableStr + whereStr;
// System.out.println(hqlStr);
// System.out.println("################");
return hqlStr;
}
}