Implementation of RM-2614 +review RM

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/modules/recordsmanagement/DEV/caveatmarkdatatype@114954 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Neil McErlean
2015-10-22 13:35:46 +00:00
parent 131eef36b1
commit 5cade63f9d
12 changed files with 151 additions and 5 deletions

View File

@@ -17,6 +17,7 @@
<bean id="caveatDAOFromJSON" class="org.alfresco.module.org_alfresco_module_rm.caveat.dao.CaveatDAOFromJSON">
<property name="configLocation" value="${rm.caveat.configFile}" />
<property name="namespaceService" ref="namespaceService" />
</bean>
<bean id="caveatDAO" class="org.alfresco.module.org_alfresco_module_rm.caveat.dao.CaveatDAOCache">

View File

@@ -4,6 +4,10 @@
"displayLabel" : "rm.caveat.classification.label",
"description" : "rm.caveat.classification.description",
"type" : "HIERARCHICAL",
"model" :
{
"property" : "clf:classification"
},
"marks" :
[
{

View File

@@ -23,6 +23,9 @@ import com.google.common.collect.ImmutableMap;
import org.alfresco.module.org_alfresco_module_rm.caveat.CaveatException.CaveatGroupNotFound;
import org.alfresco.module.org_alfresco_module_rm.caveat.scheme.CaveatGroup;
import org.alfresco.service.namespace.QName;
import java.util.Map;
/**
* A cache that ensures the underlying caveat DAO is only executed once per query.
@@ -62,6 +65,37 @@ public class CaveatDAOCache implements CaveatDAOInterface
return caveatGroup;
}
@Override public QName getCaveatGroupProperty(String caveatGroupId)
{
ensureCachePopulated();
return getGroupById(caveatGroupId).getModelProperty();
}
@Override public CaveatGroup getCaveatGroupFromProperty(QName propertyName)
{
ensureCachePopulated();
CaveatGroup matchingGroup = null;
for (Map.Entry<String, CaveatGroup> entry : caveatGroups.entrySet())
{
final CaveatGroup potentialMatch = entry.getValue();
if (propertyName.equals(getCaveatGroupProperty(potentialMatch.getId())))
{
matchingGroup = potentialMatch;
break;
}
}
if (matchingGroup == null)
{
throw new CaveatGroupNotFound("No group found for property '" + propertyName + "'");
}
else
{
return matchingGroup;
}
}
/** The first call to this method will populate the cache, subsequent calls will do nothing. */
private void ensureCachePopulated()
{

View File

@@ -19,11 +19,14 @@
package org.alfresco.module.org_alfresco_module_rm.caveat.dao;
import static org.alfresco.service.namespace.QName.createQName;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.google.common.collect.ImmutableMap;
@@ -33,6 +36,8 @@ import org.alfresco.module.org_alfresco_module_rm.caveat.CaveatException.Malform
import org.alfresco.module.org_alfresco_module_rm.caveat.scheme.CaveatGroup;
import org.alfresco.module.org_alfresco_module_rm.caveat.scheme.CaveatGroupType;
import org.alfresco.module.org_alfresco_module_rm.caveat.scheme.CaveatMark;
import org.alfresco.service.namespace.NamespaceService;
import org.alfresco.service.namespace.QName;
import org.apache.commons.io.IOUtils;
import org.json.JSONArray;
import org.json.JSONException;
@@ -57,6 +62,10 @@ public class CaveatDAOFromJSON implements CaveatDAOInterface
private static final String DESCRIPTION_JSON_KEY = "description";
/** JSON key for the group type. */
private static final String TYPE_JSON_KEY = "type";
/** JSON key for the model object. */
private static final String MODEL_JSON_KEY = "model";
/** JSON key for the property field. */
private static final String PROPERTY_JSON_KEY = "property";
/** JSON key for the caveat marks array. */
private static final String MARKS_JSON_KEY = "marks";
/** JSON key for the mark id. */
@@ -69,12 +78,19 @@ public class CaveatDAOFromJSON implements CaveatDAOInterface
/** The location of the configuration file relative to the classpath. */
private String configLocation;
private NamespaceService namespaceService;
/** Set the location of the configuration file relative to the classpath. */
public void setConfigLocation(String configLocation)
{
this.configLocation = configLocation;
}
public void setNamespaceService(NamespaceService service)
{
this.namespaceService = service;
}
/**
* {@inheritDoc}
*
@@ -137,6 +153,24 @@ public class CaveatDAOFromJSON implements CaveatDAOInterface
return caveatGroup;
}
@Override public QName getCaveatGroupProperty(String caveatGroupId)
{
return getGroupById(caveatGroupId).getModelProperty();
}
@Override public CaveatGroup getCaveatGroupFromProperty(QName propertyName)
{
// FIXME Do we need any validation to ensure that multiple caveat groups don't reuse the same property?
for (Map.Entry<String, CaveatGroup> entry : getCaveatGroups().entrySet())
{
if (propertyName.equals(entry.getValue().getModelProperty()))
{
return entry.getValue();
}
}
throw new CaveatGroupNotFound("Caveat Group not found for property '" + propertyName + "'");
}
/**
* Create a caveat group from the supplied JSON.
*
@@ -149,6 +183,15 @@ public class CaveatDAOFromJSON implements CaveatDAOInterface
String id = jsonGroup.getString(GROUP_ID_JSON_KEY);
String displayLabelKey = jsonGroup.getString(GROUP_DISPLAY_LABEL_JSON_KEY);
String descriptionKey = jsonGroup.getString(DESCRIPTION_JSON_KEY);
String modelProperty = null;
if (jsonGroup.has(MODEL_JSON_KEY))
{
JSONObject modelObj = jsonGroup.getJSONObject(MODEL_JSON_KEY);
if (modelObj.has(PROPERTY_JSON_KEY))
{
modelProperty = modelObj.getString(PROPERTY_JSON_KEY);
}
}
String caveatGroupTypeString = jsonGroup.getString(TYPE_JSON_KEY);
CaveatGroupType caveatGroupType;
try
@@ -180,7 +223,9 @@ public class CaveatDAOFromJSON implements CaveatDAOInterface
}
// Instantiate the group (and associate the marks with the group).
CaveatGroup caveatGroup = new CaveatGroup(id, displayLabelKey, descriptionKey, caveatGroupType, caveatMarks);
CaveatGroup caveatGroup = new CaveatGroup(id, displayLabelKey, descriptionKey,
modelProperty == null ? null : createQName(modelProperty, namespaceService),
caveatGroupType, caveatMarks);
return caveatGroup;
}

View File

@@ -22,6 +22,7 @@ package org.alfresco.module.org_alfresco_module_rm.caveat.dao;
import com.google.common.collect.ImmutableMap;
import org.alfresco.module.org_alfresco_module_rm.caveat.CaveatException.CaveatGroupNotFound;
import org.alfresco.module.org_alfresco_module_rm.caveat.scheme.CaveatGroup;
import org.alfresco.service.namespace.QName;
/**
* An object responsible for providing access to the configured caveat groups and marks.
@@ -44,4 +45,21 @@ public interface CaveatDAOInterface
* @throws CaveatGroupNotFound if the caveat group is not found.
*/
CaveatGroup getGroupById(String groupId) throws CaveatGroupNotFound;
/**
* Gets the property that relates to a {@link CaveatGroup}.
*
* @param caveatGroupId
* @return
* @throws CaveatGroupNotFound if a matching {@link CaveatGroup} could not be found.
*/
QName getCaveatGroupProperty(String caveatGroupId);
/**
* Gets the {@link CaveatGroup} that relates to a property.
*
* @return the matching {@link CaveatGroup} if there is one.
* @throws CaveatGroupNotFound if there was no matching group.
*/
CaveatGroup getCaveatGroupFromProperty(QName propertyName);
}

View File

@@ -30,6 +30,7 @@ import com.google.common.collect.ImmutableMap.Builder;
import org.alfresco.module.org_alfresco_module_rm.caveat.CaveatException.CaveatMarkNotFound;
import org.alfresco.module.org_alfresco_module_rm.util.CoreServicesExtras;
import org.alfresco.module.org_alfresco_module_rm.util.RMParameterCheck;
import org.alfresco.service.namespace.QName;
import org.apache.commons.lang.StringUtils;
import org.springframework.extensions.surf.util.ParameterCheck;
@@ -49,6 +50,8 @@ public class CaveatGroup implements Serializable
private final String displayLabelKey;
/** The key to retrieve the I18n'ed description of the caveat group. */
private final String descriptionKey;
/** The model property in which this caveat group will store marks on objects. */
private final QName modelProperty;
/** The relationship between marks in the group. */
private final CaveatGroupType caveatGroupType;
/** The marks that are contained in this group, ordered according to the list supplied in the constructor. */
@@ -64,9 +67,9 @@ public class CaveatGroup implements Serializable
* @param caveatGroupType The relationship between marks in the group.
* @param caveatMarks The marks that are contained in this group.
*/
public CaveatGroup(String id, CaveatGroupType caveatGroupType, List<CaveatMark> caveatMarks)
public CaveatGroup(String id, QName modelProperty, CaveatGroupType caveatGroupType, List<CaveatMark> caveatMarks)
{
this(id, null, null, caveatGroupType, caveatMarks);
this(id, null, null, modelProperty, caveatGroupType, caveatMarks);
}
/**
@@ -82,8 +85,9 @@ public class CaveatGroup implements Serializable
* @param caveatGroupType The relationship between marks in the group.
* @param caveatMarks The marks that are contained in this group.
*/
public CaveatGroup(String id, String displayLabelKey, String descriptionKey, CaveatGroupType caveatGroupType,
List<CaveatMark> caveatMarks)
public CaveatGroup(String id, String displayLabelKey, String descriptionKey,
QName modelProperty, CaveatGroupType caveatGroupType,
List<CaveatMark> caveatMarks)
{
RMParameterCheck.checkNotBlank("id", id);
ParameterCheck.mandatory("caveatGroupType", caveatGroupType);
@@ -101,6 +105,7 @@ public class CaveatGroup implements Serializable
this.id = id;
this.displayLabelKey = displayLabelKey;
this.descriptionKey = descriptionKey;
this.modelProperty = modelProperty;
this.caveatGroupType = caveatGroupType;
for (CaveatMark caveatMark : caveatMarks)
{
@@ -149,6 +154,12 @@ public class CaveatGroup implements Serializable
return CoreServicesExtras.getI18NMessageOrKey(descriptionKey);
}
/** Gets the content model property in which the caveat marks will be recorded. */
public QName getModelProperty()
{
return modelProperty;
}
/**
* Indicates how the marks in the caveat groups are related.
*/

View File

@@ -21,11 +21,16 @@ package org.alfresco.module.org_alfresco_module_rm.caveat.dao;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.Map;
import org.alfresco.module.org_alfresco_module_rm.caveat.CaveatException.MalformedConfiguration;
import org.alfresco.module.org_alfresco_module_rm.caveat.scheme.CaveatGroup;
import org.alfresco.service.namespace.NamespaceService;
import org.junit.Before;
import org.junit.Test;
/**
@@ -39,6 +44,14 @@ public class CaveatDAOFromJSONUnitTest
/** The class under test. */
CaveatDAOFromJSON caveatDAOFromJSON = new CaveatDAOFromJSON();
@Before public void initClassUnderTest()
{
NamespaceService namespaceService = mock(NamespaceService.class);
when(namespaceService.getNamespaceURI(anyString())).thenReturn("{mockedNamespace}");
caveatDAOFromJSON.setNamespaceService(namespaceService);
}
/** Test that loading the default caveat configuration file doesn't throw any exceptions. */
@Test
public void testGetCaveatGroups_defaultConfiguration()

View File

@@ -4,6 +4,10 @@
"displayLabel" : "rm.caveat.classification.group",
"description" : "rm.caveat.classification.description",
"type" : "HIERARCHICAL",
"model" :
{
"property" : "clf:classification"
},
"marks" :
[
{

View File

@@ -4,6 +4,10 @@
"displayLabel" : "rm.caveat.classification.group",
"description" : "rm.caveat.classification.description",
"type" : "HIERARCHICAL",
"model" :
{
"property" : "clf:classification"
},
"marks" :
[
{

View File

@@ -4,6 +4,10 @@
"displayLabel" : "rm.caveat.classification.group",
"description" : "rm.caveat.classification.description",
"type" : "HIERARCHICAL",
"model" :
{
"property" : "clf:classification"
},
"marks" :
[
{

View File

@@ -4,6 +4,10 @@
"displayLabel" : "rm.caveat.classification.group",
"description" : "rm.caveat.classification.description",
"type" : "HIERARCHICAL",
"model" :
{
"property" : "clf:classification"
},
"marks" :
[[
{

View File

@@ -4,6 +4,10 @@
"displayLabel" : "rm.caveat.classification.group",
"description" : "rm.caveat.classification.description",
"type" : "HIERARCHICAL",
"model" :
{
"property" : "clf:classification"
},
"marks" :
[
{