ACE-3083: Fixed facets reordering/retrieving when a default facet is removed from the config file, but it has already been persisted.

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@87764 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Jamal Kaabi-Mofrad
2014-10-10 16:31:50 +00:00
parent 36c3cb3a04
commit 76361fbf61
3 changed files with 59 additions and 9 deletions

View File

@@ -24,7 +24,6 @@ import java.util.List;
import org.alfresco.repo.dictionary.Facetable; import org.alfresco.repo.dictionary.Facetable;
import org.alfresco.repo.search.impl.solr.facet.Exceptions.DuplicateFacetId; import org.alfresco.repo.search.impl.solr.facet.Exceptions.DuplicateFacetId;
import org.alfresco.repo.search.impl.solr.facet.Exceptions.MissingFacetId; import org.alfresco.repo.search.impl.solr.facet.Exceptions.MissingFacetId;
import org.alfresco.repo.search.impl.solr.facet.Exceptions.UnrecognisedFacetId;
import org.alfresco.service.cmr.dictionary.PropertyDefinition; import org.alfresco.service.cmr.dictionary.PropertyDefinition;
import org.alfresco.service.cmr.repository.NodeRef; import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.namespace.QName; import org.alfresco.service.namespace.QName;
@@ -99,7 +98,6 @@ public interface SolrFacetService
* @param filterIds an ordered sequence of filter IDs. * @param filterIds an ordered sequence of filter IDs.
* @throws NullPointerException if filterIds is {@code null}. * @throws NullPointerException if filterIds is {@code null}.
* @throws MissingFacetId if the list is empty. * @throws MissingFacetId if the list is empty.
* @throws UnrecognisedFacetId if any of the provided filter IDs are not recognised.
* @throws DuplicateFacetId if there is a duplicate filter ID in the list. * @throws DuplicateFacetId if there is a duplicate filter ID in the list.
*/ */
public void reorderFacets(List<String> filterIds); public void reorderFacets(List<String> filterIds);

View File

