001    /**
002     * Copyright (c) 2000-2010 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.portal.search.lucene;
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.search.Field;
021    import com.liferay.portal.kernel.search.IndexWriter;
022    import com.liferay.portal.kernel.search.SearchException;
023    import com.liferay.portal.kernel.util.Validator;
024    
025    import java.io.IOException;
026    
027    import java.util.Collection;
028    
029    import org.apache.lucene.index.Term;
030    
031    /**
032     * @author Bruno Farache
033     * @author Brian Wing Shun Chan
034     * @author Allen Chiang
035     * @author Alex Wallace
036     */
037    public class LuceneIndexWriterImpl implements IndexWriter {
038    
039            public void addDocument(long companyId, Document document)
040                    throws SearchException {
041    
042                    try {
043                            LuceneHelperUtil.addDocument(
044                                    companyId, _getLuceneDocument(document));
045    
046                            if (_log.isDebugEnabled()) {
047                                    _log.debug("Added document " + document.get(Field.UID));
048                            }
049                    }
050                    catch (IOException ioe) {
051                            throw new SearchException(ioe);
052                    }
053            }
054    
055            public void addDocuments(long companyId, Collection<Document> documents)
056                    throws SearchException {
057    
058                    for (Document document : documents) {
059                            addDocument(companyId, document);
060                    }
061            }
062    
063            public void deleteDocument(long companyId, String uid)
064                    throws SearchException {
065    
066                    try {
067                            LuceneHelperUtil.deleteDocuments(
068                                    companyId, new Term(Field.UID, uid));
069    
070                            if (_log.isDebugEnabled()) {
071                                    _log.debug("Deleted document " + uid);
072                            }
073                    }
074                    catch (IOException ioe) {
075                            throw new SearchException(ioe);
076                    }
077            }
078    
079            public void deleteDocuments(long companyId, Collection<String> uids)
080                    throws SearchException {
081    
082                    for (String uid : uids) {
083                            deleteDocument(companyId, uid);
084                    }
085            }
086    
087            public void deletePortletDocuments(long companyId, String portletId)
088                    throws SearchException {
089    
090                    try {
091                            LuceneHelperUtil.deleteDocuments(
092                                    companyId, new Term(Field.PORTLET_ID, portletId));
093                    }
094                    catch (IOException ioe) {
095                            throw new SearchException(ioe);
096                    }
097            }
098    
099            public void updateDocument(long companyId, Document document)
100                    throws SearchException {
101    
102                    try {
103                            LuceneHelperUtil.updateDocument(
104                                    companyId, new Term(Field.UID, document.getUID()),
105                                    _getLuceneDocument(document));
106    
107                            if (_log.isDebugEnabled()) {
108                                    _log.debug("Updated document " + document.get(Field.UID));
109                            }
110                    }
111                    catch (IOException ioe) {
112                            throw new SearchException(ioe);
113                    }
114            }
115    
116            public void updateDocuments(long companyId, Collection<Document> documents)
117                    throws SearchException {
118    
119                    for (Document document : documents) {
120                            updateDocument(companyId, document);
121                    }
122            }
123    
124            private org.apache.lucene.document.Document _getLuceneDocument(
125                    Document document) {
126    
127                    org.apache.lucene.document.Document luceneDocument =
128                            new org.apache.lucene.document.Document();
129    
130                    Collection<Field> fields = document.getFields().values();
131    
132                    for (Field field : fields) {
133                            String name = field.getName();
134                            boolean tokenized = field.isTokenized();
135                            float boost = field.getBoost();
136    
137                            for (String value : field.getValues()) {
138                                    if (Validator.isNull(value)) {
139                                            continue;
140                                    }
141    
142                                    org.apache.lucene.document.Field luceneField = null;
143    
144                                    if (tokenized) {
145                                            luceneField = LuceneFields.getText(name, value);
146                                    }
147                                    else {
148                                            luceneField = LuceneFields.getKeyword(name, value);
149                                    }
150    
151                                    luceneField.setBoost(boost);
152    
153                                    luceneDocument.add(luceneField);
154                            }
155                    }
156    
157                    return luceneDocument;
158            }
159    
160            private static Log _log = LogFactoryUtil.getLog(
161                    LuceneIndexWriterImpl.class);
162    
163    }