diff --git a/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/ClassificationReason.java b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/ClassificationReason.java index c0cbbf6b35..eca5883ad7 100644 --- a/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/ClassificationReason.java +++ b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/ClassificationReason.java @@ -54,7 +54,15 @@ public final class ClassificationReason implements ClassificationSchemeEntity */ public String getId() { - return this.id; + return id; + } + + /** + * Returns the I18N key for the display label for the reason. + */ + public String getDisplayLabelKey() + { + return displayLabelKey; } /** diff --git a/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/ClassificationSchemeEntityFactory.java b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/ClassificationSchemeEntityFactory.java new file mode 100644 index 0000000000..77d3c94de9 --- /dev/null +++ b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/ClassificationSchemeEntityFactory.java @@ -0,0 +1,63 @@ +/* + * Copyright (C) 2005-2015 Alfresco Software Limited. + * + * This file is part of Alfresco + * + * Alfresco is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Alfresco is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Alfresco. If not, see . + */ +package org.alfresco.module.org_alfresco_module_rm.classification; + +import org.json.JSONException; +import org.json.JSONObject; + +/** + * Factory to create classification entities from JSON objects. + * + * @author tpage + * @since 3.0 + */ +public class ClassificationSchemeEntityFactory +{ + /** + * Create the classification entity from the supplied JSON. + * + * @param clazz The class to create. + * @param jsonObject The JSON object from the configuration file. + * @return The new entity. + * @throws JSONException If there is an error in the JSON. + */ + @SuppressWarnings("unchecked") + public T create(Class clazz, JSONObject jsonObject) throws JSONException + { + if (clazz == ClassificationLevel.class) + { + String id = jsonObject.getString("name"); + String displayLabelKey = jsonObject.getString("displayLabel"); + return (T) new ClassificationLevel(id, displayLabelKey); + } + else if (clazz == ClassificationReason.class) + { + String id = jsonObject.getString("id"); + String displayLabelKey = jsonObject.getString("displayLabel"); + return (T) new ClassificationReason(id, displayLabelKey); + } + else if (clazz == ExemptionCategory.class) + { + String id = jsonObject.getString("id"); + String displayLabelKey = jsonObject.getString("displayLabel"); + return (T) new ExemptionCategory(id, displayLabelKey); + } + throw new IllegalStateException("Unsupported entity type: " + clazz); + } +} diff --git a/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/ClassificationServiceBootstrap.java b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/ClassificationServiceBootstrap.java index d771cdd042..9fe69e7b7c 100644 --- a/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/ClassificationServiceBootstrap.java +++ b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/ClassificationServiceBootstrap.java @@ -153,7 +153,7 @@ public class ClassificationServiceBootstrap extends AbstractLifecycleBean implem /** Gets the list (in descending order) of classification levels - as defined in the system configuration. */ private List getConfigurationLevels() { - return classificationServiceDAO.getConfiguredLevels(); + return classificationServiceDAO.getConfiguredValues(ClassificationLevel.class); } private static boolean isEmpty(List l) { return l == null || l.isEmpty(); } @@ -222,7 +222,7 @@ public class ClassificationServiceBootstrap extends AbstractLifecycleBean implem /** Gets the list of classification reasons - as defined and ordered in the system configuration. */ private List getConfigurationReasons() { - return classificationServiceDAO.getConfiguredReasons(); + return classificationServiceDAO.getConfiguredValues(ClassificationReason.class); } /** @@ -280,7 +280,7 @@ public class ClassificationServiceBootstrap extends AbstractLifecycleBean implem /** Gets the list of exemption categories - as defined and ordered in the system configuration. */ private List getConfigurationCategories() { - return classificationServiceDAO.getConfiguredExemptionCategories(); + return classificationServiceDAO.getConfiguredValues(ExemptionCategory.class); } /** diff --git a/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/ClassificationServiceDAO.java b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/ClassificationServiceDAO.java index 4c7be0570d..514cc96bf4 100644 --- a/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/ClassificationServiceDAO.java +++ b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/ClassificationServiceDAO.java @@ -22,7 +22,9 @@ import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.Collections; +import java.util.HashMap; import java.util.List; +import java.util.Map; import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationException.MalformedConfiguration; import org.apache.commons.io.IOUtils; @@ -40,62 +42,37 @@ import org.json.JSONTokener; */ class ClassificationServiceDAO { - private String levelConfigLocation; - private String reasonConfigLocation; - private String exemptionCategoryConfigLocation; + /** A map from the simple name of a POJO type to the corresponding location of the configuration file. */ + private Map configLocations = new HashMap<>(); + + private ClassificationSchemeEntityFactory classificationSchemeEntityFactory = new ClassificationSchemeEntityFactory(); /** Set the location of the level configuration file relative to the classpath. */ - public void setLevelConfigLocation(String levelConfigLocation) { this.levelConfigLocation = levelConfigLocation; } - + public void setLevelConfigLocation(String levelConfigLocation) + { + configLocations.put(ClassificationLevel.class.getSimpleName(), levelConfigLocation); + } /** Set the location of the reasons configuration file relative to the classpath. */ - public void setReasonConfigLocation(String reasonConfigLocation) { this.reasonConfigLocation = reasonConfigLocation; } + public void setReasonConfigLocation(String reasonConfigLocation) + { + configLocations.put(ClassificationReason.class.getSimpleName(), reasonConfigLocation); + } /** Set the location of the exemption categories configuration file relative to the classpath. */ - public void setExemptionCategoryConfigLocation(String exemptionCategoryConfigLocation) { this.exemptionCategoryConfigLocation = exemptionCategoryConfigLocation; } - - /** - * Gets the list (in descending order) of classification levels - as defined in the classpath. - * - * @return the configured classification levels in descending order, or an empty list if there are none. - */ - public List getConfiguredLevels() + public void setExemptionCategoryConfigLocation(String exemptionCategoryConfigLocation) { - List result; - try (final InputStream in = this.getClass().getResourceAsStream(levelConfigLocation)) - { - if (in == null) { result = Collections.emptyList(); } - else - { - final String jsonString = IOUtils.toString(in); - final JSONArray jsonArray = new JSONArray(new JSONTokener(jsonString)); - - result = new ArrayList<>(jsonArray.length()); - - for (int i = 0; i < jsonArray.length(); i++) - { - final JSONObject nextObj = jsonArray.getJSONObject(i); - final String name = nextObj.getString("name"); - final String displayLabelKey = nextObj.getString("displayLabel"); - result.add(new ClassificationLevel(name, displayLabelKey)); - } - } - } - catch (IOException | JSONException e) - { - throw new MalformedConfiguration("Could not read classification level configuration", e); - } - return result; + configLocations.put(ExemptionCategory.class.getSimpleName(), exemptionCategoryConfigLocation); } /** - * Gets the list of classification reasons as defined in the classpath. + * Gets the list of values as defined in the classpath. * - * @return the configured classification reasons in descending order, or an empty list if there are none. + * @return The configured values, or an empty list if there are none. */ - public List getConfiguredReasons() + public List getConfiguredValues(Class clazz) { - List result; - try (final InputStream in = this.getClass().getResourceAsStream(reasonConfigLocation)) + List result; + try (final InputStream in = this.getClass().getResourceAsStream(configLocations.get(clazz.getSimpleName()))) { if (in == null) { result = Collections.emptyList(); } else @@ -108,49 +85,14 @@ class ClassificationServiceDAO for (int i = 0; i < jsonArray.length(); i++) { final JSONObject nextObj = jsonArray.getJSONObject(i); - final String id = nextObj.getString("id"); - final String displayLabelKey = nextObj.getString("displayLabel"); - result.add(new ClassificationReason(id, displayLabelKey)); + result.add(classificationSchemeEntityFactory.create(clazz, nextObj)); } } } catch (IOException | JSONException e) { - throw new MalformedConfiguration("Could not read classification reason configuration", e); - } - return result; - } - - /** - * Gets the list of exemption categories as defined in the classpath. - * - * @return the configured exemption categories in the given order, or an empty list if there are none. - */ - public List getConfiguredExemptionCategories() - { - List result; - try (final InputStream in = this.getClass().getResourceAsStream(exemptionCategoryConfigLocation)) - { - if (in == null) { result = Collections.emptyList(); } - else - { - final String jsonString = IOUtils.toString(in); - final JSONArray jsonArray = new JSONArray(new JSONTokener(jsonString)); - - result = new ArrayList<>(jsonArray.length()); - - for (int i = 0; i < jsonArray.length(); i++) - { - final JSONObject nextObj = jsonArray.getJSONObject(i); - final String id = nextObj.getString("id"); - final String displayLabelKey = nextObj.getString("displayLabel"); - result.add(new ExemptionCategory(id, displayLabelKey)); - } - } - } - catch (IOException | JSONException e) - { - throw new MalformedConfiguration("Could not read exemption category configuration", e); + String message = "Could not read " + clazz.getSimpleName() + " configuration: " + configLocations.get(clazz.getSimpleName()); + throw new MalformedConfiguration(message, e); } return result; } diff --git a/rm-server/unit-test/java/org/alfresco/module/org_alfresco_module_rm/classification/ClassificationSchemeEntityFactoryUnitTest.java b/rm-server/unit-test/java/org/alfresco/module/org_alfresco_module_rm/classification/ClassificationSchemeEntityFactoryUnitTest.java new file mode 100644 index 0000000000..7d568aa312 --- /dev/null +++ b/rm-server/unit-test/java/org/alfresco/module/org_alfresco_module_rm/classification/ClassificationSchemeEntityFactoryUnitTest.java @@ -0,0 +1,84 @@ +/* + * Copyright (C) 2005-2015 Alfresco Software Limited. + * + * This file is part of Alfresco + * + * Alfresco is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Alfresco is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Alfresco. If not, see . + */ +package org.alfresco.module.org_alfresco_module_rm.classification; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.when; +import static org.mockito.MockitoAnnotations.initMocks; + +import org.json.JSONException; +import org.json.JSONObject; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; + + +/** + * Unit tests for the {@link ClassificationSchemeEntityFactory}. + * + * @author tpage + * @since 3.0 + */ +public class ClassificationSchemeEntityFactoryUnitTest +{ + ClassificationSchemeEntityFactory classificationSchemeEntityFactory = new ClassificationSchemeEntityFactory(); + @Mock JSONObject mockJsonObject; + + @Before + public void setUp() + { + initMocks(this); + } + + @Test + public void testCreateClassificationLevel() throws JSONException + { + when(mockJsonObject.getString("name")).thenReturn("name1"); + when(mockJsonObject.getString("displayLabel")).thenReturn("displayLabelKey2"); + + ClassificationLevel classificationLevel = classificationSchemeEntityFactory.create(ClassificationLevel.class, mockJsonObject); + + assertEquals("name1", classificationLevel.getId()); + assertEquals("displayLabelKey2", classificationLevel.getDisplayLabelKey()); + } + + @Test + public void testCreateClassificationReason() throws JSONException + { + when(mockJsonObject.getString("id")).thenReturn("id1"); + when(mockJsonObject.getString("displayLabel")).thenReturn("displayLabelKey2"); + + ClassificationReason classificationReason = classificationSchemeEntityFactory.create(ClassificationReason.class, mockJsonObject); + + assertEquals("id1", classificationReason.getId()); + assertEquals("displayLabelKey2", classificationReason.getDisplayLabelKey()); + } + + @Test + public void testCreateExemptionCategory() throws JSONException + { + when(mockJsonObject.getString("id")).thenReturn("id1"); + when(mockJsonObject.getString("displayLabel")).thenReturn("displayLabelKey2"); + + ExemptionCategory exemptionCategory = classificationSchemeEntityFactory.create(ExemptionCategory.class, mockJsonObject); + + assertEquals("id1", exemptionCategory.getId()); + assertEquals("displayLabelKey2", exemptionCategory.getDisplayLabelKey()); + } +} diff --git a/rm-server/unit-test/java/org/alfresco/module/org_alfresco_module_rm/classification/ClassificationServiceBootstrapUnitTest.java b/rm-server/unit-test/java/org/alfresco/module/org_alfresco_module_rm/classification/ClassificationServiceBootstrapUnitTest.java index 882848ccf6..5dfe8c2028 100644 --- a/rm-server/unit-test/java/org/alfresco/module/org_alfresco_module_rm/classification/ClassificationServiceBootstrapUnitTest.java +++ b/rm-server/unit-test/java/org/alfresco/module/org_alfresco_module_rm/classification/ClassificationServiceBootstrapUnitTest.java @@ -124,7 +124,7 @@ public class ClassificationServiceBootstrapUnitTest @Test public void defaultLevelsConfigurationVanillaSystem() { - when(mockClassificationServiceDAO.getConfiguredLevels()).thenReturn(DEFAULT_CONFIGURED_CLASSIFICATION_LEVELS); + when(mockClassificationServiceDAO.getConfiguredValues(ClassificationLevel.class)).thenReturn(DEFAULT_CONFIGURED_CLASSIFICATION_LEVELS); when(mockAttributeService.getAttribute(anyString(), anyString(), anyString())).thenReturn(null); classificationServiceBootstrap.initConfiguredClassificationLevels(); @@ -135,7 +135,7 @@ public class ClassificationServiceBootstrapUnitTest @Test public void alternativeLevelsConfigurationPreviouslyStartedSystem() { - when(mockClassificationServiceDAO.getConfiguredLevels()).thenReturn(ALT_CLASSIFICATION_LEVELS); + when(mockClassificationServiceDAO.getConfiguredValues(ClassificationLevel.class)).thenReturn(ALT_CLASSIFICATION_LEVELS); when(mockAttributeService.getAttribute(anyString(), anyString(), anyString())) .thenReturn((Serializable) DEFAULT_CLASSIFICATION_LEVELS); @@ -159,7 +159,7 @@ public class ClassificationServiceBootstrapUnitTest when(mockAttributeService.getAttribute(anyString(), anyString(), anyString())).thenReturn(null); // We'll use a small set of placeholder classification reasons. - when(mockClassificationServiceDAO.getConfiguredReasons()).thenReturn(PLACEHOLDER_CLASSIFICATION_REASONS); + when(mockClassificationServiceDAO.getConfiguredValues(ClassificationReason.class)).thenReturn(PLACEHOLDER_CLASSIFICATION_REASONS); classificationServiceBootstrap.initConfiguredClassificationReasons(); @@ -171,7 +171,7 @@ public class ClassificationServiceBootstrapUnitTest { // The classification reasons stored are the same values that are found on the classpath. when(mockAttributeService.getAttribute(anyString(), anyString(), anyString())).thenReturn((Serializable)PLACEHOLDER_CLASSIFICATION_REASONS); - when(mockClassificationServiceDAO.getConfiguredReasons()).thenReturn(PLACEHOLDER_CLASSIFICATION_REASONS); + when(mockClassificationServiceDAO.getConfiguredValues(ClassificationReason.class)).thenReturn(PLACEHOLDER_CLASSIFICATION_REASONS); classificationServiceBootstrap.initConfiguredClassificationReasons(); @@ -192,7 +192,7 @@ public class ClassificationServiceBootstrapUnitTest // The classification reasons stored are different from those found on the classpath. when(mockAttributeService.getAttribute(anyString(), anyString(), anyString())).thenReturn( (Serializable) PLACEHOLDER_CLASSIFICATION_REASONS); - when(mockClassificationServiceDAO.getConfiguredReasons()).thenReturn(ALTERNATIVE_CLASSIFICATION_REASONS); + when(mockClassificationServiceDAO.getConfiguredValues(ClassificationReason.class)).thenReturn(ALTERNATIVE_CLASSIFICATION_REASONS); // Put the mock Appender into the log4j logger and allow warning messages to be received. org.apache.log4j.Logger log4jLogger = org.apache.log4j.Logger.getLogger(ClassificationServiceBootstrap.class); @@ -224,7 +224,7 @@ public class ClassificationServiceBootstrapUnitTest { when(mockAttributeService.getAttribute(anyString(), anyString(), anyString())) .thenReturn((Serializable) null); - when(mockClassificationServiceDAO.getConfiguredReasons()).thenReturn(null); + when(mockClassificationServiceDAO.getConfiguredValues(ClassificationReason.class)).thenReturn(null); classificationServiceBootstrap.initConfiguredClassificationReasons(); } @@ -234,7 +234,7 @@ public class ClassificationServiceBootstrapUnitTest { when(mockAttributeService.getAttribute(anyString(), anyString(), anyString())) .thenReturn((Serializable) null); - when(mockClassificationServiceDAO.getConfiguredExemptionCategories()).thenReturn(EXEMPTION_CATEGORIES); + when(mockClassificationServiceDAO.getConfiguredValues(ExemptionCategory.class)).thenReturn(EXEMPTION_CATEGORIES); classificationServiceBootstrap.initConfiguredExemptionCategories(); @@ -246,7 +246,7 @@ public class ClassificationServiceBootstrapUnitTest { when(mockAttributeService.getAttribute(anyString(), anyString(), anyString())) .thenReturn((Serializable) EXEMPTION_CATEGORIES); - when(mockClassificationServiceDAO.getConfiguredExemptionCategories()).thenReturn(EXEMPTION_CATEGORIES); + when(mockClassificationServiceDAO.getConfiguredValues(ExemptionCategory.class)).thenReturn(EXEMPTION_CATEGORIES); classificationServiceBootstrap.initConfiguredExemptionCategories(); @@ -266,7 +266,7 @@ public class ClassificationServiceBootstrapUnitTest { when(mockAttributeService.getAttribute(anyString(), anyString(), anyString())) .thenReturn((Serializable) EXEMPTION_CATEGORIES); - when(mockClassificationServiceDAO.getConfiguredExemptionCategories()).thenReturn(CHANGED_EXEMPTION_CATEGORIES); + when(mockClassificationServiceDAO.getConfiguredValues(ExemptionCategory.class)).thenReturn(CHANGED_EXEMPTION_CATEGORIES); // Put the mock Appender into the log4j logger and allow warning messages to be received. org.apache.log4j.Logger log4jLogger = org.apache.log4j.Logger.getLogger(ClassificationServiceBootstrap.class); @@ -298,7 +298,7 @@ public class ClassificationServiceBootstrapUnitTest { when(mockAttributeService.getAttribute(anyString(), anyString(), anyString())) .thenReturn((Serializable) null); - when(mockClassificationServiceDAO.getConfiguredReasons()).thenReturn(null); + when(mockClassificationServiceDAO.getConfiguredValues(ExemptionCategory.class)).thenReturn(null); classificationServiceBootstrap.initConfiguredExemptionCategories(); } diff --git a/rm-server/unit-test/java/org/alfresco/module/org_alfresco_module_rm/classification/ClassificationServiceDAOUnitTest.java b/rm-server/unit-test/java/org/alfresco/module/org_alfresco_module_rm/classification/ClassificationServiceDAOUnitTest.java index e2b973a915..acf5abffd1 100644 --- a/rm-server/unit-test/java/org/alfresco/module/org_alfresco_module_rm/classification/ClassificationServiceDAOUnitTest.java +++ b/rm-server/unit-test/java/org/alfresco/module/org_alfresco_module_rm/classification/ClassificationServiceDAOUnitTest.java @@ -45,7 +45,7 @@ public class ClassificationServiceDAOUnitTest { ClassificationServiceDAO c = new ClassificationServiceDAO(); c.setLevelConfigLocation("/alfresco/module/org_alfresco_module_rm/classification/rm-classification-levels.json"); - List config = c.getConfiguredLevels(); + List config = c.getConfiguredValues(ClassificationLevel.class); assertEquals(DEFAULT_CLASSIFICATION_LEVELS, config); } @@ -53,7 +53,7 @@ public class ClassificationServiceDAOUnitTest { ClassificationServiceDAO c = new ClassificationServiceDAO(); c.setLevelConfigLocation("/no/such/resource"); - assertTrue(c.getConfiguredLevels().isEmpty()); + assertTrue(c.getConfiguredValues(ClassificationLevel.class).isEmpty()); } @Test (expected = MalformedConfiguration.class) @@ -61,14 +61,14 @@ public class ClassificationServiceDAOUnitTest { ClassificationServiceDAO c = new ClassificationServiceDAO(); c.setLevelConfigLocation("/alfresco/classification/rm-classification-levels-malformed.json"); - c.getConfiguredLevels(); + c.getConfiguredValues(ClassificationLevel.class); } @Test public void getConfiguredReasons_readingDefaultConfigurationShouldWork() { ClassificationServiceDAO c = new ClassificationServiceDAO(); c.setReasonConfigLocation("/alfresco/module/org_alfresco_module_rm/classification/rm-classification-reasons.json"); - List config = c.getConfiguredReasons(); + List config = c.getConfiguredValues(ClassificationReason.class); assertFalse(config.isEmpty()); } @@ -76,7 +76,7 @@ public class ClassificationServiceDAOUnitTest { ClassificationServiceDAO c = new ClassificationServiceDAO(); c.setReasonConfigLocation("/no/such/resource"); - assertTrue(c.getConfiguredReasons().isEmpty()); + assertTrue(c.getConfiguredValues(ClassificationReason.class).isEmpty()); } @Test (expected = MalformedConfiguration.class) @@ -84,6 +84,6 @@ public class ClassificationServiceDAOUnitTest { ClassificationServiceDAO c = new ClassificationServiceDAO(); c.setReasonConfigLocation("/alfresco/classification/rm-classification-levels-malformed.json"); - c.getConfiguredReasons(); + c.getConfiguredValues(ClassificationReason.class); } }