Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Adding sort table after search. Done by Lagis.

  • Loading branch information...
commit 27ddae42aafbe42ac766b6eea9eea5e81b2b79f1 1 parent 67bd4bf
@AnderScore AnderScore authored
View
2  pom.xml
@@ -23,7 +23,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>texmex</groupId>
<artifactId>burrito</artifactId>
- <version>0.2.2-SNAPSHOT</version>
+ <version>0.2.3-SNAPSHOT</version>
<distributionManagement>
<repository>
<id>releases</id>
View
15 src/main/java/burrito/services/CrudServiceImpl.java
@@ -25,6 +25,7 @@
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
+import java.util.Collections;
import java.util.Date;
import java.util.List;
@@ -328,7 +329,7 @@ public CrudEntityList listEntities(String filter, String entityName,
private CrudEntityList search(Class<? extends Model> clazz, String filter,
PageMetaData<String> p) {
-
+
ItemCollection<SearchEntry> entries = searchManager.search(clazz,
filter, p);
List<Model> entities = new ArrayList<Model>();
@@ -337,6 +338,9 @@ private CrudEntityList search(Class<? extends Model> clazz, String filter,
if (entity != null)
entities.add(entity);
}
+
+ sortTable(entities, p);
+
CrudEntityList collection = new CrudEntityList();
collection.setItems(convertEntitesToCrudEntityDescriptions(entities));
collection.setPage(p.getPage());
@@ -364,6 +368,15 @@ private Long extractIDFromEntity(Model entity) {
throw new RuntimeException("Failed to get id field from entity", e);
}
}
+
+ private void sortTable(List<Model> entities, PageMetaData<String> p){
+
+ if(p.isAscending()){
+ Collections.sort(entities, new EntityComparator(p.getSortKey()));
+ }else{
+ Collections.sort(entities, Collections.reverseOrder(new EntityComparator(p.getSortKey())));
+ }
+ }
public Long save(CrudEntityDescription desc, Long clonedFromId) throws FieldValueNotUniqueException, CrudGenericException {
Class<? extends Model> clazz = extractClass(desc.getEntityName());
View
54 src/main/java/burrito/services/EntityComparator.java
@@ -0,0 +1,54 @@
+package burrito.services;
+
+import java.lang.reflect.Field;
+import java.util.Comparator;
+
+import burrito.util.EntityUtil;
+
+import siena.Model;
+
+public class EntityComparator implements Comparator<Model> {
+
+ private String sortKey ="";
+
+ public EntityComparator( String sortKey){
+ this.sortKey = sortKey;
+ }
+
+
+ @Override
+ public int compare(Model o1, Model o2) {
+ Field f1 = EntityUtil.getField(o1.getClass(), sortKey);
+ Field f2 = EntityUtil.getField(o2.getClass(), sortKey);
+
+ f1.setAccessible(true);
+ f2.setAccessible(true);
+
+ int ret = 0;
+ try {
+ @SuppressWarnings("unchecked")
+ Comparable<Object> comp1 = (Comparable<Object>)f1.get(o1);
+ @SuppressWarnings("unchecked")
+ Comparable<Object> comp2 = (Comparable<Object>)f2.get(o2);
+
+ if (comp1 == null) {
+ if (comp2 == null) {
+ return 0;
+ }
+
+ return -1;
+ }
+
+ if (comp2 == null) return 1;
+
+ ret = comp1.compareTo(comp2);
+
+ } catch (IllegalArgumentException e) {
+ throw new RuntimeException("Failed to sort table", e);
+ } catch (IllegalAccessException e) {
+ throw new RuntimeException("Failed to sort table", e);
+ }
+ return ret;
+ }
+
+}
Please sign in to comment.
Something went wrong with that request. Please try again.