001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (C) Alkacon Software (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.jlan;
029
030import java.io.IOException;
031import java.util.ArrayList;
032import java.util.List;
033
034import org.alfresco.jlan.server.filesys.FileInfo;
035import org.alfresco.jlan.server.filesys.SearchContext;
036
037/**
038 * This class represents the state of a search operation in a JLAN repository. It just contains
039 * the list of all search results and an index into that list which points to the next result
040 * which hasn't been fetched yet.<p>
041 */
042public class CmsJlanSearch extends SearchContext {
043
044    /** The files constituting the search result. */
045    private List<CmsJlanNetworkFile> m_files;
046
047    /** The position of the next unfetched result. */
048    private int m_position;
049
050    /**
051     * Creates a new instance based on a given result list.<p>
052     *
053     * @param files the result list
054     */
055    public CmsJlanSearch(List<CmsJlanNetworkFile> files) {
056
057        m_files = new ArrayList<CmsJlanNetworkFile>(files);
058    }
059
060    /**
061     * @see org.alfresco.jlan.server.filesys.SearchContext#getResumeId()
062     */
063    @Override
064    public int getResumeId() {
065
066        return m_position;
067    }
068
069    /**
070     * @see org.alfresco.jlan.server.filesys.SearchContext#hasMoreFiles()
071     */
072    @Override
073    public boolean hasMoreFiles() {
074
075        return m_position < m_files.size();
076    }
077
078    /**
079     * @see org.alfresco.jlan.server.filesys.SearchContext#nextFileInfo(org.alfresco.jlan.server.filesys.FileInfo)
080     */
081    @Override
082    public boolean nextFileInfo(FileInfo info) {
083
084        try {
085            CmsJlanNetworkFile file = nextFile();
086            if (file == null) {
087                return false;
088            }
089            info.copyFrom(file.getFileInfo());
090            return true;
091        } catch (IOException e) {
092            // shouldn't normally happen
093            throw new RuntimeException(e);
094        }
095    }
096
097    /**
098     * @see org.alfresco.jlan.server.filesys.SearchContext#nextFileName()
099     */
100    @Override
101    public String nextFileName() {
102
103        return nextFile().getName();
104    }
105
106    /**
107     * @see org.alfresco.jlan.server.filesys.SearchContext#restartAt(org.alfresco.jlan.server.filesys.FileInfo)
108     */
109    @Override
110    public boolean restartAt(FileInfo info) {
111
112        return false;
113    }
114
115    /**
116     * @see org.alfresco.jlan.server.filesys.SearchContext#restartAt(int)
117     */
118    @Override
119    public boolean restartAt(int resumeId) {
120
121        return false;
122    }
123
124    /**
125     * Returns the next file object in the search result.<p>
126     *
127     * @return the next file object
128     */
129    protected CmsJlanNetworkFile nextFile() {
130
131        if (!hasMoreFiles()) {
132            return null;
133        }
134        CmsJlanNetworkFile file = m_files.get(m_position);
135        m_position += 1;
136        return file;
137    }
138
139}