/** * Adds (or replaces) the fields targeted by this translation, by default {@code name}, {@code def} and * {@code synonym} with the {@link #getTargetLocale() targeted locale} as a suffix, for example {@code name_es}. * * @throws SolrServerException if communicating with the Solr server failed * @throws IOException if communicating with the Solr server failed */ protected void addFields() throws SolrServerException, IOException { addField("name_" + getTargetLocale().toString(), false); addField("def_" + getTargetLocale().toString(), false); addField("synonym_" + getTargetLocale().toString(), true); }
@Override public void initialize() { try { addFieldType(); addFields(); } catch (SolrServerException | IOException ex) { this.logger.error("Failed to add the required Solr fields for {}-{}: {}", getTargetLocale(), getTargetVocabularyId(), ex.getMessage(), ex); } }
/** * Check if the specified locale is supported by this translation, specifically if the {@link #getTargetLocale() * targeted locale} is a superset of it. * * @param locale the locale to check * @return {@code true} if the specified locale is supported */ protected boolean isLocaleSupported(Locale locale) { return locale.toLanguageTag().startsWith(getTargetLocale().toLanguageTag()); }
@Override public void extendTerm(VocabularyInputTerm term, Vocabulary vocabulary) { if (!isTargetLocaleEnabled()) { return; } Locale targetLocale = getTargetLocale(); String label = this.translator.getTranslation(vocabulary.getIdentifier(), term.getId().replace(':', '_') + KEY_MAP.get(NAME_KEY), targetLocale); String definition = this.translator.getTranslation(vocabulary.getIdentifier(), term.getId().replace(':', '_') + KEY_MAP.get(DESCRIPTION_KEY), targetLocale); String synonyms = this.translator.getTranslation(vocabulary.getIdentifier(), term.getId().replace(':', '_') + KEY_MAP.get(SYNONYM_KEY), targetLocale); if (StringUtils.isNotBlank(label)) { term.set(String.format(TRANSLATED_FIELD_FORMAT, NAME_KEY, targetLocale), label); } if (StringUtils.isNotBlank(definition)) { term.set(String.format(TRANSLATED_FIELD_FORMAT, DESCRIPTION_KEY, targetLocale), definition); } if (StringUtils.isNotBlank(synonyms)) { term.set(String.format(TRANSLATED_FIELD_FORMAT, SYNONYM_KEY, targetLocale), splitMultiValuedText(synonyms)); } }
@Override public void extendQuery(SolrQuery query, Vocabulary vocabulary) if (!isCurrentLocaleTargeted()) { return; if (currentLocale != null && isLocaleSupported(currentLocale)) { Locale targetLocale = getTargetLocale(); if (StringUtils.isNotBlank(query.get(DisMaxParams.PF))) { try (Formatter f = new Formatter()) {
/** * Adds (or replaces) a type definition for the {@link #addFields() fields used by this translation}. * * @throws SolrServerException if communicating with the Solr server failed * @throws IOException if communicating with the Solr server failed */ protected void addFieldType() throws SolrServerException, IOException { FieldTypeDefinition fieldTypeDefinition = new FieldTypeDefinition(); Map<String, Object> fieldTypeAttributes = new LinkedHashMap<>(); String name = "text_general_" + getTargetLocale().toString(); fieldTypeAttributes.put("name", name); fieldTypeAttributes.put("class", "solr.TextField"); fieldTypeDefinition.setAttributes(fieldTypeAttributes); AnalyzerDefinition analyzerDefinition = new AnalyzerDefinition(); analyzerDefinition.setAttributes( Collections.<String, Object>singletonMap("class", getAnalyzerType())); fieldTypeDefinition.setAnalyzer(analyzerDefinition); try { // The current version (5.5) of SolrJ/EmbeddedSolrServer doesn't support getting schema information, // so we do this the ugly way: try to add, check for errors, try to replace UpdateResponse response = new SchemaRequest.AddFieldType(fieldTypeDefinition).process(getClient()); if (response.getResponse().get("errors") != null) { response = new SchemaRequest.ReplaceFieldType(fieldTypeDefinition).process(getClient()); } this.logger.debug(response.toString()); } catch (Exception ex) { } }
/** * Adds (or replaces) a field in the schema, set up to copy the values from the field with the locale suffix * trimmed. * * @param name the name of the field to set up * @param multiValued whether the field accepts multiple values or not * @throws SolrServerException if communicating with the Solr server failed * @throws IOException if communicating with the Solr server failed */ protected void addField(String name, boolean multiValued) throws SolrServerException, IOException { Map<String, Object> fieldDefinition = new LinkedHashMap<>(); fieldDefinition.put("name", name); fieldDefinition.put("type", "text_general_" + getTargetLocale().toString()); fieldDefinition.put("indexed", true); fieldDefinition.put("stored", true); fieldDefinition.put("multiValued", multiValued); // Add or redefine the field UpdateResponse response = new SchemaRequest.AddField(fieldDefinition).process(getClient()); if (response.getResponse().get("errors") != null) { response = new SchemaRequest.ReplaceField(fieldDefinition).process(getClient()); } }
@Override public void extendTerm(VocabularyInputTerm term, Vocabulary vocabulary) { Locale targetLocale = getTargetLocale(); String label = this.translator.getTranslation(vocabulary.getIdentifier(), term.getId().replace(':', '_') + KEY_MAP.get(NAME_KEY), targetLocale); String definition = this.translator.getTranslation(vocabulary.getIdentifier(), term.getId().replace(':', '_') + KEY_MAP.get(DESCRIPTION_KEY), targetLocale); String synonyms = this.translator.getTranslation(vocabulary.getIdentifier(), term.getId().replace(':', '_') + KEY_MAP.get(SYNONYM_KEY), targetLocale); if (StringUtils.isNotBlank(label)) { term.set(String.format(TRANSLATED_FIELD_FORMAT, NAME_KEY, targetLocale), label); } if (StringUtils.isNotBlank(definition)) { term.set(String.format(TRANSLATED_FIELD_FORMAT, DESCRIPTION_KEY, targetLocale), definition); } if (StringUtils.isNotBlank(synonyms)) { term.set(String.format(TRANSLATED_FIELD_FORMAT, SYNONYM_KEY, targetLocale), splitMultiValuedText(synonyms)); } }
@Override public void extendQuery(SolrQuery query, Vocabulary vocabulary) if (!isCurrentLocaleTargeted()) { return; Locale targetLocale = getTargetLocale(); if (StringUtils.isNotBlank(query.get(DisMaxParams.PF))) { try (Formatter f = new Formatter()) {
/** * Check if the current context is set to a locale {@link #isLocaleSupported(Locale) supported} by this translation. * * @return {@code true} if the current locale is supported */ protected boolean isCurrentLocaleTargeted() { return isLocaleSupported(this.localizationContext.getCurrentLocale()); }
/** * Gets a connection to the targeted core, used for sending the schema requests. * * @return a valid solr client */ protected SolrClient getClient() { return this.coreConnection.getSolrConnection(getTargetVocabularyId()); }
@Override public boolean isVocabularySupported(Vocabulary vocabulary) { return getTargetVocabularyId().equals(vocabulary.getIdentifier()); }
@Override public void initialize() { try { addFieldType(); addFields(); } catch (SolrServerException | IOException ex) { this.logger.error("Failed to add the required Solr fields for {}-{}: {}", getTargetLocale(), getTargetVocabularyId(), ex.getMessage(), ex); } }
/** * Adds (or replaces) the fields targeted by this translation, by default {@code name}, {@code def} and * {@code synonym} with the {@link #getTargetLocale() targeted locale} as a suffix, for example {@code name_es}. * * @throws SolrServerException if communicating with the Solr server failed * @throws IOException if communicating with the Solr server failed */ protected void addFields() throws SolrServerException, IOException { addField("name_" + getTargetLocale().toString(), false); addField("def_" + getTargetLocale().toString(), false); addField("synonym_" + getTargetLocale().toString(), true); }
/** * Adds (or replaces) a field in the schema, set up to copy the values from the field with the locale suffix * trimmed. * * @param name the name of the field to set up * @param multiValued whether the field accepts multiple values or not * @throws SolrServerException if communicating with the Solr server failed * @throws IOException if communicating with the Solr server failed */ protected void addField(String name, boolean multiValued) throws SolrServerException, IOException { Map<String, Object> fieldDefinition = new LinkedHashMap<>(); fieldDefinition.put("name", name); fieldDefinition.put("type", "text_general_" + getTargetLocale().toString()); fieldDefinition.put("indexed", true); fieldDefinition.put("stored", true); fieldDefinition.put("multiValued", multiValued); // Add or redefine the field UpdateResponse response = new SchemaRequest.AddField(fieldDefinition).process(getClient()); if (response.getResponse().get("errors") != null) { response = new SchemaRequest.ReplaceField(fieldDefinition).process(getClient()); } }
/** * Check if the current context is set to a locale {@link #isLocaleSupported(Locale) supported} by this translation. * * @return {@code true} if the current locale is supported */ protected boolean isCurrentLocaleTargeted() { return isLocaleSupported(this.localizationContext.getCurrentLocale()); }
/** * Gets a connection to the targeted core, used for sending the schema requests. * * @return a valid solr client */ protected SolrClient getClient() { return this.coreConnection.getSolrConnection(this.vocabularyManager.getVocabulary(getTargetVocabularyId())); }
/** * Check if the specified locale is supported by this translation, specifically if the {@link #getTargetLocale() * targeted locale} is a superset of it. * * @param locale the locale to check * @return {@code true} if the specified locale is supported */ protected boolean isLocaleSupported(Locale locale) { return locale != null && locale.toLanguageTag().startsWith(getTargetLocale().toLanguageTag()); }
@Override public boolean isVocabularySupported(Vocabulary vocabulary) { return getTargetVocabularyId().equals(vocabulary.getIdentifier()); }
/** * Check if the {@link #getTargetLocale() locale targeted by this translation} is in the list of supported locales * by this instance. * * @return {@code true} if the current language is targeted, or if the instance is set as multilingual and the * targeted language is included in the list of supported locales, {@code false} otherwise */ protected boolean isTargetLocaleEnabled() { Locale currentLocale = this.localizationContext.getCurrentLocale(); if (isLocaleSupported(currentLocale)) { return true; } if (!this.config.getProperty("multilingual", Boolean.FALSE)) { return false; } Set<String> defaultValue = new HashSet<>(); defaultValue.add(currentLocale.getLanguage()); Set<String> enabledLanguages = this.config.getProperty("languages", defaultValue); for (String lang : enabledLanguages) { if (StringUtils.isBlank(lang)) { continue; } String trimmed = lang.trim(); if (isLocaleSupported(Locale.forLanguageTag(trimmed))) { return true; } } return false; }