Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ca config collections - showing as outdated reference when quick publishing #8

Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Ca config collections - showing as outdated reference when quick publ…
…ishing

Below is the behaviour of nested configurations :

Current behaviour: When we update or add one config in nested config all the configs inside the nested configs last modified dates are updated.
Expect behaviour: When we update or add a single config in nested config only the config that is added or updated should have last modified date updated
srikanthgurram-17 committed Dec 2, 2024

Verified

This commit was signed with the committer’s verified signature.
colleenmcginnis Colleen McGinnis
commit c0b08252a917b669ae78a96527cffab030d37dd5
Original file line number Diff line number Diff line change
@@ -30,6 +30,7 @@
import static io.wcm.caconfig.extensions.persistence.impl.PersistenceUtils.getOrCreateResource;
import static io.wcm.caconfig.extensions.persistence.impl.PersistenceUtils.replaceProperties;
import static io.wcm.caconfig.extensions.persistence.impl.PersistenceUtils.updatePageLastMod;
import static io.wcm.caconfig.extensions.persistence.impl.PersistenceUtils.isItemModifiedOrNewlyAdded;

import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
@@ -210,9 +211,11 @@ public boolean persistConfigurationCollection(@NotNull ResourceResolver resolver
// create new or overwrite existing children
for (ConfigurationPersistData item : data.getItems()) {
String path = getCollectionItemResourcePath(parentPath + "/" + item.getCollectionItemName());
ensureContainingPage(resolver, path, resourceType, configurationManagementSettings);
getOrCreateResource(resolver, path, DEFAULT_CONFIG_NODE_TYPE, item.getProperties(), configurationManagementSettings);
updatePageLastMod(resolver, pageManager, path);
if (isItemModifiedOrNewlyAdded(resolver, path, item)) {
ensureContainingPage(resolver, path, resourceType, configurationManagementSettings);
getOrCreateResource(resolver, path, DEFAULT_CONFIG_NODE_TYPE, item.getProperties(), configurationManagementSettings);
updatePageLastMod(resolver, pageManager, path);
}
}

// if resource collection parent properties are given replace them as well
Original file line number Diff line number Diff line change
@@ -38,6 +38,7 @@
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.caconfig.management.ConfigurationManagementSettings;
import org.apache.sling.caconfig.spi.ConfigurationCollectionPersistData;
import org.apache.sling.caconfig.spi.ConfigurationPersistData;
@@ -281,6 +282,34 @@ public static void commit(ResourceResolver resourceResolver, String relatedResou
}
}

/**
* Checks if the given item is modified or newly added by comparing its properties with the current state of the resource.
*
* @param resolver The ResourceResolver to access the resource.
* @param resourcePath The path of the resource to compare against.
* @param item The ConfigurationPersistData item containing the properties to compare.
* @return true if the resource does not exist or if any property value differs, false otherwise.
*/
public static boolean isItemModifiedOrNewlyAdded(ResourceResolver resolver, String resourcePath, ConfigurationPersistData item) {
Resource resource = resolver.getResource(resourcePath);
if (resource == null) {
return true; // Resource does not exist, so it is considered modified
}

ValueMap currentProperties = resource.getValueMap();
Map<String, Object> itemProperties = item.getProperties();

for (Map.Entry<String, Object> entry : itemProperties.entrySet()) {
srikanthgurram-17 marked this conversation as resolved.
Show resolved Hide resolved
String key = entry.getKey();
Object value = entry.getValue();
if (!value.equals(currentProperties.get(key))) {
return true; // Property value differs
}
}

return false; // No differences found
}

/**
* If the given resource points to an AEM page, delete the page using PageManager.
* Otherwise delete the resource using ResourceResolver.
Original file line number Diff line number Diff line change
@@ -19,16 +19,15 @@
*/
package io.wcm.caconfig.extensions.persistence.impl;

import static com.day.cq.wcm.api.NameConstants.PN_LAST_MOD;
import static io.wcm.caconfig.extensions.persistence.testcontext.PersistenceTestUtils.writeConfiguration;
import static io.wcm.caconfig.extensions.persistence.testcontext.PersistenceTestUtils.writeConfigurationCollection;
import static org.apache.sling.api.resource.ResourceResolver.PROPERTY_RESOURCE_TYPE;
import static org.apache.sling.testing.mock.caconfig.ContextPlugins.CACONFIG;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.*;

import java.util.Calendar;
import java.util.List;

import org.apache.sling.caconfig.ConfigurationBuilder;
@@ -207,10 +206,14 @@ void testListConfig_Nested() {
assertEquals(1, config2.subListConfig().length);
assertEquals("value21", config2.subListConfig()[0].stringParam());


Calendar lastModifiedConfigPage1 = configPage1.getContentResource().getValueMap().get(PN_LAST_MOD, Calendar.class);
Calendar lastModifiedConfigPage2 = configPage2.getContentResource().getValueMap().get(PN_LAST_MOD, Calendar.class);

// update config collection items
writeConfigurationCollection(context, contentPage.getPath(), ListNestedConfig.class.getName(), List.of(
ImmutableValueMap.of("stringParam", "value1-new", "intParam", 123),
ImmutableValueMap.of("stringParam", "value2-new", "intParam", 234),
ImmutableValueMap.of("stringParam", "value2", "intParam", 234),
ImmutableValueMap.of("stringParam", "value3-new", "intParam", 345)));

// read config
@@ -224,18 +227,34 @@ void testListConfig_Nested() {
assertEquals(2, config1.subListConfig().length);
assertEquals("value11", config1.subListConfig()[0].stringParam());
assertEquals("value12", config1.subListConfig()[1].stringParam());
//ConfigPage1 last modified date should be updated because it is updated
assertTrue(configLastModifiedUpdated(configPage1, false, lastModifiedConfigPage1));

config2 = configs.get(1);
assertEquals("value2-new", config2.stringParam());
assertEquals("value2", config2.stringParam());
assertEquals(234, config2.intParam());
assertEquals(1, config2.subListConfig().length);
assertEquals("value21", config2.subListConfig()[0].stringParam());
//ConfigPage2 last modified date should not be updated because it is not updated
assertFalse(configLastModifiedUpdated(configPage2, false, lastModifiedConfigPage2));

ListNestedConfig config3 = configs.get(2);
assertEquals("value3-new", config3.stringParam());
assertEquals(345, config3.intParam());
assertEquals(0, config3.subListConfig().length);
Page configPage3 = context.pageManager().getPage("/conf/test/site1/sling:configs/" + ListNestedConfig.class.getName() + "/item2");
//ConfigPage3 last modified date should be added because it is newly created
assertTrue(configLastModifiedUpdated(configPage3, true, null));
}

private boolean configLastModifiedUpdated(Page configPage, boolean newlyCreatedConfig, Calendar lastModifiedBeforeUpdateOrCreate) {
Calendar lastModifiedAfterUpdateOrCreate = configPage.getContentResource().getValueMap().get(PN_LAST_MOD, Calendar.class);
if (newlyCreatedConfig) {
//If the config is newly created then last modified date should be updated
return lastModifiedAfterUpdateOrCreate != null;
}
//If the config is updated then last modified date should be updated
return lastModifiedAfterUpdateOrCreate != null && lastModifiedAfterUpdateOrCreate.after(lastModifiedBeforeUpdateOrCreate);
}

@Test