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.timestampsql;
021    
022    import java.sql.Timestamp;
023    import java.text.DateFormat;
024    import java.util.Date;
025    import java.util.Map;
026    
027    import org.apache.isis.applib.adapters.EncoderDecoder;
028    import org.apache.isis.applib.adapters.Parser;
029    import org.apache.isis.core.commons.config.IsisConfiguration;
030    import org.apache.isis.core.metamodel.facetapi.FacetHolder;
031    import org.apache.isis.core.metamodel.facets.object.parseable.InvalidEntryException;
032    import org.apache.isis.core.metamodel.facets.properties.defaults.PropertyDefaultFacet;
033    import org.apache.isis.core.progmodel.facets.object.value.ValueSemanticsProviderContext;
034    import org.apache.isis.core.progmodel.facets.value.timestamp.TimeStampValueSemanticsProviderAbstract;
035    
036    import com.google.inject.internal.Maps;
037    
038    public class JavaSqlTimeStampValueSemanticsProvider extends TimeStampValueSemanticsProviderAbstract<java.sql.Timestamp> {
039    
040        public static final boolean isAPropertyDefaultFacet() {
041            return PropertyDefaultFacet.class.isAssignableFrom(JavaSqlTimeStampValueSemanticsProvider.class);
042        }
043    
044        private static Map<String, DateFormat> formats = Maps.newHashMap();
045    
046        static {
047            initFormats(formats);
048        }
049    
050        /**
051         * Required because implementation of {@link Parser} and {@link EncoderDecoder}.
052         */
053        public JavaSqlTimeStampValueSemanticsProvider() {
054            this(null, null, null);
055        }
056    
057        public JavaSqlTimeStampValueSemanticsProvider(final FacetHolder holder, final IsisConfiguration configuration,
058            final ValueSemanticsProviderContext context) {
059            super(holder, java.sql.Timestamp.class, configuration, context);
060        }
061    
062        // //////////////////////////////////////////////////////////////////
063        // temporal-specific stuff
064        // //////////////////////////////////////////////////////////////////
065    
066        @Override
067        protected Date dateValue(final Object value) {
068            return new Date(((Timestamp) value).getTime());
069        }
070    
071        @Override
072        protected Map<String, DateFormat> formats() {
073            return formats;
074        }
075    
076        @Override
077        protected Timestamp now() {
078            throw new InvalidEntryException("Can't change a timestamp.");
079        }
080    
081        @Override
082        protected Timestamp setDate(final Date date) {
083            return new Timestamp(date.getTime());
084        }
085    
086    }