Subversion Repositories bacoAlunos

Rev

Rev 1306 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

package pt.estgp.estgweb.services.managedidentifers;

import pt.utl.ist.berserk.logic.serviceManager.IService;
import pt.estgp.estgweb.filters.chains.ResourceAccessControlEnum;
import pt.estgp.estgweb.domain.*;
import pt.estgp.estgweb.domain.views.ManagedIdentifierCollectionView;
import pt.estgp.estgweb.domain.views.ManagedIdentifierView;
import pt.estgp.estgweb.domain.views.utils.ManagedIdentifierCollectionComparator;
import pt.estgp.estgweb.domain.dao.DaoFactory;
import pt.estgp.estgweb.services.data.RepositoryManager;
import pt.estgp.estgweb.services.data.IRepositoryFile;
import pt.estgp.estgweb.services.data.impl.DefaultRepositoryFile;

import java.io.InputStream;
import java.util.*;

/**
 * @author Jorge Machado
 * @date 4/Jun/2008
 * @see pt.estgp.estgweb.services.data
 */

public class ManagedIdentifiersService implements IService
{


    public List<ManagedIdentifierCollectionView> loadCollections(UserSession userSession)
    {
        Set<ManagedIdentifierCollection> managedIdentifierCollections = userSession.getUser().getManagedIdentifierCollectionsManager();
        Set<ManagedIdentifierCollection> ownerIdentifierCollections = userSession.getUser().getManagedIdentifierCollectionsOwner();
        Set<ManagedIdentifierCollection> mergeList = managedIdentifierCollections;
        if(mergeList == null)
        {
            if(ownerIdentifierCollections == null)
                return null;
            else
            {
                mergeList = new HashSet<ManagedIdentifierCollection>();
                mergeList.removeAll(ownerIdentifierCollections);
            }
        }
        mergeList.addAll(ownerIdentifierCollections);

        List<ManagedIdentifierCollectionView> views = ManagedIdentifierCollectionView.loadViews(managedIdentifierCollections);
        1.5.0/docs/api/java/util/Collections.html">Collections.sort(views, ManagedIdentifierCollectionComparator.getInstance());
        return views;
    }

    public List<ManagedIdentifierCollectionView> createCollection(ManagedIdentifierCollectionView collection, UserSession userSession)
    {
        ManagedIdentifierCollection managedIdentifierCollection;
        if(collection.getId() > 0)
            managedIdentifierCollection = DaoFactory.getManagedIdentifierCollectionDaoImpl().get(collection.getId());
        else
        {
            managedIdentifierCollection = DomainObjectFactory.createManagedIdentifierCollectionImpl();
            managedIdentifierCollection.setOwner(userSession.getUser());
            DaoFactory.getManagedIdentifierCollectionDaoImpl().save(managedIdentifierCollection);
        }
        collection.persistViewInObject(managedIdentifierCollection);
        return loadCollections(userSession);
    }

    public ManagedIdentifierCollectionView loadCollection(long collectionId, UserSession userSession)
    {
        ManagedIdentifierCollection collection = DaoFactory.getManagedIdentifierCollectionDaoImpl().get(collectionId);
        return new ManagedIdentifierCollectionView(collection,true);
    }


    public List<ManagedIdentifierCollectionView>  deleteCollection(long collectionId, UserSession userSession)
    {
        ManagedIdentifierCollection collection = DaoFactory.getManagedIdentifierCollectionDaoImpl().get(collectionId);
        DaoFactory.getManagedIdentifierCollectionDaoImpl().delete(collection);
        userSession.getUser().getManagedIdentifierCollectionsManager().remove(collection);
        userSession.getUser().getManagedIdentifierCollectionsOwner().remove(collection);
        return loadCollections(userSession);
    }

    public ManagedIdentifierCollectionView assignCollectionUser(long userId, long collectionId, UserSession userSession)
    {
        ManagedIdentifierCollection collection = DaoFactory.getManagedIdentifierCollectionDaoImpl().get(collectionId);
        User user = DaoFactory.getUserDaoImpl().get(userId);
        if(!collection.getManagers().contains(user))
        {
            collection.getManagers().add(user);
        }
        return new ManagedIdentifierCollectionView(collection,true);
    }

    public ManagedIdentifierCollectionView deleteCollectionUser(long userId, long collectionId, UserSession userSession)
    {
        ManagedIdentifierCollection collection = DaoFactory.getManagedIdentifierCollectionDaoImpl().get(collectionId);
        User user = DaoFactory.getUserDaoImpl().get(userId);
        collection.getManagers().remove(user);
        return new ManagedIdentifierCollectionView(collection,true);
    }

    public ManagedIdentifierView createIdentifier(ManagedIdentifierView managedIdentifierView, long collectionId, UserSession userSession)
    {
        ManagedIdentifier managedIdentifier;
        if(managedIdentifierView.getId() <= 0)
        {
            managedIdentifier = DomainObjectFactory.createManagedIdentifierImpl();
            ManagedIdentifierCollection managedIdentifierCollection = DaoFactory.getManagedIdentifierCollectionDaoImpl().load(collectionId);
            managedIdentifier.setCollection(managedIdentifierCollection);
            managedIdentifier.setOwner(userSession.getUser());
            managedIdentifierCollection.setSequenceNumber(managedIdentifierCollection.getSequenceNumber() + 1);
            managedIdentifier.setSequenceNumber(managedIdentifierCollection.getSequenceNumber());
        }
        else
            managedIdentifier = DaoFactory.getManagedIdentifierDaoImpl().get(managedIdentifierView.getId());
        managedIdentifierView.persistViewInObject(managedIdentifier);
        DaoFactory.getManagedIdentifierDaoImpl().save(managedIdentifier);
        return new ManagedIdentifierView(managedIdentifier,true);
    }

    public ManagedIdentifierView findIdentifier(long identifier, long collectionId, UserSession userSession)
    {
        ManagedIdentifier managedIdentifier = DaoFactory.getManagedIdentifierDaoImpl().findBySequence(identifier,collectionId);
        if(managedIdentifier != null)
            return new ManagedIdentifierView(managedIdentifier,true);
        else return null;
    }
}