Project

General

Profile

Download (2.54 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
* Copyright (C) 2016 EDIT
4
* European Distributed Institute of Taxonomy
5
* http://www.e-taxonomy.eu
6
*
7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8
* See LICENSE.TXT at the top of this package for the full license terms.
9
*/
10
package eu.etaxonomy.cdm.remote.controller;
11

    
12
import java.util.Arrays;
13
import java.util.List;
14

    
15
import org.apache.log4j.Logger;
16

    
17
import eu.etaxonomy.cdm.model.common.CdmBase;
18
import eu.etaxonomy.cdm.model.common.OrderedTermVocabulary;
19
import eu.etaxonomy.cdm.persistence.query.OrderHint;
20

    
21
enum OrderHintPreset {
22

    
23
    ORDER_BY_ID_ASC(OrderHint.ORDER_BY_ID),
24
    ORDER_BY_ID_DESC(OrderHint.ORDER_BY_ID_DESC),
25
    BY_ORDER_INDEX_DESC(OrderHint.BY_ORDER_INDEX_DESC),
26
    BY_ORDER_INDEX_ASC(OrderHint.BY_ORDER_INDEX),
27
    ORDER_BY_TITLE_CACHE_ASC(OrderHint.ORDER_BY_TITLE_CACHE),
28
    ORDER_BY_TITLE_CACHE_DESC(OrderHint.ORDER_BY_TITLE_CACHE_DESC),
29
    NOMENCLATURAL_SORT_ORDER_ASC(OrderHint.NOMENCLATURAL_SORT_ORDER),
30
    NOMENCLATURAL_SORT_ORDER_DESC(OrderHint.NOMENCLATURAL_SORT_ORDER_DESC);
31

    
32
    public static final Logger logger = Logger.getLogger(OrderHintPreset.class);
33

    
34
    private final List<OrderHint> orderHints;
35

    
36
    List<OrderHint> orderHints() {
37
        return orderHints;
38
    }
39

    
40
    OrderHintPreset(OrderHint... orderHint) {
41
        this.orderHints = Arrays.asList(orderHint);
42
    }
43

    
44
    /**
45
     * Checks if the OrderHintPreset is suitable for the given <code>type</code>.
46
     * In case this check fails the <code>ORDER_BY_TITLE_CACHE_ASC</code> or
47
     * <code>ORDER_BY_TITLE_CACHE_DESC</code> is returned as fallback, depending on
48
     * the sort order of the original sort order.
49
     *
50
     * @param type
51
     *
52
     * @return
53
     */
54
    public OrderHintPreset checkSuitableFor(Class<? extends CdmBase> type) {
55

    
56
        switch(this) {
57
        case BY_ORDER_INDEX_ASC:
58
            if(!OrderedTermVocabulary.class.isAssignableFrom(type)) {
59
            logger.warn("BY_ORDER_INDEX_ASC not possible with " + type.getSimpleName() +" , falling back to ORDER_BY_TITLE_CACHE_ASC");
60
                return OrderHintPreset.ORDER_BY_TITLE_CACHE_ASC;
61
            }
62
            break;
63
        case BY_ORDER_INDEX_DESC:
64
            if(!OrderedTermVocabulary.class.isAssignableFrom(type)) {
65
                logger.warn( "BY_ORDER_INDEX_DESC not possible with " + type.getSimpleName() +" , falling back to ORDER_BY_TITLE_CACHE_DESC");
66
                return OrderHintPreset.ORDER_BY_TITLE_CACHE_DESC;
67
            }
68
            break;
69
        default:
70
            // IGNORE
71
            break;
72
        }
73

    
74
        return this;
75
    }
76
}
(43-43/63)