001: package com.calipso.reportgenerator.reportmanager;
002:
003: import com.calipso.reportgenerator.reportdefinitions.*;
004:
005: import java.util.Locale;
006: import java.util.Map;
007: import java.util.Hashtable;
008: import java.io.Serializable;
009:
010: /**
011: * Traduce a los distintos lenguajes los string de las definiciones
012: */
013: public class DefinitionTraslator implements Serializable {
014: private ReportDefinition reportDefinition;
015: private ReportSourceDefinition reportSourceDefinition;
016: private ReportView reportView;
017: private Locale locale;
018: private Map localizationMap;
019:
020: public DefinitionTraslator(ReportDefinition reportDefinition,
021: Locale locale) {
022: this .reportDefinition = reportDefinition;
023: this .locale = locale;
024: }
025:
026: public DefinitionTraslator(
027: ReportSourceDefinition reportSourceDefinition, Locale locale) {
028: this .reportSourceDefinition = reportSourceDefinition;
029: this .locale = locale;
030: }
031:
032: public DefinitionTraslator(ReportDefinition reportDefinition,
033: String language, String country) {
034: this .reportDefinition = reportDefinition;
035: this .locale = new Locale(language, country);
036: }
037:
038: public DefinitionTraslator(
039: ReportSourceDefinition reportSourceDefinition,
040: String language, String country) {
041: this .reportSourceDefinition = reportSourceDefinition;
042: this .locale = new Locale(language, country);
043: }
044:
045: public String traslate(String value) {
046: if (isMultilanguage() && (localizationMap != null)
047: && (value != null)
048: && localizationMap.containsKey(value)) {
049: return localizationMap.get(value).toString();
050: } else {
051: return value;
052: }
053: }
054:
055: private boolean isMultilanguage() {
056: if (reportView != null) {
057: return reportDefinition.getMultilanguage();
058: } else if (reportDefinition != null) {
059: return reportDefinition.getMultilanguage();
060: } else if (reportSourceDefinition != null) {
061: return reportSourceDefinition.getMultilanguage();
062: }
063: return false;
064: }
065:
066: private Map getLocalizacionMap() {
067: if (localizationMap == null) {
068: Localization localization = getLocalizationFromLocale(reportDefinition
069: .getLocalizations());
070: if (localization != null) {
071: localizationMap = createMapLocazation(localization);
072: }
073: }
074: return localizationMap;
075: }
076:
077: private Map getLocalizacionMap(
078: ReportSourceDefinition reportSourceDefinition) {
079: Localization localization = getLocalizationFromLocale(reportSourceDefinition
080: .getLocalizations());
081: if (localization != null) {
082: return createMapLocazation(localization);
083: }
084: return null;
085: }
086:
087: /**
088: * Crea un Map con la lista de los literales para las conversiones
089: * @param localization
090: * @return
091: */
092: private Map createMapLocazation(Localization localization) {
093: Map localeMap = new Hashtable();
094: for (int i = 0; i < localization.getLiteralsCount(); i++) {
095: localeMap.put(localization.getLiterals()[i].getCode(),
096: localization.getLiterals()[i].getValue());
097: }
098: return localeMap;
099: }
100:
101: /**Obtiene el localization para el locale corespondiente
102: *
103: * @param localizations
104: * @return
105: */
106: private Localization getLocalizationFromLocale(
107: Localizations localizations) {
108: Localization localization;
109: if (localizations != null) {
110: for (int i = 0; i < localizations.getLocalizationCount(); i++) {
111: localization = localizations.getLocalization()[i];
112: if (localization.getLocale().equalsIgnoreCase(
113: locale.toString())) {
114: return localization;
115: }
116: ;
117: }
118: }
119: return null;
120: }
121:
122: public String getDescription() {
123: if (reportView != null) {
124: return traslate(reportView.getId());
125: } else if (reportDefinition != null) {
126: return traslate(reportDefinition.getDescription());
127: } else if (reportSourceDefinition != null) {
128: return traslate(reportSourceDefinition.getId());
129: }
130: return "";
131: }
132:
133: public DefinitionTraslator(ReportView reportView,
134: ReportDefinition reportDefinition, String language,
135: String country) {
136: this .reportView = reportView;
137: this .reportDefinition = reportDefinition;
138: this .locale = new Locale(language, country);
139: }
140:
141: public DefinitionTraslator(ReportView reportView,
142: ReportDefinition reportDefinition, Locale locale) {
143: this.reportView = reportView;
144: this.reportDefinition = reportDefinition;
145: this.locale = locale;
146: }
147:
148: }
|