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.datesql;
021
022import java.sql.Date;
023import java.util.Calendar;
024
025import org.apache.isis.applib.adapters.EncoderDecoder;
026import org.apache.isis.applib.adapters.Parser;
027import org.apache.isis.applib.clock.Clock;
028import org.apache.isis.core.commons.config.IsisConfiguration;
029import org.apache.isis.core.metamodel.facetapi.FacetHolder;
030import org.apache.isis.core.progmodel.facets.object.value.ValueSemanticsProviderContext;
031import org.apache.isis.core.progmodel.facets.value.date.DateValueSemanticsProviderAbstract;
032import org.apache.isis.core.progmodel.facets.value.dateutil.JavaUtilDateValueSemanticsProvider;
033import org.apache.isis.core.progmodel.facets.value.timesql.JavaSqlTimeValueSemanticsProvider;
034
035/**
036 * An adapter that handles {@link java.sql.Date} with only date component.
037 * 
038 * @see JavaUtilDateValueSemanticsProvider
039 * @see JavaSqlTimeValueSemanticsProvider
040 */
041public class JavaSqlDateValueSemanticsProvider extends DateValueSemanticsProviderAbstract<Date> {
042
043    private static final Date DEFAULT_VALUE = null; // no default
044
045    /**
046     * Required because implementation of {@link Parser} and
047     * {@link EncoderDecoder}.
048     */
049    public JavaSqlDateValueSemanticsProvider() {
050        this(null, null, null);
051    }
052
053    public JavaSqlDateValueSemanticsProvider(final FacetHolder holder, final IsisConfiguration configuration, final ValueSemanticsProviderContext context) {
054        super(holder, Date.class, Immutability.NOT_IMMUTABLE, EqualByContent.NOT_HONOURED, DEFAULT_VALUE, configuration, context);
055    }
056
057    @Override
058    protected Date add(final Date original, final int years, final int months, final int days, final int hours, final int minutes) {
059        final Date date = original;
060        final Calendar cal = Calendar.getInstance();
061        cal.setTime(date);
062        cal.set(Calendar.HOUR, 0);
063        cal.set(Calendar.HOUR_OF_DAY, 0);
064        cal.set(Calendar.MINUTE, 0);
065        cal.set(Calendar.SECOND, 0);
066        cal.set(Calendar.AM_PM, 0);
067        cal.set(Calendar.MILLISECOND, 0);
068
069        cal.add(Calendar.YEAR, years);
070        cal.add(Calendar.MONTH, months);
071        cal.add(Calendar.DAY_OF_MONTH, days);
072
073        return setDate(cal.getTime());
074    }
075
076    @Override
077    protected java.util.Date dateValue(final Object value) {
078        return (java.util.Date) value;
079    }
080
081    @Override
082    protected Date setDate(final java.util.Date date) {
083        return new Date(date.getTime());
084    }
085
086    @Override
087    protected Date now() {
088        return new Date(Clock.getTime());
089    }
090
091}