001/*
002 * (C) Copyright 2006-2012 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Bogdan Stefanescu
018 *     Florent Guillaume
019 *     Nicolas Chapurlat <nchapurlat@nuxeo.com>
020 */
021package org.nuxeo.ecm.core.schema.types.primitives;
022
023import java.util.Calendar;
024import java.util.Date;
025
026import org.nuxeo.ecm.core.schema.types.PrimitiveType;
027import org.nuxeo.ecm.core.schema.types.constraints.Constraint;
028import org.nuxeo.ecm.core.schema.types.constraints.DateIntervalConstraint;
029import org.nuxeo.ecm.core.schema.types.constraints.NotNullConstraint;
030import org.nuxeo.ecm.core.schema.utils.DateParser;
031
032/**
033 * The date (actually timestamp) type.
034 */
035public class DateType extends PrimitiveType {
036
037    private static final long serialVersionUID = 1L;
038
039    public static final String ID = "date";
040
041    public static final DateType INSTANCE = new DateType();
042
043    public DateType() {
044        super(ID);
045    }
046
047    @Override
048    public boolean validate(Object object) {
049        return object instanceof Date || object instanceof Calendar;
050    }
051
052    @Override
053    public Object convert(Object value) {
054        if (value instanceof Date) {
055            return value;
056        } else if (value instanceof Calendar) {
057            return value;
058        } else {
059            // TODO
060            try {
061                return Integer.valueOf((String) value);
062            } catch (NumberFormatException e) {
063                return null;
064            }
065        }
066    }
067
068    @Override
069    public Object decode(String str) {
070        Date date = DateParser.parseW3CDateTime(str);
071        if (date != null) {
072            Calendar cal = Calendar.getInstance();
073            cal.setTime(date);
074            return cal;
075        }
076        return null;
077    }
078
079    @Override
080    public String encode(Object object) {
081        if (object instanceof Date) {
082            return DateParser.formatW3CDateTime((Date) object);
083        } else if (object instanceof Calendar) {
084            return DateParser.formatW3CDateTime(((Calendar) object).getTime());
085        } else {
086            return null;
087        }
088    }
089
090    protected Object readResolve() {
091        return INSTANCE;
092    }
093
094    @Override
095    public boolean support(Class<? extends Constraint> constraint) {
096        if (NotNullConstraint.class.equals(constraint)) {
097            return true;
098        }
099        if (DateIntervalConstraint.class.equals(constraint)) {
100            return true;
101        }
102        return false;
103    }
104
105}