View Javadoc
1   /**
2    * This file Copyright (c) 2012-2014 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.HashMap;
37  import java.util.Map;
38  import java.util.Map.Entry;
39  
40  import javax.jcr.Item;
41  import javax.jcr.RepositoryException;
42  
43  import org.slf4j.Logger;
44  import org.slf4j.LoggerFactory;
45  
46  import com.vaadin.data.Property;
47  
48  /**
49   * Common base for {@link JcrItemAdapter} implementation.
50   */
51  public abstract class AbstractJcrAdapter implements JcrItemAdapter {
52  
53      private static final Logger log = LoggerFactory.getLogger(AbstractJcrAdapter.class);
54  
55      protected static final String UNIDENTIFIED = "?";
56  
57      private JcrItemId itemId;
58  
59      private final Map<String, Property> changedProperties = new HashMap<String, Property>();
60  
61      private final Map<String, Property> removedProperties = new HashMap<String, Property>();
62  
63      public AbstractJcrAdapter(Item jcrItem) {
64          initCommonAttributes(jcrItem);
65      }
66  
67      /**
68       * Init common Item attributes.
69       */
70      protected void initCommonAttributes(Item jcrItem) {
71          try {
72              setItemId(JcrItemUtil.getItemId(jcrItem));
73          } catch (RepositoryException e) {
74              log.error("Could not retrieve workspace or path of JCR Item.", e);
75              setItemId(new JcrItemId(UNIDENTIFIED, UNIDENTIFIED));
76          }
77      }
78  
79      @Override
80      public String getWorkspace() {
81          return itemId.getWorkspace();
82      }
83  
84      @Override
85      public JcrItemId getItemId() {
86          return itemId;
87      }
88  
89      public void setItemId(JcrItemId itemId) {
90          this.itemId = itemId;
91      }
92  
93      @Override
94      public javax.jcr.Item getJcrItem() {
95          try {
96              return JcrItemUtil.getJcrItem(itemId);
97          } catch (RepositoryException re) {
98              log.warn("Not able to retrieve the JcrItem ", re.getMessage());
99              return null;
100         }
101     }
102 
103     @Override
104     public boolean hasChangedProperties() {
105         return changedProperties.size() > 0;
106     }
107 
108     // ABSTRACT IMPLEMENTATION OF PROPERTY CHANGES
109 
110     protected Map<String, Property> getChangedProperties() {
111         return changedProperties;
112     }
113 
114     protected Map<String, Property> getRemovedProperties() {
115         return removedProperties;
116     }
117 
118     /**
119      * Updates and removes properties on given item, based on the {@link #changedProperties} and {@link #removedProperties} maps. Read-only properties will not be updated and null valued properties will get removed.
120      */
121     protected void updateProperties(Item item) throws RepositoryException {
122         for (Entry<String, Property> entry : changedProperties.entrySet()) {
123             if (entry.getValue().isReadOnly()) {
124                 continue;
125             }
126             updateProperty(item, entry.getKey(), entry.getValue());
127             if (ModelConstants.JCR_NAME.equals(entry.getKey())) {
128                 // As the item name has changed - the item must be refreshed to prevent an attempt to change property on an invalid old name.
129                 item = getJcrItem();
130             }
131         }
132     }
133 
134     /**
135      * Performs update of an Item based on given vaadin Property. Note that this should not persist changes into JCR.
136      * Implementation should simply make sure that updated propertyIds are mapped to the correct actions (jcrName
137      * property should be handled in a specific way).
138      */
139     protected abstract void updateProperty(Item item, String propertyId, Property property);
140 
141 }