Skip to content

Commit 0a711f6

Browse files
author
noobandy
committed
Column Metadata
1 parent a4e1774 commit 0a711f6

File tree

3 files changed

+183
-18
lines changed

3 files changed

+183
-18
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package in.anandm.apps.template.application.dto;
2+
3+
public class ColumnMetaData {
4+
5+
private String columnName;
6+
private Boolean bRegx;
7+
private Boolean bSortable;
8+
private Boolean bSearchable;
9+
private String sSearch;
10+
private String mDataProp;
11+
private String aSortDir;
12+
13+
public ColumnMetaData() {
14+
super();
15+
// TODO Auto-generated constructor stub
16+
}
17+
/**
18+
* @return the columnName
19+
*/
20+
public String getColumnName() {
21+
return columnName;
22+
}
23+
/**
24+
* @return the bRegx
25+
*/
26+
public Boolean getbRegx() {
27+
return bRegx;
28+
}
29+
/**
30+
* @return the bSortable
31+
*/
32+
public Boolean getbSortable() {
33+
return bSortable;
34+
}
35+
/**
36+
* @return the bSearchable
37+
*/
38+
public Boolean getbSearchable() {
39+
return bSearchable;
40+
}
41+
/**
42+
* @return the sSearch
43+
*/
44+
public String getsSearch() {
45+
return sSearch;
46+
}
47+
/**
48+
* @return the mDataProp
49+
*/
50+
public String getmDataProp() {
51+
return mDataProp;
52+
}
53+
/**
54+
* @return the aSortDir
55+
*/
56+
public String getaSortDir() {
57+
return aSortDir;
58+
}
59+
/**
60+
* @param columnName the columnName to set
61+
*/
62+
public void setColumnName(String columnName) {
63+
this.columnName = columnName;
64+
}
65+
/**
66+
* @param bRegx the bRegx to set
67+
*/
68+
public void setbRegx(Boolean bRegx) {
69+
this.bRegx = bRegx;
70+
}
71+
/**
72+
* @param bSortable the bSortable to set
73+
*/
74+
public void setbSortable(Boolean bSortable) {
75+
this.bSortable = bSortable;
76+
}
77+
/**
78+
* @param bSearchable the bSearchable to set
79+
*/
80+
public void setbSearchable(Boolean bSearchable) {
81+
this.bSearchable = bSearchable;
82+
}
83+
/**
84+
* @param sSearch the sSearch to set
85+
*/
86+
public void setsSearch(String sSearch) {
87+
this.sSearch = sSearch;
88+
}
89+
/**
90+
* @param mDataProp the mDataProp to set
91+
*/
92+
public void setmDataProp(String mDataProp) {
93+
this.mDataProp = mDataProp;
94+
}
95+
/**
96+
* @param aSortDir the aSortDir to set
97+
*/
98+
public void setaSortDir(String aSortDir) {
99+
this.aSortDir = aSortDir;
100+
}
101+
102+
103+
}

src/main/java/in/anandm/apps/template/application/impl/BaseRepository.java

Lines changed: 68 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
*/
44
package in.anandm.apps.template.application.impl;
55

6+
import in.anandm.apps.template.application.dto.ColumnMetaData;
67
import in.anandm.apps.template.application.dto.DataTable;
78

89
import java.io.Serializable;
910
import java.util.ArrayList;
1011
import java.util.List;
1112
import java.util.Map;
13+
import java.util.Map.Entry;
1214

1315
import javax.persistence.EntityManager;
1416
import javax.persistence.PersistenceContext;
@@ -17,6 +19,7 @@
1719
import org.springframework.stereotype.Repository;
1820

1921
import com.googlecode.genericdao.dao.jpa.GenericDAOImpl;
22+
import com.googlecode.genericdao.search.Filter;
2023
import com.googlecode.genericdao.search.Search;
2124
import com.googlecode.genericdao.search.SearchResult;
2225
import com.googlecode.genericdao.search.Sort;
@@ -45,20 +48,79 @@ public void setEntityManager(EntityManager entityManager) {
4548
}
4649

