RM-2130 (Check classification after method execution, filtering results where appropriate)

+review RM-94

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/modules/recordsmanagement/DEV/ENFORCE@106657 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Tuna Aksoy
2015-06-21 19:47:57 +00:00
parent 3772764f8e
commit 862fde1e61
5 changed files with 306 additions and 6 deletions

View File

@@ -37,6 +37,14 @@
<bean id="preMethodInvocationProcessor" <bean id="preMethodInvocationProcessor"
class="org.alfresco.module.org_alfresco_module_rm.classification.interceptor.processor.PreMethodInvocationProcessor"> class="org.alfresco.module.org_alfresco_module_rm.classification.interceptor.processor.PreMethodInvocationProcessor">
<!--
<property name="transactionService" ref="transactionService" />
<property name="classificationServiceBootstrap" ref="classificationServiceBootstrap" />
<property name="alfrescoTransactionSupport" ref="rm.alfrescoTransactionSupport" />
<property name="nodeService" ref="dbNodeService" />
<property name="dictionaryService" ref="dictionaryService" />
<property name="contentClassificationService" ref="contentClassificationService" />
-->
</bean> </bean>
<bean id="postMethodInvocationProcessor" <bean id="postMethodInvocationProcessor"
@@ -106,6 +114,11 @@
class="org.alfresco.module.org_alfresco_module_rm.classification.interceptor.processor.ListPostMethodInvocationProcessor"> class="org.alfresco.module.org_alfresco_module_rm.classification.interceptor.processor.ListPostMethodInvocationProcessor">
</bean> </bean>
<bean id="arrayPostMethodInvocationProcessor"
parent="abstractPostMethodInvocationProcessor"
class="org.alfresco.module.org_alfresco_module_rm.classification.interceptor.processor.ArrayPostMethodInvocationProcessor">
</bean>
<!-- Classification service DAO --> <!-- Classification service DAO -->
<bean id="classificationServiceDAO" class="org.alfresco.module.org_alfresco_module_rm.classification.ClassificationServiceDAO"> <bean id="classificationServiceDAO" class="org.alfresco.module.org_alfresco_module_rm.classification.ClassificationServiceDAO">

View File

