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.ui.form.field.transformer.item;
35
36 import info.magnolia.annotation.deprecation.MgnlDeprecated;
37
38 import info.magnolia.cms.beans.runtime.File;
39 import info.magnolia.cms.core.Path;
40 import info.magnolia.jcr.util.NodeTypes;
41 import info.magnolia.jcr.util.NodeUtil;
42 import info.magnolia.objectfactory.Components;
43 import info.magnolia.ui.api.i18n.I18NAuthoringSupport;
44 import info.magnolia.ui.form.field.definition.BasicUploadFieldDefinition;
45 import info.magnolia.ui.form.field.transformer.Transformer;
46 import info.magnolia.ui.form.field.upload.UploadReceiver;
47 import info.magnolia.ui.vaadin.integration.jcr.AbstractJcrNodeAdapter;
48 import info.magnolia.ui.vaadin.integration.jcr.JcrNewNodeAdapter;
49 import info.magnolia.ui.vaadin.integration.jcr.JcrNodeAdapter;
50
51 import java.io.FileInputStream;
52 import java.io.InputStream;
53 import java.io.OutputStream;
54 import java.util.ArrayList;
55 import java.util.Date;
56 import java.util.List;
57 import java.util.Locale;
58
59 import javax.inject.Inject;
60 import javax.jcr.Binary;
61 import javax.jcr.Node;
62 import javax.jcr.RepositoryException;
63
64 import org.apache.commons.io.IOUtils;
65 import org.apache.jackrabbit.JcrConstants;
66 import org.apache.jackrabbit.value.BinaryImpl;
67 import org.apache.jackrabbit.value.ValueFactoryImpl;
68 import org.slf4j.Logger;
69 import org.slf4j.LoggerFactory;
70
71 import com.vaadin.v7.data.Item;
72 import com.vaadin.v7.data.Property;
73 import com.vaadin.v7.data.util.ObjectProperty;
74
75
76
77
78
79
80
81 @Deprecated
82 @MgnlDeprecated(since = "6.2.3.", description = "Use new framework where the conversion is done in property set factory.")
83 public class FileTransformer<T extends UploadReceiver> implements Transformer<T> {
84
85 private static final Logger log = LoggerFactory.getLogger(FileTransformer.class);
86
87 protected Item relatedFormItem;
88 protected final BasicUploadFieldDefinition definition;
89 protected final Class<T> type;
90 private Locale locale;
91
92 protected String basePropertyName;
93
94
95
96
97
98 @Deprecated
99 protected String i18NPropertyName;
100
101 private I18NAuthoringSupport i18NAuthoringSupport;
102
103 @Inject
104 public FileTransformer(Item relatedFormItem, BasicUploadFieldDefinition definition, Class<T> type, I18NAuthoringSupport i18NAuthoringSupport) {
105 this.definition = definition;
106 this.relatedFormItem = relatedFormItem;
107 this.type = type;
108 this.basePropertyName = getBasePropertyName();
109 this.i18NAuthoringSupport = i18NAuthoringSupport;
110 }
111
112
113
114
115 @Deprecated
116 public FileTransformer(Item relatedFormItem, BasicUploadFieldDefinition definition, Class<T> type) {
117 this(relatedFormItem, definition, type, Components.getComponent(I18NAuthoringSupport.class));
118 }
119
120
121
122
123
124
125 @Override
126 public void writeToItem(T newValue) {
127 Item item = getOrCreateFileItem();
128 if (isValid(newValue, item)) {
129 populateItem(newValue, item);
130 getRootItem().addChild((AbstractJcrNodeAdapter) item);
131 } else {
132 handleInvalid(newValue, item);
133 }
134 }
135
136
137
138
139 @Override
140 public T readFromItem() {
141
142 JcrNodeAdapter rootItem = getRootItem();
143
144 if (!rootItem.hasChildItemChanges()) {
145 populateStoredChildItems(rootItem);
146 }
147
148 Item item = getOrCreateFileItem();
149 return createPropertyFromItem(item);
150 }
151
152
153
154
155 protected Item getOrCreateFileItem() {
156 String itemName = getItemName();
157 Item child = getRootItem().getChild(itemName);
158 if (child != null) {
159 return child;
160 }
161 Node node = null;
162 try {
163 node = getRootItem().getJcrItem();
164 if (node.hasNode(itemName) && !(getRootItem() instanceof JcrNewNodeAdapter)) {
165 child = new JcrNodeAdapter(node.getNode(itemName));
166 } else {
167 child = new JcrNewNodeAdapter(node, NodeTypes.Resource.NAME, itemName);
168 }
169 } catch (RepositoryException e) {
170 log.error("Could get or create a child Item for {} ", NodeUtil.getPathIfPossible(node), e);
171 }
172 return child;
173 }
174
175
176
177
178 protected String getItemName() {
179 if (hasI18NSupport()) {
180 if (locale != null && !i18NAuthoringSupport.isDefaultLocale(locale, relatedFormItem)) {
181 return i18NAuthoringSupport.deriveLocalisedPropertyName(this.basePropertyName, locale);
182 }
183 }
184 return this.basePropertyName;
185 }
186
187
188
189
190 public T createPropertyFromItem(Item item) {
191 T uploadReceiver = initializeUploadReceiver();
192
193
194 if (item.getItemProperty(JcrConstants.JCR_DATA) != null && item.getItemProperty(JcrConstants.JCR_DATA).getValue() != null) {
195 String fileName = item.getItemProperty(File.PROPERTY_FILENAME) != null ? (String) item.getItemProperty(File.PROPERTY_FILENAME).getValue() : "";
196 String MIMEType = item.getItemProperty(File.PROPERTY_CONTENTTYPE) != null ? String.valueOf(item.getItemProperty(File.PROPERTY_CONTENTTYPE).getValue()) : "";
197
198 try (OutputStream out = uploadReceiver.receiveUpload(fileName, MIMEType);
199 InputStream in = ((BinaryImpl) item.getItemProperty(JcrConstants.JCR_DATA).getValue()).getStream()) {
200 IOUtils.copy(in, out);
201 } catch (Exception e) {
202 e.printStackTrace();
203 }
204 }
205 return uploadReceiver;
206 }
207
208 protected T initializeUploadReceiver() {
209 return (T) Components.newInstance(UploadReceiver.class, Path.getTempDirectory());
210 }
211
212
213
214
215 protected boolean isValid(T newValue, Item item) {
216 return newValue != null && !newValue.isEmpty();
217 }
218
219
220
221
222
223
224 public Item populateItem(T newValue, Item item) {
225
226 Property<Binary> data = getOrCreateProperty(item, JcrConstants.JCR_DATA, Binary.class);
227 if (newValue != null) {
228 try {
229 data.setValue(ValueFactoryImpl.getInstance().createBinary(new FileInputStream(newValue.getFile())));
230 } catch (Exception re) {
231 log.error("Could not get Binary. Upload will not be performed", re);
232 getRootItem().removeChild((AbstractJcrNodeAdapter) item);
233 return null;
234 }
235 }
236 getOrCreateProperty(item, File.PROPERTY_FILENAME, String.class).setValue(newValue.getFileName());
237 getOrCreateProperty(item, File.PROPERTY_CONTENTTYPE, String.class).setValue(newValue.getMimeType());
238 getOrCreateProperty(item, File.PROPERTY_LASTMODIFIED, Date.class).setValue(new Date());
239 getOrCreateProperty(item, File.PROPERTY_SIZE, Long.class).setValue(newValue.getFileSize());
240 getOrCreateProperty(item, File.PROPERTY_EXTENSION, String.class).setValue(newValue.getExtension());
241 return item;
242 }
243
244
245
246
247
248
249 protected void handleInvalid(T newValue, Item item) {
250 if (((AbstractJcrNodeAdapter) item).getParent() != null) {
251 ((AbstractJcrNodeAdaptervaadin/integration/jcr/AbstractJcrNodeAdapter.html#AbstractJcrNodeAdapter">AbstractJcrNodeAdapter) item).getParent().removeChild((AbstractJcrNodeAdapter) item);
252 }
253 }
254
255
256
257
258 protected Property getOrCreateProperty(Item item, String propertyName, Class<?> type) {
259 if (item.getItemProperty(propertyName) == null) {
260 item.addItemProperty(propertyName, new ObjectProperty<>(null, type));
261 }
262 return item.getItemProperty(propertyName);
263 }
264
265
266
267
268 protected JcrNodeAdapter getRootItem() {
269 return (JcrNodeAdapter) relatedFormItem;
270 }
271
272
273
274
275 protected void populateStoredChildItems(JcrNodeAdapter rootItem) {
276 List<Node> childNodes = getStoredChildNodes(rootItem);
277 for (Node child : childNodes) {
278 JcrNodeAdapterntegration/jcr/JcrNodeAdapter.html#JcrNodeAdapter">JcrNodeAdapter item = new JcrNodeAdapter(child);
279 rootItem.addChild(item);
280 }
281 }
282
283
284
285
286 protected List<Node> getStoredChildNodes(JcrNodeAdapter parent) {
287 try {
288 if (!(parent instanceof JcrNewNodeAdapter) && parent.getJcrItem().hasNodes()) {
289 return NodeUtil.asList(NodeUtil.getNodes(parent.getJcrItem(), NodeUtil.MAGNOLIA_FILTER));
290 }
291 } catch (RepositoryException re) {
292 log.warn("Not able to access the Child Nodes of the following Node Identifier {}", parent.getItemId(), re);
293 }
294 return new ArrayList<>();
295 }
296
297 @Override
298 public String getBasePropertyName() {
299 return this.definition.getBinaryNodeName();
300 }
301
302 @Override
303 public Class<T> getType() {
304 return this.type;
305 }
306
307 @Override
308 public boolean isReadOnly() {
309 final Property property = relatedFormItem.getItemProperty(JcrConstants.JCR_DATA);
310 return property != null && property.isReadOnly();
311 }
312
313 @Override
314 @Deprecated
315 public void setReadOnly(boolean isReadOnly) {
316 log.warn("Transformer #readOnly cannot be set, it just mirrors read-only state of the underlying Property.");
317 }
318
319
320
321
322 @Override
323 public boolean hasI18NSupport() {
324 return definition.isI18n();
325 }
326
327 @Override
328 public void setLocale(Locale locale) {
329 this.locale = locale;
330 }
331
332
333
334
335 @Deprecated
336 @Override
337 public void setI18NPropertyName(String i18nPropertyName) {
338 this.i18NPropertyName = i18nPropertyName;
339 }
340
341 @Override
342 public Locale getLocale() {
343 return this.locale;
344 }
345
346 }