001/*
002 * File   : $Source$
003 * Date   : $Date$
004 * Version: $Revision$
005 *
006 * This library is part of OpenCms -
007 * the Open Source Content Management System
008 *
009 * Copyright (C) 2002 - 2009 Alkacon Software (http://www.alkacon.com)
010 *
011 * This library is free software; you can redistribute it and/or
012 * modify it under the terms of the GNU Lesser General Public
013 * License as published by the Free Software Foundation; either
014 * version 2.1 of the License, or (at your option) any later version.
015 *
016 * This library is distributed in the hope that it will be useful,
017 * but WITHOUT ANY WARRANTY; without even the implied warranty of
018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019 * Lesser General Public License for more details.
020 *
021 * For further information about Alkacon Software, please see the
022 * company website: http://www.alkacon.com
023 *
024 * For further information about OpenCms, please see the
025 * project website: http://www.opencms.org
026 *
027 * You should have received a copy of the GNU Lesser General Public
028 * License along with this library; if not, write to the Free Software
029 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
030 */
031
032package org.opencms.search;
033
034import org.opencms.db.CmsResourceState;
035import org.opencms.file.CmsResource;
036import org.opencms.util.CmsUUID;
037
038import java.util.Date;
039import java.util.List;
040
041/**
042 * A resource implementation that combines the Solr document together with a OpenCms VFS resource.<p>
043 *
044 * @since 8.5.0
045 */
046public class CmsSearchResource extends CmsResource {
047
048    /** A generated serial version UID. */
049    private static final long serialVersionUID = -323110688331457211L;
050
051    /** The Solr document. */
052    private I_CmsSearchDocument m_doc;
053
054    /**
055     * Constructor, creates a new file Object from the given resource with
056     * an empty byte array as file content, if the resource does not
057     * implement a file.<p>
058     *
059     * @param resource the base resource object to create a file from
060     * @param doc the search document
061     */
062    public CmsSearchResource(CmsResource resource, I_CmsSearchDocument doc) {
063
064        this(
065            resource.getStructureId(),
066            resource.getResourceId(),
067            resource.getRootPath(),
068            resource.getTypeId(),
069            resource.getFlags(),
070            resource.getProjectLastModified(),
071            resource.getState(),
072            resource.getDateCreated(),
073            resource.getUserCreated(),
074            resource.getDateLastModified(),
075            resource.getUserLastModified(),
076            resource.getDateReleased(),
077            resource.getDateExpired(),
078            resource.getSiblingCount(),
079            resource.getLength(),
080            resource.getDateContent(),
081            resource.getVersion(),
082            doc);
083    }
084
085    /**
086     * Constructor, creates a new file object.<p>
087     *
088     * @param structureId the id of this resources structure record
089     * @param resourceId the id of this resources resource record
090     * @param path the filename of this resource
091     * @param type the type of this resource
092     * @param flags the flags of this resource
093     * @param projectId the project id this resource was last modified in
094     * @param state the state of this resource
095     * @param dateCreated the creation date of this resource
096     * @param userCreated the id of the user who created this resource
097     * @param dateLastModified the date of the last modification of this resource
098     * @param userLastModified the id of the user who did the last modification of this resource
099     * @param dateReleased the release date of this resource
100     * @param dateExpired the expiration date of this resource
101     * @param linkCount the count of all siblings of this resource
102     * @param length the size of the file content of this resource
103     * @param dateContent the date of the last modification of the content of this resource
104     * @param version the version number of this resource
105     * @param doc the search document
106     */
107    public CmsSearchResource(
108        CmsUUID structureId,
109        CmsUUID resourceId,
110        String path,
111        int type,
112        int flags,
113        CmsUUID projectId,
114        CmsResourceState state,
115        long dateCreated,
116        CmsUUID userCreated,
117        long dateLastModified,
118        CmsUUID userLastModified,
119        long dateReleased,
120        long dateExpired,
121        int linkCount,
122        int length,
123        long dateContent,
124        int version,
125        I_CmsSearchDocument doc) {
126
127        super(
128            structureId,
129            resourceId,
130            path,
131            type,
132            false,
133            flags,
134            projectId,
135            state,
136            dateCreated,
137            userCreated,
138            dateLastModified,
139            userLastModified,
140            dateReleased,
141            dateExpired,
142            linkCount,
143            length,
144            dateContent,
145            version);
146
147        m_doc = doc;
148    }
149
150    /**
151     * Delegator.<p>
152     *
153     * {@link I_CmsSearchDocument#getFieldValueAsDate(String)}
154     *
155     * @param fieldName the field name to get the value for
156     *
157     * @return the value
158     */
159    public Date getDateField(String fieldName) {
160
161        return m_doc.getFieldValueAsDate(fieldName);
162    }
163
164    /**
165     * Returns the document.<p>
166     *
167     * @return the document
168     */
169    public I_CmsSearchDocument getDocument() {
170
171        return m_doc;
172    }
173
174    /**
175     * Delegator.<p>
176     *
177     * {@link I_CmsSearchDocument#getFieldValueAsString(String)}
178     *
179     * @param fieldName the field name to get the value for
180     *
181     * @return the value
182     */
183    public String getField(String fieldName) {
184
185        return m_doc.getFieldValueAsString(fieldName);
186    }
187
188    /**
189     * Delegator.<p>
190     *
191     * {@link I_CmsSearchDocument#getFieldValueAsString(String)}
192     *
193     * @param fieldName the field name to get the value for
194     *
195     * @return the value
196     */
197    public List<String> getMultivaluedField(String fieldName) {
198
199        return m_doc.getMultivaluedFieldAsStringList(fieldName);
200    }
201
202    /**
203     * Delegator.<p>
204     *
205     * {@link I_CmsSearchDocument#getScore()}
206     *
207     * Returns the score of this document.<p>
208     *
209     * @param maxScore the maximum score of this search
210     *
211     * @return the score
212     */
213    public int getScore(float maxScore) {
214
215        return Math.round((m_doc.getScore() / maxScore) * 100f);
216    }
217}