001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *
010 *        http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License.
018 */
019
020package org.apache.isis.core.progmodel.facets.value.datetimejoda;
021
022import java.text.DateFormat;
023import java.util.ArrayList;
024import java.util.Calendar;
025import java.util.List;
026import java.util.Locale;
027import java.util.Map;
028
029import com.google.common.collect.Maps;
030
031import org.apache.isis.applib.profiles.Localization;
032import org.apache.isis.core.commons.config.ConfigurationConstants;
033import org.apache.isis.core.commons.config.IsisConfiguration;
034import org.apache.isis.core.metamodel.facetapi.FacetHolder;
035import org.apache.isis.core.progmodel.facets.object.value.ValueSemanticsProviderContext;
036import org.apache.isis.core.progmodel.facets.value.ValueSemanticsProviderAbstractTemporal;
037
038public abstract class JodaDateTimeValueSemanticsProviderAbstract<T> extends ValueSemanticsProviderAbstractTemporal<T> {
039
040    private static Map<String, DateFormat> formats = Maps.newHashMap();
041
042    static {
043        formats.put(ISO_ENCODING_FORMAT, createDateEncodingFormat("yyyyMMdd"));
044        formats.put("iso", createDateFormat("yyyy-MM-dd"));
045        formats.put("medium", DateFormat.getDateInstance(DateFormat.MEDIUM));
046    }
047
048    public JodaDateTimeValueSemanticsProviderAbstract(final FacetHolder holder, final Class<T> adaptedClass, final T defaultValue, final IsisConfiguration configuration, final ValueSemanticsProviderContext context) {
049        super("date", holder, adaptedClass, 12, Immutability.IMMUTABLE, EqualByContent.HONOURED, defaultValue, configuration, context);
050
051        final String formatRequired = configuration.getString(ConfigurationConstants.ROOT + "value.format.date");
052        if (formatRequired == null) {
053            format = formats().get(defaultFormat());
054        } else {
055            setMask(formatRequired);
056        }
057    }
058
059
060    // //////////////////////////////////////////////////////////////////
061    // temporal-specific stuff
062    // //////////////////////////////////////////////////////////////////
063
064    @Override
065    protected void clearFields(final Calendar cal) {
066        cal.set(Calendar.HOUR, 0);
067        cal.set(Calendar.HOUR_OF_DAY, 0);
068        cal.set(Calendar.MINUTE, 0);
069        cal.set(Calendar.SECOND, 0);
070        cal.set(Calendar.AM_PM, 0);
071        cal.set(Calendar.MILLISECOND, 0);
072    }
073
074    @Override
075    protected String defaultFormat() {
076        return "medium";
077    }
078
079    @Override
080    protected boolean ignoreTimeZone() {
081        return true;
082    }
083
084    @Override
085    protected Map<String, DateFormat> formats() {
086        return formats;
087    }
088
089    @Override
090    public String toString() {
091        return "DateValueSemanticsProvider: " + format;
092    }
093
094    @Override
095    protected DateFormat format(final Localization localization) {
096        final DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, localization.getLocale());
097        dateFormat.setTimeZone(UTC_TIME_ZONE);
098        return dateFormat;
099    }
100
101    protected List<DateFormat> formatsToTry(Localization localization) {
102        List<DateFormat> formats = new ArrayList<DateFormat>();
103
104        Locale locale = localization == null ? Locale.getDefault() : localization.getLocale();
105        formats.add(DateFormat.getDateInstance(DateFormat.LONG, locale));
106        formats.add(DateFormat.getDateInstance(DateFormat.MEDIUM, locale));
107        formats.add(DateFormat.getDateInstance(DateFormat.SHORT, locale));
108        formats.add(createDateFormat("yyyy-MM-dd"));
109        formats.add(createDateFormat("yyyyMMdd"));
110
111        for (DateFormat format : formats) {
112            format.setTimeZone(UTC_TIME_ZONE);
113        }
114
115        return formats;
116    }
117
118}