Search in sources :

Example 6 with NaturalLanguage

use of org.haiku.haikudepotserver.dataobjects.NaturalLanguage in project haikudepotserver by haiku.

the class NaturalLanguageChooserTag method doStartTagInternal.

@Override
protected int doStartTagInternal() throws Exception {
    ObjectContext context = getObjectContext();
    TagWriter tagWriter = new TagWriter(pageContext.getOut());
    List<NaturalLanguage> naturalLanguages = new ArrayList<>(NaturalLanguage.getAllPopular(context));
    NaturalLanguage currentNaturalLanguage = NaturalLanguageWebHelper.deriveNaturalLanguage(context, (HttpServletRequest) pageContext.getRequest());
    Collections.sort(naturalLanguages, Comparator.comparing(_NaturalLanguage::getCode));
    tagWriter.startTag("span");
    tagWriter.writeAttribute("class", "multipage-natural-language-chooser");
    for (int i = 0; i < naturalLanguages.size(); i++) {
        NaturalLanguage naturalLanguage = naturalLanguages.get(i);
        if (0 != i) {
            tagWriter.appendValue(" ");
        }
        if (currentNaturalLanguage != naturalLanguage) {
            String path = (String) pageContext.getRequest().getAttribute(RequestDispatcher.FORWARD_REQUEST_URI);
            UriComponentsBuilder builder = ServletUriComponentsBuilder.newInstance().path(path);
            builder.replaceQueryParam(WebConstants.KEY_NATURALLANGUAGECODE, naturalLanguage.getCode());
            tagWriter.startTag("a");
            tagWriter.writeAttribute("href", builder.build().toString());
            tagWriter.appendValue(naturalLanguage.getCode());
            // a
            tagWriter.endTag();
        } else {
            tagWriter.startTag("strong");
            tagWriter.writeAttribute("class", "banner-actions-text");
            tagWriter.appendValue(naturalLanguage.getCode());
            // strong
            tagWriter.endTag();
        }
    }
    // span
    tagWriter.endTag();
    return SKIP_BODY;
}
Also used : UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) ServletUriComponentsBuilder(org.springframework.web.servlet.support.ServletUriComponentsBuilder) TagWriter(org.springframework.web.servlet.tags.form.TagWriter) ArrayList(java.util.ArrayList) ObjectContext(org.apache.cayenne.ObjectContext) NaturalLanguage(org.haiku.haikudepotserver.dataobjects.NaturalLanguage) org.haiku.haikudepotserver.dataobjects.auto._NaturalLanguage(org.haiku.haikudepotserver.dataobjects.auto._NaturalLanguage)

Example 7 with NaturalLanguage

use of org.haiku.haikudepotserver.dataobjects.NaturalLanguage in project haikudepotserver by haiku.

the class NaturalLanguageWebHelper method deriveNaturalLanguage.

/**
 * <p>This will look at parameters on the supplied request and will return a natural language.  It will
 * resort to English language if no other language is able to be derived.</p>
 */
public static NaturalLanguage deriveNaturalLanguage(ObjectContext context, HttpServletRequest request) {
    Preconditions.checkNotNull(context);
    if (null != request) {
        String naturalLanguageCode = request.getParameter(WebConstants.KEY_NATURALLANGUAGECODE);
        if (!Strings.isNullOrEmpty(naturalLanguageCode)) {
            Optional<NaturalLanguage> naturalLanguageOptional = NaturalLanguage.tryGetByCode(context, naturalLanguageCode);
            if (naturalLanguageOptional.isPresent()) {
                return naturalLanguageOptional.get();
            } else {
                LOGGER.info("the natural language '{}' was specified, but was not able to be found", naturalLanguageCode);
            }
        }
        // see if we can deduce it from the locale.
        Locale locale = request.getLocale();
        if (null != locale) {
            Iterator<String> langI = Splitter.on(Pattern.compile("[-_]")).split(locale.toLanguageTag()).iterator();
            if (langI.hasNext()) {
                Optional<NaturalLanguage> naturalLanguageOptional = NaturalLanguage.tryGetByCode(context, langI.next());
                if (naturalLanguageOptional.isPresent() && naturalLanguageOptional.get().getIsPopular()) {
                    return naturalLanguageOptional.get();
                }
            }
        }
    }
    return NaturalLanguage.tryGetByCode(context, NaturalLanguage.CODE_ENGLISH).get();
}
Also used : Locale(java.util.Locale) NaturalLanguage(org.haiku.haikudepotserver.dataobjects.NaturalLanguage)

Aggregations

NaturalLanguage (org.haiku.haikudepotserver.dataobjects.NaturalLanguage)7 ObjectContext (org.apache.cayenne.ObjectContext)6 Locale (java.util.Locale)3 Preconditions (com.google.common.base.Preconditions)2 Hashing (com.google.common.hash.Hashing)2 MediaType (com.google.common.net.MediaType)2 SyndContentImpl (com.rometools.rome.feed.synd.SyndContentImpl)2 SyndEntry (com.rometools.rome.feed.synd.SyndEntry)2 SyndEntryImpl (com.rometools.rome.feed.synd.SyndEntryImpl)2 Collections (java.util.Collections)2 List (java.util.List)2 Optional (java.util.Optional)2 Collectors (java.util.stream.Collectors)2 ServerRuntime (org.apache.cayenne.configuration.server.ServerRuntime)2 ObjectSelect (org.apache.cayenne.query.ObjectSelect)2 StringUtils (org.apache.commons.lang3.StringUtils)2 Pkg (org.haiku.haikudepotserver.dataobjects.Pkg)2 PkgVersion (org.haiku.haikudepotserver.dataobjects.PkgVersion)2 FeedSpecification (org.haiku.haikudepotserver.feed.model.FeedSpecification)2 SyndEntrySupplier (org.haiku.haikudepotserver.feed.model.SyndEntrySupplier)2