Clover icon

Magnolia Resources App Module 2.4.2

  1. Project Clover database Fri Nov 6 2015 16:17:22 CET
  2. Package info.magnolia.resources.app.form.field.transformer.item

File ResourceFileTransformer.java

 

Coverage histogram

../../../../../../../../img/srcFileCovDistChart7.png
63% of files have more coverage

Code metrics

4
27
8
1
135
77
13
0.48
3.38
8
1.62

Classes

Class Line # Actions
ResourceFileTransformer 64 27 0% 13 13
0.666666766.7%
 

Contributing tests

This file is covered by 4 tests. .

Source view

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  4 toggle @Inject
69    public ResourceFileTransformer(Item relatedFormItem, BasicUploadFieldDefinition definition, Class<T> type) {
70  4 super(relatedFormItem, definition, type);
71    }
72   
 
73  0 toggle @Override
74    public void writeToItem(T newValue) {
75  0 Item item = getOrCreateFileItem();
76  0 if (isValid(newValue, item)) {
77  0 populateItem(newValue, item);
78    } else {
79  0 handleInvalid(newValue, item);
80    }
81    }
82   
 
83  0 toggle @Override
84    protected void handleInvalid(T newValue, Item item) {
85  0 log.info("Can't handle {} as it is null or empty", newValue);
86    }
87   
 
88  2 toggle @Override
89    public T readFromItem() {
90  2 Item item = getOrCreateFileItem();
91  2 return createPropertyFromItem(item);
92    }
93   
 
94  2 toggle @Override
95    protected Item getOrCreateFileItem() {
96  2 String itemName = getItemName();
97   
98  2 getOrCreateProperty(getRoot(), itemName, type);
99  2 return getRoot();
100    }
101   
 
102  1 toggle @Override
103    protected JcrNodeAdapter getRootItem() {
104  1 throw new UnsupportedOperationException("Not used by this class. Please use getRoot() method instead");
105    }
106   
 
107  1 toggle @SuppressWarnings("unchecked")
108    @Override
109    public Item populateItem(T newValue, Item item) {
110   
111  1 Property<Binary> data = getOrCreateProperty(item, JcrConstants.JCR_DATA, Binary.class);
112  1 if (newValue != null) {
113  1 try (InputStream is = new FileInputStream(newValue.getFile())) {
114  1 data.setValue(ValueFactoryImpl.getInstance().createBinary(is));
115    } catch (RepositoryException | IOException e) {
116  0 log.error("Could not get Binary. Upload will not be performed", e);
117  0 getRoot().removeItemProperty(item);
118  0 return null;
119    }
120    }
121  1 getOrCreateProperty(item, JcrConstants.JCR_MIMETYPE, String.class).setValue(newValue.getMimeType());
122  1 getOrCreateProperty(item, FileProperties.PROPERTY_FILENAME, String.class).setValue(newValue.getFileName());
123  1 getOrCreateProperty(item, FileProperties.PROPERTY_LASTMODIFIED, Date.class).setValue(new Date());
124  1 getOrCreateProperty(item, FileProperties.PROPERTY_SIZE, Long.class).setValue(newValue.getFileSize());
125  1 getOrCreateProperty(item, FileProperties.PROPERTY_EXTENSION, String.class).setValue(newValue.getExtension());
126  1 return item;
127    }
128   
129    /**
130    * Defines the root item used to retrieve and create child items. Replaces {@link #getRootItem()}.
131    */
 
132  5 toggle protected Item getRoot() {
133  5 return relatedFormItem;
134    }
135    }