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    
020    package org.apache.isis.core.progmodel.facets.value.timestamp;
021    
022    import java.text.DateFormat;
023    import java.util.Date;
024    import java.util.Map;
025    
026    import org.apache.isis.applib.value.TimeStamp;
027    import org.apache.isis.core.commons.config.ConfigurationConstants;
028    import org.apache.isis.core.commons.config.IsisConfiguration;
029    import org.apache.isis.core.metamodel.facetapi.FacetHolder;
030    import org.apache.isis.core.progmodel.facets.object.value.ValueSemanticsProviderContext;
031    import org.apache.isis.core.progmodel.facets.value.ValueSemanticsProviderAbstractTemporal;
032    
033    public abstract class TimeStampValueSemanticsProviderAbstract<T> extends ValueSemanticsProviderAbstractTemporal<T> {
034    
035        private static final Object DEFAULT_VALUE = null; // no default
036        private static final int TYPICAL_LENGTH = 12;
037    
038        private static final boolean IMMUTABLE = false;
039        private static final boolean EQUAL_BY_CONTENT = false;
040    
041        protected static void initFormats(final Map<String, DateFormat> formats) {
042            formats.put("iso", createDateFormat("yyyy-MM-dd HH:mm:ss.SSS"));
043            formats.put(ISO_ENCODING_FORMAT, createDateFormat("yyyyMMdd'T'HHmmssSSS"));
044            formats.put("medium", DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.LONG));
045            formats.put("short", DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.LONG));
046        }
047    
048        @SuppressWarnings("unchecked")
049        public TimeStampValueSemanticsProviderAbstract(final FacetHolder holder, final Class<T> adaptedClass,
050            final IsisConfiguration configuration, final ValueSemanticsProviderContext context) {
051            super("timestamp", holder, adaptedClass, TYPICAL_LENGTH, IMMUTABLE, EQUAL_BY_CONTENT, (T) DEFAULT_VALUE,
052                configuration, context);
053            final String formatRequired = configuration.getString(ConfigurationConstants.ROOT + "value.format.timestamp");
054            if (formatRequired == null) {
055                format = formats().get(defaultFormat());
056            } else {
057                setMask(formatRequired);
058            }
059        }
060    
061        @Override
062        public int getLevel() {
063            return TIMESTAMP;
064        }
065    
066        @Override
067        protected T add(final T original, final int years, final int months, final int days, final int hours,
068            final int minutes) {
069            return original;
070        }
071    
072        @Override
073        protected Date dateValue(final Object value) {
074            return new Date(((TimeStamp) value).longValue());
075        }
076    
077        @Override
078        protected String defaultFormat() {
079            return "short";
080        }
081    
082        @Override
083        public String toString() {
084            return "TimeStampValueSemanticsProvider: " + format;
085        }
086    
087    }