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,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<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; }
/**
* 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<ClassificationLevel> getConfiguredLevels()
public void setExemptionCategoryConfigLocation(String exemptionCategoryConfigLocation)
{
List<ClassificationLevel> 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<ClassificationReason> getConfiguredReasons()
public <T extends ClassificationSchemeEntity> List<T> getConfiguredValues(Class<T> clazz)
{
List<ClassificationReason> result;
try (final InputStream in = this.getClass().getResourceAsStream(reasonConfigLocation))
List<T> 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<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;
}