From 026f9203a8eec7bbb169844e846227598aa45f76 Mon Sep 17 00:00:00 2001 From: Derek Hulley Date: Fri, 15 Dec 2006 14:27:17 +0000 Subject: [PATCH] ContentData - The default locale if one is not specified - Client code must still handle null locales for backwards compatibility NodeService - Moved support methods for instrinsic properties Locale - The Alfresco String representation of a Locale is x_y_z, even if x, y or z are "" - This makes the SQL like function more accurate for searches against locale properties git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@4618 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261 --- .../alfresco/repo/domain/PropertyValue.java | 7 +- .../repo/domain/hibernate/LocaleUserType.java | 123 ++++++++++++++++++ .../repo/domain/hibernate/Node.hbm.xml | 1 + .../repo/node/AbstractNodeServiceImpl.java | 44 ------- .../repo/node/db/DbNodeServiceImpl.java | 50 ++++++- .../service/cmr/repository/ContentData.java | 12 +- .../cmr/repository/ContentDataTest.java | 13 +- .../datatype/DefaultTypeConverter.java | 7 +- .../datatype/DefaultTypeConverterTest.java | 6 + 9 files changed, 200 insertions(+), 63 deletions(-) create mode 100644 source/java/org/alfresco/repo/domain/hibernate/LocaleUserType.java diff --git a/source/java/org/alfresco/repo/domain/PropertyValue.java b/source/java/org/alfresco/repo/domain/PropertyValue.java index 6b9eb9cf75..341dcc7724 100644 --- a/source/java/org/alfresco/repo/domain/PropertyValue.java +++ b/source/java/org/alfresco/repo/domain/PropertyValue.java @@ -134,12 +134,7 @@ public class PropertyValue implements Cloneable, Serializable @Override Serializable convert(Serializable value) { - String str = DefaultTypeConverter.INSTANCE.convert(String.class, value); - if (value instanceof Locale && str.length() < 6) - { - str += "_"; - } - return str; + return DefaultTypeConverter.INSTANCE.convert(String.class, value); } }, DATE diff --git a/source/java/org/alfresco/repo/domain/hibernate/LocaleUserType.java b/source/java/org/alfresco/repo/domain/hibernate/LocaleUserType.java new file mode 100644 index 0000000000..484b1d3ec8 --- /dev/null +++ b/source/java/org/alfresco/repo/domain/hibernate/LocaleUserType.java @@ -0,0 +1,123 @@ +/* + * Copyright (C) 2005 Alfresco, Inc. + * + * Licensed under the Mozilla Public License version 1.1 + * with a permitted attribution clause. You may obtain a + * copy of the License at + * + * http://www.alfresco.org/legal/license.txt + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, + * either express or implied. See the License for the specific + * language governing permissions and limitations under the + * License. + */ +package org.alfresco.repo.domain.hibernate; + +import java.io.Serializable; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Types; +import java.util.Locale; + +import org.alfresco.service.cmr.repository.datatype.DefaultTypeConverter; +import org.alfresco.util.EqualsHelper; +import org.hibernate.HibernateException; +import org.hibernate.usertype.UserType; + +/** + * Custom type to hide the persistence of {@link java.util.Locale locale} instances. + * + * @author Derek Hulley + */ +public class LocaleUserType implements UserType +{ + private static int[] SQL_TYPES = new int[] {Types.VARCHAR}; + + public Class returnedClass() + { + return Locale.class; + } + + /** + * @see #SQL_TYPES + */ + public int[] sqlTypes() + { + return SQL_TYPES; + } + + public boolean isMutable() + { + return false; + } + + public boolean equals(Object x, Object y) throws HibernateException + { + return EqualsHelper.nullSafeEquals(x, y); + } + + public int hashCode(Object x) throws HibernateException + { + return x.hashCode(); + } + + public Object deepCopy(Object value) throws HibernateException + { + // the qname is immutable + return value; + } + + public Object nullSafeGet(ResultSet rs, String[] names, Object owner) throws HibernateException, SQLException + { + String localeStr = rs.getString(names[0]); + if (localeStr == null) + { + return null; + } + else + { + Locale locale = DefaultTypeConverter.INSTANCE.convert(Locale.class, localeStr); + return locale; + } + } + + public void nullSafeSet(PreparedStatement stmt, Object value, int index) throws HibernateException, SQLException + { + // we want to ensure that the value is consistent w.r.t. the use of '_' + if (value == null) + { + stmt.setNull(index, Types.VARCHAR); + } + else + { + String localeStr = value.toString(); + if (localeStr.length() < 6) + { + localeStr += "_"; + } + stmt.setString(index, localeStr); + } + } + + public Object replace(Object original, Object target, Object owner) throws HibernateException + { + // qname is immutable + return original; + } + + public Object assemble(Serializable cached, Object owner) throws HibernateException + { + // qname is serializable + return cached; + } + + public Serializable disassemble(Object value) throws HibernateException + { + // locale is serializable + return (Locale) value; + } +} diff --git a/source/java/org/alfresco/repo/domain/hibernate/Node.hbm.xml b/source/java/org/alfresco/repo/domain/hibernate/Node.hbm.xml index 0309f85558..9883edc8ff 100644 --- a/source/java/org/alfresco/repo/domain/hibernate/Node.hbm.xml +++ b/source/java/org/alfresco/repo/domain/hibernate/Node.hbm.xml @@ -7,6 +7,7 @@ + - * This method can be used to ensure that the information already stored - * by the node key is not duplicated by the properties. - * - * @param properties properties to change - */ - protected void removeReferencableProperties(Map properties) - { - properties.remove(ContentModel.PROP_STORE_PROTOCOL); - properties.remove(ContentModel.PROP_STORE_IDENTIFIER); - properties.remove(ContentModel.PROP_NODE_UUID); - properties.remove(ContentModel.PROP_NODE_DBID); - } - - /** - * Adds all properties used by the - * {@link ContentModel#ASPECT_REFERENCEABLE referencable aspect}. - *

- * This method can be used to ensure that the values used by the aspect - * are present as node properties. - *

- * This method also ensures that the {@link ContentModel#PROP_NAME name property} - * is always present as a property on a node. - * - * @param nodeRef the node reference containing the values required - * @param nodeDbId the database-assigned ID - * @param properties the node properties - */ - protected void addReferencableProperties(NodeRef nodeRef, Long nodeDbId, Map properties) - { - properties.put(ContentModel.PROP_STORE_PROTOCOL, nodeRef.getStoreRef().getProtocol()); - properties.put(ContentModel.PROP_STORE_IDENTIFIER, nodeRef.getStoreRef().getIdentifier()); - properties.put(ContentModel.PROP_NODE_UUID, nodeRef.getId()); - properties.put(ContentModel.PROP_NODE_DBID, nodeDbId); - // add the ID as the name, if required - if (properties.get(ContentModel.PROP_NAME) == null) - { - properties.put(ContentModel.PROP_NAME, nodeRef.getId()); - } - } - /** * Defers to the pattern matching overload * diff --git a/source/java/org/alfresco/repo/node/db/DbNodeServiceImpl.java b/source/java/org/alfresco/repo/node/db/DbNodeServiceImpl.java index 4c03320e77..d7e3fa6a70 100644 --- a/source/java/org/alfresco/repo/node/db/DbNodeServiceImpl.java +++ b/source/java/org/alfresco/repo/node/db/DbNodeServiceImpl.java @@ -45,7 +45,6 @@ import org.alfresco.service.cmr.dictionary.AssociationDefinition; import org.alfresco.service.cmr.dictionary.ChildAssociationDefinition; import org.alfresco.service.cmr.dictionary.ClassDefinition; import org.alfresco.service.cmr.dictionary.DataTypeDefinition; -import org.alfresco.service.cmr.dictionary.DictionaryService; import org.alfresco.service.cmr.dictionary.InvalidAspectException; import org.alfresco.service.cmr.dictionary.InvalidTypeException; import org.alfresco.service.cmr.dictionary.PropertyDefinition; @@ -750,6 +749,49 @@ public class DbNodeServiceImpl extends AbstractNodeServiceImpl // done } + + /** + * Remove properties that should not be persisted as general properties. Where necessary, the + * properties are set on the node. + * + * @param node the node to set properties on + * @param properties properties to change + */ + private void extractIntrinsicProperties(Node node, Map properties) + { + properties.remove(ContentModel.PROP_STORE_PROTOCOL); + properties.remove(ContentModel.PROP_STORE_IDENTIFIER); + properties.remove(ContentModel.PROP_NODE_UUID); + properties.remove(ContentModel.PROP_NODE_DBID); + } + + /** + * Adds all properties used by the + * {@link ContentModel#ASPECT_REFERENCEABLE referencable aspect}. + *

+ * This method can be used to ensure that the values used by the aspect + * are present as node properties. + *

+ * This method also ensures that the {@link ContentModel#PROP_NAME name property} + * is always present as a property on a node. + * + * @param node the node with the values + * @param nodeRef the node reference containing the values required + * @param properties the node properties + */ + private void addIntrinsicProperties(Node node, Map properties) + { + NodeRef nodeRef = node.getNodeRef(); + properties.put(ContentModel.PROP_STORE_PROTOCOL, nodeRef.getStoreRef().getProtocol()); + properties.put(ContentModel.PROP_STORE_IDENTIFIER, nodeRef.getStoreRef().getIdentifier()); + properties.put(ContentModel.PROP_NODE_UUID, nodeRef.getId()); + properties.put(ContentModel.PROP_NODE_DBID, node.getId()); + // add the ID as the name, if required + if (properties.get(ContentModel.PROP_NAME) == null) + { + properties.put(ContentModel.PROP_NAME, nodeRef.getId()); + } + } public Map getProperties(NodeRef nodeRef) throws InvalidNodeRefException { @@ -759,8 +801,6 @@ public class DbNodeServiceImpl extends AbstractNodeServiceImpl private Map getPropertiesImpl(Node node) throws InvalidNodeRefException { - NodeRef nodeRef = node.getNodeRef(); - Map nodeProperties = node.getProperties(); Map ret = new HashMap(nodeProperties.size()); // copy values @@ -776,7 +816,7 @@ public class DbNodeServiceImpl extends AbstractNodeServiceImpl ret.put(propertyQName, value); } // spoof referencable properties - addReferencableProperties(nodeRef, node.getId(), ret); + addIntrinsicProperties(node, ret); // done return ret; } @@ -866,7 +906,7 @@ public class DbNodeServiceImpl extends AbstractNodeServiceImpl ParameterCheck.mandatory("properties", properties); // remove referencable properties - removeReferencableProperties(properties); + extractIntrinsicProperties(node, properties); // copy properties onto node Map nodeProperties = node.getProperties(); diff --git a/source/java/org/alfresco/service/cmr/repository/ContentData.java b/source/java/org/alfresco/service/cmr/repository/ContentData.java index 124b06c1f6..5b6a15e19c 100644 --- a/source/java/org/alfresco/service/cmr/repository/ContentData.java +++ b/source/java/org/alfresco/service/cmr/repository/ContentData.java @@ -21,6 +21,7 @@ import java.util.Locale; import java.util.StringTokenizer; import org.alfresco.i18n.I18NUtil; +import org.alfresco.service.cmr.repository.datatype.DefaultTypeConverter; import org.alfresco.util.EqualsHelper; /** @@ -126,7 +127,7 @@ public class ContentData implements Serializable } /** - * Create a content data without a locale. + * Create a content data using the {@link I18NUtil#getLocale() default locale}. * * @see #ContentData(String, String, long, String, Locale) */ @@ -146,7 +147,8 @@ public class ContentData implements Serializable * @param mimetype the content mimetype. This is mandatory if the contentUrl is specified. * @param size the content size. * @param encoding the content encoding (may be null). - * @param locale the locale of the content (may be null). + * @param locale the locale of the content (may be null). If null, the + * {@link I18NUtil#getLocale() default locale} will be used. */ public ContentData(String contentUrl, String mimetype, long size, String encoding, Locale locale) { @@ -155,6 +157,10 @@ public class ContentData implements Serializable this.mimetype = mimetype; this.size = size; this.encoding = encoding; + if (locale == null) + { + locale = I18NUtil.getLocale(); + } this.locale = locale; } @@ -184,7 +190,7 @@ public class ContentData implements Serializable .append("|mimetype=").append(mimetype == null ? "" : mimetype) .append("|size=").append(size) .append("|encoding=").append(encoding == null ? "" : encoding) - .append("|locale=").append(locale == null ? "" : locale); + .append("|locale=").append(locale == null ? "" : DefaultTypeConverter.INSTANCE.convert(String.class, locale)); return sb.toString(); } diff --git a/source/java/org/alfresco/service/cmr/repository/ContentDataTest.java b/source/java/org/alfresco/service/cmr/repository/ContentDataTest.java index 6021674855..ab1f8113aa 100644 --- a/source/java/org/alfresco/service/cmr/repository/ContentDataTest.java +++ b/source/java/org/alfresco/service/cmr/repository/ContentDataTest.java @@ -16,10 +16,13 @@ */ package org.alfresco.service.cmr.repository; -import org.alfresco.i18n.I18NUtil; +import java.util.Locale; import junit.framework.TestCase; +import org.alfresco.i18n.I18NUtil; +import org.alfresco.service.cmr.repository.datatype.DefaultTypeConverter; + /** * @see org.alfresco.service.cmr.repository.ContentData * @@ -35,18 +38,20 @@ public class ContentDataTest extends TestCase public void testToAndFromString() throws Exception { - ContentData property = new ContentData(null, null, 0L, null); + Locale locale = I18NUtil.getLocale(); + String localeStr = DefaultTypeConverter.INSTANCE.convert(String.class, locale); + ContentData property = new ContentData(null, null, 0L, null, null); // check null string String propertyStr = property.toString(); assertEquals("Null values not converted correctly", - "contentUrl=|mimetype=|size=0|encoding=|locale=", propertyStr); + "contentUrl=|mimetype=|size=0|encoding=|locale=" + localeStr, + propertyStr); // convert back ContentData checkProperty = ContentData.createContentProperty(propertyStr); assertEquals("Conversion from string failed", property, checkProperty); - String localeStr = I18NUtil.getLocale().toString(); property = new ContentData("uuu", "mmm", 123L, "eee", I18NUtil.getLocale()); // convert to a string diff --git a/source/java/org/alfresco/service/cmr/repository/datatype/DefaultTypeConverter.java b/source/java/org/alfresco/service/cmr/repository/datatype/DefaultTypeConverter.java index fa519c488a..09e97e7466 100644 --- a/source/java/org/alfresco/service/cmr/repository/datatype/DefaultTypeConverter.java +++ b/source/java/org/alfresco/service/cmr/repository/datatype/DefaultTypeConverter.java @@ -287,7 +287,12 @@ public class DefaultTypeConverter { public String convert(Locale source) { - return source.toString(); + String localeStr = source.toString(); + if (localeStr.length() < 6) + { + localeStr += "_"; + } + return localeStr; } }); diff --git a/source/java/org/alfresco/service/cmr/repository/datatype/DefaultTypeConverterTest.java b/source/java/org/alfresco/service/cmr/repository/datatype/DefaultTypeConverterTest.java index 3314d1dca2..50bacee82d 100644 --- a/source/java/org/alfresco/service/cmr/repository/datatype/DefaultTypeConverterTest.java +++ b/source/java/org/alfresco/service/cmr/repository/datatype/DefaultTypeConverterTest.java @@ -90,9 +90,12 @@ public class DefaultTypeConverterTest extends TestCase assertEquals(ISO8601DateFormat.format(date), DefaultTypeConverter.INSTANCE.convert(String.class, date)); assertEquals("P0Y25D", DefaultTypeConverter.INSTANCE.convert(String.class, new Duration("P0Y25D"))); assertEquals("woof", DefaultTypeConverter.INSTANCE.convert(String.class, "woof")); + // MLText MLText mlText = new MLText("woof"); mlText.addValue(Locale.SIMPLIFIED_CHINESE, "缂"); assertEquals("woof", DefaultTypeConverter.INSTANCE.convert(String.class, mlText)); + // Locale + assertEquals("fr_FR_", DefaultTypeConverter.INSTANCE.convert(String.class, Locale.FRANCE)); } public void testFromString() @@ -114,6 +117,9 @@ public class DefaultTypeConverterTest extends TestCase MLText converted = DefaultTypeConverter.INSTANCE.convert(MLText.class, "woof"); assertEquals("woof", converted.getValue(Locale.getDefault())); + + assertEquals(Locale.FRANCE, DefaultTypeConverter.INSTANCE.convert(Locale.class, "fr_FR")); + assertEquals(Locale.FRANCE, DefaultTypeConverter.INSTANCE.convert(Locale.class, "fr_FR_")); } public void testPrimativeAccessors()