001    /**
002     * Copyright (c) 2000-2012 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.portlet.dynamicdatamapping.util;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.search.Document;
020    import com.liferay.portal.kernel.util.StringBundler;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.Validator;
023    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
024    import com.liferay.portlet.dynamicdatamapping.storage.Field;
025    import com.liferay.portlet.dynamicdatamapping.storage.Fields;
026    
027    import java.io.Serializable;
028    
029    import java.util.Date;
030    import java.util.Iterator;
031    
032    /**
033     * @author Alexander Chow
034     */
035    public class DDMIndexerImpl implements DDMIndexer {
036    
037            public void addAttributes(
038                    Document document, DDMStructure ddmStructure, Fields fields) {
039    
040                    Iterator<Field> itr = fields.iterator();
041    
042                    while (itr.hasNext()) {
043                            Field field = itr.next();
044    
045                            try {
046                                    String indexType = ddmStructure.getFieldProperty(
047                                            field.getName(), "indexType");
048    
049                                    if (Validator.isNull(indexType)) {
050                                            continue;
051                                    }
052    
053                                    String name = encodeName(
054                                            ddmStructure.getStructureId(), field.getName());
055    
056                                    Serializable value = field.getValue();
057    
058                                    if (value instanceof Boolean) {
059                                            document.addKeyword(name, (Boolean)value);
060                                    }
061                                    else if (value instanceof Date) {
062                                            document.addDate(name, (Date)value);
063                                    }
064                                    else if (value instanceof Double) {
065                                            document.addKeyword(name, (Double)value);
066                                    }
067                                    else if (value instanceof Integer) {
068                                            document.addKeyword(name, (Integer)value);
069                                    }
070                                    else {
071                                            String valueString = String.valueOf(value);
072    
073                                            if (indexType.equals("keyword")) {
074                                                    document.addKeyword(name, valueString);
075                                            }
076                                            else {
077                                                    document.addText(name, valueString);
078                                            }
079                                    }
080                            }
081                            catch (Exception e) {
082                                    if (_log.isWarnEnabled()) {
083                                            _log.warn(e, e);
084                                    }
085                            }
086                    }
087            }
088    
089            public String encodeName(long ddmStructureId, String fieldName) {
090                    StringBundler sb = new StringBundler(5);
091    
092                    sb.append(_FIELD_NAMESPACE);
093                    sb.append(StringPool.FORWARD_SLASH);
094                    sb.append(ddmStructureId);
095                    sb.append(StringPool.FORWARD_SLASH);
096                    sb.append(fieldName);
097    
098                    return sb.toString();
099            }
100    
101            private static final String _FIELD_NAMESPACE = "ddm";
102    
103            private static Log _log = LogFactoryUtil.getLog(DDMIndexerImpl.class);
104    
105    }