1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
|
22 |
|
|
23 |
|
|
24 |
|
|
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
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 |
|
@link |
61 |
|
|
62 |
|
@param |
63 |
|
|
|
|
| 66.7% |
Uncovered Elements: 13 (39) |
Complexity: 13 |
Complexity Density: 0.48 |
|
64 |
|
public class ResourceFileTransformer<T extends UploadReceiver> extends FileTransformer<T> { |
65 |
|
|
66 |
|
private static final Logger log = LoggerFactory.getLogger(ResourceFileTransformer.class); |
67 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
68 |
4 |
@Inject... |
69 |
|
public ResourceFileTransformer(Item relatedFormItem, BasicUploadFieldDefinition definition, Class<T> type) { |
70 |
4 |
super(relatedFormItem, definition, type); |
71 |
|
} |
72 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
73 |
0 |
@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 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
83 |
0 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
88 |
2 |
@Override... |
89 |
|
public T readFromItem() { |
90 |
2 |
Item item = getOrCreateFileItem(); |
91 |
2 |
return createPropertyFromItem(item); |
92 |
|
} |
93 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
94 |
2 |
@Override... |
95 |
|
protected Item getOrCreateFileItem() { |
96 |
2 |
String itemName = getItemName(); |
97 |
|
|
98 |
2 |
getOrCreateProperty(getRoot(), itemName, type); |
99 |
2 |
return getRoot(); |
100 |
|
} |
101 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
102 |
1 |
@Override... |
103 |
|
protected JcrNodeAdapter getRootItem() { |
104 |
1 |
throw new UnsupportedOperationException("Not used by this class. Please use getRoot() method instead"); |
105 |
|
} |
106 |
|
|
|
|
| 75% |
Uncovered Elements: 4 (16) |
Complexity: 5 |
Complexity Density: 0.36 |
|
107 |
1 |
@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 |
|
@link |
131 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
132 |
5 |
protected Item getRoot() {... |
133 |
5 |
return relatedFormItem; |
134 |
|
} |
135 |
|
} |