Skip to content

Commit

Permalink
Finished search implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
sergei-doroshenko committed Mar 14, 2014
1 parent 3d7cd9f commit b5b3bff
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 88 deletions.
202 changes: 147 additions & 55 deletions src/main/java/org/training/issuetracker/data/hiber/IssueImplHiber.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

import org.apache.log4j.Logger;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Disjunction;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -55,7 +57,7 @@ public int getIssueRecordsCount() throws DaoException {
@Override
public List getIssueList(SearchFilterParams params) throws DaoException {

DetachedCriteria criteria = DetachedCriteria.forClass(Issue.class);
DetachedCriteria criteria = DetachedCriteria.forClass(Issue.class, "issue");
int page = params.getPage();
int rows = params.getRows();
int firstResult = (page - 1) * rows;
Expand Down Expand Up @@ -96,62 +98,152 @@ public DetachedCriteria addSearch (DetachedCriteria criteria, String filters) th
List<SearchRule> rules = filter.getRules();
Map<String, Set<SearchRule>> checkedRules = checkDoubles(rules);

//Key is field
for (String key : checkedRules.keySet()) {
Set<SearchRule> rulesSet = checkedRules.get(key);
boolean isNeedExpr = (rulesSet.size() > 1);

for (SearchRule rule : rulesSet) {
logger.debug("Rule " + rule);
String field = rule.getField();
String data = rule.getData();
Object value = getSearchValue(field, data);
switch (rule.getOp()) {
case "eq" : {
criteria.add(Restrictions.eq(rule.getField(), value));
break;
}
case "ne" : {
criteria.add(Restrictions.ne(field, value));
break;
}
case "lt" : {
criteria.add(Restrictions.lt(field, value));
break;
}
case "le" : {
criteria.add(Restrictions.le(field, value));
break;
}
case "gt" : {
criteria.add(Restrictions.gt(field, value));
break;
}
case "ge" : {
criteria.add(Restrictions.ge(field, value));
break;
}
case "bw" : {
break;
}
case "bn" : {
break;
}
case "ew" : {
break;
}
case "en" : {
break;
}
case "cn" : {
break;
}
case "nc" : {
break;
}
default : {
break;
}
};
if(key.equals("project")) {
criteria.createAlias("issue.project", "project");
}
if(key.equals("build")) {
criteria.createAlias("issue.build", "build");
}
//Add OR
if (rulesSet.size() > 1) {
Disjunction disjunction = Restrictions.disjunction();
for (SearchRule rule : rulesSet) {

String field = rule.getField();
String data = rule.getData();
Object value = getSearchValue(field, data);
switch (rule.getOp()) {
case "eq" : {
disjunction.add(Restrictions.eq(field, value));
break;
}
case "ne" : {
disjunction.add(Restrictions.ne(field, value));
break;
}
case "lt" : {
disjunction.add(Restrictions.lt(field, value));
break;
}
case "le" : {
disjunction.add(Restrictions.le(field, value));
break;
}
case "gt" : {
disjunction.add(Restrictions.gt(field, value));
break;
}
case "ge" : {
disjunction.add(Restrictions.ge(field, value));
break;
}
case "bw" : {
if(field.equals("project")) {
disjunction.add(Restrictions.like("project.name", (String) value, MatchMode.START));
} else if (field.equals("build")) {
disjunction.add(Restrictions.like("build.name", (String) value, MatchMode.START));
} else {
disjunction.add(Restrictions.like(field, (String) value, MatchMode.START));
}
break;
}
case "ew" : {
if(field.equals("project")) {
disjunction.add(Restrictions.like("project.name", (String) value, MatchMode.END));
} else if (field.equals("build")) {
disjunction.add(Restrictions.like("build.name", (String) value, MatchMode.END));
} else {
disjunction.add(Restrictions.like(field, (String) value, MatchMode.END));
}
break;
}
case "cn" : {
if(field.equals("project")) {
disjunction.add(Restrictions.like("project.name", (String) value, MatchMode.ANYWHERE));
} else if (field.equals("build")) {
disjunction.add(Restrictions.like("build.name", (String) value, MatchMode.ANYWHERE));
} else {
disjunction.add(Restrictions.like(field, (String) value, MatchMode.ANYWHERE));
}
break;
}
default : {
break;
}
};
}
criteria.add(disjunction);

} else {
for (SearchRule rule : rulesSet) {

String field = rule.getField();
String data = rule.getData();
Object value = getSearchValue(field, data);
switch (rule.getOp()) {
case "eq" : {
criteria.add(Restrictions.eq(rule.getField(), value));
break;
}
case "ne" : {
criteria.add(Restrictions.ne(field, value));
break;
}
case "lt" : {
criteria.add(Restrictions.lt(field, value));
break;
}
case "le" : {
criteria.add(Restrictions.le(field, value));
break;
}
case "gt" : {
criteria.add(Restrictions.gt(field, value));
break;
}
case "ge" : {
criteria.add(Restrictions.ge(field, value));
break;
}
case "bw" : {
if(field.equals("project")) {
criteria.add(Restrictions.like("project.name", (String) value, MatchMode.START));
} else if (field.equals("build")) {
criteria.add(Restrictions.like("build.name", (String) value, MatchMode.START));
} else {
criteria.add(Restrictions.like(field, (String) value, MatchMode.START));
}
break;
}
case "ew" : {
if(field.equals("project")) {
criteria.add(Restrictions.like("project.name", (String) value, MatchMode.END));
} else if (field.equals("build")) {
criteria.add(Restrictions.like("build.name", (String) value, MatchMode.END));
} else {
criteria.add(Restrictions.like(field, (String) value, MatchMode.END));
}
break;
}
case "cn" : {
if(field.equals("project")) {
criteria.add(Restrictions.like("project.name", (String) value, MatchMode.ANYWHERE));
} else if (field.equals("build")) {
criteria.add(Restrictions.like("build.name", (String) value, MatchMode.ANYWHERE));
} else {
criteria.add(Restrictions.like(field, (String) value, MatchMode.ANYWHERE));
}
break;
}
default : {
break;
}
};
}
}
}
}
Expand All @@ -160,7 +252,7 @@ public DetachedCriteria addSearch (DetachedCriteria criteria, String filters) th
}

public Object getSearchValue (String field, String data) throws NumberFormatException, DaoException {
Object value = null;
Object value = data;
if (field.equals("id")){
value = Long.parseLong(data);
}
Expand Down
30 changes: 0 additions & 30 deletions src/main/java/org/training/issuetracker/utils/SearchRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,12 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.training.issuetracker.domain.DAO.PropDAO;
import org.training.issuetracker.domain.DAO.PropertyType;
import org.training.issuetracker.exceptions.DaoException;

@Service
public class SearchRule {
private String field;
private String op;
private String data;

@Autowired
private PropDAO propDAO;

public SearchRule() { }

public String getField() {
Expand All @@ -38,31 +32,10 @@ public String getData() {
return data;
}

// public Object getData() throws NumberFormatException, DaoException {
// Object value = data;
//
// if (field.equals("id")){
// value = Long.parseLong(data);
// }
//
// if (field.equals("status")){
// value = propDAO.getProp(PropertyType.STATUS, Long.parseLong(data));
// }
// return value;
// }

public void setData(String data) {
this.data = data;
}

public PropDAO getPropDAO() {
return propDAO;
}

public void setPropDAO(PropDAO propDAO) {
this.propDAO = propDAO;
}

@Override
public String toString() {
return "SearchRule [field=" + field + ", op=" + op + ", data=" + data
Expand All @@ -71,7 +44,4 @@ public String toString() {

public enum Op {eq, bw}

// "rules":[
//{"field":"id","op":"eq","data":"3"},
//{"field":"createBy","op":"bw","data":"Tim"}]}
}
6 changes: 3 additions & 3 deletions src/main/webapp/js/issuetracker-tables.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ var issue_model = [
{name: "modifyBy", index: "modifyBy", width: 75,
searchrules: { "required": true}, stype: 'select', searchoptions: {sopt: ['eq', 'ne'], dataUrl: '/issuetracker/user/options'}},
{name: "summary", index: 'summary', width: 100,
searchrules: { "required": true}, searchoptions: {sopt: ['bw', 'bn', 'ew', 'en', 'cn', 'nc']}},//hidden: true, searchoptions: {searchhidden: true},
searchrules: { "required": true}, searchoptions: {sopt: ['bw', 'ew', 'cn']}},//hidden: true, searchoptions: {searchhidden: true},
{name: "status", index: 'status', width: 70,
searchrules: { "required": true}, stype: 'select', searchoptions: {sopt: ['eq', 'ne'], dataUrl: '/issuetracker/prop/options/STATUS'}},
{name: "resolution", index: "resolution", width: 70,
Expand All @@ -31,9 +31,9 @@ var issue_model = [
{name: "priority", index: 'priority', width: 70, formatter: colorFormatter,
searchrules: { "required": true}, stype: 'select', searchoptions: {sopt: ['eq', 'ne'], dataUrl: '/issuetracker/prop/options/PRIORITY'}},
{name: "project", index: "project", width: 150,
searchrules: { "required": true}, searchoptions: {sopt: ['bw', 'bn', 'ew', 'en', 'cn', 'nc']}},
searchrules: { "required": true}, searchoptions: {sopt: ['bw', 'ew', 'cn']}},
{name: "build", index: "build", width: 70,
searchrules: { "required": true}, searchoptions: {sopt: ['bw', 'bn', 'ew', 'en', 'cn', 'nc']}},
searchrules: { "required": true}, searchoptions: {sopt: ['bw', 'ew', 'cn']}},
{name: "assignee", index: 'assignee', width: 100,
searchrules: { "required": true}, stype: 'select', searchoptions: {sopt: ['eq', 'ne'], dataUrl: '/issuetracker/user/options'}}
];
Expand Down

0 comments on commit b5b3bff

Please sign in to comment.