Project

General

Profile

Download (5.82 KB) Statistics
| Branch: | Tag: | Revision:
1
package org.bgbm.biovel.drf.checklist;
2

    
3
import java.util.EnumSet;
4
import java.util.Iterator;
5
import java.util.List;
6

    
7
import org.bgbm.biovel.drf.client.AbstractClient;
8
import org.bgbm.biovel.drf.client.ServiceProviderInfo;
9
import org.bgbm.biovel.drf.query.IQueryClient;
10
import org.bgbm.biovel.drf.tnr.msg.Query;
11
import org.bgbm.biovel.drf.tnr.msg.TnrMsg;
12
import org.bgbm.biovel.drf.utils.TnrMsgUtils;
13
import org.slf4j.Logger;
14
import org.slf4j.LoggerFactory;
15

    
16
public abstract class BaseChecklistClient<QC extends IQueryClient> extends AbstractClient<QC> {
17

    
18
    protected Logger logger = LoggerFactory.getLogger(BaseChecklistClient.class);
19

    
20
    protected final static String CHECKLIST_KEY = "checklist";
21
    protected final static String CHECKLIST_URL_KEY = "checklist_url";
22
    protected final static String COPYRIGHT_URL_KEY = "copyright_url";
23
    protected final static String CHECKLIST_LIST = "checklist_list";
24

    
25
    public BaseChecklistClient() {
26
        super();
27
    }
28

    
29
    public BaseChecklistClient(String checklistInfoJson) throws DRFChecklistException {
30
        super(checklistInfoJson);
31
    }
32

    
33
    public BaseChecklistClient(ServiceProviderInfo spInfo) throws DRFChecklistException {
34
        super(spInfo);
35
    }
36

    
37

    
38
    /**
39
     * @param tnrMsg
40
     * @return
41
     * @throws DRFChecklistException
42
     */
43
    protected Query singleQueryFrom(TnrMsg tnrMsg) throws DRFChecklistException {
44
        List<Query> queryList = tnrMsg.getQuery();
45
        if(queryList.size() ==  0) {
46
            throw new DRFChecklistException("query list is empty");
47
        }
48

    
49
        if(queryList.size() > 1) {
50
            throw new DRFChecklistException("query list has more than one query");
51
        }
52
        Query query = queryList.get(0);
53
        return query;
54
    }
55

    
56
    public void queryChecklist(List<TnrMsg> tnrMsgs) throws DRFChecklistException {
57

    
58
        TnrMsg finalTnrMsg = new TnrMsg();
59
        Iterator<TnrMsg> itrTnrMsg = tnrMsgs.iterator();
60
        while(itrTnrMsg.hasNext()) {
61
            Iterator<Query> itrQuery = itrTnrMsg.next().getQuery().iterator();
62
            while(itrQuery.hasNext()) {
63
                finalTnrMsg.getQuery().add(itrQuery.next());
64
            }
65
        }
66

    
67
        queryChecklist(finalTnrMsg);
68
    }
69

    
70
    /**
71
     *
72
     * @param tnrMsg
73
     * @throws DRFChecklistException
74
     *
75
     * TODO remove parameter SearchMode, since it in now included in the TnrMsg.query.request
76
     */
77
    public void queryChecklist(TnrMsg tnrMsg) throws DRFChecklistException {
78

    
79
//        TnrMsgUtils.updateWithSearchMode(tnrMsg, mode); // ...... remove
80
        TnrMsgUtils.assertSearchModeSet(tnrMsg, true);
81

    
82
        SearchMode mode = TnrMsgUtils.getSearchMode(tnrMsg);
83

    
84
        if(!getSearchModes().contains(mode)){
85
            throw new DRFChecklistException("Unsupported SearchMode");
86
        }
87
        if (getSearchModes().contains(mode)){
88
            switch(mode){
89
            case scientificNameExact:
90
                 resolveScientificNamesExact(tnrMsg);
91
                 break;
92
            case scientificNameLike:
93
                resolveScientificNamesLike(tnrMsg);
94
                break;
95
            case vernacularNameExact:
96
                resolveVernacularNamesExact(tnrMsg);
97
                break;
98
            case vernacularNameLike:
99
                resolveVernacularNamesLike(tnrMsg);
100
                break;
101
            case findByIdentifier:
102
                if(checkSupportedIdentifieres(tnrMsg)){
103
                    findByIdentifier(tnrMsg);
104
                } else {
105
                    logger.info("The queries contain unsupported identifier strings");
106
                    throw new UnsupportedIdentifierException("Queries contain unsupported identifier strings");
107
                }
108
                break;
109
            default:
110
                throw new DRFChecklistException("Unimplemented SearchMode");
111
            }
112
        } else {
113
            logger.info("Search mode " + mode + " not supported by this ChecklistClient implementation");
114
        }
115
    }
116

    
117
    /**
118
     * Checks all the queries of the <code>tnrMsg</code> of they
119
     * contain a query string which is supported as identifier by the
120
     * implementing checklist client.
121
     * <p>
122
     * Fails if only one of the query string is not supported!
123
     *
124
     * @param tnrMsg
125
     * @return
126
     */
127
    private boolean checkSupportedIdentifieres(TnrMsg tnrMsg) {
128
        for (Query q : tnrMsg.getQuery()){
129
            if(! isSupportedIdentifier(q.getRequest().getQueryString())){
130
                return false;
131
            }
132
        }
133
        return true;
134
    }
135

    
136
    /**
137
     * Searches for scientific names which exactly match the given query string.
138
     *
139
     * @param tnrMsg
140
     * @throws DRFChecklistException
141
     */
142
    public abstract void resolveScientificNamesExact(TnrMsg tnrMsg) throws DRFChecklistException;
143

    
144
    /**
145
     * Searches for scientific names which start with the given query string.
146
     *
147
     * @param tnrMsg
148
     * @throws DRFChecklistException
149
     */
150
    public abstract void resolveScientificNamesLike(TnrMsg tnrMsg) throws DRFChecklistException;
151

    
152
    /**
153
     * Searches for taxa with an vernacular name that exactly match the given query string.
154
     *
155
     * @param tnrMsg
156
     * @throws DRFChecklistException
157
     */
158
    public abstract void resolveVernacularNamesExact(TnrMsg tnrMsg) throws DRFChecklistException;
159

    
160
    /**
161
     * Searches for taxa with an vernacular name that contains the given query string.
162
     *
163
     * @param tnrMsg
164
     * @throws DRFChecklistException
165
     */
166
    public abstract void resolveVernacularNamesLike(TnrMsg tnrMsg) throws DRFChecklistException;
167

    
168
    /**
169
     * Searches taxa having a specific identifier
170
     *
171
     * @param tnrMsg
172
     * @throws DRFChecklistException
173
     */
174
    public abstract void  findByIdentifier(TnrMsg tnrMsg) throws DRFChecklistException;
175

    
176
    public abstract EnumSet<SearchMode> getSearchModes();
177

    
178
    public abstract boolean isSupportedIdentifier(String value);
179

    
180
}
(2-2/12)