@@ -48,7 +48,6 @@ import org.alfresco.repo.policy.PolicyComponent;
import org.alfresco.repo.search.impl.solr.facet.Exceptions.DuplicateFacetId; import org.alfresco.repo.search.impl.solr.facet.Exceptions.DuplicateFacetId;
import org.alfresco.repo.search.impl.solr.facet.Exceptions.IllegalArgument; import org.alfresco.repo.search.impl.solr.facet.Exceptions.IllegalArgument;
import org.alfresco.repo.search.impl.solr.facet.Exceptions.MissingFacetId; import org.alfresco.repo.search.impl.solr.facet.Exceptions.MissingFacetId;
import org.alfresco.repo.search.impl.solr.facet.Exceptions.UnrecognisedFacetId;
import org.alfresco.repo.search.impl.solr.facet.SolrFacetProperties.CustomProperties; import org.alfresco.repo.search.impl.solr.facet.SolrFacetProperties.CustomProperties;
import org.alfresco.repo.security.authentication.AuthenticationUtil; import org.alfresco.repo.security.authentication.AuthenticationUtil;
import org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork; import org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork;
@@ -588,8 +587,44 @@ public class SolrFacetServiceImpl extends AbstractLifecycleBean implements SolrF
// Persisted facets // Persisted facets
Map<String, SolrFacetProperties> persistedProperties = getPersistedFacetProperties(); Map<String, SolrFacetProperties> persistedProperties = getPersistedFacetProperties();
// The persisted facets will override the default facets for(Entry<String, SolrFacetProperties> entry : persistedProperties.entrySet())
mergedMap.putAll(persistedProperties); {
final String facetId = entry.getKey();
/*
* If the default facet has been removed from the config file and
* the facet was persisted as its property was modified, then, the
* persisted node needs to be deleted. This should be done to avoid
* errors when loading the facets. Also, as all the properties of
* the facet may not have been persisted and the default facet
* doesn't exist anymore, there is no way of merging the
* non-persisted properties.
*/
if(entry.getValue().isDefault() && !defaultFP.containsKey(facetId))
{
AuthenticationUtil.runAs(new RunAsWork<Void>()
{
@Override
public Void doWork() throws Exception
{
return retryingTransactionHelper.doInTransaction(new RetryingTransactionCallback<Void>()
{
public Void execute() throws Exception
{
deleteFacet(facetId);
logger.info("Deleted [" + facetId + "] node, as the filter has been removed from the config file!");
return null;
}
}, false);
}
}, AuthenticationUtil.getSystemUserName());
}
else
{
// The persisted facets will override the default facets
mergedMap.put(facetId, entry.getValue());
}
}
List<String> facetOrder = getFacetOrder(); List<String> facetOrder = getFacetOrder();
// Sort the merged maps // Sort the merged maps
@@ -736,13 +771,16 @@ public class SolrFacetServiceImpl extends AbstractLifecycleBean implements SolrF
final List<SolrFacetProperties> existingFacets = getFacets(); final List<SolrFacetProperties> existingFacets = getFacets();
final Map<String, SolrFacetProperties> sortedFacets = new LinkedHashMap<>(); // maintains insertion order final Map<String, SolrFacetProperties> sortedFacets = new LinkedHashMap<>(); // maintains insertion order
final List<String> removedFacetIds = new ArrayList<>();
for (String facetId : facetIds) for (String facetId : facetIds)
{ {
SolrFacetProperties facet = getFacet(facetId); SolrFacetProperties facet = getFacet(facetId);
if (facet == null) if (facet == null)
{ {
throw new UnrecognisedFacetId("Cannot reorder facets as ID not recognised:", facetId); // ACE-3083
logger.warn("Facet with [" + facetId + "] ID does not exist. Removing it from the facets' ordering list");
removedFacetIds.add(facetId);
} }
else if (sortedFacets.containsKey(facetId)) else if (sortedFacets.containsKey(facetId))
{ {
@@ -765,6 +803,14 @@ public class SolrFacetServiceImpl extends AbstractLifecycleBean implements SolrF
// The alternative is changing the service API to look like <T extends Serializable & List<String>> // The alternative is changing the service API to look like <T extends Serializable & List<String>>
// which is a bit verbose for an API. // which is a bit verbose for an API.
ArrayList<String> serializableProp = new ArrayList<>(facetIds); ArrayList<String> serializableProp = new ArrayList<>(facetIds);
if (removedFacetIds.size() > 0)
{
boolean result = serializableProp.removeAll(removedFacetIds);
if (result)
{
logger.info("Removed " + removedFacetIds + " from the facets' ordering list.");
}
}
nodeService.setProperty(getFacetsRoot(), SolrFacetModel.PROP_FACET_ORDER, serializableProp); nodeService.setProperty(getFacetsRoot(), SolrFacetModel.PROP_FACET_ORDER, serializableProp);
} }
} }

View File

@@ -163,17 +163,23 @@ public class SolrFacetServiceImplTest
}); });
} }
@Test(expected=UnrecognisedFacetId.class) @Test
public void reorderUnrecognisedFacetIdsShouldFail() throws Exception public void reorderUnrecognisedFacetIdsShouldFail() throws Exception
{ {
TRANSACTION_HELPER.doInTransaction(new RetryingTransactionCallback<Void>() TRANSACTION_HELPER.doInTransaction(new RetryingTransactionCallback<Void>()
{ {
@Override public Void execute() throws Throwable @Override public Void execute() throws Throwable
{ {
final List<String> facetIds = getExistingFacetIds(); final List<String> existingFacetIds = getExistingFacetIds();
final List<String> facetIds = new ArrayList<>(existingFacetIds);
facetIds.add("unrecognisedID"); facetIds.add("unrecognisedID");
SOLR_FACET_SERVICE.reorderFacets(facetIds); SOLR_FACET_SERVICE.reorderFacets(facetIds);
final List<String> newfacetIds = getExistingFacetIds();
assertEquals(existingFacetIds, newfacetIds);
return null; return null;
} }