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.timestampsql;
021
022import java.sql.Timestamp;
023import java.text.DateFormat;
024import java.util.Date;
025import java.util.Map;
026
027import com.google.common.collect.Maps;
028
029import org.apache.isis.applib.adapters.EncoderDecoder;
030import org.apache.isis.applib.adapters.Parser;
031import org.apache.isis.core.commons.config.IsisConfiguration;
032import org.apache.isis.core.metamodel.facetapi.FacetHolder;
033import org.apache.isis.core.metamodel.facets.object.parseable.InvalidEntryException;
034import org.apache.isis.core.metamodel.facets.properties.defaults.PropertyDefaultFacet;
035import org.apache.isis.core.progmodel.facets.object.value.ValueSemanticsProviderContext;
036import org.apache.isis.core.progmodel.facets.value.timestamp.TimeStampValueSemanticsProviderAbstract;
037
038public 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
052     * {@link EncoderDecoder}.
053     */
054    public JavaSqlTimeStampValueSemanticsProvider() {
055        this(null, null, null);
056    }
057
058    public JavaSqlTimeStampValueSemanticsProvider(final FacetHolder holder, final IsisConfiguration configuration, 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}