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.date; 021 022import java.text.DateFormat; 023import java.util.ArrayList; 024import java.util.Calendar; 025import java.util.List; 026import java.util.Locale; 027import java.util.Map; 028 029import com.google.common.collect.Maps; 030 031import org.apache.isis.applib.profiles.Localization; 032import org.apache.isis.core.commons.config.ConfigurationConstants; 033import org.apache.isis.core.commons.config.IsisConfiguration; 034import org.apache.isis.core.metamodel.facetapi.FacetHolder; 035import org.apache.isis.core.progmodel.facets.object.value.ValueSemanticsProviderContext; 036import org.apache.isis.core.progmodel.facets.value.ValueSemanticsProviderAbstractTemporal; 037 038public abstract class DateValueSemanticsProviderAbstract<T> extends ValueSemanticsProviderAbstractTemporal<T> { 039 040 private static Map<String, DateFormat> formats = Maps.newHashMap(); 041 042 static { 043 formats.put(ISO_ENCODING_FORMAT, createDateEncodingFormat("yyyyMMdd")); 044 formats.put("iso", createDateFormat("yyyy-MM-dd")); 045 formats.put("medium", DateFormat.getDateInstance(DateFormat.MEDIUM)); 046 } 047 048 public DateValueSemanticsProviderAbstract(final FacetHolder holder, final Class<T> adaptedClass, final Immutability immutability, final EqualByContent equalByContent, final T defaultValue, final IsisConfiguration configuration, final ValueSemanticsProviderContext context) { 049 super("date", holder, adaptedClass, 12, immutability, equalByContent, defaultValue, configuration, context); 050 051 final String formatRequired = configuration.getString(ConfigurationConstants.ROOT + "value.format.date"); 052 if (formatRequired == null) { 053 format = formats().get(defaultFormat()); 054 } else { 055 setMask(formatRequired); 056 } 057 } 058 059 // ////////////////////////////////////////////////////////////////// 060 // DateValueFacet 061 // ////////////////////////////////////////////////////////////////// 062 063 // ////////////////////////////////////////////////////////////////// 064 // temporal-specific stuff 065 // ////////////////////////////////////////////////////////////////// 066 067 @Override 068 protected void clearFields(final Calendar cal) { 069 cal.set(Calendar.HOUR, 0); 070 cal.set(Calendar.HOUR_OF_DAY, 0); 071 cal.set(Calendar.MINUTE, 0); 072 cal.set(Calendar.SECOND, 0); 073 cal.set(Calendar.AM_PM, 0); 074 cal.set(Calendar.MILLISECOND, 0); 075 } 076 077 @Override 078 protected String defaultFormat() { 079 return "medium"; 080 } 081 082 @Override 083 protected boolean ignoreTimeZone() { 084 return true; 085 } 086 087 @Override 088 protected Map<String, DateFormat> formats() { 089 return formats; 090 } 091 092 @Override 093 public String toString() { 094 return "DateValueSemanticsProvider: " + format; 095 } 096 097 @Override 098 protected DateFormat format(final Localization localization) { 099 final DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, localization.getLocale()); 100 dateFormat.setTimeZone(UTC_TIME_ZONE); 101 return dateFormat; 102 } 103 104 protected List<DateFormat> formatsToTry(Localization localization) { 105 List<DateFormat> formats = new ArrayList<DateFormat>(); 106 107 Locale locale = localization == null ? Locale.getDefault() : localization.getLocale(); 108 formats.add(DateFormat.getDateInstance(DateFormat.LONG, locale)); 109 formats.add(DateFormat.getDateInstance(DateFormat.MEDIUM, locale)); 110 formats.add(DateFormat.getDateInstance(DateFormat.SHORT, locale)); 111 formats.add(createDateFormat("yyyy-MM-dd")); 112 formats.add(createDateFormat("yyyyMMdd")); 113 114 for (DateFormat format : formats) { 115 format.setTimeZone(UTC_TIME_ZONE); 116 } 117 118 return formats; 119 } 120 121}