View Javadoc
1   /**
2    * This file Copyright (c) 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.workbench.column.definition;
35  
36  import info.magnolia.jcr.util.PropertyUtil;
37  import info.magnolia.objectfactory.ComponentProvider;
38  import info.magnolia.ui.contentapp.configuration.column.ConfiguredColumnDefinition;
39  import info.magnolia.ui.vaadin.integration.jcr.JcrItemUtil;
40  import info.magnolia.ui.vaadin.integration.jcr.JcrNodeAdapter;
41  
42  import javax.jcr.Node;
43  import javax.jcr.RepositoryException;
44  
45  import com.vaadin.data.ValueProvider;
46  import com.vaadin.ui.renderers.HtmlRenderer;
47  import com.vaadin.v7.data.Item;
48  import com.vaadin.v7.data.util.ObjectProperty;
49  
50  /**
51   * New content app framework Wrapper for deprecated {@link ColumnDefinition}.
52   *
53   * @deprecated since 6.0.
54   */
55  @Deprecated
56  public class CompatibilityColumnDefinition extends ConfiguredColumnDefinition {
57  
58      private AbstractColumnDefinition deprecatedColumnDefinition;
59  
60      public CompatibilityColumnDefinition() {
61          setRenderer(HtmlRenderer.class);
62          setValueProvider(CompatibilityValueProvider.class);
63      }
64  
65      public void init() {
66          if (getDeprecatedColumnDefinition() != null) {
67              getDeprecatedColumnDefinition().setName(getName());
68              getDeprecatedColumnDefinition().setPropertyName(getName());
69              setCaption(getName());
70          }
71      }
72  
73      public AbstractColumnDefinition getDeprecatedColumnDefinition() {
74          return deprecatedColumnDefinition;
75      }
76  
77      public void setDeprecatedColumnDefinition(AbstractColumnDefinition deprecatedColumnDefinition) {
78          this.deprecatedColumnDefinition = deprecatedColumnDefinition;
79      }
80  
81      /**
82       * {@link com.vaadin.data.ValueProvider} for {@link info.magnolia.ui.workbench.column.definition.CompatibilityColumnDefinition}.
83       *
84       * @deprecated since 6.0.
85       */
86      @Deprecated
87      public static class CompatibilityValueProvider implements ValueProvider<javax.jcr.Item, Object> {
88  
89          private final CompatibilityColumnDefinition compatibilityColumnDefinition;
90          private final ColumnFormatter columnFormatter;
91  
92          public CompatibilityValueProvider(CompatibilityColumnDefinition compatibilityColumnDefinition, ComponentProvider componentProvider) {
93              this.compatibilityColumnDefinition = compatibilityColumnDefinition;
94  
95              ColumnDefinition columnDefinition = compatibilityColumnDefinition.getDeprecatedColumnDefinition();
96              if (columnDefinition != null && columnDefinition.getFormatterClass() != null) {
97                  columnFormatter = componentProvider.newInstance(columnDefinition.getFormatterClass(), columnDefinition);
98              } else {
99                  columnFormatter = null;
100             }
101         }
102 
103         @Override
104         public Object apply(javax.jcr.Item item) {
105             if (!item.isNode()) {
106                 return null;
107             }
108 
109             final Node node = (Node) item;
110             if (compatibilityColumnDefinition.getDeprecatedColumnDefinition() != null) {
111                 Object propertyValueObject = PropertyUtil.getPropertyValueObject(node, compatibilityColumnDefinition.getDeprecatedColumnDefinition().getPropertyName());
112                 if (columnFormatter != null) {
113                     try {
114                         return columnFormatter.generateCell(new com.vaadin.v7.ui.Table() {
115                             @Override
116                             public Item getItem(Object itemId) {
117                                 return new JcrNodeAdapter(node);
118                             }
119 
120                             @Override
121                             public com.vaadin.v7.data.Property getContainerProperty(Object itemId, Object propertyId) {
122                                 return propertyValueObject == null ? null : new ObjectProperty<>(propertyValueObject);
123                             }
124                         }, JcrItemUtil.getItemId(node), compatibilityColumnDefinition.getName());
125                     } catch (RepositoryException e) {
126                         throw new RuntimeException(e);
127                     }
128                 } else {
129                     return propertyValueObject;
130                 }
131             } else {
132                 return null;
133             }
134         }
135     }
136 }