4750
public DataTable<T> getDataTable(Class<T> entityClass,Map<String, String> params){
51+
52+
Integer totalColumns = Integer.valueOf(params.get("iColumns"));
53+
String[] columnNames = params.get("sColumns").split(",");
54+
ColumnMetaData[] columnMetaDatas = new ColumnMetaData[totalColumns];
55+
56+
for (Entry<String, String> entry : params.entrySet()) {
57+
String key = entry.getKey();
58+
String[] tokens = key.split("_");
59+
if(tokens.length > 1){
60+
String propPrefix = tokens[0];
61+
int columnIndex = Integer.valueOf(tokens[1]);
62+
63+
ColumnMetaData columnMetaData = columnMetaDatas[columnIndex];
64+
65+
66+
if( columnMetaData == null){
67+
columnMetaData = new ColumnMetaData();
68+
columnMetaData.setColumnName(columnNames[columnIndex]);
69+
}
70+
71+
if("bRegx".equals(propPrefix)){
72+
columnMetaData.setbRegx(Boolean.valueOf(entry.getValue()));
73+
}
74+
75+
if("bSearchable".equals(propPrefix)){
76+
columnMetaData.setbSearchable(Boolean.valueOf(entry.getValue()));
77+
}
78+
79+
if("bSortable".equals(propPrefix)){
80+
columnMetaData.setbSortable(Boolean.valueOf(entry.getValue()));
81+
}
82+
83+
if("mDataProp".equals(propPrefix)){
84+
columnMetaData.setmDataProp(entry.getValue());
85+
}
86+
87+
if("aSortDir".equals(propPrefix)){
88+
columnMetaData.setaSortDir(entry.getValue());
89+
}
90+
91+
92+
if("sSearch".equals(propPrefix)){
93+
columnMetaData.setsSearch(entry.getValue());
94+
}
95+
96+
columnMetaDatas[columnIndex] = columnMetaData;
97+
}
98+
99+
100+
}
48101

49102
Search search = new Search(entityClass);
50103
String searchText = params.get("sSearch");
51104
Integer sEcho = Integer.valueOf(params.get("sEcho"));
52105
Integer start = Integer.valueOf(params.get("iDisplayStart"));
53106
Integer noOfRecordsPerPage = Integer.valueOf(params.get("iDisplayLength"));
54-
55-
//search.addFetches(dataTableProperties);
56-
if(searchText != null && !"".equals(searchText.trim())){
57-
search.addFilterILike("userAccount.userId", searchText);
58-
}
107+
108+
109+
List<Sort> sorts = new ArrayList<Sort>();
110+
List<Filter> filters = new ArrayList<Filter>();
111+
112+
for (ColumnMetaData columnMetaData : columnMetaDatas) {
113+
if(searchText != null && !"".equals(searchText.trim()) && columnMetaData.getbSearchable()){
114+
filters.add(Filter.ilike(columnMetaData.getColumnName(), "%"+searchText+"%"));
115+
}
116+
}
117+
Filter[] filterArray = new Filter[filters.size()];
118+
119+
search.addFilterOr(filters.toArray(filterArray));
120+
59121
search.setFirstResult(start);
60122
search.setMaxResults(noOfRecordsPerPage);
61-
List<Sort> sorts = new ArrayList<Sort>();
123+
62124
sorts.add(Sort.desc("userAccount.userId", true));
63125
search.setSorts(sorts);
64126
SearchResult<T> searchresult = _searchAndCount(search);

src/main/webapp/WEB-INF/views/pages/tables/data.jsp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@
268268
<td>A</td>
269269
</tr>
270270
<tr>
271-
<td>Webkit</td>
271+
<td>Webkit</td>type
272272
<td>OmniWeb 5.5</td>
273273
<td>OSX.4+</td>
274274
<td>420</td>
@@ -543,24 +543,24 @@
543543
"bServerSide": true,
544544
"sAjaxSource": "${pageContext.request.contextPath}/ajax/userData",
545545
"aoColumns": [
546-
{ "mData": "id","sName": "userAccount.id"},
547-
{ "mData": "userId","sName": "userAccount.userId"},
548-
{ "mData": "firstName","sName": "userProfile.firstName"},
549-
{ "mData": "lastName","sName": "userProfile.lastName"},
550-
{ "mData": "gender","sName": "userProfile.gender"},
551-
{ "mData": "dob","sName": "userProfile.dob"},
552-
{ "mData": "emailId","sName": "userProfile.emailId"},
553-
{ "mData": "contactNumber","sName": "userProfile.contactNumber"},
554-
{ "mData": "expireOn","sName": "userAccount.expireOn"},
555-
{ "mData": "admin","sName": "userAccount.admin",
546+
{ "mData": "id","sName": "id","bSearchable":false},
547+
{ "mData": "userId","sName": "userAccount.userId","bSearchable":true},
548+
{ "mData": "firstName","sName": "userProfile.firstName","bSearchable":true},
549+
{ "mData": "lastName","sName": "userProfile.lastName","bSearchable":true},
550+
{ "mData": "gender","sName": "userProfile.gender","bSearchable":false},
551+
{ "mData": "dob","sName": "userProfile.dob","bSearchable":false},
552+
{ "mData": "emailId","sName": "userProfile.emailId","bSearchable":true},
553+
{ "mData": "contactNumber","sName": "userProfile.contactNumber","bSearchable":true},
554+
{ "mData": "expireOn","sName": "userAccount.expireOn","bSearchable":false},
555+
{ "mData": "admin","sName": "userAccount.admin","bSearchable":false,
556556
"fnRender" : function(obj) {
557557
if (obj.aData.admin)
558558
return '<input type="checkbox" name="aCheckBox" class="dt_checked" checked value="'+obj.aData.admin+'\"/>';
559559
560560
return '<input type="checkbox" name="aCheckBox" class="dt_checked" value="'+obj.aData.admin+'\"/>';
561561
}
562562
},
563-
{ "mData": "enabled","sName": "userAccount.enabled",
563+
{ "mData": "enabled","sName": "userAccount.enabled","bSearchable":false,
564564
"fnRender" : function(obj) {
565565
if (obj.aData.enabled)
566566
return '<input type="checkbox" name="aCheckBox" class="dt_checked" checked value="'+obj.aData.enabled+'\"/>';

0 commit comments

Comments
 (0)