The sorting should always be in descending order for numbers when first clicking on a new search term, i. e. another Keyword type (area or regular).

This commit is contained in:
Tom Vahlman 2012-04-13 14:35:13 +02:00
parent 6aaa4dd4f2
commit 1ed21f3b75
2 changed files with 111 additions and 107 deletions
src/main/java/se/su/dsv/scipro/admin/panels/match

@ -10,10 +10,11 @@
This page contains a list of all supervisors with number of research areas and keywords attached to them.
You may filter the list by searching for name of supervisor. Click on the supervisors name to see which research areas and keywords that is attached.
</div>
<form wicket:id="filterForm">
<label for="supervisorField">Filter using supervisor name:</label><br />
<input style="width:300px;" wicket:id="supervisorField" type="text" />
<input type="submit" wicket:id="filterButton" value="Filter"/>
<label for="autoComplete">Filter using supervisor name:</label><br />
<input wicket:id="autoComplete" /><br />
<form wicket:id="showAllForm">
<div><button wicket:id="showAllButton">Show all</button></div>
</form>
<table wicket:id="table" class="rounded-corner">
<thead>

@ -9,21 +9,21 @@ import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.EmptyPanel;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.odlabs.wiquery.ui.dialog.Dialog;
import org.springframework.data.domain.Sort;
import se.su.dsv.scipro.data.dataobjects.Employee;
import se.su.dsv.scipro.match.dao.interfaces.KeywordTypeDao;
import se.su.dsv.scipro.match.dao.interfaces.SupervisorDao;
import se.su.dsv.scipro.match.dataobject.Keyword;
import se.su.dsv.scipro.match.dataobject.KeywordType;
import se.su.dsv.scipro.reusable.EmployeeAutoComplete;
import se.su.dsv.scipro.springdata.services.SupervisorService;
public class AdminSupervisorKeywordPanel extends Panel {
@ -31,7 +31,7 @@ public class AdminSupervisorKeywordPanel extends Panel {
private static final long serialVersionUID = -6390936928562430893L;
@SpringBean
private SupervisorDao supervisorDao;
private SupervisorDao supervisorDao;
@SpringBean
private SupervisorService supervisorService;
@ -39,57 +39,60 @@ public class AdminSupervisorKeywordPanel extends Panel {
@SpringBean
private KeywordTypeDao keywordTypeDao;
public AdminSupervisorKeywordPanel(String str) {
super(str);
public AdminSupervisorKeywordPanel(String str) {
super(str);
Dialog dialog = new Dialog("detailsDialog");
setUpDialog(dialog);
WebMarkupContainer tableContainer = new WebMarkupContainer("table");
TextField<String> supervisorField = new TextField<String>("supervisorField", new Model<String>());
StringBuffer supervisorName = new StringBuffer();
add(new FilterForm(tableContainer, supervisorField, supervisorName));
setUpTable(dialog, tableContainer, supervisorName);
final WebMarkupContainer tableContainer = new WebMarkupContainer("table");
final EmployeeAutoComplete autoCompleteObjectField = createDsvAutocompleteComponent(tableContainer);
add(autoCompleteObjectField.setOutputMarkupId(true));
add(new Form<Void>("showAllForm").add(createShowAllButton(tableContainer, autoCompleteObjectField)));
setUpTable(dialog, tableContainer, autoCompleteObjectField);
}
private void setUpDialog(Dialog dialog) {
dialog.setModal(true);
dialog.setAutoOpen(false);
dialog.setWidth(500);
dialog.setHeight(400);
dialog.setTitle("Supervisor details");
dialog.add(new EmptyPanel("dialogContent"));
add(dialog.setOutputMarkupId(true));
}
private EmployeeAutoComplete createDsvAutocompleteComponent(final WebMarkupContainer tableContainer) {
return new EmployeeAutoComplete("autoComplete") {
private static final long serialVersionUID = 8758471618644888953L;
private void getSortOrder(StringBuffer sortDirection, StringBuffer orderBy__, String orderBy, final StringBuffer supervisorName) {
if(!supervisorName.toString().isEmpty()) {
String addToSearch = "true";
}
if ((sortDirection.toString().isEmpty() && !orderBy.equals("keywords.all.size")) || sortDirection.toString().equalsIgnoreCase("asc")) {
sortDirection.delete(0, sortDirection.length());
sortDirection.append("desc");
} else {
sortDirection.delete(0, sortDirection.length());
sortDirection.append("asc");
}
orderBy__.delete(0, orderBy__.length());
orderBy__.append(orderBy);
}
private List<Employee> addEmployeesToList(final StringBuffer supervisorName, List<Employee> employeeList) {
List<Employee> employeeListName = new ArrayList<Employee>();
for(Employee employee : employeeList) {
if(employee.getNameAsString().equals(supervisorName.toString())) {
employeeListName.add(employee);
@Override
public void action(AjaxRequestTarget pTarget, Employee newSelection) {
pTarget.addComponent(tableContainer);
}
}
if(!employeeListName.isEmpty()) {
return employeeListName;
}
return employeeList;
};
}
private void loadEmployeeList(List<Employee> employeeList, StringBuffer orderBy, StringBuffer sortDirection) {
private AjaxButton createShowAllButton(final WebMarkupContainer tableContainer, final EmployeeAutoComplete autoCompleteObjectField) {
return new AjaxButton("showAllButton") { //button to click for performing overriden method
private static final long serialVersionUID = 1L;
@Override
protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
List<Employee> employeeList = new ArrayList<Employee>();
employeeList.addAll(supervisorService.findAllEmployees(new Sort(Sort.Direction.ASC, "user.lastName")));
tableContainer.get("supervisorList").setDefaultModelObject(employeeList);
autoCompleteObjectField.setDefaultModelObject(null);
autoCompleteObjectField.clearInput();
target.addComponent(tableContainer);
target.addComponent(autoCompleteObjectField);
}
};
}
private void setUpDialog(Dialog dialog) {
dialog.setModal(true);
dialog.setAutoOpen(false);
dialog.setWidth(500);
dialog.setHeight(400);
dialog.setTitle("Supervisor details");
dialog.add(new EmptyPanel("dialogContent"));
add(dialog.setOutputMarkupId(true));
}
private void changeOrderBy(StringBuffer orderBy, String newOrderBy) {
orderBy.delete(0, orderBy.length());
orderBy.append(newOrderBy);
}
private void loadEmployeeList(List<Employee> employeeList, StringBuffer orderBy, StringBuffer sortDirection, StringBuffer previousOrderBy) {
KeywordType keywordType = keywordTypeDao.findByType(KeywordTypeDao.TYPE.REGULAR);
if(orderBy.toString().equals("keywords.all.size")) {
keywordType = keywordTypeDao.findByType(KeywordTypeDao.TYPE.REGULAR);
@ -97,24 +100,46 @@ if(!supervisorName.toString().isEmpty()) {
keywordType = keywordTypeDao.findByType(KeywordTypeDao.TYPE.RESEARCH_AREA);
}
if(!orderBy.toString().equalsIgnoreCase(previousOrderBy.toString())) { // the direction should always be desc for numbers when there is a new sort term
sortDirection.delete(0, sortDirection.length());
sortDirection.append("desc");
}
if(sortDirection.toString().equalsIgnoreCase("asc")) {
for(Employee employee : supervisorService.findSupervisorDescDirection(keywordType.getName())) {
for(Employee employee : supervisorService.findSupervisorBySQL(keywordType.getName(), "asc")) {
if(!employeeList.contains(employee)) {
employeeList.add(employee);
}
}
} else {
for(Employee employee : supervisorService.findSupervisorDescDirection(keywordType.getName())) {
} else if(sortDirection.toString().equalsIgnoreCase("desc")) {
for(Employee employee : supervisorService.findSupervisorBySQL(keywordType.getName(), "desc")) {
if(!employeeList.contains(employee)) {
employeeList.add(employee);
}
}
}
changePreviousOrderBy(previousOrderBy, orderBy);
if(sortDirection.toString().equalsIgnoreCase("asc")) {
changeSortDirection(sortDirection, "desc");
} else if(sortDirection.toString().equalsIgnoreCase("desc")) {
changeSortDirection(sortDirection, "asc");
}
}
private void setUpTable(final Dialog dialog, final WebMarkupContainer tableContainer, final StringBuffer supervisorName) {
private void changeSortDirection(StringBuffer sortDirection, String direction) {
sortDirection.delete(0, sortDirection.length());
sortDirection.append(direction);
}
private void changePreviousOrderBy(StringBuffer previousOrderBy, StringBuffer orderBy) {
previousOrderBy.delete(0, previousOrderBy.length());
previousOrderBy.append(orderBy.toString());
}
private void setUpTable(final Dialog dialog, final WebMarkupContainer tableContainer, final EmployeeAutoComplete autoCompleteObjectField) {
final StringBuffer sortDirection = new StringBuffer();
final StringBuffer orderBy = new StringBuffer();
final StringBuffer previousOrderBy = new StringBuffer();
final ListView<Employee> dataView = new ListView<Employee>("supervisorList", new LoadableDetachableModel<List<Employee>>() {
private static final long serialVersionUID = -2395796971679213814L;
@ -122,25 +147,28 @@ if(!supervisorName.toString().isEmpty()) {
@Override
protected List<Employee> load() {
List<Employee> employeeList = new ArrayList<Employee>();
if(!orderBy.toString().equals("areas.all.size") && !orderBy.toString().equals("keywords.all.size")) {
org.springframework.data.domain.Sort sorter;
if (sortDirection.toString().isEmpty() || sortDirection.toString().equalsIgnoreCase("asc")) {
sorter = new org.springframework.data.domain.Sort(org.springframework.data.domain.Sort.Direction.ASC, "user.lastName");
employeeList.addAll(supervisorService.findAllEmployees(sorter));
} else {
sorter = new org.springframework.data.domain.Sort(org.springframework.data.domain.Sort.Direction.DESC, "user.lastName");
employeeList.addAll(supervisorService.findAllEmployees(sorter));
}
} else {
loadEmployeeList(employeeList, orderBy, sortDirection);
}
if(supervisorName.toString().isEmpty()) {
if(autoCompleteObjectField.getDefaultModelObject() != null) {
employeeList.add((Employee) autoCompleteObjectField.getDefaultModelObject());
return employeeList;
} else if (orderBy.toString().isEmpty()) {
employeeList.addAll(supervisorService.findAllEmployees(new Sort(Sort.Direction.ASC, "user.lastName"))); // the direction should always be asc for characters when there is a new sort term
changeSortDirection(sortDirection, "desc");
previousOrderBy.append("user.lastName");
} else if (orderBy.toString().equals("user.lastName")) { // if there has been a previous search and the search term was different than the present search term or direction should be asc
if((!previousOrderBy.toString().isEmpty() && !previousOrderBy.toString().equals(orderBy.toString())) || sortDirection.toString().equalsIgnoreCase("asc")) {
employeeList.addAll(supervisorService.findAllEmployees(new Sort(Sort.Direction.ASC, "user.lastName")));
changeSortDirection(sortDirection, "desc");
} else if(sortDirection.toString().equalsIgnoreCase("desc")){
employeeList.addAll(supervisorService.findAllEmployees(new Sort(Sort.Direction.DESC, "user.lastName")));
changeSortDirection(sortDirection, "asc");
}
changePreviousOrderBy(previousOrderBy, orderBy);
} else {
loadEmployeeList(employeeList, orderBy, sortDirection, previousOrderBy);
}
return addEmployeesToList(supervisorName, employeeList);
return employeeList;
}
}) {
private static final long serialVersionUID = 3572766096237883198L;
@Override
@ -149,9 +177,7 @@ if(!supervisorName.toString().isEmpty()) {
List<Keyword> areaKeywords = supervisor.getKeywords().getFiltered(KeywordTypeDao.TYPE.RESEARCH_AREA.toDbName());
List<Keyword> regularKeywords = supervisor.getKeywords().getFiltered(KeywordTypeDao.TYPE.REGULAR.toDbName());
final AjaxLink<Void> detailsLink = new AjaxLink<Void>("detailsLink") {
private static final long serialVersionUID = 8902222410746133732L;
@Override
public void onClick(AjaxRequestTarget target) {
target.addComponent(dialog);
@ -172,15 +198,15 @@ if(!supervisorName.toString().isEmpty()) {
}
};
final AjaxLink<Void> nameSortLink = new AjaxLink<Void>("nameSortLink") {
private static final long serialVersionUID = -6459164267551936706L;
@Override
public void onClick(AjaxRequestTarget target) {
getSortOrder(sortDirection, orderBy, "user.lastName", supervisorName);
final AjaxLink<Void> nameSortLink = new AjaxLink<Void>("nameSortLink") {
private static final long serialVersionUID = -6459164267551936706L;
@Override
public void onClick(AjaxRequestTarget target) {
changeOrderBy(orderBy, "user.lastName");
target.addComponent(tableContainer);
}
};
tableContainer.add(nameSortLink);
}
};
tableContainer.add(nameSortLink);
final AjaxLink<Void> numberOfAreasSortLink = new AjaxLink<Void>("numberOfAreasSortLink") {
@ -188,7 +214,7 @@ if(!supervisorName.toString().isEmpty()) {
@Override
public void onClick(AjaxRequestTarget target) {
getSortOrder(sortDirection, orderBy, "areas.all.size", supervisorName);
changeOrderBy(orderBy, "areas.all.size");
target.addComponent(tableContainer);
}
};
@ -200,35 +226,12 @@ if(!supervisorName.toString().isEmpty()) {
@Override
public void onClick(AjaxRequestTarget target) {
getSortOrder(sortDirection, orderBy, "keywords.all.size", supervisorName);
changeOrderBy(orderBy, "keywords.all.size");
target.addComponent(tableContainer);
}
};
tableContainer.add(numberOfRegularsSortLink);
tableContainer.add(dataView);
add(tableContainer.setOutputMarkupId(true));
}
private class FilterForm extends Form<Void> {
private static final long serialVersionUID = 3488715734113880308L;
public FilterForm(final WebMarkupContainer tableContainer, final TextField<String> supervisorField, final StringBuffer supervisorName) {
super("filterForm");
AjaxButton filterButton = new AjaxButton("filterButton") {
private static final long serialVersionUID = -8216928567709372986L;
@Override
protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
supervisorName.append(supervisorField.getModelObject());
target.addComponent(tableContainer);
}
};
add(supervisorField);
add(filterButton);
}
}
tableContainer.add(dataView);
add(tableContainer.setOutputMarkupId(true));
}
}