annotate artifact-database/src/main/java/de/intevation/artifactdatabase/DefaultCallMeta.java @ 363:4d3298295a64

Enhanced the State interface with a new method initialize() which might be used to initialize the State based on a model Artifact. artifacts/trunk@3623 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Mon, 09 Jan 2012 08:15:35 +0000
parents 933bbc9fc11f
children
rev   line source
100
933bbc9fc11f Added license file and license headers.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 93
diff changeset
1 /*
933bbc9fc11f Added license file and license headers.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 93
diff changeset
2 * Copyright (c) 2010 by Intevation GmbH
933bbc9fc11f Added license file and license headers.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 93
diff changeset
3 *
933bbc9fc11f Added license file and license headers.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 93
diff changeset
4 * This program is free software under the LGPL (>=v2.1)
933bbc9fc11f Added license file and license headers.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 93
diff changeset
5 * Read the file LGPL.txt coming with the software for details
933bbc9fc11f Added license file and license headers.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 93
diff changeset
6 * or visit http://www.gnu.org/licenses/ if it does not exist.
933bbc9fc11f Added license file and license headers.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 93
diff changeset
7 */
933bbc9fc11f Added license file and license headers.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 93
diff changeset
8
48
41c225c8bd41 Add i18n support via "Accept-Language" HTTP headers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
9 package de.intevation.artifactdatabase;
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 import de.intevation.artifacts.CallMeta;
41c225c8bd41 Add i18n support via "Accept-Language" HTTP headers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
12 import de.intevation.artifacts.PreferredLocale;
41c225c8bd41 Add i18n support via "Accept-Language" HTTP headers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
13
93
e27cf9c84eb8 Unified imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 91
diff changeset
14 import java.util.Locale;
e27cf9c84eb8 Unified imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 91
diff changeset
15
48
41c225c8bd41 Add i18n support via "Accept-Language" HTTP headers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
16 /**
87
0f48188a6e02 Added some javadoc to the artifactdatabase module. Not done yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 77
diff changeset
17 * Default implementation of CallMeta. It provides a list of
0f48188a6e02 Added some javadoc to the artifactdatabase module. Not done yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 77
diff changeset
18 * preferred langauages and implements an intersection mechanism
0f48188a6e02 Added some javadoc to the artifactdatabase module. Not done yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 77
diff changeset
19 * to figure out the best matching language given a list of server
0f48188a6e02 Added some javadoc to the artifactdatabase module. Not done yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 77
diff changeset
20 * provided languages.
77
48d1a9a082c2 Bring @author javadoc tags in form '@author <a href="john.doe@example.com">John Doe</a>'
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 51
diff changeset
21 * @author <a href="mailto:sascha.teichmann@intevation.de">Sascha L. Teichmann</a>
48
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 class DefaultCallMeta
41c225c8bd41 Add i18n support via "Accept-Language" HTTP headers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
24 implements CallMeta
41c225c8bd41 Add i18n support via "Accept-Language" HTTP headers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
25 {
87
0f48188a6e02 Added some javadoc to the artifactdatabase module. Not done yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 77
diff changeset
26 /**
0f48188a6e02 Added some javadoc to the artifactdatabase module. Not done yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 77
diff changeset
27 * The list of preferred languages.
0f48188a6e02 Added some javadoc to the artifactdatabase module. Not done yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 77
diff changeset
28 */
48
41c225c8bd41 Add i18n support via "Accept-Language" HTTP headers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
29 protected PreferredLocale [] languages;
41c225c8bd41 Add i18n support via "Accept-Language" HTTP headers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
30
87
0f48188a6e02 Added some javadoc to the artifactdatabase module. Not done yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 77
diff changeset
31 /**
0f48188a6e02 Added some javadoc to the artifactdatabase module. Not done yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 77
diff changeset
32 * Default constructor.
0f48188a6e02 Added some javadoc to the artifactdatabase module. Not done yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 77
diff changeset
33 */
48
41c225c8bd41 Add i18n support via "Accept-Language" HTTP headers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
34 public DefaultCallMeta() {
41c225c8bd41 Add i18n support via "Accept-Language" HTTP headers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
35 }
41c225c8bd41 Add i18n support via "Accept-Language" HTTP headers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
36
87
0f48188a6e02 Added some javadoc to the artifactdatabase module. Not done yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 77
diff changeset
37 /**
0f48188a6e02 Added some javadoc to the artifactdatabase module. Not done yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 77
diff changeset
38 * Creates new DefaultCallMeta with a given list of languages.
0f48188a6e02 Added some javadoc to the artifactdatabase module. Not done yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 77
diff changeset
39 * @param languages The list of preferred languages.
0f48188a6e02 Added some javadoc to the artifactdatabase module. Not done yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 77
diff changeset
40 */
48
41c225c8bd41 Add i18n support via "Accept-Language" HTTP headers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
41 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
42 this.languages = languages;
48
41c225c8bd41 Add i18n support via "Accept-Language" HTTP headers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
43 }
41c225c8bd41 Add i18n support via "Accept-Language" HTTP headers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
44
41c225c8bd41 Add i18n support via "Accept-Language" HTTP headers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
45 public PreferredLocale [] getLanguages() {
41c225c8bd41 Add i18n support via "Accept-Language" HTTP headers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
46 return languages;
41c225c8bd41 Add i18n support via "Accept-Language" HTTP headers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
47 }
49
823850c63d95 Convenience method in call meta to get preferred language.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 48
diff changeset
48
823850c63d95 Convenience method in call meta to get preferred language.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 48
diff changeset
49 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
50 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
51 return null;
823850c63d95 Convenience method in call meta to get preferred language.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 48
diff changeset
52 }
823850c63d95 Convenience method in call meta to get preferred language.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 48
diff changeset
53
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
54 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
55 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
56
0b04fa8a19c6 Better implementation for fetching the preferred locale via meta of call context.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 49
diff changeset
57 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
58 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
59 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
60
0b04fa8a19c6 Better implementation for fetching the preferred locale via meta of call context.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 49
diff changeset
61 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
62 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
63 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
64 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
65 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
66 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
67 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
68 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
69 }
0b04fa8a19c6 Better implementation for fetching the preferred locale via meta of call context.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 49
diff changeset
70 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
71 // 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
72 }
0b04fa8a19c6 Better implementation for fetching the preferred locale via meta of call context.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 49
diff changeset
73 }
0b04fa8a19c6 Better implementation for fetching the preferred locale via meta of call context.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 49
diff changeset
74 }
0b04fa8a19c6 Better implementation for fetching the preferred locale via meta of call context.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 49
diff changeset
75
0b04fa8a19c6 Better implementation for fetching the preferred locale via meta of call context.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 49
diff changeset
76 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
77 ? 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
78 : best;
49
823850c63d95 Convenience method in call meta to get preferred language.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 48
diff changeset
79 }
48
41c225c8bd41 Add i18n support via "Accept-Language" HTTP headers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
80 }
91
730ff077a58c More javadoc, some code formatting.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 87
diff changeset
81 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org