annotate artifact-database/src/main/java/de/intevation/artifactdatabase/DefaultCallMeta.java @ 57:cf9848c85755 0.2

RELEASE 0.2: Summarized NEWS and Changes artifacts/trunk@338 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Hans Plum <hans.plum@intevation.de>
date Fri, 13 Nov 2009 18:37:51 +0000
parents 3a668da76463
children 48d1a9a082c2
rev   line source
48
41c225c8bd41 Add i18n support via "Accept-Language" HTTP headers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
1 package de.intevation.artifactdatabase;
41c225c8bd41 Add i18n support via "Accept-Language" HTTP headers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
2
49
823850c63d95 Convenience method in call meta to get preferred language.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 48
diff changeset
3 import java.util.Locale;
823850c63d95 Convenience method in call meta to get preferred language.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 48
diff changeset
4
48
41c225c8bd41 Add i18n support via "Accept-Language" HTTP headers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
5 import de.intevation.artifacts.CallMeta;
41c225c8bd41 Add i18n support via "Accept-Language" HTTP headers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
6 import de.intevation.artifacts.PreferredLocale;
41c225c8bd41 Add i18n support via "Accept-Language" HTTP headers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
7
41c225c8bd41 Add i18n support via "Accept-Language" HTTP headers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
8 /**
41c225c8bd41 Add i18n support via "Accept-Language" HTTP headers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
9 * @author Sascha L. Teichmann (sascha.teichmann@intevation.de)
41c225c8bd41 Add i18n support via "Accept-Language" HTTP headers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
10 */
41c225c8bd41 Add i18n support via "Accept-Language" HTTP headers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
11 public class DefaultCallMeta
41c225c8bd41 Add i18n support via "Accept-Language" HTTP headers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
12 implements CallMeta
41c225c8bd41 Add i18n support via "Accept-Language" HTTP headers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
13 {
41c225c8bd41 Add i18n support via "Accept-Language" HTTP headers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
14 protected PreferredLocale [] languages;
41c225c8bd41 Add i18n support via "Accept-Language" HTTP headers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
15
41c225c8bd41 Add i18n support via "Accept-Language" HTTP headers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
16 public DefaultCallMeta() {
41c225c8bd41 Add i18n support via "Accept-Language" HTTP headers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
17 }
41c225c8bd41 Add i18n support via "Accept-Language" HTTP headers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
18
41c225c8bd41 Add i18n support via "Accept-Language" HTTP headers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
19 public DefaultCallMeta(PreferredLocale [] languages) {
51
3a668da76463 Fixed problem with non-initialized languages in call meta.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 50
diff changeset
20 this.languages = languages;
48
41c225c8bd41 Add i18n support via "Accept-Language" HTTP headers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
21 }
41c225c8bd41 Add i18n support via "Accept-Language" HTTP headers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
22
41c225c8bd41 Add i18n support via "Accept-Language" HTTP headers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
23 public PreferredLocale [] getLanguages() {
41c225c8bd41 Add i18n support via "Accept-Language" HTTP headers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
24 return languages;
41c225c8bd41 Add i18n support via "Accept-Language" HTTP headers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
25 }
49
823850c63d95 Convenience method in call meta to get preferred language.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 48
diff changeset
26
823850c63d95 Convenience method in call meta to get preferred language.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 48
diff changeset
27 public Locale getPreferredLocale(Locale [] locales) {
823850c63d95 Convenience method in call meta to get preferred language.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 48
diff changeset
28 if (locales == null || locales.length == 0) {
823850c63d95 Convenience method in call meta to get preferred language.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 48
diff changeset
29 return null;
823850c63d95 Convenience method in call meta to get preferred language.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 48
diff changeset
30 }
823850c63d95 Convenience method in call meta to get preferred language.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 48
diff changeset
31
50
0b04fa8a19c6 Better implementation for fetching the preferred locale via meta of call context.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 49
diff changeset
32 Locale best = null;
0b04fa8a19c6 Better implementation for fetching the preferred locale via meta of call context.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 49
diff changeset
33 float quality = -Float.MAX_VALUE;
0b04fa8a19c6 Better implementation for fetching the preferred locale via meta of call context.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 49
diff changeset
34
0b04fa8a19c6 Better implementation for fetching the preferred locale via meta of call context.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 49
diff changeset
35 for (int i = 0; i < locales.length; ++i) {
0b04fa8a19c6 Better implementation for fetching the preferred locale via meta of call context.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 49
diff changeset
36 Locale wish = locales[i];
0b04fa8a19c6 Better implementation for fetching the preferred locale via meta of call context.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 49
diff changeset
37 String wishLanguage = wish.getLanguage();
0b04fa8a19c6 Better implementation for fetching the preferred locale via meta of call context.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 49
diff changeset
38
0b04fa8a19c6 Better implementation for fetching the preferred locale via meta of call context.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 49
diff changeset
39 for (int j = 0; j < languages.length; ++j) {
0b04fa8a19c6 Better implementation for fetching the preferred locale via meta of call context.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 49
diff changeset
40 PreferredLocale have = languages[j];
0b04fa8a19c6 Better implementation for fetching the preferred locale via meta of call context.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 49
diff changeset
41 Locale haveLocale = have.getLocale();
0b04fa8a19c6 Better implementation for fetching the preferred locale via meta of call context.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 49
diff changeset
42 if (haveLocale.getLanguage().equals(wishLanguage)) {
0b04fa8a19c6 Better implementation for fetching the preferred locale via meta of call context.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 49
diff changeset
43 float haveQuality = have.getQuality();
0b04fa8a19c6 Better implementation for fetching the preferred locale via meta of call context.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 49
diff changeset
44 if (haveQuality > quality) {
0b04fa8a19c6 Better implementation for fetching the preferred locale via meta of call context.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 49
diff changeset
45 quality = haveQuality;
0b04fa8a19c6 Better implementation for fetching the preferred locale via meta of call context.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 49
diff changeset
46 best = wish;
0b04fa8a19c6 Better implementation for fetching the preferred locale via meta of call context.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 49
diff changeset
47 }
0b04fa8a19c6 Better implementation for fetching the preferred locale via meta of call context.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 49
diff changeset
48 break; // Languages should not contain
0b04fa8a19c6 Better implementation for fetching the preferred locale via meta of call context.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 49
diff changeset
49 // same locale twice.
0b04fa8a19c6 Better implementation for fetching the preferred locale via meta of call context.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 49
diff changeset
50 }
0b04fa8a19c6 Better implementation for fetching the preferred locale via meta of call context.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 49
diff changeset
51 }
0b04fa8a19c6 Better implementation for fetching the preferred locale via meta of call context.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 49
diff changeset
52 }
0b04fa8a19c6 Better implementation for fetching the preferred locale via meta of call context.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 49
diff changeset
53
0b04fa8a19c6 Better implementation for fetching the preferred locale via meta of call context.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 49
diff changeset
54 return best == null
0b04fa8a19c6 Better implementation for fetching the preferred locale via meta of call context.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 49
diff changeset
55 ? locales[0]
0b04fa8a19c6 Better implementation for fetching the preferred locale via meta of call context.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 49
diff changeset
56 : best;
49
823850c63d95 Convenience method in call meta to get preferred language.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 48
diff changeset
57 }
48
41c225c8bd41 Add i18n support via "Accept-Language" HTTP headers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
58 }
41c225c8bd41 Add i18n support via "Accept-Language" HTTP headers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
59 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8:

http://dive4elements.wald.intevation.org