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.kernel.repository.cmis.search;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.search.Field;
020    import com.liferay.portal.kernel.util.DateFormatFactoryUtil;
021    import com.liferay.portal.kernel.util.PropsKeys;
022    import com.liferay.portal.kernel.util.PropsUtil;
023    import com.liferay.portal.kernel.util.StringPool;
024    import com.liferay.portal.kernel.util.StringUtil;
025    
026    import java.text.DateFormat;
027    import java.text.ParseException;
028    
029    import java.util.Date;
030    
031    /**
032     * @author Mika Koivisto
033     */
034    public class CMISParameterValueUtil {
035    
036            public static String formatParameterValue(String field, String value) {
037                    return formatParameterValue(field, value, false);
038            }
039    
040            public static String formatParameterValue(
041                    String field, String value, boolean wildcard) {
042    
043                    if (field.equals(Field.CREATE_DATE) ||
044                            field.equals(Field.MODIFIED_DATE)) {
045    
046                            try {
047                                    DateFormat searchSimpleDateFormat =
048                                            DateFormatFactoryUtil.getSimpleDateFormat(
049                                                    _INDEX_DATE_FORMAT_PATTERN);
050    
051                                    Date date = searchSimpleDateFormat.parse(value);
052    
053                                    DateFormat cmisSimpleDateFormat =
054                                            DateFormatFactoryUtil.getSimpleDateFormat(
055                                                    "yyyy-MM-dd'T'HH:mm:ss.000'Z'");
056    
057                                    value = cmisSimpleDateFormat.format(date);
058                            }
059                            catch (ParseException pe) {
060                                    _log.warn(
061                                            "Unable to parse date " + value + " for field " + field);
062                            }
063                    }
064                    else {
065                            value = StringUtil.replace(
066                                    value,
067                                    new String[] {StringPool.APOSTROPHE, StringPool.UNDERLINE},
068                                    new String[] {"\\'", "\\_"});
069    
070                            if (wildcard) {
071                                    value = StringUtil.replace(
072                                            value, new String[] {StringPool.PERCENT, StringPool.STAR},
073                                            new String[] {"\\%", StringPool.PERCENT});
074                            }
075                    }
076    
077                    return value;
078            }
079    
080            private static final String _INDEX_DATE_FORMAT_PATTERN = PropsUtil.get(
081                    PropsKeys.INDEX_DATE_FORMAT_PATTERN);
082    
083            private static Log _log = LogFactoryUtil.getLog(
084                    CMISParameterValueUtil.class);
085    
086    }