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