Entire class where search is performed.And form is not initiated.It is ActionForm coming from Action Class.
Business Class Code
Code:
public List search(SessionFactory sessionFactory, ActionForm form) {
List searchresult = null;
try {
AlterSession(session);
GeneralUtils webutil = new GeneralUtils();
HashMap temp = (HashMap)PropertyUtils.describe(form); //Storing all data from formbean into a HashMap.
HashMap params = webutil.validateformMap(temp); //getting parameters from Hashmap created.
session = sessionFactory.openSession();
AlterSession(session);
Criteria crit = session.createCriteria(form.getClass());
traceLogger.info("Search Result HashMap ==>" + params);
//First Getting Date Field
String dateFields = getS(params, "dateFields");
String fieldName = null;
String[] dateList = null;
String startTag = "_from";
String endTag = "_to";
String startDate = null;
String endDate = null;
params.remove("tableName");
params.remove("action");
if (dateFields != null) {
dateList = dateFields.split(",");
for (int i = 0; i < dateList.length; i++) {
fieldName = dateList[i];
String tempFieldname = fieldName;
traceLogger.info("tempFieldname " + tempFieldname);
startDate = getS(params, tempFieldname + startTag);
endDate = getS(params, tempFieldname + endTag);
traceLogger.info("startDate " + startDate);
traceLogger.info("endDate " + endDate);
params.remove(tempFieldname + startTag);
params.remove(tempFieldname + endTag);
if (startDate != null && endDate != null) {
if (startDate.length() == 10 &&
endDate.length() == 10) {
if(tempFieldname!=null && !tempFieldname.equalsIgnoreCase("") && tempFieldname.equalsIgnoreCase("createdate")){
crit.add(Restrictions.between(tempFieldname, startDate, endDate));
}
else{
//crit.add(Expression.sql("TO_DATE("+tempFieldname+",'DD/MM/YYYY') between '"+startDate+"' and '"+endDate+"'"));
crit.add(Restrictions.sqlRestriction("TO_DATE("+tempFieldname+",'DD/MM/YYYY') between '"+startDate+"' and '"+endDate+"'"));
}
}
}
}
}
params.remove("dateFields");
// Get Order By Clause
String orderBy = "";
if (getS(params, "orderBy") != null) {
orderBy = getS(params, "orderBy");
crit.addOrder(Order.asc(orderBy));
}
params.remove("orderBy");
params.remove("selTender");
params.remove("primaryKey");
params.remove("ad_details");
Iterator columns = params.keySet().iterator();
String columnName, value;
while (columns.hasNext()) {
columnName = (String)columns.next();
traceLogger.info("Column Name xxx ==>" + columnName);
// System.out.println(columnName);
if (getS(params, columnName) != null &&
(getS(params, columnName)).length() > 0) {
traceLogger.info("Column Name ==>" + columnName);
value = getS(params, columnName);
traceLogger.info("value ==>" + value);
if (value != null) {
if (value.indexOf("'") != -1)
value = value.replaceAll("'", "'||chr(39)||'");
}
if (value.substring(0, 1).equals("!")) {
crit.add(Restrictions.ne(columnName,
value.substring(1, value.length())));
//fieldClause += "("+columnName +" != '"+ value.substring(1, value.length()) + "')";
}
else if (value.substring(0, 1).equals(">")) {
crit.add(Restrictions.gt(columnName,
value.substring(1, value.length())));
//fieldClause += "("+columnName +" > '"+value.substring(1,value.length()) + "')";
} else if (value.substring(0, 1).equals("<")) {
crit.add(Restrictions.le(columnName,
value.substring(1, value.length())));
//fieldClause += "("+columnName +" < '"+value.substring(1,value.length()) + "')";
} else if (value.indexOf("%") >= 0) {
crit.add(Restrictions.ilike(columnName, value));
} else {
crit.add(Restrictions.eq(columnName, value));
}
}
}
traceLogger.info("Search Result HashMap 2 ==>" + params);
// Get Order By Clause
searchresult = crit.list();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
try {
session.flush();
} catch (Exception e) {
e.printStackTrace();
}
session.close();
} catch (JDBCException e) {
e.printStackTrace();
}
} //Finally
return searchresult;
} //End of search