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.AmetysRepositoryException; 026import org.ametys.plugins.repository.RepositoryConstants; 027import org.ametys.plugins.repository.data.repositorydata.ModifiableRepositoryData; 028import org.ametys.plugins.repository.data.repositorydata.RepositoryData; 029import org.ametys.plugins.repository.data.repositorydata.impl.JCRRepositoryData; 030import org.ametys.plugins.repository.jcr.SimpleAmetysObject; 031 032/** 033 * Translation helper: get and set translations for a content. 034 */ 035public final class TranslationHelper 036{ 037 038 /** The translations metadata name. */ 039 public static final String TRANSLATIONS_METADATA = "translations"; 040 041 private TranslationHelper() 042 { 043 // Hide the default constructor. 044 } 045 046 /** 047 * Get the translations of a content. 048 * @param content the Content. 049 * @return the translations as a Map of language -> content ID. 050 * FIXME CMS-9941: Use the DataHolder Internal API 051 */ 052 public static Map<String, String> getTranslations(Content content) 053 { 054 Map<String, String> translations = new HashMap<>(); 055 056 RepositoryData repositoryData = _getContentRepositoryData(content); 057 if (repositoryData.hasValue(TRANSLATIONS_METADATA)) 058 { 059 try 060 { 061 RepositoryData translationsRepositoryData = repositoryData.getRepositoryData(TRANSLATIONS_METADATA); 062 for (String language : translationsRepositoryData.getDataNames()) 063 { 064 String contentId = translationsRepositoryData.getString(language); 065 if (StringUtils.isNotBlank(contentId)) 066 { 067 translations.put(language, contentId); 068 } 069 } 070 } 071 catch (AmetysRepositoryException e) 072 { 073 throw new AmetysRepositoryException("Error while getting translations for content " + content.getId(), e); 074 } 075 } 076 077 return translations; 078 } 079 080 /** 081 * Set the translations on a content. 082 * @param content the Content. 083 * @param translations the translations to set, as a Map of language -> content ID. 084 * FIXME CMS-9941: Use the DataHolder Internal API 085 */ 086 public static void setTranslations(ModifiableContent content, Map<String, String> translations) 087 { 088 ModifiableRepositoryData repositoryData = _getContentRepositoryData(content); 089 ModifiableRepositoryData translationsRepositoryData; 090 if (repositoryData.hasValue(TRANSLATIONS_METADATA)) 091 { 092 translationsRepositoryData = repositoryData.getRepositoryData(TRANSLATIONS_METADATA); 093 } 094 else 095 { 096 translationsRepositoryData = repositoryData.addRepositoryData(TRANSLATIONS_METADATA, RepositoryConstants.COMPOSITE_METADTA_NODETYPE); 097 } 098 099 for (String language : translationsRepositoryData.getDataNames()) 100 { 101 translationsRepositoryData.removeValue(language); 102 } 103 104 for (String language : translations.keySet()) 105 { 106 String contentId = translations.get(language); 107 translationsRepositoryData.setValue(language, contentId); 108 } 109 110 content.saveChanges(); 111 } 112 113 private static ModifiableRepositoryData _getContentRepositoryData(Content content) 114 { 115 if (content instanceof SimpleAmetysObject) 116 { 117 return new JCRRepositoryData(((SimpleAmetysObject) content).getNode()); 118 } 119 else 120 { 121 throw new IllegalArgumentException("Unable to acces to the translation of the content '" + content.getId() + "'."); 122 } 123 } 124 125}