View Javadoc
1   /**
2    * This file Copyright (c) 2015-2017 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.resources.app.form.field.transformer.item;
35  
36  import info.magnolia.cms.beans.runtime.FileProperties;
37  import info.magnolia.objectfactory.Components;
38  import info.magnolia.ui.api.i18n.I18NAuthoringSupport;
39  import info.magnolia.ui.form.field.definition.BasicUploadFieldDefinition;
40  import info.magnolia.ui.form.field.transformer.item.FileTransformer;
41  import info.magnolia.ui.form.field.upload.UploadReceiver;
42  import info.magnolia.ui.vaadin.integration.jcr.JcrNodeAdapter;
43  
44  import java.io.FileInputStream;
45  import java.io.IOException;
46  import java.io.InputStream;
47  import java.util.Date;
48  
49  import javax.inject.Inject;
50  import javax.jcr.Binary;
51  import javax.jcr.RepositoryException;
52  
53  import org.apache.jackrabbit.JcrConstants;
54  import org.apache.jackrabbit.value.ValueFactoryImpl;
55  import org.slf4j.Logger;
56  import org.slf4j.LoggerFactory;
57  
58  import com.vaadin.v7.data.Item;
59  import com.vaadin.v7.data.Property;
60  
61  /**
62   * Implementation of a {@link info.magnolia.ui.form.field.transformer.Transformer} that handles a Binary Item and has no notion of JCR.
63   * 
64   * @param <T> property type used by the related field.
65   */
66  public class ResourceFileTransformer<T extends UploadReceiver> extends FileTransformer<T> {
67  
68      private static final Logger log = LoggerFactory.getLogger(ResourceFileTransformer.class);
69  
70      @Inject
71      public ResourceFileTransformer(Item relatedFormItem, BasicUploadFieldDefinition definition, Class<T> type, I18NAuthoringSupport i18NAuthoringSupport) {
72          super(relatedFormItem, definition, type, i18NAuthoringSupport);
73      }
74  
75      /**
76       * @deprecated since 2.5.3 - use {@link #ResourceFileTransformer(Item, BasicUploadFieldDefinition, Class, I18NAuthoringSupport)} instead.
77       */
78      @Deprecated
79      public ResourceFileTransformer(Item relatedFormItem, BasicUploadFieldDefinition definition, Class<T> type) {
80          this(relatedFormItem, definition, type, Components.getComponent(I18NAuthoringSupport.class));
81      }
82  
83      @Override
84      public void writeToItem(T newValue) {
85          Item item = getOrCreateFileItem();
86          if (isValid(newValue, item)) {
87              populateItem(newValue, item);
88          } else {
89              handleInvalid(newValue, item);
90          }
91      }
92  
93      @Override
94      protected void handleInvalid(T newValue, Item item) {
95          log.info("Can't handle {} as it is null or empty", newValue);
96      }
97  
98      @Override
99      public T readFromItem() {
100         Item item = getOrCreateFileItem();
101         return createPropertyFromItem(item);
102     }
103 
104     @Override
105     protected Item getOrCreateFileItem() {
106         String itemName = getItemName();
107 
108         getOrCreateProperty(getRoot(), itemName, type);
109         return getRoot();
110     }
111 
112     @Override
113     protected JcrNodeAdapter getRootItem() {
114         throw new UnsupportedOperationException("Not used by this class. Please use getRoot() method instead");
115     }
116 
117     @SuppressWarnings("unchecked")
118     @Override
119     public Item populateItem(T newValue, Item item) {
120 
121         Property<Binary> data = getOrCreateProperty(item, JcrConstants.JCR_DATA, Binary.class);
122         if (newValue != null) {
123             try (InputStream is = new FileInputStream(newValue.getFile())) {
124                 data.setValue(ValueFactoryImpl.getInstance().createBinary(is));
125             } catch (RepositoryException | IOException e) {
126                 log.error("Could not get Binary. Upload will not be performed", e);
127                 getRoot().removeItemProperty(item);
128                 return null;
129             }
130         }
131         getOrCreateProperty(item, JcrConstants.JCR_MIMETYPE, String.class).setValue(newValue.getMimeType());
132         getOrCreateProperty(item, FileProperties.PROPERTY_FILENAME, String.class).setValue(newValue.getFileName());
133         getOrCreateProperty(item, FileProperties.PROPERTY_LASTMODIFIED, Date.class).setValue(new Date());
134         getOrCreateProperty(item, FileProperties.PROPERTY_SIZE, Long.class).setValue(newValue.getFileSize());
135         getOrCreateProperty(item, FileProperties.PROPERTY_EXTENSION, String.class).setValue(newValue.getExtension());
136         return item;
137     }
138 
139     /**
140      * Defines the root item used to retrieve and create child items. Replaces {@link #getRootItem()}.
141      */
142     protected Item getRoot() {
143         return relatedFormItem;
144     }
145 }