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.util.bridges.alloy;
016    
017    import com.liferay.portal.kernel.search.Document;
018    import com.liferay.portal.kernel.search.Field;
019    import com.liferay.portal.kernel.search.Hits;
020    import com.liferay.portal.kernel.util.GetterUtil;
021    import com.liferay.portal.model.BaseModel;
022    
023    import java.util.ArrayList;
024    import java.util.List;
025    
026    import javax.portlet.PortletURL;
027    
028    /**
029     * @author Brian Wing Shun Chan
030     */
031    public class AlloySearchResult {
032    
033            public List<BaseModel<?>> getBaseModels() throws Exception {
034                    if (baseModels != null) {
035                            return baseModels;
036                    }
037    
038                    List<BaseModel<?>> baseModels = new ArrayList<BaseModel<?>>();
039    
040                    Document[] documents = hits.getDocs();
041    
042                    for (int i = 0; i < documents.length; i++) {
043                            Document document = hits.doc(i);
044    
045                            long entryClassPK = GetterUtil.getLong(
046                                    document.get(Field.ENTRY_CLASS_PK));
047    
048                            BaseModel<?> baseModel = alloyServiceInvoker.fetchModel(
049                                    entryClassPK);
050    
051                            if (baseModel == null) {
052                                    continue;
053                            }
054    
055                            baseModels.add(baseModel);
056                    }
057    
058                    this.baseModels = baseModels;
059    
060                    return baseModels;
061            }
062    
063            public Hits getHits() {
064                    return hits;
065            }
066    
067            public PortletURL getPortletURL() {
068                    return portletURL;
069            }
070    
071            public int getSize() {
072                    return size;
073            }
074    
075            protected void afterPropertiesSet() {
076                    size = hits.getLength();
077            }
078    
079            protected void setAlloyServiceInvoker(
080                    AlloyServiceInvoker alloyServiceInvoker) {
081    
082                    this.alloyServiceInvoker = alloyServiceInvoker;
083            }
084    
085            protected void setHits(Hits hits) {
086                    this.hits = hits;
087            }
088    
089            protected void setPortletURL(PortletURL portletURL) {
090                    this.portletURL = portletURL;
091            }
092    
093            protected AlloyServiceInvoker alloyServiceInvoker;
094            protected List<BaseModel<?>> baseModels;
095            protected Hits hits;
096            protected PortletURL portletURL;
097            protected int size;
098    
099    }