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.timesql;
021
022 import java.sql.Time;
023 import java.text.DateFormat;
024 import java.util.Calendar;
025 import java.util.Date;
026 import java.util.Map;
027
028 import org.apache.isis.applib.adapters.EncoderDecoder;
029 import org.apache.isis.applib.adapters.Parser;
030 import org.apache.isis.applib.clock.Clock;
031 import org.apache.isis.core.commons.config.IsisConfiguration;
032 import org.apache.isis.core.metamodel.facetapi.FacetHolder;
033 import org.apache.isis.core.progmodel.facets.object.value.ValueSemanticsProviderContext;
034 import org.apache.isis.core.progmodel.facets.value.time.TimeValueSemanticsProviderAbstract;
035
036 import com.google.inject.internal.Maps;
037
038 /**
039 * Treats {@link java.sql.Time} as a time-only value type.
040 *
041 */
042 public class JavaSqlTimeValueSemanticsProvider extends TimeValueSemanticsProviderAbstract<java.sql.Time> {
043 private static Map<String, DateFormat> formats = Maps.newHashMap();
044
045 static {
046 initFormats(formats);
047 }
048
049 /**
050 * Required because implementation of {@link Parser} and {@link EncoderDecoder}.
051 */
052 public JavaSqlTimeValueSemanticsProvider() {
053 this(null, null, null);
054 }
055
056 public JavaSqlTimeValueSemanticsProvider(final FacetHolder holder, final IsisConfiguration configuration,
057 final ValueSemanticsProviderContext context) {
058 super(holder, java.sql.Time.class, configuration, context);
059 }
060
061 @Override
062 public Time add(final Time original, final int years, final int months, final int days, final int hours,
063 final int minutes) {
064 final java.sql.Time time = original;
065 final Calendar cal = Calendar.getInstance();
066 cal.setTime(time);
067 cal.set(Calendar.YEAR, 0);
068 cal.set(Calendar.MONTH, 0);
069 cal.set(Calendar.DAY_OF_MONTH, 0);
070 cal.set(Calendar.MILLISECOND, 0);
071
072 cal.add(Calendar.HOUR, hours);
073 cal.add(Calendar.MINUTE, minutes);
074
075 return setDate(cal.getTime());
076 }
077
078 @Override
079 public java.util.Date dateValue(final Object object) {
080 final java.sql.Time time = (Time) object;
081 return time == null ? null : new java.util.Date(time.getTime());
082 }
083
084 @Override
085 protected Map<String, DateFormat> formats() {
086 return formats;
087 }
088
089 @Override
090 protected Time now() {
091 return new java.sql.Time(Clock.getTime());
092 }
093
094 @Override
095 protected Time setDate(final Date date) {
096 return new java.sql.Time(date.getTime());
097 }
098
099 }