View Javadoc
1   /**
2    * This file Copyright (c) 2019 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.ui.form.field.definition.migration;
35  
36  import info.magnolia.ui.field.CompositeFieldDefinition;
37  import info.magnolia.ui.field.UploadFieldDefinition;
38  import info.magnolia.ui.form.field.transformer.item.FileTransformer;
39  
40  import java.io.File;
41  import java.util.Collections;
42  
43  /**
44   * Converter from old {@link info.magnolia.ui.form.field.definition.BasicUploadFieldDefinition} to wrapped {@link UploadFieldDefinition} in {@link CompositeFieldDefinition}.
45   */
46  public class BasicUploadFieldDefinitionConverter extends AbstractComplexFieldDefinitionConverter<File, CompositeFieldDefinition<File>, info.magnolia.ui.form.field.definition.BasicUploadFieldDefinition> {
47  
48      @Override
49      public CompositeFieldDefinition<File> convert(info.magnolia.ui.form.field.definition.BasicUploadFieldDefinition oldDefinition) {
50          CompositeFieldDefinition<File> wrappedDefinition = super.convert(oldDefinition);
51  
52          UploadFieldDefinition newDefinition = new UploadFieldDefinitionConverter().convert(oldDefinition);
53          newDefinition.setLabel("");
54          newDefinition.setName(oldDefinition.getName());
55  
56          wrappedDefinition.setName(oldDefinition.getBinaryNodeName());
57          wrappedDefinition.setProperties(Collections.singletonList(newDefinition));
58  
59          return wrappedDefinition;
60      }
61  
62      @Override
63      CompositeFieldDefinition<File> createNewDefinition(info.magnolia.ui.form.field.definition.BasicUploadFieldDefinition oldDefinition) {
64          return new CompositeFieldDefinition<>();
65      }
66  
67      @Override
68      protected boolean supportsTransformerClass(Class transformerClass) {
69          return transformerClass.equals(FileTransformer.class);
70      }
71  
72      /**
73       * Converter from old {@link info.magnolia.ui.form.field.definition.BasicUploadFieldDefinition} to new {@link UploadFieldDefinition}.
74       */
75      private class UploadFieldDefinitionConverter extends AbstractConfiguredFieldDefinitionConverter<File, UploadFieldDefinition, info.magnolia.ui.form.field.definition.BasicUploadFieldDefinition> {
76  
77          @Override
78          public UploadFieldDefinition convert(info.magnolia.ui.form.field.definition.BasicUploadFieldDefinition oldDefinition) {
79              UploadFieldDefinition newDefinition = super.convert(oldDefinition);
80              newDefinition.setAllowedMimeTypePattern(oldDefinition.getAllowedMimeTypePattern());
81              newDefinition.setDeleteLabel(oldDefinition.getDeleteCaption());
82              newDefinition.setDropZoneLabel(oldDefinition.getDropZoneCaption());
83              newDefinition.setErrorNoteLabel(oldDefinition.getErrorNoteCaption());
84              newDefinition.setFileDetailFormatLabel(oldDefinition.getFileDetailFormatCaption());
85              newDefinition.setFileDetailHeaderLabel(oldDefinition.getFileDetailHeaderCaption());
86              newDefinition.setFileDetailNameLabel(oldDefinition.getFileDetailNameCaption());
87              newDefinition.setFileDetailSizeLabel(oldDefinition.getFileDetailSizeCaption());
88              newDefinition.setFileDetailSourceLabel(oldDefinition.getFileDetailSourceCaption());
89              newDefinition.setInProgressLabel(oldDefinition.getInProgressCaption());
90              newDefinition.setInProgressRatioLabel(oldDefinition.getInProgressRatioCaption());
91              newDefinition.setMaxUploadSize(oldDefinition.getMaxUploadSize());
92              newDefinition.setSelectAnotherLabel(oldDefinition.getSelectAnotherCaption());
93              newDefinition.setSelectNewLabel(oldDefinition.getSelectNewCaption());
94              newDefinition.setSizeInterruption(oldDefinition.getSizeInterruption());
95              newDefinition.setSuccessNoteLabel(oldDefinition.getSuccessNoteCaption());
96              newDefinition.setTypeInterruption(oldDefinition.getTypeInterruption());
97              newDefinition.setUserInterruption(oldDefinition.getUserInterruption());
98              newDefinition.setWarningNoteLabel(oldDefinition.getWarningNoteCaption());
99              return newDefinition;
100         }
101 
102         @Override
103         UploadFieldDefinition createNewDefinition(info.magnolia.ui.form.field.definition.BasicUploadFieldDefinition oldDefinition) {
104             return new UploadFieldDefinition();
105         }
106 
107         @Override
108         File convertDefaultValue(info.magnolia.ui.form.field.definition.BasicUploadFieldDefinition oldDefinition, UploadFieldDefinition newDefinition) {
109             return null;
110         }
111     }
112 }