@@ -0,0 +1,87 @@
/*
* 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.interceptor.processor;
import static java.lang.reflect.Array.newInstance;
import static org.alfresco.util.ParameterCheck.mandatory;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
/**
* Array Post Method Invocation Processor
*
* @author Tuna Aksoy
* @since 3.0
*/
public class ArrayPostMethodInvocationProcessor extends BasePostMethodInvocationProcessor
{
/**
* @see org.alfresco.module.org_alfresco_module_rm.classification.interceptor.processor.BasePostMethodInvocationProcessor#getClassName()
*/
@Override
protected Class<Array> getClassName()
{
return Array.class;
}
/**
* @see org.alfresco.module.org_alfresco_module_rm.classification.interceptor.processor.BasePostMethodInvocationProcessor#process(java.lang.Object)
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public <T> T process(T object)
{
mandatory("object", object);
T result = object;
T[] objects = (T[]) result;
T obj = objects[0];
BasePostMethodInvocationProcessor processor = getPostMethodInvocationProcessor().getProcessor(obj);
if (processor != null)
{
int length = objects.length;
List processedObjects = new ArrayList();
for (int i = 0; i < length; i++)
{
Object processedObject = processor.process(objects[i]);
if (processedObject != null)
{
processedObjects.add(processedObject);
}
}
int size = processedObjects.size();
T[] objs = (T[]) newInstance(obj.getClass(), size);
for (int i = 0; i < size; i++)
{
objs[i] = (T) processedObjects.get(i);
}
result = (T) objs;
}
return result;
}
}

View File

@@ -20,6 +20,7 @@ package org.alfresco.module.org_alfresco_module_rm.classification.interceptor.pr
import static org.alfresco.util.ParameterCheck.mandatory; import static org.alfresco.util.ParameterCheck.mandatory;
import java.lang.reflect.Array;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
@@ -76,13 +77,21 @@ public class PostMethodInvocationProcessor
BasePostMethodInvocationProcessor result = null; BasePostMethodInvocationProcessor result = null;
Class<? extends Object> clazz = object.getClass(); Class<? extends Object> clazz = object.getClass();
Set<Entry<Class<?>, BasePostMethodInvocationProcessor>> processorsEntrySet = getProcessors().entrySet(); if (clazz.isArray())
for (Map.Entry<Class<?>, BasePostMethodInvocationProcessor> processorEntry : processorsEntrySet)
{ {
if (processorEntry.getKey().isAssignableFrom(clazz)) result = getProcessors().get(Array.class);
}
if (result == null)
{
Set<Entry<Class<?>, BasePostMethodInvocationProcessor>> processorsEntrySet = getProcessors().entrySet();
for (Map.Entry<Class<?>, BasePostMethodInvocationProcessor> processorEntry : processorsEntrySet)
{ {
result = processorEntry.getValue(); if (processorEntry.getKey().isAssignableFrom(clazz))
break; {
result = processorEntry.getValue();
break;
}
} }
} }

View File

@@ -48,6 +48,7 @@ import org.springframework.context.ApplicationContextAware;
*/ */
public class PreMethodInvocationProcessor implements ApplicationContextAware public class PreMethodInvocationProcessor implements ApplicationContextAware
{ {
/** Key to mark the transaction as processing */
private static final String KEY_PROCESSING = generate(); private static final String KEY_PROCESSING = generate();
private ApplicationContext applicationContext; private ApplicationContext applicationContext;
@@ -88,6 +89,120 @@ public class PreMethodInvocationProcessor implements ApplicationContextAware
return (DictionaryService)applicationContext.getBean("dictionaryService"); return (DictionaryService)applicationContext.getBean("dictionaryService");
} }
// /** Transaction service */
// private TransactionService transactionService;
//
// /** Classification service bootstrap */
// private ClassificationServiceBootstrap classificationServiceBootstrap;
//
// /** Alfresco transaction support */
// private AlfrescoTransactionSupport alfrescoTransactionSupport;
//
// /** Node service */
// private NodeService nodeService;
//
// /** Dictionary service */
// private DictionaryService dictionaryService;
//
// /** Content classification service */
// private ContentClassificationService contentClassificationService;
//
// /**
// * @return the transactionService
// */
// protected TransactionService getTransactionService()
// {
// return this.transactionService;
// }
//
// /**
// * @return the classificationServiceBootstrap
// */
// protected ClassificationServiceBootstrap getClassificationServiceBootstrap()
// {
// return this.classificationServiceBootstrap;
// }
//
// /**
// * @return the alfrescoTransactionSupport
// */
// protected AlfrescoTransactionSupport getAlfrescoTransactionSupport()
// {
// return this.alfrescoTransactionSupport;
// }
//
// /**
// * @return the nodeService
// */
// protected NodeService getNodeService()
// {
// return this.nodeService;
// }
//
// /**
// * @return the dictionaryService
// */
// protected DictionaryService getDictionaryService()
// {
// return this.dictionaryService;
// }
//
// /**
// * @return the contentClassificationService
// */
// protected ContentClassificationService getContentClassificationService()
// {
// return this.contentClassificationService;
// }
//
// /**
// * @param transactionService the transactionService to set
// */
// public void setTransactionService(TransactionService transactionService)
// {
// this.transactionService = transactionService;
// }
//
// /**
// * @param classificationServiceBootstrap the classificationServiceBootstrap to set
// */
// public void setClassificationServiceBootstrap(ClassificationServiceBootstrap classificationServiceBootstrap)
// {
// this.classificationServiceBootstrap = classificationServiceBootstrap;
// }
//
// /**
// * @param alfrescoTransactionSupport the alfrescoTransactionSupport to set
// */
// public void setAlfrescoTransactionSupport(AlfrescoTransactionSupport alfrescoTransactionSupport)
// {
// this.alfrescoTransactionSupport = alfrescoTransactionSupport;
// }
//
// /**
// * @param nodeService the nodeService to set
// */
// public void setNodeService(NodeService nodeService)
// {
// this.nodeService = nodeService;
// }
//
// /**
// * @param dictionaryService the dictionaryService to set
// */
// public void setDictionaryService(DictionaryService dictionaryService)
// {
// this.dictionaryService = dictionaryService;
// }
//
// /**
// * @param contentClassificationService the contentClassificationService to set
// */
// public void setContentClassificationService(ContentClassificationService contentClassificationService)
// {
// this.contentClassificationService = contentClassificationService;
// }
/** /**
* Checks if the current user is cleared to see the items * Checks if the current user is cleared to see the items
* passed as parameters to the current method invocation. * passed as parameters to the current method invocation.
@@ -100,7 +215,7 @@ public class PreMethodInvocationProcessor implements ApplicationContextAware
mandatory("invocation", invocation); mandatory("invocation", invocation);
// do in transaction // do in transaction
return getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Boolean>() return /*getTransactionService().*/getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Boolean>()
{ {
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
public Boolean execute() throws Throwable public Boolean execute() throws Throwable

View File

@@ -0,0 +1,76 @@
/*
* 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.interceptor.processor;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.when;
import org.alfresco.module.org_alfresco_module_rm.classification.ContentClassificationService;
import org.alfresco.module.org_alfresco_module_rm.test.util.BaseUnitTest;
import org.alfresco.service.cmr.repository.NodeRef;
import org.apache.commons.lang3.ArrayUtils;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
/**
* Array Post Method Invocation Processor Unit Test
*
* @author Tuna Aksoy
* @since 3.0
*/
public class ArrayPostMethodInvocationProcessorUnitTest extends BaseUnitTest
{
@InjectMocks ArrayPostMethodInvocationProcessor arrayPostMethodInvocationProcessor;
@Mock private ContentClassificationService mockedContentClassificationService;
@Mock private PostMethodInvocationProcessor mockedPostMethodInvocationProcessor;
@Test
public void testArrayPostMethodInvocationProcessor()
{
NodeRefPostMethodInvocationProcessor processor = new NodeRefPostMethodInvocationProcessor();
processor.setNodeService(mockedNodeService);
processor.setDictionaryService(mockedDictionaryService);
processor.setContentClassificationService(mockedContentClassificationService);
NodeRef nodeRef1 = generateNodeRef();
NodeRef nodeRef2 = generateNodeRef();
NodeRef nodeRef3 = generateNodeRef();
NodeRef nodeRef4 = generateNodeRef();
when(mockedDictionaryService.isSubClass(mockedNodeService.getType(nodeRef1), TYPE_CONTENT)).thenReturn(true);
when(mockedDictionaryService.isSubClass(mockedNodeService.getType(nodeRef2), TYPE_CONTENT)).thenReturn(true);
when(mockedDictionaryService.isSubClass(mockedNodeService.getType(nodeRef3), TYPE_CONTENT)).thenReturn(true);
when(mockedDictionaryService.isSubClass(mockedNodeService.getType(nodeRef4), TYPE_CONTENT)).thenReturn(true);
when(mockedContentClassificationService.hasClearance(nodeRef1)).thenReturn(true);
when(mockedContentClassificationService.hasClearance(nodeRef2)).thenReturn(false);
when(mockedContentClassificationService.hasClearance(nodeRef3)).thenReturn(true);
when(mockedContentClassificationService.hasClearance(nodeRef4)).thenReturn(false);
when(mockedPostMethodInvocationProcessor.getProcessor(Mockito.any())).thenReturn(processor);
NodeRef[] nodes = new NodeRef[] { nodeRef1, nodeRef2, nodeRef3, nodeRef4 };
NodeRef[] processedNodes = arrayPostMethodInvocationProcessor.process(nodes);
assertEquals(2, processedNodes.length);
assertTrue(ArrayUtils.contains(processedNodes, nodeRef1));
assertTrue(ArrayUtils.contains(processedNodes, nodeRef3));
}
}