From 4f9a8ae0d413e8e5d28ff6e00ed041e03d17b02d Mon Sep 17 00:00:00 2001 From: Robert von Burg Date: Tue, 28 Jan 2014 21:22:52 +0100 Subject: [PATCH] [New] Implemented an EnumHandler --- .../query/enums/DefaultEnumHandler.java | 126 ++++++++++++++++++ .../runtime/query/enums/EnumHandler.java | 26 ++++ .../runtime/query/enums/EnumValue.java | 78 +++++++++++ .../runtime/query/enums/StrolchEnum.java | 86 ++++++++++++ .../test/query/enums/EnumHandlerTest.java | 63 +++++++++ .../config/StrolchConfiguration.xml | 12 ++ .../resources/transienttest/data/Enums.xml | 34 +++++ .../transienttest/data/StrolchModel.xml | 1 + 8 files changed, 426 insertions(+) create mode 100644 src/main/java/li/strolch/runtime/query/enums/DefaultEnumHandler.java create mode 100644 src/main/java/li/strolch/runtime/query/enums/EnumHandler.java create mode 100644 src/main/java/li/strolch/runtime/query/enums/EnumValue.java create mode 100644 src/main/java/li/strolch/runtime/query/enums/StrolchEnum.java create mode 100644 src/test/java/li/strolch/runtime/test/query/enums/EnumHandlerTest.java create mode 100644 src/test/resources/transienttest/data/Enums.xml diff --git a/src/main/java/li/strolch/runtime/query/enums/DefaultEnumHandler.java b/src/main/java/li/strolch/runtime/query/enums/DefaultEnumHandler.java new file mode 100644 index 000000000..01c8c0d32 --- /dev/null +++ b/src/main/java/li/strolch/runtime/query/enums/DefaultEnumHandler.java @@ -0,0 +1,126 @@ +/* + * Copyright 2013 Robert von Burg + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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 li.strolch.runtime.query.enums; + +import java.text.MessageFormat; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import java.util.Set; + +import li.strolch.agent.api.ComponentContainer; +import li.strolch.agent.api.StrolchComponent; +import li.strolch.exception.StrolchException; +import li.strolch.model.Locator; +import li.strolch.model.ParameterBag; +import li.strolch.model.Resource; +import li.strolch.model.parameter.StringParameter; +import li.strolch.persistence.api.StrolchTransaction; +import li.strolch.runtime.StrolchConstants; +import li.strolch.runtime.configuration.ComponentConfiguration; +import ch.eitchnet.utils.dbc.DBC; + +/** + * @author Robert von Burg + */ +public class DefaultEnumHandler extends StrolchComponent implements EnumHandler { + + private static final String PROP_REALM = "realm"; + + private String realm; + private Map enumLocators; + + /** + * @param container + * @param componentName + */ + public DefaultEnumHandler(ComponentContainer container, String componentName) { + super(container, componentName); + } + + @Override + public void initialize(ComponentConfiguration configuration) { + + this.enumLocators = new HashMap<>(); + this.realm = configuration.getString(PROP_REALM, StrolchConstants.DEFAULT_REALM); + + Set enumNames = configuration.getPropertyKeys(); + for (String enumName : enumNames) { + if (enumName.equals(PROP_REALM)) { + continue; + } + + String enumLocatorS = configuration.getString(enumName, null); + Locator enumLocator = Locator.valueOf(enumLocatorS); + logger.info(MessageFormat.format("Registered enum {0} at {1}", enumName, enumLocator)); + enumLocators.put(enumName, enumLocator); + } + + super.initialize(configuration); + } + + @Override + public StrolchEnum getEnum(String name, Locale locale) { + + DBC.PRE.assertNotEmpty("Enum name must be given!", name); + DBC.PRE.assertNotNull("Locale must be given!", locale); + + Locator enumLocator = this.enumLocators.get(name); + if (enumLocator == null) + throw new StrolchException(MessageFormat.format("No enumeration is configured for the name {0}", name)); + + try (StrolchTransaction tx = getContainer().getRealm(realm).openTx()) { + Resource enumeration = tx.findElement(enumLocator); + ParameterBag enumValuesByLanguage = findParameterBagByLanguage(enumeration, locale); + + Set parameterKeySet = enumValuesByLanguage.getParameterKeySet(); + List values = new ArrayList<>(parameterKeySet.size()); + for (String paramKey : parameterKeySet) { + StringParameter enumParam = enumValuesByLanguage.getParameter(paramKey); + values.add(new EnumValue(paramKey, enumParam.getValue())); + } + + StrolchEnum strolchEnum = new StrolchEnum(name, locale, values); + return strolchEnum; + } + } + + /** + * @param enumeration + * @param locale + * @return + */ + private ParameterBag findParameterBagByLanguage(Resource enumeration, Locale locale) { + + String localeS = locale.getLanguage() + "_" + locale.getCountry() + "_" + locale.getVariant(); + if (enumeration.hasParameterBag(localeS)) + return enumeration.getParameterBag(localeS); + + localeS = locale.getLanguage() + "_" + locale.getCountry(); + if (enumeration.hasParameterBag(localeS)) + return enumeration.getParameterBag(localeS); + + localeS = locale.getLanguage(); + if (enumeration.hasParameterBag(localeS)) + return enumeration.getParameterBag(localeS); + + String msg = "No enumeration exists for language {0} on enumeration {1}"; + msg = MessageFormat.format(msg, locale, enumeration.getLocator()); + throw new StrolchException(msg); + } +} diff --git a/src/main/java/li/strolch/runtime/query/enums/EnumHandler.java b/src/main/java/li/strolch/runtime/query/enums/EnumHandler.java new file mode 100644 index 000000000..89ea67f16 --- /dev/null +++ b/src/main/java/li/strolch/runtime/query/enums/EnumHandler.java @@ -0,0 +1,26 @@ +/* + * Copyright 2013 Robert von Burg + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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 li.strolch.runtime.query.enums; + +import java.util.Locale; + +/** + * @author Robert von Burg + */ +public interface EnumHandler { + + public StrolchEnum getEnum(String name, Locale locale); +} diff --git a/src/main/java/li/strolch/runtime/query/enums/EnumValue.java b/src/main/java/li/strolch/runtime/query/enums/EnumValue.java new file mode 100644 index 000000000..479508b9a --- /dev/null +++ b/src/main/java/li/strolch/runtime/query/enums/EnumValue.java @@ -0,0 +1,78 @@ +/* + * Copyright 2013 Robert von Burg + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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 li.strolch.runtime.query.enums; + +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlAttribute; +import javax.xml.bind.annotation.XmlRootElement; + +/** + * @author Robert von Burg + */ +@XmlAccessorType(XmlAccessType.NONE) +@XmlRootElement(name = "EnumValue") +public class EnumValue { + + @XmlAttribute(name = "name") + private String name; + @XmlAttribute(name = "value") + private String value; + + public EnumValue() { + // no-arg constructor for JAXB + } + + /** + * @param name + * @param value + */ + public EnumValue(String name, String value) { + super(); + this.name = name; + this.value = value; + } + + /** + * @return the name + */ + public String getName() { + return this.name; + } + + /** + * @param name + * the name to set + */ + public void setName(String name) { + this.name = name; + } + + /** + * @return the value + */ + public String getValue() { + return this.value; + } + + /** + * @param value + * the value to set + */ + public void setValue(String value) { + this.value = value; + } +} diff --git a/src/main/java/li/strolch/runtime/query/enums/StrolchEnum.java b/src/main/java/li/strolch/runtime/query/enums/StrolchEnum.java new file mode 100644 index 000000000..6db0819d6 --- /dev/null +++ b/src/main/java/li/strolch/runtime/query/enums/StrolchEnum.java @@ -0,0 +1,86 @@ +/* + * Copyright 2013 Robert von Burg + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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 li.strolch.runtime.query.enums; + +import java.util.List; +import java.util.Locale; + +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlAttribute; +import javax.xml.bind.annotation.XmlRootElement; +import javax.xml.bind.annotation.XmlType; + +/** + * @author Robert von Burg + */ +@XmlAccessorType(XmlAccessType.NONE) +@XmlRootElement(name = "StrolchEnum") +@XmlType(propOrder = { "name", "locale", "values" }) +public class StrolchEnum { + + @XmlAttribute(name = "name") + private String name; + @XmlAttribute(name = "locale") + private String locale; + private Locale localeL; + @XmlAttribute(name = "values") + private List values; + + public StrolchEnum() { + // no-arg constructor for JAXB + } + + /** + * @param name + * @param locale + * @param values + */ + public StrolchEnum(String name, Locale locale, List values) { + this.name = name; + this.locale = locale.toString(); + this.localeL = locale; + this.values = values; + } + + /** + * @return the name + */ + public String getName() { + return this.name; + } + + /** + * @return the locale as string + */ + public String getLocale() { + return this.locale.toString(); + } + + /** + * @return the locale + */ + public Locale getLocaleL() { + return this.localeL; + } + + /** + * @return the values + */ + public List getValues() { + return this.values; + } +} diff --git a/src/test/java/li/strolch/runtime/test/query/enums/EnumHandlerTest.java b/src/test/java/li/strolch/runtime/test/query/enums/EnumHandlerTest.java new file mode 100644 index 000000000..c52173166 --- /dev/null +++ b/src/test/java/li/strolch/runtime/test/query/enums/EnumHandlerTest.java @@ -0,0 +1,63 @@ +/* + * Copyright 2013 Robert von Burg + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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 li.strolch.runtime.test.query.enums; + +import static org.junit.Assert.assertEquals; + +import java.util.Locale; + +import li.strolch.agent.api.ComponentContainer; +import li.strolch.agent.api.StrolchAgent; +import li.strolch.runtime.query.enums.EnumHandler; +import li.strolch.runtime.query.enums.StrolchEnum; +import li.strolch.runtime.test.component.ComponentContainerTest; + +import org.junit.Test; + +/** + * @author Robert von Burg + */ +public class EnumHandlerTest { + + private static final String ENUM_HANDLER_TEST_RUNTIME = "target/EnumHandlerTest/"; + + @Test + public void shouldFindByLocator() { + + StrolchAgent agent = ComponentContainerTest.startContainer(ENUM_HANDLER_TEST_RUNTIME, + ComponentContainerTest.PATH_TRANSIENT_CONTAINER); + ComponentContainer container = agent.getContainer(); + + EnumHandler enumHandler = container.getComponent(EnumHandler.class); + StrolchEnum sexEnum = enumHandler.getEnum("sex", Locale.ENGLISH); + assertEquals("sex", sexEnum.getName()); + assertEquals("en", sexEnum.getLocale()); + assertEquals(3, sexEnum.getValues().size()); + assertEquals("both", sexEnum.getValues().get(0).getValue()); + + StrolchEnum salutationsEnum = enumHandler.getEnum("salutations", Locale.UK); + assertEquals("salutations", salutationsEnum.getName()); + assertEquals("en_GB", salutationsEnum.getLocale()); + assertEquals(3, salutationsEnum.getValues().size()); + assertEquals("Mr", salutationsEnum.getValues().get(0).getValue()); + + StrolchEnum religionsEnum = enumHandler.getEnum("religions", Locale.CANADA); + assertEquals("religions", religionsEnum.getName()); + assertEquals("en_CA", religionsEnum.getLocale()); + assertEquals(9, religionsEnum.getValues().size()); + assertEquals("Orthodox", religionsEnum.getValues().get(0).getValue()); + } +} diff --git a/src/test/resources/transienttest/config/StrolchConfiguration.xml b/src/test/resources/transienttest/config/StrolchConfiguration.xml index a33aadaec..e27bd48cf 100644 --- a/src/test/resources/transienttest/config/StrolchConfiguration.xml +++ b/src/test/resources/transienttest/config/StrolchConfiguration.xml @@ -32,4 +32,16 @@ true + + EnumHandler + li.strolch.runtime.query.enums.EnumHandler + li.strolch.runtime.query.enums.DefaultEnumHandler + ElementMapHandler + + defaultRealm + Resource/Enumeration/salutations + Resource/Enumeration/sex + Resource/Enumeration/religions + + \ No newline at end of file diff --git a/src/test/resources/transienttest/data/Enums.xml b/src/test/resources/transienttest/data/Enums.xml new file mode 100644 index 000000000..62ddb9c61 --- /dev/null +++ b/src/test/resources/transienttest/data/Enums.xml @@ -0,0 +1,34 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/test/resources/transienttest/data/StrolchModel.xml b/src/test/resources/transienttest/data/StrolchModel.xml index cb2396975..9fd6b2020 100644 --- a/src/test/resources/transienttest/data/StrolchModel.xml +++ b/src/test/resources/transienttest/data/StrolchModel.xml @@ -23,6 +23,7 @@ +