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.time;
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 org.apache.isis.applib.profiles.Localization;
030import org.apache.isis.core.commons.config.ConfigurationConstants;
031import org.apache.isis.core.commons.config.IsisConfiguration;
032import org.apache.isis.core.metamodel.facetapi.FacetHolder;
033import org.apache.isis.core.progmodel.facets.object.value.ValueSemanticsProviderContext;
034import org.apache.isis.core.progmodel.facets.value.ValueSemanticsProviderAbstractTemporal;
035
036public abstract class TimeValueSemanticsProviderAbstract<T> extends ValueSemanticsProviderAbstractTemporal<T> {
037
038    private static final Object DEFAULT_VALUE = null; // no default
039    private static final int TYPICAL_LENGTH = 8;
040
041    protected static void initFormats(final Map<String, DateFormat> formats) {
042        formats.put(ISO_ENCODING_FORMAT, createDateEncodingFormat("HHmmssSSS")); 
043        formats.put("short", DateFormat.getTimeInstance(DateFormat.SHORT)); 
044    }
045
046    @SuppressWarnings("unchecked")
047    public TimeValueSemanticsProviderAbstract(final FacetHolder holder, final Class<T> adaptedClass, final IsisConfiguration configuration, final ValueSemanticsProviderContext context) {
048        super("time", holder, adaptedClass, TYPICAL_LENGTH, Immutability.NOT_IMMUTABLE, EqualByContent.NOT_HONOURED, (T) DEFAULT_VALUE, configuration, context);
049
050        final String formatRequired = configuration.getString(ConfigurationConstants.ROOT + "value.format.time");
051        if (formatRequired == null) {
052            format = formats().get(defaultFormat());
053        } else {
054            setMask(formatRequired);
055        }
056    }
057
058    // //////////////////////////////////////////////////////////////////
059    // DateValueFacet
060    // //////////////////////////////////////////////////////////////////
061
062    // //////////////////////////////////////////////////////////////////
063    // temporal-specific stuff
064    // //////////////////////////////////////////////////////////////////
065
066    @Override
067    protected void clearFields(final Calendar cal) {
068        cal.set(Calendar.YEAR, 1970);
069        cal.set(Calendar.MONTH, 0);
070        cal.set(Calendar.DAY_OF_MONTH, 1);
071    }
072
073    @Override
074    protected String defaultFormat() {
075        return "short";
076    }
077
078    @Override
079    public String toString() {
080        return "TimeValueSemanticsProvider: " + format;
081    }
082    
083@Override 
084     protected DateFormat format(final Localization localization) { 
085         final DateFormat dateFormat = DateFormat.getTimeInstance(DateFormat.SHORT, localization.getLocale()); 
086         dateFormat.setTimeZone(UTC_TIME_ZONE); 
087         return dateFormat; 
088     } 
089  
090     protected List<DateFormat> formatsToTry(Localization localization) { 
091         List<DateFormat> formats = new ArrayList<DateFormat>(); 
092          
093         Locale locale = localization == null ? Locale.getDefault() : localization.getLocale(); 
094         formats.add(DateFormat.getTimeInstance(DateFormat.LONG, locale)); 
095         formats.add(DateFormat.getTimeInstance(DateFormat.MEDIUM, locale)); 
096         formats.add(DateFormat.getTimeInstance(DateFormat.SHORT, locale)); 
097         formats.add(createDateFormat("HH:mm:ss.SSS")); 
098         formats.add(createDateFormat("HHmmssSSS")); 
099         formats.add(createDateFormat("HH:mm:ss")); 
100         formats.add(createDateFormat("HHmmss")); 
101  
102         for (DateFormat format : formats) { 
103             format.setTimeZone(UTC_TIME_ZONE); 
104         } 
105          
106         return formats; 
107     } 
108}