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.timestamp;
021
022import java.text.DateFormat;
023import java.util.ArrayList;
024import java.util.Date;
025import java.util.List;
026import java.util.Map;
027
028import org.apache.isis.applib.profiles.Localization;
029import org.apache.isis.applib.value.TimeStamp;
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 TimeStampValueSemanticsProviderAbstract<T> extends ValueSemanticsProviderAbstractTemporal<T> {
037
038    private static final Object DEFAULT_VALUE = null; // no default
039    private static final int TYPICAL_LENGTH = 25;
040
041    protected static void initFormats(final Map<String, DateFormat> formats) {
042        formats.put(ISO_ENCODING_FORMAT, createDateEncodingFormat("yyyyMMdd'T'HHmmssSSS")); 
043        formats.put("short", DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.LONG));
044    }
045
046    @SuppressWarnings("unchecked")
047    public TimeStampValueSemanticsProviderAbstract(final FacetHolder holder, final Class<T> adaptedClass, final IsisConfiguration configuration, final ValueSemanticsProviderContext context) {
048        super("timestamp", holder, adaptedClass, TYPICAL_LENGTH, Immutability.NOT_IMMUTABLE, EqualByContent.NOT_HONOURED, (T) DEFAULT_VALUE, configuration, context);
049        final String formatRequired = configuration.getString(ConfigurationConstants.ROOT + "value.format.timestamp");
050        if (formatRequired == null) {
051            format = formats().get(defaultFormat());
052        } else {
053            setMask(formatRequired);
054        }
055    }
056
057    @Override
058    protected T add(final T original, final int years, final int months, final int days, final int hours, final int minutes) {
059        return original;
060    }
061
062    @Override
063    protected Date dateValue(final Object value) {
064        return new Date(((TimeStamp) value).longValue());
065    }
066
067    @Override
068    protected String defaultFormat() {
069        return "short";
070    }
071
072    @Override
073    public String toString() {
074        return "TimeStampValueSemanticsProvider: " + format;
075    }
076
077    @Override
078    protected DateFormat format(final Localization localization) {
079        final DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.LONG, localization.getLocale());
080        dateFormat.setTimeZone(localization.getTimeZone());
081        return dateFormat;
082    }
083
084    protected List<DateFormat> formatsToTry(Localization localization) {
085        List<DateFormat> formats = new ArrayList<DateFormat>();
086
087        formats.add(DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.LONG, localization.getLocale()));
088        formats.add(DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.LONG, localization.getLocale()));
089        formats.add(createDateFormat("yyyy-MM-dd HH:mm:ss.SSS"));
090
091        for (DateFormat format : formats) {
092            format.setTimeZone(localization.getTimeZone());
093        }
094
095        return formats;
096    }
097}