View Javadoc
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 info.magnolia.cms.core.Path;
37  
38  import javax.jcr.Item;
39  import javax.jcr.Node;
40  import javax.jcr.RepositoryException;
41  
42  import org.apache.commons.lang3.StringUtils;
43  import org.slf4j.Logger;
44  import org.slf4j.LoggerFactory;
45  
46  import com.vaadin.v7.data.Property;
47  
48  /**
49   * Used to create a new Node based on an Vaadin Item. This node adapter uses the parent node to
50   * initialize the global properties (workspace, path....). No references is made to an existing JCR
51   * node (except for the parent of the node to create). However, after applying changes to the
52   * item, the reference will be held to the newly created node.
53   */
54  @Deprecated
55  public class JcrNewNodeAdapter extends JcrNodeAdapter {
56  
57      private static final Logger log = LoggerFactory.getLogger(JcrNewNodeAdapter.class);
58      /**
59       * Whether changes were already applied to the node.
60       */
61      private boolean appliedChanges = false;
62  
63      /**
64       * @param parentNode Parent of the node to create.
65       * @param nodeType Type node to create.
66       */
67      public JcrNewNodeAdapter(Node parentNode, String nodeType) {
68          this(parentNode, nodeType, null);
69      }
70  
71      /**
72       * @param parentNode Parent of the node to create.
73       * @param nodeType Type node to create.
74       * @param nodeName Name of the new node.
75       */
76      public JcrNewNodeAdapter(Node parentNode, String nodeType, String nodeName) {
77          super(parentNode);
78          setPrimaryNodeTypeName(nodeType);
79          setNodeName(nodeName);
80          try {
81              JcrItemId parentId = JcrItemUtil.getItemId(parentNode);
82              setItemId(new JcrNewNodeItemId(parentId.getUuid(), parentId.getWorkspace(), nodeType, nodeName));
83          } catch (RepositoryException e) {
84              log.error("Failed to initialize JcrNewNodeAdapter: " + e.getMessage(), e);
85          }
86      }
87  
88  
89      /**
90       * Returns item property of a new node.
91       */
92      @Override
93      public Property getItemProperty(Object propertyId) {
94          // If changes were already applied, behave like a JcrNodeAdapter
95          if (appliedChanges) {
96              return super.getItemProperty(propertyId);
97          }
98          return getChangedProperties().get(propertyId);
99      }
100 
101     /**
102      * Create a new subNode of the parent Node or return the existing one if already created.
103      *
104      * If called a second time, apply changes of {@link JcrNodeAdapter} will be called.
105      */
106     @Override
107     public Node applyChanges() throws RepositoryException {
108         // If changes were already applied, behave like a JcrNodeAdapter
109         if (appliedChanges) {
110             return super.applyChanges();
111         }
112 
113         Node parent = getJcrItem();
114 
115         // Create a Node Name if not defined
116         if (StringUtils.isBlank(getNodeName())) {
117             setNodeName(getUniqueNewItemName(parent));
118         }
119 
120         // Create the new node
121         Node node = parent.addNode(getNodeName(), getPrimaryNodeTypeName());
122         log.debug("create a new node for parent " + parent.getPath() + " with name " + getNodeName());
123 
124         // Update properties
125         updateProperties(node);
126 
127         // Update child nodes
128         if (!getChildren().isEmpty()) {
129             for (AbstractJcrNodeAdapter child : getChildren().values()) {
130                 if (child instanceof JcrNewNodeAdapter) {
131                     // Set parent node (parent could be newly created)
132                     child.initCommonAttributes(node);
133                 }
134                 child.applyChanges();
135             }
136         }
137         // Update parent
138         if (!appliedChanges) {
139             setParent(new JcrNodeAdapter(parent));
140         }
141 
142         appliedChanges = true;
143 
144         // Update itemId to new node
145         setItemId(JcrItemUtil.getItemId(node));
146         return node;
147     }
148 
149     @Override
150     protected void markPropertyForDeletion(String propertyId) {
151         if (appliedChanges) {
152             super.markPropertyForDeletion(propertyId);
153         }
154     }
155 
156     @Override
157     public void setNodeName(String nodeName) {
158         super.setNodeName(nodeName);
159         if (getItemId() instanceof JcrNewNodeItemId) {
160             ((JcrNewNodeItemId) getItemId()).setName(nodeName);
161         }
162     }
163 
164     @Override
165     public void setItemId(JcrItemId itemId) {
166         JcrItemId actualItemId;
167         if (appliedChanges) {
168             actualItemId = new JcrNodeItemId(itemId.getUuid(), itemId.getWorkspace());
169         } else {
170             actualItemId = new JcrNewNodeItemId(itemId.getUuid(), itemId.getWorkspace(), getPrimaryNodeTypeName(), getNodeName());
171         }
172         super.setItemId(actualItemId);
173     }
174 
175     /**
176      * Create a new unique nodeName. If JCR_NAME if part of the properties, use this property as
177      * desired nodeName.
178      */
179     private String getUniqueNewItemName(final Item item) throws RepositoryException {
180         if (item == null) {
181             throw new IllegalArgumentException("Item cannot be null");
182         }
183 
184         String nodeName = "";
185         if (getChangedProperties().containsKey(ModelConstants.JCR_NAME)) {
186             nodeName = (String) getChangedProperties().get(ModelConstants.JCR_NAME).getValue();
187             getChangedProperties().remove(ModelConstants.JCR_NAME);
188             nodeName = Path.getValidatedLabel(nodeName);
189         }
190 
191         return Path.getUniqueLabel(item.getSession(), item.getPath(), nodeName);
192     }
193 
194     @Override
195     public boolean isNew() {
196         // before applying changes getJcrItem() returns the parent of the node being created,
197         // therefore we can't check it for isNew(), as it would always be false (parent already exists in JCR).
198         return !appliedChanges || getJcrItem().isNew();
199     }
200 }