import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
+import java.util.Collections;
+import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import org.splat.service.UserService;
import org.splat.service.dto.Proxy;
import org.splat.service.dto.SearchFilterDTO;
+import org.splat.service.dto.StudyDTO;
import org.splat.service.technical.ProjectSettingsService;
import org.splat.som.ApplicationRights;
import org.splat.wapp.Constants;
refreshResult, selectContextType, selectContextValue, cancelSelect, removeContext
}
+ /**
+ * A criteria to sort studies by.
+ */
+ private SortCriterion _newSortedBy;
+
+ /**
+ * Sort order key in the session.
+ */
+ protected static final String ORDER_KEY = "isDescendingOrder";
+
+ /**
+ * Sort criterion key in the session.
+ */
+ protected static final String CRITERION_KEY = "sortCriterion";
+
// ==============================================================================================================================
// Action methods
// ==============================================================================================================================
+ /**
+ * 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;
+ }
+
/**
* Perform actions according to the current mode.
*
_filter = filter;
}
}
+
+ /**
+ * Get the newSortedBy.
+ * @return the newSortedBy
+ */
+ public SortCriterion getNewSortedBy() {
+ return _newSortedBy;
+ }
+
+ /**
+ * Set the newSortedBy.
+ * @param newSortedBy the newSortedBy to set
+ */
+ public void setNewSortedBy(final SortCriterion newSortedBy) {
+ _newSortedBy = newSortedBy;
+ }
}
\ No newline at end of file