View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.syncope.client.ui.commons.markup.html.form;
20  
21  import com.googlecode.wicket.kendo.ui.resource.KendoCultureResourceReference;
22  import java.io.Serializable;
23  import java.text.ParseException;
24  import java.util.Date;
25  import java.util.List;
26  import java.util.Optional;
27  import java.util.function.Predicate;
28  import org.apache.commons.lang3.time.FastDateFormat;
29  import org.apache.syncope.common.lib.Attr;
30  import org.apache.syncope.common.lib.Attributable;
31  import org.apache.wicket.markup.head.IHeaderResponse;
32  import org.apache.wicket.markup.head.JavaScriptHeaderItem;
33  import org.apache.wicket.markup.html.list.ListItem;
34  import org.apache.wicket.model.IModel;
35  import org.apache.wicket.model.Model;
36  import org.springframework.util.StringUtils;
37  
38  public abstract class DateFieldPanel extends FieldPanel<Date> {
39  
40      private static final long serialVersionUID = -428975732068281726L;
41  
42      protected final FastDateFormat fmt;
43  
44      protected DateFieldPanel(final String id, final String name, final IModel<Date> model, final FastDateFormat fmt) {
45          super(id, name, model);
46          this.fmt = fmt;
47      }
48  
49      @Override
50      public FieldPanel<Date> setNewModel(final List<Serializable> list) {
51          setNewModel(new Model<>() {
52  
53              private static final long serialVersionUID = 527651414610325237L;
54  
55              @Override
56              public Date getObject() {
57                  Date date = null;
58                  if (list != null && !list.isEmpty() && StringUtils.hasText(list.get(0).toString())) {
59                      try {
60                          // Parse string using datePattern
61                          date = fmt.parse(list.get(0).toString());
62                      } catch (ParseException e) {
63                          LOG.error("invalid parse exception", e);
64                      }
65                  }
66  
67                  return date;
68              }
69  
70              @Override
71              public void setObject(final Date object) {
72                  list.clear();
73                  if (object != null) {
74                      list.add(fmt.format(object));
75                  }
76              }
77          });
78  
79          return this;
80      }
81  
82      @SuppressWarnings("rawtypes")
83      @Override
84      public FieldPanel<Date> setNewModel(final ListItem item) {
85          IModel<Date> model = new Model<>() {
86  
87              private static final long serialVersionUID = 6799404673615637845L;
88  
89              @Override
90              public Date getObject() {
91                  Date date = null;
92  
93                  final Object obj = item.getModelObject();
94  
95                  if (obj != null && !obj.toString().isEmpty()) {
96                      if (obj instanceof String) {
97                          // Parse string using datePattern
98                          try {
99                              date = fmt.parse(obj.toString());
100                         } catch (ParseException e) {
101                             LOG.error("While parsing date", e);
102                         }
103                     } else if (obj instanceof Date) {
104                         // Don't parse anything
105                         date = (Date) obj;
106                     } else {
107                         // consider Long
108                         date = new Date((Long) obj);
109                     }
110                 }
111 
112                 return date;
113             }
114 
115             @Override
116             @SuppressWarnings("unchecked")
117             public void setObject(final Date object) {
118                 item.setModelObject(Optional.ofNullable(object).map(fmt::format).orElse(null));
119             }
120         };
121 
122         field.setModel(model);
123         return this;
124     }
125 
126     @Override
127     public FieldPanel<Date> setNewModel(final Attributable attributable, final String schema) {
128         field.setModel(new Model<>() {
129 
130             private static final long serialVersionUID = -4214654722524358000L;
131 
132             @Override
133             public Date getObject() {
134                 return attributable.getPlainAttr(schema).map(Attr::getValues).filter(Predicate.not(List::isEmpty)).
135                         map(values -> {
136                             try {
137                                 return fmt.parse(values.get(0));
138                             } catch (ParseException e) {
139                                 LOG.error("While parsing date", e);
140                                 return null;
141                             }
142                         }).orElse(null);
143             }
144 
145             @Override
146             public void setObject(final Date object) {
147                 attributable.getPlainAttr(schema).ifPresent(plainAttr -> {
148                     plainAttr.getValues().clear();
149                     Optional.ofNullable(object).ifPresent(o -> plainAttr.getValues().add(fmt.format(object)));
150                 });
151             }
152         });
153 
154         return this;
155     }
156 
157     @Override
158     public void renderHead(final IHeaderResponse response) {
159         super.renderHead(response);
160         response.render(JavaScriptHeaderItem.forReference(new KendoCultureResourceReference(getLocale())));
161     }
162 }