001/* 002 * Copyright 2018 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.cms.data.type; 017 018import java.util.ArrayList; 019import java.util.List; 020import java.util.Locale; 021import java.util.Map; 022 023import org.apache.avalon.framework.configuration.Configuration; 024import org.apache.avalon.framework.configuration.ConfigurationException; 025import org.xml.sax.ContentHandler; 026import org.xml.sax.SAXException; 027 028import org.ametys.core.model.type.AbstractElementType; 029import org.ametys.plugins.repository.metadata.MultilingualString; 030import org.ametys.plugins.repository.metadata.MultilingualStringHelper; 031import org.ametys.runtime.model.exception.BadItemTypeException; 032 033/** 034 * Abstract class for multilingual string type of elements 035 */ 036public abstract class AbstractMultilingualStringElementType extends AbstractElementType<MultilingualString> 037{ 038 public MultilingualString castValue(String value) throws BadItemTypeException 039 { 040 try 041 { 042 return MultilingualStringHelper.fromString(value); 043 } 044 catch (IllegalArgumentException e) 045 { 046 throw new BadItemTypeException("Unable to cast '" + value + "' to a multilingual string"); 047 } 048 } 049 050 @Override 051 public String toString(MultilingualString value) 052 { 053 return MultilingualStringHelper.toString(value); 054 } 055 056 @Override 057 protected void _singleValueToSAX(ContentHandler contentHandler, String tagName, MultilingualString value, Locale locale) throws SAXException 058 { 059 MultilingualStringHelper.sax(contentHandler, tagName, value, locale); 060 } 061 062 @Override 063 public MultilingualString parseConfiguration(Configuration configuration) throws ConfigurationException 064 { 065 return MultilingualStringHelper.fromConfiguration(configuration); 066 } 067 068 @Override 069 protected Object _singleValueToJSON(MultilingualString value) 070 { 071 return MultilingualStringHelper.toJson(value); 072 } 073 074 @SuppressWarnings("unchecked") 075 public Object fromJSONForClient(Object json) 076 { 077 if (json == null) 078 { 079 return json; 080 } 081 else if (json instanceof Map) 082 { 083 return MultilingualStringHelper.fromJSON((Map<String, Object>) json); 084 } 085 else if (json instanceof List) 086 { 087 List<MultilingualString> multilingualStrings = new ArrayList<>(); 088 for (Map<String, Object> singleJson : (List<Map<String, Object>>) json) 089 { 090 multilingualStrings.add(MultilingualStringHelper.fromJSON(singleJson)); 091 } 092 return multilingualStrings.toArray(new MultilingualString[multilingualStrings.size()]); 093 } 094 else 095 { 096 throw new IllegalArgumentException("Try to convert the non user JSON object '" + json + "' into a multilingual string"); 097 } 098 } 099 100 public boolean isSimple() 101 { 102 return true; 103 } 104}