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.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    }