001    /**
002     * Copyright (c) 2000-2011 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.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    }