1 /** 2 * This file Copyright (c) 2014-2015 Magnolia International 3 * Ltd. (http://www.magnolia-cms.com). All rights reserved. 4 * 5 * 6 * This file is dual-licensed under both the Magnolia 7 * Network Agreement and the GNU General Public License. 8 * You may elect to use one or the other of these licenses. 9 * 10 * This file is distributed in the hope that it will be 11 * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the 12 * implied warranty of MERCHANTABILITY or FITNESS FOR A 13 * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT. 14 * Redistribution, except as permitted by whichever of the GPL 15 * or MNA you select, is prohibited. 16 * 17 * 1. For the GPL license (GPL), you can redistribute and/or 18 * modify this file under the terms of the GNU General 19 * Public License, Version 3, as published by the Free Software 20 * Foundation. You should have received a copy of the GNU 21 * General Public License, Version 3 along with this program; 22 * if not, write to the Free Software Foundation, Inc., 51 23 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 24 * 25 * 2. For the Magnolia Network Agreement (MNA), this file 26 * and the accompanying materials are made available under the 27 * terms of the MNA which accompanies this distribution, and 28 * is available at http://www.magnolia-cms.com/mna.html 29 * 30 * Any modifications to this file must keep this entire header 31 * intact. 32 * 33 */ 34 package info.magnolia.ui.form.field.transformer.multi; 35 36 import info.magnolia.jcr.util.NodeTypes; 37 import info.magnolia.ui.api.i18n.I18NAuthoringSupport; 38 import info.magnolia.ui.form.field.definition.ConfiguredFieldDefinition; 39 import info.magnolia.ui.vaadin.integration.jcr.JcrNewNodeAdapter; 40 import info.magnolia.ui.vaadin.integration.jcr.JcrNodeAdapter; 41 42 import javax.jcr.Node; 43 import javax.jcr.RepositoryException; 44 45 import org.slf4j.Logger; 46 import org.slf4j.LoggerFactory; 47 48 import com.vaadin.data.Item; 49 import com.vaadin.data.util.PropertysetItem; 50 51 /** 52 * This delegating {@link info.magnolia.ui.form.field.transformer.Transformer Transformer} is dedicated to the {@link info.magnolia.ui.form.field.MultiField MultiField}; 53 * it considers entries as child nodes of a sub-node (named after the field) and delegates property handling to their respective sub-fields. 54 * <p> 55 * The storage strategy is that of the {@link info.magnolia.ui.form.field.transformer.multi.MultiValueSubChildrenNodePropertiesTransformer MultiValueSubChildrenNodePropertiesTransformer}: 56 * <ul> 57 * <li>rootItem (relatedFormItem) 58 * <ul> 59 * <li>subNode (single sub-node for MultiField entries)<br> 60 * <ul> 61 * <li>0 (first entry of the MultiField)<br> 62 * <li>1 (second entry of the MultiField)<br> 63 * <li>... 64 * </ul> 65 * </ul> 66 * </ul> 67 */ 68 public class DelegatingMultiValueSubnodeTransformer extends DelegatingMultiValueFieldTransformer { 69 70 private static final Logger log = LoggerFactory.getLogger(MultiValueSubChildrenNodeTransformer.class); 71 72 private JcrNodeAdapter subNode; 73 74 public DelegatingMultiValueSubnodeTransformer(Item relatedFormItem, ConfiguredFieldDefinition definition, Class<PropertysetItem> type, I18NAuthoringSupport i18NAuthoringSupport) { 75 super(relatedFormItem, definition, type, i18NAuthoringSupport); 76 } 77 78 /** 79 * Overridden to keep only the digit part in child-node names. 80 */ 81 @Override 82 protected String getSubItemBaseName() { 83 return ""; 84 } 85 86 /** 87 * Overridden to get an intermediate sub-node where the multiple nodes are stored, rather than directly under the root node. 88 * <p> 89 * The sub-node is created if it doesn't exist, and is named after the multi-value field definition. 90 */ 91 @Override 92 protected JcrNodeAdapter getRootItem() { 93 if (subNode == null) { 94 JcrNodeAdapter rootItem = super.getRootItem(); 95 Node rootJcrItem = rootItem.getJcrItem(); 96 try { 97 if (rootJcrItem.hasNode(definition.getName())) { 98 subNode = new JcrNodeAdapter(rootJcrItem.getNode(definition.getName())); 99 } else { 100 subNode = new JcrNewNodeAdapter(rootJcrItem, NodeTypes.ContentNode.NAME, definition.getName()); 101 } 102 rootItem.addChild(subNode); 103 } catch (RepositoryException e) { 104 log.warn(String.format("Could not determine whether form item '%s' had a child node named '%s'", rootJcrItem, definition.getName()), e); 105 } 106 } 107 return subNode; 108 } 109 110 }