Search in sources :

Example 11 with ConfigurationPersistData

use of org.apache.sling.caconfig.spi.ConfigurationPersistData in project sling by apache.

the class CustomConfigurationPersistenceStrategy method persistConfigurationCollection.

@Override
public boolean persistConfigurationCollection(ResourceResolver resourceResolver, String configResourceCollectionParentPath, ConfigurationCollectionPersistData data) {
    Resource configResourceParent = getOrCreateResource(resourceResolver, configResourceCollectionParentPath, ValueMap.EMPTY);
    // delete existing children and create new ones
    deleteChildren(configResourceParent);
    for (ConfigurationPersistData item : data.getItems()) {
        String path = configResourceParent.getPath() + "/" + item.getCollectionItemName() + "/" + CHILD_NODE_NAME;
        getOrCreateResource(resourceResolver, path, item.getProperties());
    }
    // if resource collection parent properties are given replace them as well
    if (data.getProperties() != null) {
        replaceProperties(configResourceParent, data.getProperties());
    }
    commit(resourceResolver);
    return true;
}
Also used : ConfigurationPersistData(org.apache.sling.caconfig.spi.ConfigurationPersistData) Resource(org.apache.sling.api.resource.Resource)

Example 12 with ConfigurationPersistData

use of org.apache.sling.caconfig.spi.ConfigurationPersistData in project sling by apache.

the class CustomConfigurationPersistenceStrategy2 method persistConfigurationCollection.

@Override
public boolean persistConfigurationCollection(ResourceResolver resourceResolver, String configResourceCollectionParentPath, ConfigurationCollectionPersistData data) {
    String parentPath = getCollectionParentResourcePath(configResourceCollectionParentPath);
    Resource configResourceParent = getOrCreateResource(resourceResolver, parentPath, ValueMap.EMPTY);
    // delete existing children and create new ones
    deleteChildren(configResourceParent);
    for (ConfigurationPersistData item : data.getItems()) {
        String path = configResourceParent.getPath() + "/" + item.getCollectionItemName();
        getOrCreateResource(resourceResolver, path, item.getProperties());
    }
    // if resource collection parent properties are given replace them as well
    if (data.getProperties() != null) {
        replaceProperties(configResourceParent, data.getProperties());
    }
    commit(resourceResolver);
    return true;
}
Also used : ConfigurationPersistData(org.apache.sling.caconfig.spi.ConfigurationPersistData) Resource(org.apache.sling.api.resource.Resource)

Example 13 with ConfigurationPersistData

use of org.apache.sling.caconfig.spi.ConfigurationPersistData in project sling by apache.

the class ConfigurationManagerImplTest method testPersistConfigurationCollection.

@Test
public void testPersistConfigurationCollection() throws Exception {
    underTest.persistConfigurationCollection(contextResourceNoConfig, CONFIG_COL_NAME, new ConfigurationCollectionPersistData(ImmutableList.of(new ConfigurationPersistData(ImmutableMap.<String, Object>of("prop1", "value1")).collectionItemName("0"), new ConfigurationPersistData(ImmutableMap.<String, Object>of("prop2", 5)).collectionItemName("1"))));
    context.resourceResolver().commit();
    String configPath0 = getConfigCollectionItemPersistPath(getConfigCollectionParentPersistPath("/conf/testNoConfig/sling:configs/" + CONFIG_COL_NAME) + "/0");
    ValueMap props0 = context.resourceResolver().getResource(configPath0).getValueMap();
    assertEquals("value1", props0.get("prop1"));
    String configPath1 = getConfigCollectionItemPersistPath(getConfigCollectionParentPersistPath("/conf/testNoConfig/sling:configs/" + CONFIG_COL_NAME) + "/1");
    ValueMap props1 = context.resourceResolver().getResource(configPath1).getValueMap();
    assertEquals((Integer) 5, props1.get("prop2"));
}
Also used : ConfigurationCollectionPersistData(org.apache.sling.caconfig.spi.ConfigurationCollectionPersistData) ConfigurationPersistData(org.apache.sling.caconfig.spi.ConfigurationPersistData) ValueMap(org.apache.sling.api.resource.ValueMap) Test(org.junit.Test)

Example 14 with ConfigurationPersistData

use of org.apache.sling.caconfig.spi.ConfigurationPersistData in project sling by apache.

the class ConfigurationManagerImplTest method testPersistConfiguration.

@Test
public void testPersistConfiguration() throws Exception {
    underTest.persistConfiguration(contextResourceNoConfig, CONFIG_NAME, new ConfigurationPersistData(ImmutableMap.<String, Object>of("prop1", "value1")));
    context.resourceResolver().commit();
    String configPath = getConfigPersistPath("/conf/testNoConfig/sling:configs/" + CONFIG_NAME);
    ValueMap props = context.resourceResolver().getResource(configPath).getValueMap();
    assertEquals("value1", props.get("prop1"));
}
Also used : ConfigurationPersistData(org.apache.sling.caconfig.spi.ConfigurationPersistData) ValueMap(org.apache.sling.api.resource.ValueMap) Test(org.junit.Test)

Example 15 with ConfigurationPersistData

use of org.apache.sling.caconfig.spi.ConfigurationPersistData in project sling by apache.

the class ConfigurationManagerImplTest method testPersistConfiguration_Nested.

@Test
public void testPersistConfiguration_Nested() throws Exception {
    underTest.persistConfiguration(contextResourceLevel2, getConfigResolvePath(getConfigResolvePath(CONFIG_NESTED_NAME) + "/propSub") + "/propSubLevel2", new ConfigurationPersistData(ImmutableMap.<String, Object>of("prop1", "value1_persist")));
    context.resourceResolver().commit();
    ConfigurationData configData = underTest.getConfiguration(contextResourceLevel2, CONFIG_NESTED_NAME);
    ConfigurationData subData = configData.getValues().get("propSub", ConfigurationData.class);
    ConfigurationData subDataLevel2 = subData.getValues().get("propSubLevel2", ConfigurationData.class);
    assertEquals("value1_persist", subDataLevel2.getValues().get("prop1", String.class));
    assertEquals("value1_persist", subDataLevel2.getEffectiveValues().get("prop1", String.class));
    assertFalse(subDataLevel2.getValues().get("prop4", false));
    assertFalse(subDataLevel2.getEffectiveValues().get("prop4", false));
}
Also used : ConfigurationPersistData(org.apache.sling.caconfig.spi.ConfigurationPersistData) ConfigurationData(org.apache.sling.caconfig.management.ConfigurationData) Test(org.junit.Test)

Aggregations

ConfigurationPersistData (org.apache.sling.caconfig.spi.ConfigurationPersistData)17 Test (org.junit.Test)12 ConfigurationCollectionPersistData (org.apache.sling.caconfig.spi.ConfigurationCollectionPersistData)9 Resource (org.apache.sling.api.resource.Resource)8 ValueMap (org.apache.sling.api.resource.ValueMap)4 ConfigurationPersistenceStrategy2 (org.apache.sling.caconfig.spi.ConfigurationPersistenceStrategy2)4 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 SimpleConfig (org.apache.sling.caconfig.it.example.SimpleConfig)2 ConfigurationData (org.apache.sling.caconfig.management.ConfigurationData)2 ConfigurationManager (org.apache.sling.caconfig.management.ConfigurationManager)2 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)1 DefaultConfigurationPersistenceStrategy (org.apache.sling.caconfig.impl.def.DefaultConfigurationPersistenceStrategy)1