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.date;
021    
022    import java.text.DateFormat;
023    import java.util.Calendar;
024    import java.util.Map;
025    
026    import org.apache.isis.core.commons.config.ConfigurationConstants;
027    import org.apache.isis.core.commons.config.IsisConfiguration;
028    import org.apache.isis.core.metamodel.facetapi.FacetHolder;
029    import org.apache.isis.core.progmodel.facets.object.value.ValueSemanticsProviderContext;
030    import org.apache.isis.core.progmodel.facets.value.ValueSemanticsProviderAbstractTemporal;
031    
032    import com.google.inject.internal.Maps;
033    
034    public abstract class DateValueSemanticsProviderAbstract<T> extends ValueSemanticsProviderAbstractTemporal<T> {
035    
036        private static Map<String, DateFormat> formats = Maps.newHashMap();
037    
038        static {
039            formats.put("iso", createDateFormat("yyyy-MM-dd"));
040            formats.put(ISO_ENCODING_FORMAT, createDateFormat("yyyyMMdd"));
041            formats.put("long", DateFormat.getDateInstance(DateFormat.LONG));
042            formats.put("medium", DateFormat.getDateInstance(DateFormat.MEDIUM));
043            formats.put("short", DateFormat.getDateInstance(DateFormat.SHORT));
044        }
045    
046        public DateValueSemanticsProviderAbstract(final FacetHolder holder, final Class<T> adaptedClass,
047            final boolean immutable, final boolean equalByContent, final T defaultValue,
048            final IsisConfiguration configuration, final ValueSemanticsProviderContext context) {
049            super("date", holder, adaptedClass, 12, immutable, 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        @Override
064        public int getLevel() {
065            return DATE_ONLY;
066        }
067    
068        // //////////////////////////////////////////////////////////////////
069        // temporal-specific stuff
070        // //////////////////////////////////////////////////////////////////
071    
072        @Override
073        protected void clearFields(final Calendar cal) {
074            cal.set(Calendar.HOUR, 0);
075            cal.set(Calendar.HOUR_OF_DAY, 0);
076            cal.set(Calendar.MINUTE, 0);
077            cal.set(Calendar.SECOND, 0);
078            cal.set(Calendar.AM_PM, 0);
079            cal.set(Calendar.MILLISECOND, 0);
080        }
081    
082        @Override
083        protected String defaultFormat() {
084            return "medium";
085        }
086    
087        @Override
088        protected boolean ignoreTimeZone() {
089            return true;
090        }
091    
092        @Override
093        protected Map<String, DateFormat> formats() {
094            return formats;
095        }
096    
097        @Override
098        public String toString() {
099            return "DateValueSemanticsProvider: " + format;
100        }
101    
102    }