View Javadoc

1   /**
2    * This file Copyright (c) 2012 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.context.MgnlContext;
37  
38  import java.util.HashMap;
39  import java.util.Map;
40  import java.util.Map.Entry;
41  
42  import javax.jcr.Item;
43  import javax.jcr.RepositoryException;
44  
45  import org.slf4j.Logger;
46  import org.slf4j.LoggerFactory;
47  
48  import com.vaadin.data.Property;
49  import com.vaadin.data.Property.ValueChangeEvent;
50  
51  
52  /**
53   * Common base for {@link JcrItemAdapter} implementation.
54   */
55  public abstract class AbstractJcrAdapter implements Property.ValueChangeListener, JcrItemAdapter {
56  
57      private static final Logger log = LoggerFactory.getLogger(AbstractJcrAdapter.class);
58  
59      static final String UNIDENTIFIED = "?";
60  
61      private boolean isNode;
62  
63      private String workspace;
64  
65      private String path;
66  
67      private final Map<String, Property> changedProperties = new HashMap<String, Property>();
68  
69      private final Map<String, Property> removedProperties = new HashMap<String, Property>();
70  
71      public AbstractJcrAdapter(Item jcrItem) {
72          initCommonAttributes(jcrItem);
73      }
74  
75      /**
76       * Init common Item attributes.
77       */
78      protected void initCommonAttributes(Item jcrItem) {
79          isNode = jcrItem.isNode();
80          try {
81              workspace = jcrItem.getSession().getWorkspace().getName();
82              path = jcrItem.getPath();
83          } catch (RepositoryException e) {
84              log.error("Could not retrieve workspace or path of JCR Item.", e);
85              path = UNIDENTIFIED;
86              workspace = UNIDENTIFIED;
87          }
88      }
89  
90      @Override
91      public boolean isNode() {
92          return isNode;
93      }
94  
95      @Override
96      public String getWorkspace() {
97          return workspace;
98      }
99  
100     @Override
101     public String getPath() {
102         return path;
103     }
104 
105     protected void setPath(String path) {
106         this.path = path;
107     }
108 
109     /**
110      * @return The represented JCR Item, or null in case of {@link RepositoryException}.
111      */
112     @Override
113     public javax.jcr.Item getJcrItem() {
114         try {
115             return MgnlContext.getJCRSession(workspace).getItem(path);
116         } catch (RepositoryException re) {
117             log.warn("Not able to retrieve the JcrItem ", re.getMessage());
118             return null;
119         }
120     }
121 
122     // ABSTRACT IMPLEMENTATION OF PROPERTY CHANGES
123 
124     protected Map<String, Property> getChangedProperties() {
125         return changedProperties;
126     }
127 
128     protected Map<String, Property> getRemovedProperties() {
129         return removedProperties;
130     }
131 
132     /**
133      * Listener to DefaultProperty value change event. Get this event when a property has changed, and propagate this
134      * Vaadin Property value change to the corresponding JCR property.
135      */
136     @Override
137     public void valueChange(ValueChangeEvent event) {
138         Property property = event.getProperty();
139         if (property instanceof DefaultProperty) {
140             String propertyId = ((DefaultProperty) property).getPropertyName();
141             getChangedProperties().put(propertyId, property);
142         }
143     }
144 
145     /**
146      * Updates and removes properties on the JCR Item represented by this adapter, based on the
147      * {@link #changedProperties} and {@link #removedProperties} maps. Read-only properties will not be updated.
148      */
149     public void updateProperties() throws RepositoryException {
150         updateProperties(getJcrItem());
151     }
152 
153     /**
154      * Updates and removes properties on given item, based on the {@link #changedProperties} and
155      * {@link #removedProperties} maps. Read-only properties will not be updated.
156      */
157     public void updateProperties(Item item) throws RepositoryException {
158         for (Entry<String, Property> entry : changedProperties.entrySet()) {
159             if (entry.getValue().isReadOnly()) {
160                 continue;
161             }
162             updateProperty(item, entry.getKey(), entry.getValue());
163         }
164     }
165 
166     /**
167      * Performs update of an Item based on given vaadin Property. Note that this should not persist changes into JCR.
168      * Implementation should simply make sure that updated propertyIds are mapped to the correct actions (jcrName
169      * property should be handled in a specific way).
170      */
171     abstract protected void updateProperty(Item item, String propertyId, Property property);
172 
173 }