+ /**
+ * StudyDTO sort criteria.
+ */
+ enum SortCriterion {
+ /**
+ * Reference.
+ */
+ REFERENCE,
+ /**
+ * Name.
+ */
+ NAME,
+ /**
+ * Creation date.
+ */
+ CREATEDATE,
+ /**
+ * Modification date.
+ */
+ MODIFDATE,
+ /**
+ * The person responsible.
+ */
+ RESPONSIBLE
+ }
+
+ /**
+ * StudyDTO comparator class.
+ */
+ private class StudyComparator implements Comparator<StudyDTO> {
+
+ /**
+ * The criteria by which studies are compared.
+ */
+ SortCriterion _criterion = SortCriterion.NAME;
+
+ /**
+ * Constructor from comparison criteria.
+ * @param criterion
+ * the criteria
+ */
+ public StudyComparator(final SortCriterion criterion) {
+ _criterion = criterion;
+ }
+
+ /**
+ * {@inheritDoc}
+ * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
+ */
+ public int compare(final StudyDTO first, final StudyDTO second) {
+ switch (_criterion) {
+ case REFERENCE:
+ return first.getReference().compareTo(second.getReference());
+ case CREATEDATE:
+ return first.getDate().compareTo(second.getDate());
+ case MODIFDATE:
+ return first.getLastModificationDate().compareTo(
+ second.getLastModificationDate());
+ case RESPONSIBLE:
+ return getText(first.getAuthorName())
+ .compareTo(getText(second.getAuthorName()));
+ default:
+ return first.getTitle().compareTo(second.getTitle());
+ }
+ }
+ }
+
+ /**
+ * Set search results sort order.
+ * @return
+ * SUCCESS if successfully found and sorted search results;
+ * ERROR otherwise
+ */
+ public String doSetOrder() {
+ String res = ERROR;
+ _result = (List<Proxy>) getSession().get(RESULT_KEY);
+ Boolean order = (Boolean) getSession().get(ORDER_KEY);
+ SortCriterion oldSortedBy = (SortCriterion) getSession().get(CRITERION_KEY);
+
+ if (_result != null) {
+
+ if (_newSortedBy != null && !_newSortedBy.equals(oldSortedBy)) { // Sort by new criterion
+ // Direct cast into collection of another type just won't work in Java
+ Collections.sort((List<StudyDTO>)(List<?>) _result, new StudyComparator(_newSortedBy));
+ getSession().put(CRITERION_KEY, _newSortedBy);
+ getSession().put(ORDER_KEY, false);
+ } else {
+ if (order == null) {
+ order = false;
+ } else { // need to change the order
+ order = !order;
+ }
+ getSession().put(ORDER_KEY, order);
+ Collections.reverse(_result);
+ }
+ res = SUCCESS;
+ }
+
+ return res;
+ }
+