001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
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    }