001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.typeconverter;
016    
017    import java.util.Date;
018    
019    import jodd.typeconverter.ConvertBean;
020    import jodd.typeconverter.TypeConverter;
021    
022    import jodd.util.CsvUtil;
023    
024    /**
025     * @author Raymond Aug??
026     */
027    public class DateArrayConverter implements TypeConverter<Date[]> {
028    
029            public DateArrayConverter(ConvertBean convertBean) {
030                    _convertBean = convertBean;
031            }
032    
033            @Override
034            public Date[] convert(Object value) {
035                    if (value == null) {
036                            return null;
037                    }
038    
039                    Class<?> type = value.getClass();
040    
041                    if (type.isArray() == false) {
042                            if (type == String.class) {
043                                    String[] values = CsvUtil.toStringArray(value.toString());
044    
045                                    return convertArray(values);
046                            }
047    
048                            return new Date[] {_convertBean.toDate(value)};
049                    }
050    
051                    Class<?> componentType = type.getComponentType();
052    
053                    if (componentType.isPrimitive()) {
054                            if (type == long[].class) {
055                                    long[] values = (long[])value;
056    
057                                    Date[] results = new Date[values.length];
058    
059                                    for (int i = 0; i < values.length; i++) {
060                                            results[i] = _convertBean.toDate(values[i]);
061                                    }
062    
063                                    return results;
064                            }
065                    }
066    
067                    return convertArray((Object[])value);
068            }
069    
070            protected Date[] convertArray(Object[] values) {
071                    Date[] results = new Date[values.length];
072    
073                    for (int i = 0; i < values.length; i++) {
074                            results[i] = _convertBean.toDate(values[i]);
075                    }
076    
077                    return results;
078            }
079    
080            private ConvertBean _convertBean;
081    
082    }