Sergey Shepelevich pushed to branch master at cms-community / hippo-configuration-management
Commits: 1cef1f29 by Peter Centgraf at 2017-11-09T16:42:50+01:00 HCM-228 Implement HippoResourceFileMapper to solve actual user issue -- bad names for asset resources after esv2yaml - - - - - 2 changed files: - + model/src/main/java/org/onehippo/cm/model/mapper/HippoResourceFileMapper.java - model/src/test/java/org/onehippo/cm/model/mapper/ValueFileMapperProviderTest.java Changes: ===================================== model/src/main/java/org/onehippo/cm/model/mapper/HippoResourceFileMapper.java ===================================== --- /dev/null +++ b/model/src/main/java/org/onehippo/cm/model/mapper/HippoResourceFileMapper.java @@ -0,0 +1,88 @@ +/* + * Copyright 2016-2017 Hippo B.V. (http://www.onehippo.com) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onehippo.cm.model.mapper; + +import java.util.Arrays; +import java.util.Optional; + +import org.apache.commons.lang3.StringUtils; +import org.onehippo.cm.model.tree.DefinitionNode; +import org.onehippo.cm.model.tree.DefinitionProperty; +import org.onehippo.cm.model.tree.PropertyType; +import org.onehippo.cm.model.tree.Value; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * File mapper for hippo:resource node type -- assumes that either hippo:filename or the node name of the parent asset + * set node will be a reasonable default file name. (This does NOT go up to the grandparent hippo:handle and extract + * the hippo:name that might be set by a user "Rename..." action in the CMS. This choice is debatable.) + */ +public class HippoResourceFileMapper extends AbstractFileMapper { + + private static final Logger logger = LoggerFactory.getLogger(HippoResourceFileMapper.class); + + static final String HIPPO_RESOURCE = "hippo:resource"; + private static final String DEFAULT_FILENAME = "data.bin"; + private static final String HIPPO_FILENAME = "hippo:filename"; + + @Override + public String apply(Value value) { + + try { + final DefinitionProperty property = value.getParent(); + final DefinitionNode resourceNode = property.getParent(); + + if (!isType(resourceNode, HIPPO_RESOURCE)) { + return null; + } + + final DefinitionProperty filename = resourceNode.getProperty(HIPPO_FILENAME); + String name; + String folderPath = "."; + + // if the hippo:resource has an explicit hippo:filename property, use that + if (filename != null) { + name = filename.getValue().getString(); + } + // otherwise, use the parent node's name as the filename + // EXCEPT! first handle the odd case of a hippo:resource directly attached to the repository root node + else if (resourceNode.getJcrPath().getParent().isRoot()) { + name = DEFAULT_FILENAME; + folderPath = constructFilePathFromJcrPath(resourceNode.getPath()); + } + else { + // the parent node usually has a good name, but it might be an SNS + name = mapNodeNameToFileName(resourceNode.getJcrPath().getParent().getLastSegment().suppressIndex().toString()); + + // one might be tempted to use the parent node to generate the folder path, since we'll repeat "asset" + // BUT! hippo:resource might appear in more contexts than just assets, so we need to be conservative + folderPath = constructFilePathFromJcrPath(resourceNode.getPath()); + } + return String.format("%s/%s", folderPath, name); + } catch (Exception e) { + logger.error("HippoResourceFileMapper failed", e); + return null; + } + } + + private Optional<Integer> calculateArrayIndex(DefinitionProperty property, Value value) { + if (property.getType() == PropertyType.LIST || property.getType() == PropertyType.SET) { + return Optional.of(Arrays.asList(property.getValues()).indexOf(value)); + } + return Optional.empty(); + } +} ===================================== model/src/test/java/org/onehippo/cm/model/mapper/ValueFileMapperProviderTest.java ===================================== --- a/model/src/test/java/org/onehippo/cm/model/mapper/ValueFileMapperProviderTest.java +++ b/model/src/test/java/org/onehippo/cm/model/mapper/ValueFileMapperProviderTest.java @@ -46,7 +46,7 @@ public class ValueFileMapperProviderTest extends AbstractBaseTest { public void testLoadPlugins() { ValueFileMapperProvider provider = ValueFileMapperProvider.getInstance(); List<ValueFileMapper> list = provider.valueFileMappers; - assertEquals(4, list.size()); + assertEquals(5, list.size()); assertTrue(list.stream().anyMatch(x -> x.getClass().equals(NtFileMapper.class))); assertTrue(list.stream().anyMatch(x -> x.getClass().equals(DummyValueFileMapper.class))); assertTrue(list.stream().anyMatch(x -> x.getClass().equals(OtherValueFileMapper.class))); View it on GitLab: https://code.onehippo.org/cms-community/hippo-configuration-management/commit/1cef1f29ca719bb3182e38906f9c40fb89a9bcb7 --- View it on GitLab: https://code.onehippo.org/cms-community/hippo-configuration-management/commit/1cef1f29ca719bb3182e38906f9c40fb89a9bcb7 You're receiving this email because of your account on code.onehippo.org.
_______________________________________________ Hippocms-svn mailing list Hippocms-svn@lists.onehippo.org https://lists.onehippo.org/mailman/listinfo/hippocms-svn