View Javadoc
1   /**
2    * This file Copyright (c) 2015-2017 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.module.rssaggregator.field.transformer;
35  
36  import info.magnolia.objectfactory.Components;
37  import info.magnolia.ui.api.i18n.I18NAuthoringSupport;
38  import info.magnolia.ui.form.field.definition.CompositeFieldDefinition;
39  import info.magnolia.ui.form.field.definition.ConfiguredFieldDefinition;
40  import info.magnolia.ui.form.field.definition.SwitchableFieldDefinition;
41  import info.magnolia.ui.form.field.transformer.basic.BasicTransformer;
42  import info.magnolia.ui.vaadin.integration.jcr.DefaultProperty;
43  
44  import java.util.ArrayList;
45  import java.util.List;
46  
47  import javax.inject.Inject;
48  
49  import org.slf4j.Logger;
50  import org.slf4j.LoggerFactory;
51  
52  import com.vaadin.v7.data.Item;
53  import com.vaadin.v7.data.Property;
54  import com.vaadin.v7.data.util.PropertysetItem;
55  
56  /**
57   * Transformer for RSSConfig switchable field to handle saving cronString in configuration.
58   */
59  public class RSSSwitchableFieldTransformer extends BasicTransformer<PropertysetItem> {
60  
61      private static final Logger log = LoggerFactory.getLogger(RSSSwitchableFieldTransformer.class);
62  
63      protected List<String> fieldsName;
64  
65      private static String FIELD_KEY_DISABLED = "Disabled";
66      private static String FIELD_KEY_CRONMAKER = "CronMaker";
67      private static String FIELD_KEY_CRONSTRING = "CronString";
68  
69      private static String CONFIG_PROPERTY_NAME_AUTOMATEDIMPORT = "automatedImport";
70      private static String CONFIG_PROPERTY_NAME_CRON = "cron";
71  
72      private static String FIELD_KEY_VALUE = "value";
73      private static String FIELD_KEY_UNIT = "unit";
74  
75      private static String UNIT_MINUTES = "minutes";
76      private static String UNIT_HOURS = "hours";
77      private static String UNIT_DAYS = "days";
78  
79      private static String CRON_MINUTES_PATTERN = "0 0/%d * 1/1 * ? *";
80      private static String CRON_HOURS_PATTERN = "0 0 0/%d 1/1 * ? *";
81      private static String CRON_DAYS_PATTERN = "0 0 0 1/%d * ? *";
82  
83      @Inject
84      public RSSSwitchableFieldTransformer(Item relatedFormItem, ConfiguredFieldDefinition definition, Class<PropertysetItem> type, I18NAuthoringSupport i18NAuthoringSupport, List<String> fieldsName) {
85          super(relatedFormItem, definition, type, i18NAuthoringSupport);
86          fieldsName = new ArrayList<>();
87          List<ConfiguredFieldDefinition> fields = ((SwitchableFieldDefinition) definition).getFields();
88          for (ConfiguredFieldDefinition field : fields) {
89              if (field instanceof CompositeFieldDefinition) {
90                  fieldsName.addAll(((CompositeFieldDefinition) field).getFieldNames());
91              }
92          }
93          this.fieldsName = fieldsName;
94      }
95  
96      /**
97       * @deprecated since 2.5.2 use {@link #RSSSwitchableFieldTransformer(Item, ConfiguredFieldDefinition, Class, I18NAuthoringSupport, List)}
98       */
99      @Deprecated
100     public RSSSwitchableFieldTransformer(Item relatedFormItem, ConfiguredFieldDefinition definition, Class<PropertysetItem> type, List<String> fieldsName) {
101         this(relatedFormItem, definition, type, Components.getComponent(I18NAuthoringSupport.class), fieldsName);
102     }
103 
104     @Override
105     public void writeToItem(PropertysetItem newValues) {
106         String selected = "";
107 
108         String propertyName = definePropertyName();
109         if (newValues.getItemProperty(propertyName) != null) {
110             selected = newValues.getItemProperty(propertyName).getValue().toString();
111             relatedFormItem.addItemProperty(propertyName, newValues.getItemProperty(propertyName));
112 
113             if (selected.equals(FIELD_KEY_DISABLED)) {
114                 relatedFormItem.addItemProperty(CONFIG_PROPERTY_NAME_AUTOMATEDIMPORT, new DefaultProperty<Boolean>(false));
115             } else if (selected.equals(FIELD_KEY_CRONMAKER)) {
116                 relatedFormItem.addItemProperty(CONFIG_PROPERTY_NAME_AUTOMATEDIMPORT, new DefaultProperty<Boolean>(true));
117                 compositeToCronStringTransform((PropertysetItem) newValues.getItemProperty(FIELD_KEY_CRONMAKER).getValue());
118             } else if (selected.equals(FIELD_KEY_CRONSTRING)) {
119                 relatedFormItem.addItemProperty(CONFIG_PROPERTY_NAME_AUTOMATEDIMPORT, new DefaultProperty<Boolean>(true));
120                 relatedFormItem.addItemProperty(CONFIG_PROPERTY_NAME_CRON, newValues.getItemProperty(FIELD_KEY_CRONSTRING));
121             }
122         }
123     }
124 
125     @Override
126     public PropertysetItem readFromItem() {
127         PropertysetItem switchableFieldNewValues = new PropertysetItem();
128 
129         String propertyName = definePropertyName();
130         if (relatedFormItem.getItemProperty(propertyName) != null) {
131             switchableFieldNewValues.addItemProperty(propertyName, relatedFormItem.getItemProperty(propertyName));
132             switchableFieldNewValues.addItemProperty(FIELD_KEY_DISABLED, relatedFormItem.getItemProperty(FIELD_KEY_DISABLED));
133         }
134         if (relatedFormItem.getItemProperty(CONFIG_PROPERTY_NAME_CRON) != null) {
135             switchableFieldNewValues.addItemProperty(FIELD_KEY_CRONMAKER, new DefaultProperty<PropertysetItem>(PropertysetItem.class, cronStringToCompositeTransform()));
136             switchableFieldNewValues.addItemProperty(FIELD_KEY_CRONSTRING, relatedFormItem.getItemProperty(CONFIG_PROPERTY_NAME_CRON));
137         }
138 
139         return switchableFieldNewValues;
140     }
141 
142     private void compositeToCronStringTransform(PropertysetItem newValues) {
143         String cronString = "";
144 
145         // transform to cron string and save to cron property
146         Property<?> value = newValues.getItemProperty(FIELD_KEY_VALUE);
147         Property<?> unit = newValues.getItemProperty(FIELD_KEY_UNIT);
148         int number;
149 
150         try {
151             number = Integer.parseInt(value.getValue().toString());
152         } catch (NumberFormatException ex) {
153             log.warn("Property expect integer. Wrong integer format. Setting default value \"every 15 minutes\".");
154             cronString = String.format(CRON_MINUTES_PATTERN, 15);
155             relatedFormItem.addItemProperty(CONFIG_PROPERTY_NAME_CRON, new DefaultProperty<String>(String.class, cronString));
156             return;
157         }
158 
159         if (unit.getValue().toString().equalsIgnoreCase(UNIT_MINUTES) && number > 0) {
160             cronString = String.format(CRON_MINUTES_PATTERN, number);
161         } else if (unit.getValue().toString().equalsIgnoreCase(UNIT_HOURS) && number > 0) {
162             cronString = String.format(CRON_HOURS_PATTERN, number);
163         } else if (unit.getValue().toString().equalsIgnoreCase(UNIT_DAYS) && number > 0) {
164             cronString = String.format(CRON_DAYS_PATTERN, number);
165         } else {
166             //default value "every 15 minutes"
167             cronString = String.format(CRON_MINUTES_PATTERN, 15);
168         }
169 
170         relatedFormItem.addItemProperty(CONFIG_PROPERTY_NAME_CRON, new DefaultProperty<String>(String.class, cronString));
171     }
172 
173     private PropertysetItem cronStringToCompositeTransform() {
174         String cronString = "";
175         PropertysetItem newValue = new PropertysetItem();
176         if (relatedFormItem.getItemProperty(CONFIG_PROPERTY_NAME_CRON) != null) {
177             cronString = relatedFormItem.getItemProperty(CONFIG_PROPERTY_NAME_CRON).getValue().toString();
178         }
179 
180         String[] cronStringArray = cronString.split(" ");
181         if (cronStringArray.length == 7) {
182             if (cronStringArray[1].contains("0/")) {
183                 newValue.addItemProperty(FIELD_KEY_VALUE, new DefaultProperty<String>(String.class, cronStringArray[1].split("/")[1]));
184                 newValue.addItemProperty(FIELD_KEY_UNIT, new DefaultProperty<String>(String.class, UNIT_MINUTES));
185             } else if (cronStringArray[2].contains("0/")) {
186                 newValue.addItemProperty(FIELD_KEY_VALUE, new DefaultProperty<String>(String.class, cronStringArray[2].split("/")[1]));
187                 newValue.addItemProperty(FIELD_KEY_UNIT, new DefaultProperty<String>(String.class, UNIT_HOURS));
188             } else if (cronStringArray[3].contains("1/")) {
189                 newValue.addItemProperty(FIELD_KEY_VALUE, new DefaultProperty<String>(String.class, cronStringArray[3].split("/")[1]));
190                 newValue.addItemProperty(FIELD_KEY_UNIT, new DefaultProperty<String>(String.class, UNIT_DAYS));
191             } else {
192                 //unsupported cron value, return default value "every 15 minutes"
193                 newValue.addItemProperty(FIELD_KEY_VALUE, new DefaultProperty<String>(String.class, "15"));
194                 newValue.addItemProperty(FIELD_KEY_UNIT, new DefaultProperty<String>(String.class, "minutes"));
195             }
196         } else {
197             //invalid cron format, return value null, unit minutes
198             newValue.addItemProperty(FIELD_KEY_VALUE, new DefaultProperty<String>(String.class, ""));
199             newValue.addItemProperty(FIELD_KEY_UNIT, new DefaultProperty<String>(String.class, "minutes"));
200         }
201 
202         return newValue;
203     }
204 }