Fixed AR-1361: MLText persistence doesn't use serialization

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@5767 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Derek Hulley
2007-05-24 04:01:50 +00:00
parent 504b174852
commit f4f417707a
8 changed files with 196 additions and 11 deletions

View File

@@ -50,8 +50,6 @@ public class MLText extends HashMap<Locale, String>
{
private static final long serialVersionUID = -3696135175650511841L;
private Locale defaultLocale;
public MLText()
{
super(3, 0.75F);
@@ -82,7 +80,6 @@ public class MLText extends HashMap<Locale, String>
public MLText(Locale locale, String value)
{
super(3, 0.75F);
defaultLocale = locale;
super.put(locale, value);
}
@@ -164,7 +161,7 @@ public class MLText extends HashMap<Locale, String>
if (match == null)
{
// No close matches for the locale - go for the default locale
locale = defaultLocale;
locale = I18NUtil.getLocale();
match = I18NUtil.getNearestLocale(locale, options);
if (match == null)
{

View File

@@ -37,9 +37,15 @@ import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.Map;
import org.alfresco.error.AlfrescoRuntimeException;
import org.alfresco.i18n.I18NUtil;
import org.alfresco.repo.attributes.Attribute;
import org.alfresco.repo.attributes.MapAttribute;
import org.alfresco.repo.attributes.MapAttributeValue;
import org.alfresco.repo.attributes.StringAttribute;
import org.alfresco.repo.attributes.StringAttributeValue;
import org.alfresco.service.cmr.repository.AssociationRef;
import org.alfresco.service.cmr.repository.ChildAssociationRef;
import org.alfresco.service.cmr.repository.ContentData;
@@ -73,7 +79,6 @@ import org.alfresco.util.VersionNumber;
*/
public class DefaultTypeConverter
{
/**
* Default Type Converter
*/
@@ -295,6 +300,41 @@ public class DefaultTypeConverter
}
});
//
// Attributes
//
INSTANCE.addConverter(MapAttribute.class, MLText.class, new TypeConverter.Converter<MapAttribute, MLText>()
{
public MLText convert(MapAttribute source)
{
MLText ret = new MLText();
for (Map.Entry<String, Attribute> entry : source.entrySet())
{
String localeStr = entry.getKey();
Locale locale;
try
{
locale = INSTANCE.convert(Locale.class, localeStr);
}
catch (TypeConversionException e)
{
throw new TypeConversionException(
"MapAttribute string key cannot be converted to a locales:" + localeStr, e);
}
Attribute valueAttribute = entry.getValue();
if (valueAttribute instanceof StringAttribute)
{
ret.put(locale, valueAttribute.getStringValue());
}
else
{
throw new TypeConversionException(
"MapAttribute must contain Locale-String mappings to convert to MLText");
}
}
return ret;
}
});
//
// From Locale
@@ -339,6 +379,20 @@ public class DefaultTypeConverter
}
});
INSTANCE.addConverter(MLText.class, Attribute.class, new TypeConverter.Converter<MLText, Attribute>()
{
public Attribute convert(MLText source)
{
Attribute attribute = new MapAttributeValue();
for (Map.Entry<Locale, String> entry : source.entrySet())
{
String localeStr = INSTANCE.convert(String.class, entry.getKey());
Attribute stringAttribute = new StringAttributeValue(entry.getValue());
attribute.put(localeStr, stringAttribute);
}
return attribute;
}
});
//
// From enum

View File

@@ -34,6 +34,10 @@ import java.util.Locale;
import junit.framework.TestCase;
import org.alfresco.repo.attributes.Attribute;
import org.alfresco.repo.attributes.MapAttributeValue;
import org.alfresco.repo.attributes.StringAttribute;
import org.alfresco.repo.attributes.StringAttributeValue;
import org.alfresco.service.cmr.repository.MLText;
import org.alfresco.util.ISO8601DateFormat;
import org.alfresco.util.VersionNumber;
@@ -145,6 +149,32 @@ public class DefaultTypeConverterTest extends TestCase
assertEquals(new VersionNumber("1.2.3"), DefaultTypeConverter.INSTANCE.convert(VersionNumber.class, "1.2.3"));
}
String localeStrEn = DefaultTypeConverter.INSTANCE.convert(String.class, Locale.ENGLISH);
String localeStrFr = DefaultTypeConverter.INSTANCE.convert(String.class, Locale.FRENCH);
public void testToMLText()
{
StringAttribute stringAttributeEn = new StringAttributeValue("English text");
StringAttribute stringAttributeFr = new StringAttributeValue("French text");
MapAttributeValue mapAttributeValue = new MapAttributeValue();
mapAttributeValue.put(localeStrEn, stringAttributeEn);
mapAttributeValue.put(localeStrFr, stringAttributeFr);
MLText mlText = DefaultTypeConverter.INSTANCE.convert(MLText.class, mapAttributeValue);
assertEquals("MapAttribute to MLText failed", "English text", mlText.getValue(Locale.ENGLISH));
assertEquals("MapAttribute to MLText failed", "French text", mlText.getValue(Locale.FRENCH));
}
public void testFromMLText()
{
MLText mlText = new MLText();
mlText.put(Locale.ENGLISH, "English");
mlText.put(Locale.FRENCH, "French");
Attribute attribute = DefaultTypeConverter.INSTANCE.convert(Attribute.class, mlText);
assertTrue("Attribute is of the wrong type", attribute instanceof MapAttributeValue);
assertNotNull("ML value not mapped", attribute.get(localeStrEn));
assertNotNull("ML value not mapped", attribute.get(localeStrFr));
}
public void testPrimativeAccessors()
{