1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.fatwire.gst.foundation.facade.mda;
18
19 import java.util.Arrays;
20 import java.util.Collection;
21 import java.util.Collections;
22 import java.util.List;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26
27 import COM.FutureTense.Interfaces.FTValList;
28 import COM.FutureTense.Interfaces.ICS;
29 import COM.FutureTense.Interfaces.IList;
30 import COM.FutureTense.Util.IterableIListWrapper;
31 import COM.FutureTense.Util.ftErrors;
32
33 import com.fatwire.assetapi.data.AssetId;
34 import com.fatwire.cs.core.db.PreparedStmt;
35 import com.fatwire.cs.core.db.StatementParam;
36 import com.fatwire.gst.foundation.CSRuntimeException;
37 import com.fatwire.gst.foundation.IListUtils;
38 import com.fatwire.gst.foundation.facade.ics.ICSFactory;
39 import com.fatwire.gst.foundation.facade.runtag.asset.AssetLoadByName;
40 import com.fatwire.gst.foundation.facade.runtag.render.LogDep;
41 import com.fatwire.mda.Dimension;
42 import com.fatwire.mda.DimensionException;
43 import com.fatwire.mda.DimensionFilterInstance;
44 import com.fatwire.mda.DimensionManager;
45 import com.fatwire.mda.DimensionSetInstance;
46 import com.fatwire.mda.DimensionableAssetManager;
47 import com.fatwire.system.Session;
48 import com.openmarket.xcelerate.asset.AssetIdImpl;
49
50
51
52
53
54
55
56
57 public final class LocaleUtils {
58 private static final Log _log = LogFactory.getLog(LocaleUtils.class);
59
60 private LocaleUtils() {
61 }
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87 @Deprecated
88 public static AssetId findTranslation(String c, String cid, String preferredLocaleDimensionId, String site) {
89 return findTranslation(ICSFactory.getOrCreateICS(), new AssetIdImpl(c, Long.valueOf(cid)),
90 preferredLocaleDimensionId, site);
91 }
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112 public static AssetId findTranslation(ICS ics, String c, String cid, String preferredLocaleDimensionId, String site) {
113 return findTranslation(ics, new AssetIdImpl(c, Long.valueOf(cid)), preferredLocaleDimensionId, site);
114 }
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140 @Deprecated
141 public static AssetId findTranslation(AssetId id, String preferredLocaleDimensionIdString, String site) {
142 ICS ics = ICSFactory.getOrCreateICS();
143 return findTranslation(ics, id, preferredLocaleDimensionIdString, site);
144 }
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165 public static AssetId findTranslation(ICS ics, AssetId id, String preferredLocaleDimensionIdString, String site) {
166 if (preferredLocaleDimensionIdString == null) {
167 throw new IllegalArgumentException("Required preferred locale dimension ID not provided");
168 }
169 long preferredDimension = Long.valueOf(preferredLocaleDimensionIdString);
170
171 long dimensionSetId = locateDimensionSetForSite(ics, site);
172
173 return findTranslation(ics, id, preferredDimension, dimensionSetId);
174 }
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194 public static AssetId findTranslation(ICS ics, AssetId id, long preferredDimension, long dimensionSetId) {
195 if (id == null) {
196 throw new IllegalArgumentException("Required Asset ID missing");
197 }
198
199 DimensionableAssetManager mgr = DimensionUtils.getDAM(ics);
200
201 if (_isInputAssetDimensionPreferred(mgr, id, preferredDimension)) {
202 _log.debug("Input dimension is already in the preferred dimension. Not invoking dimension set filter. Asset: "
203 + id + ", dimension: " + preferredDimension);
204 return id;
205 } else {
206 _log.debug("About to look for translations. Input asset id: " + id + ", dimension set: " + dimensionSetId
207 + ", preferred dimension: " + preferredDimension);
208 }
209
210
211
212
213 DimensionSetInstance dimset = getDimensionSet(ics, dimensionSetId);
214 return findTranslation(ics, id, preferredDimension, dimset);
215
216 }
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239 public static AssetId findTranslation(ICS ics, AssetId id, long preferredDimension, String dimensionSetName) {
240 if (id == null) {
241 throw new IllegalArgumentException("Required Asset ID missing");
242 }
243 Dimension locale = DimensionUtils.getLocaleAsDimension(ics, id);
244
245 if (locale == null) {
246 _log.debug("Asset is not localized. Not invoking dimension set filter. Asset: " + id);
247 return id;
248 }
249 if (locale.getId().getId() == preferredDimension) {
250 _log.debug("Input dimension is already in the preferred dimension. Not invoking dimension set filter. Asset: "
251 + id + ", dimension: " + preferredDimension);
252 return id;
253 } else {
254 _log.debug("About to look for translations. Input asset id: " + id + ", dimension set: "
255 + dimensionSetName + ", preferred dimension: " + preferredDimension);
256 }
257
258
259
260
261 DimensionSetInstance dimset = getDimensionSet(ics, dimensionSetName);
262
263 return findTranslation(ics, id, preferredDimension, dimset);
264 }
265
266
267
268
269
270
271
272
273
274 public static AssetId findTranslation(ICS ics, AssetId id, long preferredDimension, DimensionSetInstance dimset)
275 throws IllegalStateException {
276 AssetId preferredDim = new AssetIdImpl("Dimension", preferredDimension);
277 List<AssetId> preferredDims = Collections.singletonList(preferredDim);
278 Collection<AssetId> relatives = findTranslation(DimensionUtils.getDM(ics), Collections.singletonList(id), preferredDims, dimset );
279
280
281
282 if (relatives == null) {
283 _log.warn("No translation found for asset " + id + " in dimension set " + dimset.getId()
284 + " for dimension " + preferredDimension + ".");
285 return null;
286 } else {
287 switch (relatives.size()) {
288 case 0: {
289 _log.warn("No translation found for " + id + " in dimension set " + dimset.getId()
290 + " for dimension " + preferredDimension + ".");
291
292
293
294
295
296
297
298 return null;
299 }
300 case 1: {
301 AssetId relative = relatives.iterator().next();
302 _log.trace("LocaleUtils.findTranslation: RELATIVE FOUND... " + relative.getType() + " '"
303 + relative.getId() + "' // errno = " + ics.GetErrno());
304 return relative;
305
306 }
307 default: {
308 throw new IllegalStateException("found more than one translation for asset " + id
309 + " and that is not supposed to be possible.");
310 }
311 }
312 }
313 }
314
315
316
317
318
319
320
321
322
323
324
325
326
327 public static Collection<AssetId> findTranslation(DimensionManager dimensionManager, List<AssetId> toFilterList, Collection<AssetId> preferredDimensionIds, DimensionSetInstance dimSet) {
328 try {
329 return DimensionUtils.filterAssets(dimensionManager, toFilterList, preferredDimensionIds, dimSet);
330 } catch (DimensionException e) {
331 throw new CSRuntimeException("Failed to translate assets. Input assets:"+toFilterList+", Preferred Dimensions: "+preferredDimensionIds+", DimensionSet:"+dimSet, ftErrors.exceptionerr, e);
332 }
333 }
334
335
336
337
338 private static boolean _isInputAssetDimensionPreferred(DimensionableAssetManager mgr, AssetId id,
339 long preferredDimension) {
340 Dimension dim = DimensionUtils.getLocaleAsDimension(mgr, id);
341 if (dim == null)
342 return true;
343
344 return dim.getId().getId() == preferredDimension;
345 }
346
347 private static final PreparedStmt FIND_DIMSET_FOR_SITE_PREPAREDSTMT = new PreparedStmt(
348 "select ds.id as id from DimensionSet ds, Publication p, AssetPublication ap where p.name = ? and p.id = ap.pubid and ap.assetid = ds.id and ds.status != 'VO' order by ds.updateddate",
349 Arrays.asList("DimensionSet", "AssetPublication", "Publication"));
350
351 static {
352 FIND_DIMSET_FOR_SITE_PREPAREDSTMT.setElement(0, "Publication", "name");
353 }
354
355
356
357
358
359
360
361
362
363
364 public static long locateDimensionSetForSite(ICS ics, String site) {
365 if (site == null) {
366 throw new IllegalArgumentException("Required site name missing");
367 }
368 StatementParam params = FIND_DIMSET_FOR_SITE_PREPAREDSTMT.newParam();
369 params.setString(0, site);
370 IList results = ics.SQL(FIND_DIMSET_FOR_SITE_PREPAREDSTMT, params, true);
371 int numRows = results != null && results.hasData() ? results.numRows() : 0;
372 if (numRows == 0) {
373 throw new IllegalStateException(
374 "A DimensionSet has not been defined for site '"
375 + site
376 + "'. Cannot determine any translation unless some locales (Dimensions) are enabled for that site. Aborting operation.");
377 }
378 if (numRows > 1) {
379 String msg = "More than one dimension set found in site " + site
380 + ". Exactly one is expected. Dimension set ids: ";
381 for (IList row : new IterableIListWrapper(results)) {
382 String id = IListUtils.getStringValue(row, "id");
383 LogDep.logDep(ics, "DimensionSet", id);
384 msg += id + " ";
385 }
386 throw new IllegalStateException(msg + ".");
387 }
388 results.moveTo(1);
389 String id = IListUtils.getStringValue(results, "id");
390 LogDep.logDep(ics, "DimensionSet", id);
391 if (_log.isTraceEnabled()) _log.trace("Looked up dimset for site "+site+" and found "+id);
392 return Long.valueOf(id);
393 }
394
395 private static DimensionFilterInstance _getPopulatedDimensionFilter(Session ses, DimensionSetInstance dimset,
396 long localeDimensionId) {
397
398
399
400
401
402 Dimension thePreferredDimension = ((DimensionManager) ses.getManager(DimensionManager.class.getName()))
403 .loadDimension(localeDimensionId);
404 if (thePreferredDimension == null) {
405 throw new RuntimeException("Attempted to load Dimension with id " + localeDimensionId
406 + " but it came back null");
407 }
408 return _getPopulatedDimensionFilter(dimset, thePreferredDimension);
409 }
410
411 private static DimensionFilterInstance _getPopulatedDimensionFilter(DimensionSetInstance dimset,
412 Dimension preferredDimension) {
413 DimensionFilterInstance filter;
414 try {
415 filter = dimset.getFilter();
416 } catch (DimensionException e) {
417 throw new RuntimeException("Could not get Dimension Filter from DimensionSet", e);
418 }
419
420 filter.setDimensonPreference(Collections.singletonList(preferredDimension));
421 return filter;
422 }
423
424 public static DimensionSetInstance getDimensionSet(ICS ics, long theDimSetId) {
425 final String DIMSET_OBJ_NAME = "LocaleUtils:findTranslation:theDimensionSet:DimensionSet";
426
427
428 ics.SetObj(DIMSET_OBJ_NAME, null);
429 FTValList args = new FTValList();
430 args.put("NAME", DIMSET_OBJ_NAME);
431 args.put("TYPE", "DimensionSet");
432 args.put("OBJECTID", Long.toString(theDimSetId));
433 args.put("EDITABLE", "FALSE");
434 ics.runTag("ASSET.LOAD", args);
435
436 if (ics.GetErrno() < 0) {
437 throw new IllegalStateException("Could not load dimension set. Errno: " + ics.GetErrno());
438 }
439
440 Object o = ics.GetObj(DIMSET_OBJ_NAME);
441 if (o == null) {
442 throw new IllegalStateException("Could not load dimension set but we got no errno... unexpected...");
443 }
444
445 DimensionSetInstance dimset;
446 if (o instanceof DimensionSetInstance) {
447 dimset = (DimensionSetInstance) o;
448 } else {
449 throw new IllegalStateException("Loaded an asset that is not a Dimension Set");
450 }
451 return dimset;
452 }
453
454 public static DimensionSetInstance getDimensionSet(ICS ics, String name) {
455 final String DIMSET_OBJ_NAME = "LocaleUtils:findTranslation:theDimensionSet:DimensionSet";
456 ics.SetObj(DIMSET_OBJ_NAME, null);
457 AssetLoadByName a = new AssetLoadByName();
458 a.setAssetType("DimensionSet");
459 a.setAssetName(name);
460 a.setEditable(false);
461 a.setName(DIMSET_OBJ_NAME);
462 a.execute(ics);
463
464 if (ics.GetErrno() < 0) {
465 throw new IllegalStateException("Could not load dimension set. Errno: " + ics.GetErrno());
466 }
467
468 Object o = ics.GetObj(DIMSET_OBJ_NAME);
469 ics.SetObj(DIMSET_OBJ_NAME, null);
470 if (o == null) {
471 throw new IllegalStateException("Could not load dimension set but we got no errno... unexpected...");
472 }
473
474 DimensionSetInstance dimset;
475 if (o instanceof DimensionSetInstance) {
476 dimset = (DimensionSetInstance) o;
477 } else {
478 throw new IllegalStateException("Loaded an asset that is not a Dimension Set");
479 }
480 return dimset;
481 }
482
483 }