001/* 002 * Copyright 2011 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ametys.odf.translation; 017 018import java.util.HashMap; 019import java.util.Map; 020 021import org.apache.commons.lang.StringUtils; 022 023import org.ametys.cms.repository.Content; 024import org.ametys.cms.repository.ModifiableContent; 025import org.ametys.plugins.repository.metadata.CompositeMetadata; 026import org.ametys.plugins.repository.metadata.ModifiableCompositeMetadata; 027import org.ametys.plugins.repository.metadata.UnknownMetadataException; 028 029/** 030 * Translation helper: get and set translations for a content. 031 */ 032public final class TranslationHelper 033{ 034 035 /** The translations metadata name. */ 036 public static final String TRANSLATIONS_METADATA = "translations"; 037 038 private TranslationHelper() 039 { 040 // Hide the default constructor. 041 } 042 043 /** 044 * Get the translations of a content. 045 * @param content the Content. 046 * @return the translations as a Map of language -> content ID. 047 */ 048 public static Map<String, String> getTranslations(Content content) 049 { 050 Map<String, String> translations = new HashMap<>(); 051 052 try 053 { 054 CompositeMetadata translationsMeta = content.getMetadataHolder().getCompositeMetadata(TRANSLATIONS_METADATA); 055 for (String language : translationsMeta.getMetadataNames()) 056 { 057 String contentId = translationsMeta.getString(language, ""); 058 if (StringUtils.isNotBlank(contentId)) 059 { 060 translations.put(language, contentId); 061 } 062 } 063 } 064 catch (UnknownMetadataException e) 065 { 066 // Ignore, just return an empty map. 067 } 068 069 return translations; 070 } 071 072 /** 073 * Set the translations on a content. 074 * @param content the Content. 075 * @param translations the translations to set, as a Map of language -> content ID. 076 */ 077 public static void setTranslations(ModifiableContent content, Map<String, String> translations) 078 { 079 ModifiableCompositeMetadata translationsMeta = content.getMetadataHolder().getCompositeMetadata(TRANSLATIONS_METADATA, true); 080 081 for (String language : translationsMeta.getMetadataNames()) 082 { 083 translationsMeta.removeMetadata(language); 084 } 085 086 for (String language : translations.keySet()) 087 { 088 String contentId = translations.get(language); 089 translationsMeta.setMetadata(language, contentId); 090 content.saveChanges(); 091 } 092 } 093 094}