RM-2319 Refactor ClassificationServiceDAO.

All the config loading is now done by a single method using a factory to
create the individual entities.

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/modules/recordsmanagement/HEAD@106526 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Tom Page
2015-06-19 09:52:07 +00:00
parent cbe6ca551e
commit 88ee670c81
7 changed files with 199 additions and 102 deletions

View File

@@ -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;
}
/**

View File

@@ -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 <http://www.gnu.org/licenses/>.
*/
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 extends ClassificationSchemeEntity> T create(Class<T> 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);
}
}

View File

@@ -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<ClassificationLevel> 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<ClassificationReason> 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<ExemptionCategory> getConfigurationCategories()
{
return classificationServiceDAO.getConfiguredExemptionCategories();
return classificationServiceDAO.getConfiguredValues(ExemptionCategory.class);
}
/**

View File

@@ -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,28 +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<String, String> 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; }
public void setExemptionCategoryConfigLocation(String exemptionCategoryConfigLocation)
{
configLocations.put(ExemptionCategory.class.getSimpleName(), exemptionCategoryConfigLocation);
}
/**
* Gets the list (in descending order) of classification levels - as defined in the classpath.
* Gets the list of values as defined in the classpath.
*
* @return the configured classification levels 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<ClassificationLevel> getConfiguredLevels()
public <T extends ClassificationSchemeEntity> List<T> getConfiguredValues(Class<T> clazz)
{
List<ClassificationLevel> result;
try (final InputStream in = this.getClass().getResourceAsStream(levelConfigLocation))
List<T> result;
try (final InputStream in = this.getClass().getResourceAsStream(configLocations.get(clazz.getSimpleName())))
{
if (in == null) { result = Collections.emptyList(); }
else
@@ -74,83 +85,14 @@ class ClassificationServiceDAO
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));
result.add(classificationSchemeEntityFactory.create(clazz, nextObj));
}
}
}
catch (IOException | JSONException e)
{
throw new MalformedConfiguration("Could not read classification level configuration", e);
}
return result;
}
/**
* Gets the list of classification reasons as defined in the classpath.
*
* @return the configured classification reasons in descending order, or an empty list if there are none.
*/
public List<ClassificationReason> getConfiguredReasons()
{
List<ClassificationReason> result;
try (final InputStream in = this.getClass().getResourceAsStream(reasonConfigLocation))
{
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 ClassificationReason(id, displayLabelKey));
}
}
}
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<ExemptionCategory> getConfiguredExemptionCategories()
{
List<ExemptionCategory> 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;
}

View File

@@ -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 <http://www.gnu.org/licenses/>.
*/
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());
}
}

View File

@@ -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();
}

View File

@@ -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<ClassificationLevel> config = c.getConfiguredLevels();
List<ClassificationLevel> 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<ClassificationReason> config = c.getConfiguredReasons();
List<ClassificationReason> 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);
}
}