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.portlet.dynamicdatamapping.util;
016    
017    import com.liferay.portal.kernel.search.Document;
018    import com.liferay.portal.kernel.util.StringBundler;
019    import com.liferay.portal.kernel.util.StringPool;
020    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
021    import com.liferay.portlet.dynamicdatamapping.storage.Field;
022    import com.liferay.portlet.dynamicdatamapping.storage.Fields;
023    
024    import java.io.Serializable;
025    
026    import java.util.Date;
027    import java.util.Iterator;
028    
029    /**
030     * @author Alexander Chow
031     */
032    public class DDMIndexerImpl implements DDMIndexer {
033    
034            @Override
035            public void addAttributes(
036                    Document document, DDMStructure ddmStructure, Fields fields) {
037    
038                    Iterator<Field> itr = fields.iterator();
039    
040                    while (itr.hasNext()) {
041                            Field field = itr.next();
042    
043                            String name = encodeName(
044                                    ddmStructure.getStructureId(), field.getName());
045    
046                            Serializable value = field.getValue();
047    
048                            if (value instanceof Boolean) {
049                                    document.addKeyword(name, (Boolean)value);
050                            }
051                            else if (value instanceof Date) {
052                                    document.addDate(name, (Date)value);
053                            }
054                            else if (value instanceof Double) {
055                                    document.addKeyword(name, (Double)value);
056                            }
057                            else if (value instanceof Integer) {
058                                    document.addKeyword(name, (Integer)value);
059                            }
060                            else {
061                                    String valueString = String.valueOf(value);
062    
063                                    document.addText(name, valueString);
064                            }
065                    }
066            }
067    
068            @Override
069            public String encodeName(long ddmStructureId, String fieldName) {
070                    StringBundler sb = new StringBundler(5);
071    
072                    sb.append(_FIELD_NAMESPACE);
073                    sb.append(StringPool.FORWARD_SLASH);
074                    sb.append(ddmStructureId);
075                    sb.append(StringPool.FORWARD_SLASH);
076                    sb.append(fieldName);
077    
078                    return sb.toString();
079            }
080    
081            private static final String _FIELD_NAMESPACE = "ddm";
082    
083    }