import musicServer.*;
import java.util.*;
import java.io.*;
import org.omg.CORBA.*;

/**
 * Models a collection of albums.
 */
public class MusicCollection extends _MusicCollectionIImplBase implements Serializable {
    private Vector          _vecAlbums;
    private String          _sUserName;
    private String          _sPassword;

    private transient BOA   _boa;
    private Vector          _vecActivatedObjects;

    private boolean         _bObjectsDeactivated = false;

    public MusicCollection(String sUserName, String sPassword, BOA boa) {
        super();
        _sUserName = sUserName;
        _sPassword = sPassword;
        _vecAlbums = new Vector();
        _boa = boa;
        _vecActivatedObjects = new Vector();
    }

    /**
     * Invoked after being de-serialized with a new reference to the BOA
     */
    public void updateTransientData(BOA boa) {
        _boa = boa;
    }

    /**
     * Obtains all AlbumI objects ordered by artist name
     */
    public AlbumI[] getAllAlbumsByArtistName() {
        AlbumI[] albums = getAllAlbums();
        AlbumSorter.sortByArtistName(albums);
        return albums;
    }

    /**
     * Obtains all AlbumI objects ordered by album name
     */
    public AlbumI[] getAllAlbumsByAlbumName() {
        AlbumI[] albums = getAllAlbums();
        AlbumSorter.sortByAlbumName(albums);
        return albums;
    }

    /**
     * Obtains all AlbumI objects in default order
     */
    public AlbumI[] getAllAlbums() {
        if(_bObjectsDeactivated) {
            _bObjectsDeactivated = false;
            Enumeration e = _vecAlbums.elements();
            while(e.hasMoreElements()) {
                _boa.obj_is_ready((org.omg.CORBA.Object)e.nextElement());
            }
        }

        AlbumI[] returnValue = new AlbumI[_vecAlbums.size()];
        _vecAlbums.copyInto(returnValue);
        return returnValue;
    }

    /**
     * Adds an AlbumI object to the collection
     */
    public void addAlbum(AlbumI album) {
        _vecAlbums.addElement(album);
    }

    /**
     * Removes an AlbumI object from the collection
     */
    public void deleteAlbum(AlbumI album) {
        _vecAlbums.removeElement(album);
    }

    /**
     * Obtains an empty AlbumI object
     */
    public AlbumI obtainEmptyAlbum() {
        AlbumI returnValue = new Album();
        _boa.obj_is_ready(returnValue);
        _vecActivatedObjects.addElement(returnValue);
        return returnValue;
    }

    public void sUserName(String sUserName) { _sUserName = sUserName; }
    public String sUserName() { return _sUserName; }

    public void sPassword(String sPassword) { _sPassword = sPassword; }
    public String sPassword() { return _sPassword; }

    /**
     * Deactivates all activated objects
     */
    public void deactivateObjects() {
        _bObjectsDeactivated = true;
        Enumeration e = _vecAlbums.elements();
        while(e.hasMoreElements()) {
            _boa.deactivate_obj((org.omg.CORBA.Object)e.nextElement());
        }
    }

}
