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.search.QueryConfig;
021    import com.liferay.portal.kernel.util.DateFormatFactoryUtil;
022    import com.liferay.portal.kernel.util.PropsKeys;
023    import com.liferay.portal.kernel.util.PropsUtil;
024    import com.liferay.portal.kernel.util.StringPool;
025    import com.liferay.portal.kernel.util.StringUtil;
026    import com.liferay.portal.kernel.util.Validator;
027    
028    import java.text.DateFormat;
029    import java.text.ParseException;
030    
031    import java.util.Date;
032    
033    /**
034     * @author Mika Koivisto
035     */
036    public class CMISParameterValueUtil {
037    
038            public static String formatParameterValue(String field, String value) {
039                    return formatParameterValue(field, value, false, null);
040            }
041    
042            public static String formatParameterValue(
043                    String field, String value, boolean wildcard) {
044    
045                    return formatParameterValue(field, value, wildcard, null);
046            }
047    
048            public static String formatParameterValue(
049                    String field, String value, boolean wildcard, QueryConfig queryConfig) {
050    
051                    if (field.equals(Field.CREATE_DATE) ||
052                            field.equals(Field.MODIFIED_DATE)) {
053    
054                            try {
055                                    DateFormat searchSimpleDateFormat =
056                                            DateFormatFactoryUtil.getSimpleDateFormat(
057                                                    _INDEX_DATE_FORMAT_PATTERN);
058    
059                                    Date date = searchSimpleDateFormat.parse(value);
060    
061                                    DateFormat cmisSimpleDateFormat =
062                                            DateFormatFactoryUtil.getSimpleDateFormat(
063                                                    "yyyy-MM-dd'T'HH:mm:ss.000'Z'");
064    
065                                    value = cmisSimpleDateFormat.format(date);
066                            }
067                            catch (ParseException pe) {
068                                    if (_log.isWarnEnabled()) {
069                                            _log.warn(
070                                                    "Unable to parse date " + value + " for field " +
071                                                            field);
072                                    }
073                            }
074                    }
075                    else {
076                            String productName = StringPool.BLANK;
077    
078                            if (queryConfig != null) {
079                                    productName = (String)queryConfig.getAttribute(
080                                            "repositoryProductName");
081                            }
082    
083                            if (Validator.isNotNull(productName) &&
084                                    productName.startsWith("Alfresco")) {
085    
086                                    value = StringUtil.replace(
087                                            value, new String[] {StringPool.APOSTROPHE},
088                                            new String[] {"\\'"});
089                            }
090                            else {
091                                    value = StringUtil.replace(
092                                            value,
093                                            new String[] {StringPool.APOSTROPHE, StringPool.UNDERLINE},
094                                            new String[] {"\\'", "\\_"});
095                            }
096    
097                            if (wildcard) {
098                                    value = StringUtil.replace(
099                                            value, new String[] {StringPool.PERCENT, StringPool.STAR},
100                                            new String[] {"\\%", StringPool.PERCENT});
101                            }
102                    }
103    
104                    return value;
105            }
106    
107            private static final String _INDEX_DATE_FORMAT_PATTERN = PropsUtil.get(
108                    PropsKeys.INDEX_DATE_FORMAT_PATTERN);
109    
110            private static Log _log = LogFactoryUtil.getLog(
111                    CMISParameterValueUtil.class);
112    
113    }