[New] Implemented an EnumHandler

This commit is contained in:
Robert von Burg 2014-01-28 21:22:52 +01:00
parent 6db47346da
commit 4f9a8ae0d4
8 changed files with 426 additions and 0 deletions

View File

@ -0,0 +1,126 @@
/*
* Copyright 2013 Robert von Burg <eitch@eitchnet.ch>
*
* 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 <eitch@eitchnet.ch>
*/
public class DefaultEnumHandler extends StrolchComponent implements EnumHandler {
private static final String PROP_REALM = "realm";
private String realm;
private Map<String, Locator> 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<String> 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<String> parameterKeySet = enumValuesByLanguage.getParameterKeySet();
List<EnumValue> 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);
}
}

View File

@ -0,0 +1,26 @@
/*
* Copyright 2013 Robert von Burg <eitch@eitchnet.ch>
*
* 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 <eitch@eitchnet.ch>
*/
public interface EnumHandler {
public StrolchEnum getEnum(String name, Locale locale);
}

View File

@ -0,0 +1,78 @@
/*
* Copyright 2013 Robert von Burg <eitch@eitchnet.ch>
*
* 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 <eitch@eitchnet.ch>
*/
@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;
}
}

View File

@ -0,0 +1,86 @@
/*
* Copyright 2013 Robert von Burg <eitch@eitchnet.ch>
*
* 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 <eitch@eitchnet.ch>
*/
@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<EnumValue> values;
public StrolchEnum() {
// no-arg constructor for JAXB
}
/**
* @param name
* @param locale
* @param values
*/
public StrolchEnum(String name, Locale locale, List<EnumValue> 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<EnumValue> getValues() {
return this.values;
}
}

View File

@ -0,0 +1,63 @@
/*
* Copyright 2013 Robert von Burg <eitch@eitchnet.ch>
*
* 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 <eitch@eitchnet.ch>
*/
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());
}
}

View File

@ -32,4 +32,16 @@
<verbose>true</verbose>
</Properties>
</Component>
<Component>
<name>EnumHandler</name>
<api>li.strolch.runtime.query.enums.EnumHandler</api>
<impl>li.strolch.runtime.query.enums.DefaultEnumHandler</impl>
<depends>ElementMapHandler</depends>
<Properties>
<realm>defaultRealm</realm>
<salutations>Resource/Enumeration/salutations</salutations>
<sex>Resource/Enumeration/sex</sex>
<religions>Resource/Enumeration/religions</religions>
</Properties>
</Component>
</StrolchConfiguration>

View File

@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<StrolchModel>
<Resource Id="salutations" Name="Salutations" Type="Enumeration">
<ParameterBag Id="en" Name="Salutations" Type="Enumeration">
<Parameter Id="mr" name="Mr" Type="String" Value="Mr" />
<Parameter Id="mrs" name="Mrs" Type="String" Value="Mrs" />
<Parameter Id="ms" name="Ms" Type="String" Value="Ms" />
</ParameterBag>
</Resource>
<Resource Id="sex" Name="Sex" Type="Enumeration">
<ParameterBag Id="en" Name="Sex" Type="Enumeration">
<Parameter Id="male" name="Male" Type="String" Value="male" />
<Parameter Id="female" name="Female" Type="String" Value="female" />
<Parameter Id="both" name="Both" Type="String" Value="both" />
</ParameterBag>
</Resource>
<Resource Id="religions" Name="Religions" Type="Enumeration">
<ParameterBag Id="en" Name="Religions" Type="Enumeration">
<Parameter Id="Roman Catholic" name="Roman Catholic" Type="String" Value="Roman Catholic" />
<Parameter Id="Protestant" name="Protestant" Type="String" Value="Protestant" />
<Parameter Id="Orthodox" name="Orthodox" Type="String" Value="Orthodox" />
<Parameter Id="Christian" name="Anglican" Type="String" Value="Anglican" />
<Parameter Id="Muslim" name="Muslim" Type="String" Value="Muslim" />
<Parameter Id="Hindu" name="Hindu" Type="String" Value="Hindu" />
<Parameter Id="Buddhist" name="Buddhist" Type="String" Value="Buddhist" />
<Parameter Id="Jewish" name="Jewish" Type="String" Value="Jewish" />
<Parameter Id="Atheist" name="Atheist" Type="String" Value="Atheist" />
</ParameterBag>
</Resource>
</StrolchModel>

View File

@ -23,6 +23,7 @@
</ParameterBag>
</Order>
<IncludeFile file="Enums.xml" />
<IncludeFile file="Resources.xml" />
<IncludeFile file="Orders.xml" />