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