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