Skip to content

Commit

Permalink
#25618 Support basic metadata from config (#25745)
Browse files Browse the repository at this point in the history
* #25618 Support basic metadata from config

* #25618 Renaming basic metadata config property

* #25618 Implementing IT to get keywords from basic metadata if configured

* #25618 Fixing tests

* #25618 Loading basic metadata keys lazily

* #25618 Fixing IT

* Adding comments in test

---------

Co-authored-by: nollymar <[email protected]>
  • Loading branch information
nollymar and nollymar authored Aug 17, 2023
1 parent ec6a839 commit ff9a106
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import static com.dotcms.datagen.TestDataUtils.removeAnyMetadata;
import static com.dotcms.rest.api.v1.temp.TempFileAPITest.mockHttpServletRequest;
import static com.dotcms.storage.FileMetadataAPI.BINARY_METADATA_VERSION;
import static com.dotcms.storage.FileMetadataAPIImpl.BASIC_METADATA_OVERRIDE_KEYS;
import static com.dotcms.storage.StoragePersistenceProvider.DEFAULT_STORAGE_TYPE;
import static com.dotcms.storage.model.Metadata.CUSTOM_PROP_PREFIX;
import static org.junit.Assert.assertEquals;
Expand Down Expand Up @@ -95,6 +96,27 @@ public void Test_Get_Metadata_Property() throws Exception {
assertTrue(fileAssetContent.get(FileAssetAPI.META_DATA_FIELD) instanceof Map);
}

/**
* <b>Method to test:</b> {@link FileMetadataAPI#generateContentletMetadata(Contentlet)} <br>
* <b>Given scenario:</b> The property {@link FileMetadataAPIImpl#BASIC_METADATA_OVERRIDE_KEYS} is set<br>
* <b>Expected Result:</b> The basic metadata must get the value set in {@link FileMetadataAPIImpl#BASIC_METADATA_OVERRIDE_KEYS}
* @throws Exception
*/
@Test
public void Test_Get_BasicMetadataWhenOverridePropertyIsSet() throws Exception {
prepareIfNecessary();
final FileMetadataAPI metadataAPI = new FileMetadataAPIImpl(APILocator.getFileStorageAPI(),
CacheLocator.getMetadataCache(), () -> CollectionsUtils.set("keywords"));
final Contentlet fileAssetContent = getFileAssetContent(true, 1, TestFile.PDF);
Metadata metadata = metadataAPI.generateContentletMetadata(
fileAssetContent).getBasicMetadataMap().get("fileAsset");

//we get the keywords from the pdf file. It should have this value: keyword1,keyword2
assertNotNull(metadata);
assertEquals(1, metadata.getMap().size());
assertEquals("keyword1,keyword2", metadata.getMap().get("keywords"));
}

/**
* <b>Method to test:</b> {@link FileMetadataAPI#getFullMetadataNoCache(File, Supplier)}<br>
* <b>Given scenario:</b> Getting metadata from a urlMap<br>
Expand Down
Binary file not shown.
18 changes: 15 additions & 3 deletions dotCMS/src/main/java/com/dotcms/storage/FileMetadataAPIImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableMap;
import com.liferay.util.StringPool;
import io.vavr.Lazy;
import io.vavr.Tuple;
import io.vavr.Tuple2;
import io.vavr.control.Try;
Expand Down Expand Up @@ -54,17 +55,29 @@
*/
public class FileMetadataAPIImpl implements FileMetadataAPI {

public static final String BASIC_METADATA_OVERRIDE_KEYS = "BASIC_METADATA_OVERRIDE_KEYS";
private final FileStorageAPI fileStorageAPI;
private final MetadataCache metadataCache;

private Lazy<Set<String>> basicMetadataKeySet;

public FileMetadataAPIImpl() {
this(APILocator.getFileStorageAPI(), CacheLocator.getMetadataCache());
}

private FileMetadataAPIImpl(final FileStorageAPI fileStorageAPI, final MetadataCache metadataCache) {
this(fileStorageAPI, metadataCache, () -> Arrays.stream(
Config.getStringProperty(BASIC_METADATA_OVERRIDE_KEYS,
String.join(",", BasicMetadataFields.keyMap().keySet())).split(","))
.map(String::trim).collect(Collectors.toSet()));
}

@VisibleForTesting
FileMetadataAPIImpl(final FileStorageAPI fileStorageAPI, final MetadataCache metadataCache) {
FileMetadataAPIImpl(final FileStorageAPI fileStorageAPI, final MetadataCache metadataCache,
Supplier<? extends Set<String>> basicMetadataKeySetSupplier) {
this.fileStorageAPI = fileStorageAPI;
this.metadataCache = metadataCache;
this.basicMetadataKeySet = Lazy.of(basicMetadataKeySetSupplier);
}

/**
Expand Down Expand Up @@ -441,8 +454,7 @@ private Metadata getFullMetadataNoCache(final Contentlet contentlet,
* @return
*/
private Map<String, Serializable> filterNonBasicMetadataFields(final Map<String, Serializable> originalMap) {
final Set<String> basicMetadataFieldsSet = BasicMetadataFields.keyMap().keySet();
return originalMap.entrySet().stream().filter(entry -> basicMetadataFieldsSet
return originalMap.entrySet().stream().filter(entry -> basicMetadataKeySet.get()
.contains(entry.getKey()) || entry.getKey().startsWith(Metadata.CUSTOM_PROP_PREFIX) ).collect(
Collectors.toMap(Entry::getKey, Entry::getValue));
}
Expand Down

0 comments on commit ff9a106

Please sign in to comment.