Skip to content

Commit

Permalink
REPO-5191 Bug: T-Engine should provide mapping rather than the repo. (#…
Browse files Browse the repository at this point in the history
…227)

Bug found while reviewing documents on how to create a custom metadata extractor. The original refactor had left the repo doing the mapping. It should have been passing the fully qualified repo properties to the T-Engine to do the mapping.

Linked to:
    Alfresco/acs-packaging#1826
    Alfresco/alfresco-transform-core#316
  • Loading branch information
alandavis committed Jan 7, 2021
1 parent be8e779 commit 04d699c
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 128 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* #%L
* Alfresco Repository
* %%
* Copyright (C) 2005 - 2020 Alfresco Software Limited
* Copyright (C) 2005 - 2021 Alfresco Software Limited
* %%
* This file is part of the Alfresco software.
* If the software was purchased under a paid Alfresco license, the terms of
Expand Down Expand Up @@ -1348,8 +1348,9 @@ public void embed(

try
{
embedInternal(nodeRef, mapSystemToRaw(properties), reader, writer);
if(logger.isDebugEnabled())
Map<String, Serializable> metadata = mapSystemToRaw(properties);
embedInternal(nodeRef, metadata, reader, writer);
if (logger.isDebugEnabled())
{
logger.debug("Embedded Metadata into " + writer);
}
Expand Down Expand Up @@ -1462,7 +1463,7 @@ private Map<QName, Serializable> mapRawToSystem(Map<String, Serializable> rawMet
* @param systemMetadata Metadata keyed by system properties
* @return Returns the metadata keyed by the content file metadata properties
*/
private Map<String, Serializable> mapSystemToRaw(Map<QName, Serializable> systemMetadata)
protected Map<String, Serializable> mapSystemToRaw(Map<QName, Serializable> systemMetadata)
{
Map<String, Serializable> metadataProperties = new HashMap<String, Serializable>(systemMetadata.size() * 2 + 1);
for (Map.Entry<QName, Serializable> entry : systemMetadata.entrySet())
Expand Down Expand Up @@ -2261,45 +2262,4 @@ protected void embedInternal(Map<String, Serializable> metadata, ContentReader r
{
// TODO make this an abstract method once more extracters support embedding
}

public static Map<String, String> convertMetadataToStrings(Map<String, Serializable> properties)
{
Map<String, String> propertiesAsStrings = new HashMap<>();
for (String metadataKey : properties.keySet())
{
Serializable value = properties.get(metadataKey);
if (value == null)
{
continue;
}
if (value instanceof Collection<?>)
{
for (Object singleValue : (Collection<?>) value)
{
try
{
// Convert to a string value
propertiesAsStrings.put(metadataKey, DefaultTypeConverter.INSTANCE.convert(String.class, singleValue));
}
catch (TypeConversionException e)
{
logger.info("Could not convert " + metadataKey + ": " + e.getMessage());
}
}
}
else
{
try
{
// Convert to a string value
propertiesAsStrings.put(metadataKey, DefaultTypeConverter.INSTANCE.convert(String.class, value));
}
catch (TypeConversionException e)
{
logger.info("Could not convert " + metadataKey + ": " + e.getMessage());
}
}
}
return propertiesAsStrings;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* #%L
* Alfresco Repository
* %%
* Copyright (C) 2020 Alfresco Software Limited
* Copyright (C) 2021 Alfresco Software Limited
* %%
* This file is part of the Alfresco software.
* If the software was purchased under a paid Alfresco license, the terms of
Expand Down Expand Up @@ -31,7 +31,6 @@
import org.alfresco.model.ContentModel;
import org.alfresco.repo.action.executer.ContentMetadataExtracter;
import org.alfresco.repo.content.transform.TransformerDebug;
import org.alfresco.repo.rendition2.RenditionDefinitionRegistry2;
import org.alfresco.repo.rendition2.RenditionDefinitionRegistry2Impl;
import org.alfresco.repo.rendition2.RenditionService2;
import org.alfresco.repo.rendition2.TransformDefinition;
Expand All @@ -44,6 +43,8 @@
import org.alfresco.service.cmr.repository.ContentWriter;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.cmr.repository.datatype.DefaultTypeConverter;
import org.alfresco.service.cmr.repository.datatype.TypeConversionException;
import org.alfresco.service.cmr.tagging.TaggingService;
import org.alfresco.service.namespace.NamespaceException;
import org.alfresco.service.namespace.NamespacePrefixResolver;
Expand All @@ -59,6 +60,7 @@
import java.io.InputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
Expand Down Expand Up @@ -306,6 +308,64 @@ private String extractMappingToString(Map<String, Set<String>> map)
}
}

/**
* As T-Engines do the mapping, all this method can do is convert QNames to fully qualified Strings and the
* values to Strings or a Collection of Strings.
* @param systemMetadata Metadata keyed by system properties
* @return the original map but with QNames turned into Strings.
*/
@Override
protected Map<String, Serializable> mapSystemToRaw(Map<QName, Serializable> systemMetadata)
{
Map<String, Serializable> metadataProperties = new HashMap<>(systemMetadata.size());
for (Map.Entry<QName, Serializable> entry : systemMetadata.entrySet())
{
Serializable serializableValue = entry.getValue();
if (serializableValue == null)
{
continue;
}

QName modelProperty = entry.getKey();
String key = modelProperty.toString();

if (serializableValue instanceof Collection<?>)
{
Collection<?> serializableCollection = (Collection<?>) serializableValue;
ArrayList<String> collection = new ArrayList<>(serializableCollection.size());
for (Object singleValue : serializableCollection)
{
try
{
String value = DefaultTypeConverter.INSTANCE.convert(String.class, singleValue);
collection.add(value);
}
catch (TypeConversionException e)
{
logger.info("Could not convert " + key + ": " + e.getMessage());
}
}
if (!collection.isEmpty())
{
metadataProperties.put(key, collection);
}
}
else
{
try
{
String value = DefaultTypeConverter.INSTANCE.convert(String.class, serializableValue);
metadataProperties.put(key, value);
}
catch (TypeConversionException e)
{
logger.info("Could not convert " + key + ": " + e.getMessage());
}
}
}
return metadataProperties;
}

@Override
protected void embedInternal(NodeRef nodeRef, Map<String, Serializable> metadata, ContentReader reader, ContentWriter writer)
{
Expand Down Expand Up @@ -475,10 +535,9 @@ private Map<String, Serializable> readMetadata(InputStream transformInputStream)

private String metadataToString(Map<String, Serializable> metadata)
{
Map<String, String> metadataAsStrings = AbstractMappingMetadataExtracter.convertMetadataToStrings(metadata);
try
{
return jsonObjectMapper.writeValueAsString(metadataAsStrings);
return jsonObjectMapper.writeValueAsString(metadata);
}
catch (JsonProcessingException e)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* #%L
* Alfresco Repository
* %%
* Copyright (C) 2005 - 2020 Alfresco Software Limited
* Copyright (C) 2005 - 2021 Alfresco Software Limited
* %%
* This file is part of the Alfresco software.
* If the software was purchased under a paid Alfresco license, the terms of
Expand Down Expand Up @@ -36,7 +36,6 @@
import org.alfresco.repo.content.transform.TransformerDebug;
import org.alfresco.repo.content.transform.UnsupportedTransformationException;
import org.alfresco.repo.rendition2.RenditionDefinition2;
import org.alfresco.repo.rendition2.RenditionDefinitionRegistry2;
import org.alfresco.repo.rendition2.RenditionDefinitionRegistry2Impl;
import org.alfresco.repo.rendition2.RenditionService2Impl;
import org.alfresco.repo.rendition2.TransformClient;
Expand Down Expand Up @@ -97,6 +96,7 @@
import static org.alfresco.model.ContentModel.PROP_CREATOR;
import static org.alfresco.model.ContentModel.PROP_MODIFIED;
import static org.alfresco.model.ContentModel.PROP_MODIFIER;
import static org.alfresco.model.ContentModel.PROP_TITLE;
import static org.alfresco.repo.rendition2.RenditionService2Impl.SOURCE_HAS_NO_CONTENT;

/**
Expand Down Expand Up @@ -145,6 +145,7 @@ public class AsynchronousExtractorTest extends BaseSpringTest
private Map<QName, Serializable> origProperties;
private Map<QName, Serializable> expectedProperties;
private Map<QName, Serializable> properties;
private Map<String, String> transformOptionsPassedToTEngine;

private class TestAsynchronousExtractor extends AsynchronousExtractor
{
Expand Down Expand Up @@ -215,8 +216,20 @@ public boolean isEmbedderSupported(String sourceMimetype, long sourceSizeInBytes
return true;
}

@Override
protected Map<String, Serializable> mapSystemToRaw(Map<QName, Serializable> systemMetadata)
{
// Add a property value that is a Collection, to ensure we can handle it.
Map<QName, Serializable> metadataWithCollection = new HashMap<>(systemMetadata);
Serializable collection = new ArrayList(Set.of("one", "two", "three"));
metadataWithCollection.put(PROP_TITLE, collection);

return super.mapSystemToRaw(metadataWithCollection);
}

private void mockTransform(NodeRef sourceNodeRef, RenditionDefinition2 renditionDefinition, int sourceContentHashCode)
{
transformOptionsPassedToTEngine = renditionDefinition.getTransformOptions();
try
{
transformerDebug.pushMisc();
Expand Down Expand Up @@ -604,8 +617,26 @@ public void testEmbed() throws Exception
File file = new File(resource.toURI());
long fileSize = file.length();

assertAsyncMetadataExecute(contentMetadataEmbedder, "quick/quick.html", // just replace the pdf with html!
// Replace the source pdf with html so we can see the content change size
assertAsyncMetadataExecute(contentMetadataEmbedder, "quick/quick.html",
UNCHANGED_HASHCODE, fileSize, expectedProperties, OverwritePolicy.PRAGMATIC);

// Check the metadata sent to the T-Engine contains one of the fixed property values.
String metadata = transformOptionsPassedToTEngine.get("metadata");
System.err.println("METADATA="+metadata);
assertTrue("System properties were not set: simple value", metadata.contains("\"{http://www.alfresco.org/model/content/1.0}creator\":\"System\""));

// Check the metadata sent to the T-Engine contains the collection value added by the mockTransform.
// The order of elements in the collection may change, so we cannot use a simple string compare.
int i = metadata.indexOf("\"{http://www.alfresco.org/model/content/1.0}title\":[");
assertTrue("The title is missing: "+metadata, i > 0);
int j = metadata.indexOf(']', i);
assertTrue("No closing ] : "+metadata.substring(i), j > 0);
String collection = metadata.substring(i, j);
assertTrue("There should have 3 elements: "+collection, collection.split(",").length == 3);
assertTrue("\"one\" is missing", collection.contains("\"one\""));
assertTrue("\"two\" is missing", collection.contains("\"two\""));
assertTrue("\"three\" is missing", collection.contains("\"three\""));
}

@Test
Expand Down
Loading

0 comments on commit 04d699c

Please sign in to comment.