/* ======================================================================
   Parts Copyright 2006 University of Leeds, Oxford University, University of the Highlands and Islands.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

====================================================================== */

package org.bodington.assessment;

import java.util.Enumeration;
import java.util.List;
import java.util.Vector;

import org.bodington.database.*;
import org.bodington.server.*;

public class PeerMarkerPaper extends org.bodington.server.resources.Resource {

    private PrimaryKey peer_mark_paper_id = null;

    private int once_only;

    private int self_allowed;

    private Vector questionsinorder;

    public Class sessionClass() {
        // This method is derived from class
        // org.bodington.server.resources.Resource
        // to do: code goes here
        return org.bodington.assessment.PeerMarkerSessionImpl.class;
    }

    public static PeerMarkerPaper findPeerMarkerPaper(PrimaryKey key)
            throws BuildingServerException {
        return (PeerMarkerPaper) findPersistentObject(key,
                "org.bodington.assessment.PeerMarkerPaper");
    }

    public static Enumeration findPeerMarkerPapers(String where)
            throws BuildingServerException {
        return findPersistentObjects(where,
                "org.bodington.assessment.PeerMarkerPaper");
    }

    public static Enumeration findPeerMarkerPapers(String where, String order)
            throws BuildingServerException {
        return findPersistentObjects(where, order,
                "org.bodington.assessment.PeerMarkerPaper");
    }

    public PrimaryKey getPrimaryKey() {
        return getPeerMarkerPaperId();
    }

    public void setPrimaryKey(PrimaryKey key) {
        setPeerMarkerPaperId(key);
    }

    public PrimaryKey getPeerMarkerPaperId() {
        return peer_mark_paper_id;
    }

    public void setPeerMarkerPaperId(PrimaryKey key) {
        peer_mark_paper_id = key;
        setResourceId(key);
        setUnsaved();
    }

    public int getOnceOnly() {
        return once_only;
    }

    public void setOnceOnly(int b) {
        once_only = b;
        setUnsaved();
    }

    public int getSelfAllowed() {
        return self_allowed;
    }

    public void setSelfAllowed(int b) {
        self_allowed = b;
        setUnsaved();
    }
    
    /**
     * Get a list of the questions. The items in this list are instances of
     * {@link PeerMarkerQuestion}. The list returned is a shallow copy of the
     * underlying list used by this object.
     * @return a list of the questions.
     * @throws BuildingServerException
     */
    public List getQuestions()
        throws BuildingServerException
    {
        initialize();
        return (List) questionsinorder.clone();
    }

    /**
     * Get the question at the specified index. If the index has no
     * corresponding entry, this method returns <code>null</code>.
     * @param n the specified index.
     * @return the question at the specified index, or <code>null</code> if
     *         the index has no corresponding entry.
     * @throws BuildingServerException
     */
    public PeerMarkerQuestion getQuestionAt( int n )
    throws BuildingServerException
    {
        initialize();
        if ( n<0 || n>= questionsinorder.size() )
            return null;
        
        return (PeerMarkerQuestion)questionsinorder.elementAt( n );
    }

    public PeerMarkerQuestion createQuestion() {
        return null;
    }

    public void removeQuestion(PeerMarkerQuestion q) {
    }

    public int getResourceType() {
        return RESOURCE_PEERMARKER;
    }
    
    /**
     * Initialize this object. This method is used internally to lazy load the 
     * questions of this PeerMarkerPaper from persistent storage.
     * @throws BuildingServerException
     */
    private void initialize() throws BuildingServerException
    {
        if (questionsinorder == null)
            updateQuestionList();
    }
    
    /**
     * Update the list of questions. This method is used internally to synch
     * the list of questions with persistent storage. Questions are sorted
     * by value of the ordinal.
     * @throws BuildingServerException
     */
    private void updateQuestionList() throws BuildingServerException
    {
        questionsinorder = new Vector();
        Enumeration e = PeerMarkerQuestion.findPeerMarkerQuestions("resource_id = "
            + getPeerMarkerPaperId(), "ordinal");
        while ( e.hasMoreElements() )
            questionsinorder.addElement( e.nextElement() );
    }
}
