1 /** 2 * This file Copyright (c) 2012-2018 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.vaadin.integration.jcr; 35 36 import java.util.Collection; 37 import java.util.Collections; 38 39 import javax.jcr.Node; 40 import javax.jcr.RepositoryException; 41 42 import org.slf4j.Logger; 43 import org.slf4j.LoggerFactory; 44 45 import com.vaadin.v7.data.Property; 46 47 /** 48 * Base implementation of an {@link com.vaadin.v7.data.Item} wrapping/representing a {@link javax.jcr.Node}. 49 * Implements {@link com.vaadin.v7.data.Property.ValueChangeListener} in order to inform/change JCR property when a 50 * Vaadin property has changed. 51 * <pre> 52 * <p> 53 * Jcr properties are read from Repository as long as they are not modified. 54 * <p> 55 * Jcr properties are updated or created if they: 56 * <ul> 57 * <li>Previously existed and where modified. 58 * <li>Newly created and set (an empty created property is not stored into Jcr repository) 59 * </ul> 60 * <p> 61 * Create a JcrNodeAdapter: 62 * <ul> 63 * <li>Just create a new JcrNodeAdapter with the related Jcr Node as parameter. 64 * </ul> 65 * <p> 66 * Properties: 67 * <ul> 68 * <li>getItemProperty(Object id) will return the current stored JCR property if not yet modified or the modified one. 69 * <li>If the property do not exist null will be returned. 70 * <li>In this case we have to create a new Property and attach this property to the JcrNodeAdapter, i.e. 71 * <p> 72 * <code> 73 * property p = DefaultPropertyUtil.newDefaultProperty(...) 74 * jcrNodeAdapter.addItemProperty(...) 75 * </code> 76 * </ul> 77 * </pre> 78 */ 79 public class JcrNodeAdapter extends AbstractJcrNodeAdapter { 80 81 private static final Logger log = LoggerFactory.getLogger(JcrNodeAdapter.class); 82 83 public JcrNodeAdapter(Node jcrNode) { 84 super(jcrNode); 85 try { 86 setNodeName(jcrNode.getName()); 87 } catch (RepositoryException e) { 88 log.error("Could not access the node name", e); 89 } 90 } 91 92 /** 93 * Get Vaadin Property from a Jcr Property. 94 * If the Property was already modified, get this Property from the local changedProperties map - else 95 * delegate to super implementation. 96 * 97 * @param propertyId id of the property to be retrieved 98 * @return the Property with the provided propertyId 99 */ 100 @Override 101 public Property getItemProperty(Object propertyId) { 102 // since we add props as strings, we need to look for them in same fashion 103 propertyId = propertyId.toString(); 104 return getChangedProperties().containsKey(propertyId) ? getChangedProperties().get(propertyId) : super.getItemProperty(propertyId); 105 } 106 107 @Override 108 public Collection<?> getItemPropertyIds() { 109 return Collections.unmodifiableCollection(getChangedProperties().keySet()); 110 } 111 112 @Override 113 public boolean addItemProperty(Object propertyId, Property property) throws UnsupportedOperationException { 114 log.debug("Adding new Property Item named [{}] with value [{}]", propertyId, property.getValue()); 115 116 // Store Property. 117 getChangedProperties().put(propertyId.toString(), property); 118 119 try { 120 // mark property read-only if current item is protected 121 boolean readOnly = isNodeProtected(getJcrItem()) || isPropertyProtected(propertyId.toString()); 122 if (readOnly) { 123 property.setReadOnly(true); 124 } 125 } catch (RepositoryException e) { 126 log.error("Failed to evaluate if node is protected", e); 127 } 128 129 // Remove a property from a removedProperties map 130 getRemovedProperties().remove(propertyId); 131 132 return true; 133 } 134 135 /** 136 * Remove a property from an Item. 137 * If the property was already modified (or just added), remove it for the changedProperties Map 138 * If the JCR node had the property before, mark it for deletion into the removedProperties Map. 139 */ 140 @Override 141 public boolean removeItemProperty(Object id) { 142 boolean res = false; 143 144 if (getChangedProperties().containsKey(id)) { 145 getChangedProperties().remove(id); 146 res = true; 147 } 148 if (jcrItemHasProperty((String) id)) { 149 markPropertyForDeletion((String) id); 150 res = true; 151 } 152 return res; 153 } 154 155 private boolean jcrItemHasProperty(String propertyName) { 156 try { 157 return getJcrItem().hasProperty(propertyName); 158 } catch (RepositoryException e) { 159 log.error("Could not determine if property [{}] exists", propertyName, e); 160 return false; 161 } 162 } 163 164 @Override 165 public boolean isNew() { 166 return false; 167 } 168 }