Package de.innovationgate.wgpublisher.lang

Source Code of de.innovationgate.wgpublisher.lang.DynamicLanguageBehaviour

/*******************************************************************************
* Copyright 2009, 2010 Innovation Gate GmbH. All Rights Reserved.
*
* This file is part of the OpenWGA server platform.
*
* OpenWGA is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* In addition, a special exception is granted by the copyright holders
* of OpenWGA called "OpenWGA plugin exception". You should have received
* a copy of this exception along with OpenWGA in file COPYING.
* If not, see <http://www.openwga.com/gpl-plugin-exception>.
*
* OpenWGA is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenWGA in file COPYING.
* If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/

package de.innovationgate.wgpublisher.lang;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import de.innovationgate.webgate.api.WGAPIException;
import de.innovationgate.webgate.api.WGBackendException;
import de.innovationgate.webgate.api.WGContent;
import de.innovationgate.webgate.api.WGContentKey;
import de.innovationgate.webgate.api.WGDatabase;
import de.innovationgate.webgate.api.WGLanguage;
import de.innovationgate.webgate.api.WGStructEntry;
import de.innovationgate.wgpublisher.labels.WGAResourceBundleManager;
import de.innovationgate.wgpublisher.webtml.utils.TMLContext;

public class DynamicLanguageBehaviour implements LanguageBehaviour {

    public WGContent requestSelectContentForName(WGDatabase db, HttpServletRequest req, String name, boolean isBI) throws WGAPIException {
        WGContent content = LanguageBehaviourTools.chooseNamedContentByRequestLocales(db, name, req, isBI);
        if (content != null) {
            return content;
        }
        else {
            return db.getContentByName(name, db.getDefaultLanguage());
        }
    }

    public WGContent requestSelectContentForPage(WGStructEntry page, HttpServletRequest req, boolean isBI) throws WGAPIException {
        WGContent content =  LanguageBehaviourTools.chooseContentByRequestLocales(page, req, isBI);
        if (content != null) {
            return content;
        }
        else {
            return LanguageBehaviourTools.getRelevantContent(page, page.getDatabase().getDefaultLanguage(), isBI);
        }
    }

    public WGLanguage requestSelectDatabaseLanguage(WGDatabase db, HttpServletRequest req) throws WGAPIException {
        WGLanguage lang =  LanguageBehaviourTools.chooseLanguageByRequestLocales(db, req);
        if (lang != null) {
            return lang;
        }
       
        return null;
    }

    public String webtmlFetchLabel(WGAResourceBundleManager manager, TMLContext context, String container, String file, String key) throws WGAPIException {
       
        String label = null;

        // First try: Current context locale
        String currentLangName = null;
        if (LanguageBehaviourTools.isMultiLanguageContext(context)) {
            currentLangName = context.content().getLanguage().getName();
            Locale currentLangLocale = WGLanguage.languageNameToLocale(currentLangName);
            try {
               
                label = LanguageBehaviourTools.fetchLabelForLanguage(manager, container, file, key, currentLangLocale);
                if (label != null) {
                    return label;
                }
            }
            catch (IOException e) {
                context.getlog().error("Exception retrieving label " + container + "/" + file + "/" + key + " for language " + currentLangLocale.toString() + " from DB " + manager.getDb().getDbReference(), e);
            }  
        }
       
        // Second try: Main context locale
        if (LanguageBehaviourTools.isMultiLanguageContext(context.getmaincontext())) {
            String mainLangName = context.getmaincontext().content().getLanguage().getName();
            if (!mainLangName.equals(currentLangName)) {
                Locale mainLangLocale = WGLanguage.languageNameToLocale(mainLangName);
                try {
                   
                    label = LanguageBehaviourTools.fetchLabelForLanguage(manager, container, file, key, mainLangLocale);
                    if (label != null) {
                        return label;
                    }
                }
                catch (IOException e) {
                    context.getlog().error("Exception retrieving label " + container + "/" + file + "/" + key + " for language " + mainLangLocale.toString() + " from DB " + manager.getDb().getDbReference(), e);
                }
            }
        }
       
        // Third try: Request locales
        if (context.iswebenvironment()) {
            Enumeration<Locale> locales = context.getrequest().getLocales();
            while (locales.hasMoreElements()) {
                Locale locale = locales.nextElement();
                try {
                   
                    label = LanguageBehaviourTools.fetchLabelForLanguage(manager, container, file, key, locale);
                    if (label != null) {
                        return label;
                    }
                }
                catch (IOException e) {
                    context.getlog().error("Exception retrieving label " + container + "/" + file + "/" + key + " for language " + locale.toString() + " from DB " + manager.getDb().getDbReference(), e);
                }
            }
        }

        // Fourth try: Database default language
        Locale defLangLocale = WGLanguage.languageNameToLocale(manager.getDb().getDefaultLanguage());
        try {
           
            label = LanguageBehaviourTools.fetchLabelForLanguage(manager, container, file, key, defLangLocale);
            if (label != null) {
                return label;
            }
        }
        catch (IOException e) {
            context.getlog().error("Exception retrieving label " + container + "/" + file + "/" + key + " for language " + defLangLocale.toString() + " from DB " + manager.getDb().getDbReference(), e);
        }
       
        // Fifth try: Label fallback language
        try {
            label = LanguageBehaviourTools.fetchLabelForFallbackLanguage(manager, container, file, key, true);
            if (label != null) {
                return label;
            }
        }
        catch (IOException e) {
            throw new WGBackendException("Exception retrieving label", e);
        }
       
       
        return null;
       
       
       
    }

    public String webtmlGetPreferredLanguage(WGDatabase db, TMLContext context) throws WGAPIException {
     // If neither current nor main context are multilanguage then we might just choose a content from the request locales
        if (!LanguageBehaviourTools.isMultiLanguageContext(context.getmaincontext())) {
            if (context.iswebenvironment()) {
                WGLanguage lang = requestSelectDatabaseLanguage(db, context.getrequest());
                if (lang != null) {
                    return lang.getName();
                }
                else {
                    return db.getDefaultLanguage();
                }
            }
            else {
                return db.getDefaultLanguage();
            }
        }
       
        return (String) context.getmaincontext().meta("LANGUAGE");
    }

    public List<WGLanguage> webtmlQueryLanguages(WGDatabase db, TMLContext context) throws WGAPIException {

        HashSet<WGLanguage> langs = new LinkedHashSet<WGLanguage>();
       
        if (LanguageBehaviourTools.isMultiLanguageContext(context)) {
            WGLanguage contextLanguage = LanguageBehaviourTools.getDBLocalLanguage(db, context.content().getLanguage());
            if (contextLanguage != null) {
                langs.add(contextLanguage);
            }
        }
       
        if (LanguageBehaviourTools.isMultiLanguageContext(context.getmaincontext())) {
            WGLanguage mainLanguage = LanguageBehaviourTools.getDBLocalLanguage(db, context.getmaincontext().content().getLanguage());
            if (mainLanguage != null) {
                langs.add(mainLanguage);
            }
        }
       
        Enumeration<Locale> locales = context.getrequest().getLocales();
        while (locales.hasMoreElements()) {
            Locale locale = locales.nextElement();
            WGLanguage lang = db.getLanguageForLocale(locale);
            if (lang != null && !lang.isDummy()) {
                langs.add(lang);
            }
        }
       
        langs.add(db.getLanguage(db.getDefaultLanguage()));
       
        return new ArrayList<WGLanguage>(langs);
       
    }

    public WGContent webtmlSelectContentForName(WGDatabase db, TMLContext context, String name, boolean isBI) throws WGAPIException {

        WGContent content = null;
       
        // First try: Current context language
        String currentLangName = null;
        if (LanguageBehaviourTools.isMultiLanguageContext(context)) {
            currentLangName = context.content().getLanguage().getName();
            content = db.getContentByName(name, currentLangName);
            if (content != null) {
                return content;
            }
        }

        // First try: Main context language
        if (LanguageBehaviourTools.isMultiLanguageContext(context.getmaincontext())) {
            String mainLangName = context.getmaincontext().content().getLanguage().getName();
                if (!mainLangName.equals(currentLangName)) {
                content = db.getContentByName(name, mainLangName);
                if (content != null) {
                    return content;
                }
            }
        }
       
        // Third try: Request locales
        if (context.iswebenvironment()) {
            content = LanguageBehaviourTools.chooseNamedContentByRequestLocales(db, name, (HttpServletRequest) context.getrequest(), isBI);
            if (content != null) {
                return content;
            }
        }

        // Fourth try: Database default language
        String defaultLangName = db.getDefaultLanguage();
        content = db.getContentByName(name, defaultLangName);
        if (content != null) {
            return content;
        }
       
        return null;
    }

    public WGContent webtmlSelectContentForPage(WGStructEntry page, TMLContext context, boolean isBI) throws WGAPIException {

        WGContent content = null;
       
        // First try: Current context language
        String currentLangName = null;
        if (LanguageBehaviourTools.isMultiLanguageContext(context)) {
            currentLangName = context.content().getLanguage().getName();
            content = LanguageBehaviourTools.getRelevantContent(page, currentLangName, isBI);
            if (content != null) {
                return content;
            }
        }

        // Second try: Main context language
        if (LanguageBehaviourTools.isMultiLanguageContext(context.getmaincontext())) {
            String mainLangName = context.getmaincontext().content().getLanguage().getName();
            if (!mainLangName.equals(currentLangName)) {
                content = LanguageBehaviourTools.getRelevantContent(page, mainLangName, isBI);
                if (content != null) {
                    return content;
                }
            }
        }
               
        // Third try: Request locales
        if (context.iswebenvironment()) {
            content = LanguageBehaviourTools.chooseContentByRequestLocales(page, context.getrequest(), isBI);
            if (content != null) {
                return content;
            }
        }

        // Fourth try: Database default language
        String defaultLangName = page.getDatabase().getDefaultLanguage();
        content = LanguageBehaviourTools.getRelevantContent(page, defaultLangName, isBI);
        if (content != null) {
            return content;
        }
       
        return null;
       
    }

    public WGLanguage webtmlSelectDatabaseLanguage(WGDatabase db, TMLContext context) throws WGAPIException {
       
        WGLanguage lang = null;
       
        // First try: Current context language
        String currentLangName = null;
        if (LanguageBehaviourTools.isMultiLanguageContext(context)) {
            currentLangName = context.content().getLanguage().getName();
            if (LanguageBehaviourTools.isMultiLanguageContext(context)) {
                lang = db.getLanguage(currentLangName);
                if (lang != null && !lang.isDummy()) {
                    return lang;
                }
            }
        }
       
        // Second try: Main context language
        if (LanguageBehaviourTools.isMultiLanguageContext(context.getmaincontext())) {
            String mainLangName = context.getmaincontext().content().getLanguage().getName();
            if (!mainLangName.equals(currentLangName)) {
                lang = db.getLanguage(mainLangName);
                if (lang != null && !lang.isDummy()) {
                    return lang;
                }
            }
        }
               
        // Third try: Request locales
        if (context.iswebenvironment()) {
            lang = LanguageBehaviourTools.chooseLanguageByRequestLocales(db, context.getrequest());
            if (lang != null && !lang.isDummy()) {
                return lang;
            }
        }

        // Fourth try: Database default language
        String defaultLangName = db.getDefaultLanguage();
        lang = db.getLanguage(defaultLangName);
        if (lang != null && !lang.isDummy()) {
            return lang;
        }
       
        return null;
    }

}
TOP

Related Classes of de.innovationgate.wgpublisher.lang.DynamicLanguageBehaviour

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.