001    /**
002     * Copyright (c) 2000-2011 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.repository.cmis.search;
016    
017    import com.liferay.portal.kernel.search.Field;
018    import com.liferay.portal.kernel.util.DateFormatFactoryUtil;
019    import com.liferay.portal.kernel.util.StringPool;
020    import com.liferay.portal.kernel.util.StringUtil;
021    import com.liferay.portal.util.PropsValues;
022    
023    import java.text.DateFormat;
024    import java.text.ParseException;
025    
026    import java.util.Date;
027    
028    /**
029     * @author Mika Koivisto
030     */
031    public class CMISParameterValueUtil {
032    
033            public static String formatParameterValue(String field, String value) {
034                    return formatParameterValue(field, value, false);
035            }
036    
037            public static String formatParameterValue(
038                    String field, String value, boolean wildcard) {
039    
040                    if (field.equals(Field.CREATE_DATE)) {
041                            try {
042                                    DateFormat searchSimpleDateFormat =
043                                            DateFormatFactoryUtil.getSimpleDateFormat(
044                                                    PropsValues.INDEX_DATE_FORMAT_PATTERN);
045    
046                                    Date createDate = searchSimpleDateFormat.parse(value);
047    
048                                    DateFormat cmisSimpleDateFormat =
049                                            DateFormatFactoryUtil.getSimpleDateFormat(
050                                                    "yyyy-MM-dd'T'HH:mm:ss.sss'Z'");
051    
052                                    value = cmisSimpleDateFormat.format(createDate);
053                            }
054                            catch (ParseException pe) {
055                            }
056                    }
057                    else {
058                            value = StringUtil.replace(value, StringPool.APOSTROPHE, "\\'");
059                            value = StringUtil.replace(value, StringPool.UNDERLINE, "\\_");
060    
061                            if (wildcard) {
062                                    value = StringUtil.replace(value, StringPool.PERCENT, "\\%");
063                                    value = StringPool.PERCENT.concat(value).concat(
064                                            StringPool.PERCENT);
065                            }
066                    }
067    
068                    return value;
069            }
070    
071    }