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.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            public void addAttributes(
035                    Document document, DDMStructure ddmStructure, Fields fields) {
036    
037                    Iterator<Field> itr = fields.iterator();
038    
039                    while (itr.hasNext()) {
040                            Field field = itr.next();
041    
042                            String name = encodeName(
043                                    ddmStructure.getStructureId(), field.getName());
044    
045                            Serializable value = field.getValue();
046    
047                            if (value instanceof Boolean) {
048                                    document.addKeyword(name, (Boolean)value);
049                            }
050                            else if (value instanceof Date) {
051                                    document.addDate(name, (Date)value);
052                            }
053                            else if (value instanceof Double) {
054                                    document.addKeyword(name, (Double)value);
055                            }
056                            else if (value instanceof Integer) {
057                                    document.addKeyword(name, (Integer)value);
058                            }
059                            else {
060                                    String valueString = String.valueOf(value);
061    
062                                    document.addText(name, valueString);
063                            }
064                    }
065            }
066    
067            public String encodeName(long ddmStructureId, String fieldName) {
068                    StringBundler sb = new StringBundler(5);
069    
070                    sb.append(_FIELD_NAMESPACE);
071                    sb.append(StringPool.FORWARD_SLASH);
072                    sb.append(ddmStructureId);
073                    sb.append(StringPool.FORWARD_SLASH);
074                    sb.append(fieldName);
075    
076                    return sb.toString();
077            }
078    
079            private static final String _FIELD_NAMESPACE = "ddm";
080    
081    }