1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package com.fatwire.gst.foundation.taglib;
17
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import java.util.Collections;
21 import java.util.Enumeration;
22 import java.util.List;
23 import java.util.Locale;
24
25 import COM.FutureTense.Interfaces.ICS;
26
27 import com.fatwire.assetapi.data.AssetId;
28 import com.fatwire.gst.foundation.facade.mda.DimensionUtils;
29 import com.fatwire.gst.foundation.facade.mda.LocaleUtils;
30 import com.fatwire.mda.Dimension;
31 import com.fatwire.mda.DimensionException;
32 import com.fatwire.mda.DimensionFilterInstance;
33 import com.fatwire.mda.DimensionManager;
34 import com.fatwire.mda.DimensionSetInstance;
35
36 import org.apache.commons.logging.Log;
37 import org.apache.commons.logging.LogFactory;
38
39
40
41
42
43
44
45 public abstract class MultilingualGsfSimpleTag extends GsfSimpleTag {
46 protected static final Log LOG = LogFactory.getLog("com.fatwire.gst.foundation.taglib");
47
48 private String dimensionSetName = null;
49 private long dimensionSetId = -1L;
50 private String localeName = null;
51 private long localeId = -1L;
52
53 public final void setDimset(String s) {
54 try {
55 dimensionSetId = Long.parseLong(s);
56 } catch (NumberFormatException e) {
57 dimensionSetName = s;
58 dimensionSetId = -1L;
59 }
60 }
61
62 public final void setLocale(String s) {
63 try {
64 localeId = Long.parseLong(s);
65 } catch (NumberFormatException e) {
66 localeName = s;
67 localeId = -1L;
68 }
69 }
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92 protected final DimensionFilterInstance getDimensionFilter() {
93 ICS ics = getICS();
94 DimensionFilterInstance filter;
95 try {
96 Collection<AssetId> preferredLocales = getPreferredLocales();
97 DimensionSetInstance dimSet = getDimensionSet();
98 filter = DimensionUtils.getDimensionFilter(DimensionUtils.getDM(ics), preferredLocales, dimSet);
99 if (LOG.isDebugEnabled()) {
100 LOG.debug("Multilingual-enabled tag located dimension filter: " + filter + " in dimensionSet " + dimSet + " with preferred locales: " + preferredLocales + " ");
101 }
102 } catch (DimensionException e) {
103 LOG.error("Multilingual-enabled tag could not locate dimension filter", e);
104 filter = null;
105 } catch (RuntimeException e) {
106 LOG.error("Multilingual-enabled tag could not locate dimension filter", e);
107 filter = null;
108 }
109 return filter;
110 }
111
112
113
114
115
116 protected final AssetId getExplicitlySpecifiedLocale() {
117 ICS ics = getICS();
118
119 if (localeId != -1L) {
120 DimensionManager dm = DimensionUtils.getDM(ics);
121 Dimension d = dm.loadDimension(localeId);
122 if (d != null) {
123 LOG.trace("Preferred locale explicitly set to " + localeId);
124 return d.getId();
125 }
126 }
127
128
129 if (localeName != null) {
130 Dimension d = DimensionUtils.getDimensionForName(ics, localeName);
131 if (d != null) {
132 LOG.trace("Preferred locale explicitly set to " + localeName);
133 return d.getId();
134 }
135 }
136 return null;
137 }
138
139
140
141
142 protected final Collection<AssetId> getPreferredLocales() {
143 AssetId result = getExplicitlySpecifiedLocale();
144 if (result != null) return Collections.singleton(result);
145
146 ICS ics = getICS();
147
148
149 String localeVar = getICS().GetVar("locale");
150 try {
151 long localeIdFromVar = Long.parseLong(localeVar);
152 DimensionManager dm = DimensionUtils.getDM(ics);
153 Dimension d = dm.loadDimension(localeIdFromVar);
154 if (d != null) {
155 LOG.trace("Preferred locale detected in ICS context using 'locale' variable: " + localeIdFromVar);
156 return Collections.singletonList(d.getId());
157 }
158 } catch (NumberFormatException e) {
159
160 try {
161 Dimension d = DimensionUtils.getDimensionForName(ics, localeVar);
162 if (d != null) {
163 LOG.trace("Preferred locale detected in ICS context using 'locale' variable: " + localeVar);
164 return Collections.singletonList(d.getId());
165 }
166 } catch (Exception ex) {
167
168 }
169 }
170
171
172 String localeSSVar = getICS().GetSSVar("locale");
173 try {
174 long localeIdFromSSVar = Long.parseLong(localeSSVar);
175 DimensionManager dm = DimensionUtils.getDM(ics);
176 Dimension d = dm.loadDimension(localeIdFromSSVar);
177 if (d != null) {
178 LOG.trace("Preferred locale detected in ICS context using 'locale' session variable: " + localeIdFromSSVar);
179 return Collections.singletonList(d.getId());
180 }
181 } catch (NumberFormatException e) {
182
183 try {
184 Dimension d = DimensionUtils.getDimensionForName(ics, localeSSVar);
185 if (d != null) {
186 LOG.trace("Preferred locale detected in ICS context using 'locale' session variable: " + localeSSVar);
187 return Collections.singletonList(d.getId());
188 }
189 } catch (Exception ex) {
190
191 }
192 }
193
194
195 List<AssetId> preferredLocales = new ArrayList<AssetId>();
196 @SuppressWarnings({ "rawtypes", "deprecation" })
197 Enumeration locales = ics.getIServlet().getServletRequest().getLocales();
198 while (locales.hasMoreElements()) {
199 Locale locale = (Locale) locales.nextElement();
200 if (locale != null) {
201 String localeName = locale.toString();
202 if (localeName != null && localeName.length() > 0) {
203 try {
204 Dimension dimension = DimensionUtils.getDimensionForName(ics, localeName);
205 preferredLocales.add(dimension.getId());
206 LOG.trace("Found registered locale in user's Accept-Language header (or default): " + localeName);
207 } catch (RuntimeException e) {
208
209
210 LOG.trace("Found a locale in the user's Accept-Language header, but it was not registered as a dimension: " + localeName + " (this is not usually an error)", e);
211 }
212 }
213 }
214 }
215 return preferredLocales;
216 }
217
218 private final DimensionSetInstance getDimensionSet() {
219 if (dimensionSetName != null) {
220 return LocaleUtils.getDimensionSet(getICS(), dimensionSetName);
221 }
222 if (dimensionSetId != -1L) {
223 return LocaleUtils.getDimensionSet(getICS(), dimensionSetId);
224 }
225 try {
226 ICS ics = getICS();
227 String site = ics.GetVar("site");
228 if (site != null && site.length() > 0) {
229 long discoveredId = LocaleUtils.locateDimensionSetForSite(ics, site);
230 LOG.trace("Auto-discovered dimension set because there is only one in site " + site + ": DimensionSet:" + discoveredId);
231 return LocaleUtils.getDimensionSet(ics, discoveredId);
232 }
233 } catch (RuntimeException e) {
234 LOG.trace("Could not auto-discovered dimensionset: " + e);
235 }
236 throw new IllegalArgumentException("DimensionSet not found");
237 }
238 }