Subversion Repositories bacoAlunos

Rev

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

Rev Author Line No. Line
456 jmachado 1
package pt.estgp.estgweb.services.srusources;
2
 
3
import pt.utl.ist.berserk.logic.serviceManager.IService;
4
import pt.estgp.estgweb.services.data.RepositoryService;
5
import pt.estgp.estgweb.services.expceptions.ServiceException;
6
import pt.estgp.estgweb.domain.views.CourseView;
7
import pt.estgp.estgweb.domain.views.SruSourceView;
8
import pt.estgp.estgweb.domain.views.SruSourceCollectionView;
9
import pt.estgp.estgweb.domain.*;
10
import pt.estgp.estgweb.domain.dao.DaoFactory;
11
import org.apache.log4j.Logger;
12
 
13
import java.util.HashSet;
14
import java.util.List;
15
import java.util.ArrayList;
16
 
17
/*
18
 * @author Goncalo Luiz gedl [AT] rnl [DOT] ist [DOT] utl [DOT] pt
19
 *
20
 *
21
 * Created at 17/Out/2003 , 23:45:24
22
 *
23
 */
24
/**
25
 * @author Jorge Machado
26
 *
27
 *
28
 * Created at 17/Out/2003 , 23:45:24
29
 *
30
 */
31
public class SruSourceService implements IService
32
{
33
 
34
    private static final 1.5.0/docs/api/java/util/logging/Logger.html">Logger logger = 1.5.0/docs/api/java/util/logging/Logger.html">Logger.getLogger(SruSourceService.class);
35
 
36
 
37
 
38
    public SruSourceView submit(SruSourceView sruSourceView, UserSession userSession) throws ServiceException
39
    {
40
        if(sruSourceView.getId() <= 0)
41
        {
42
            logger.info("Creating new SRU Source with URL: " + sruSourceView.getUrl());
43
 
44
            SruSource sruSource = DomainObjectFactory.createSruSourceImpl();
45
            sruSourceView.persistViewInObject(sruSource);
46
            sruSource.setCreator(userSession.getUser());
47
            DaoFactory.getSruSourceDaoImpl().save(sruSource);
48
            if(sruSourceView.getCollections() != null)
49
            {
50
                addNewCollections(sruSource,sruSourceView);
51
            }
52
        }
53
        else
54
        {
55
            SruSource sruSource = DaoFactory.getSruSourceDaoImpl().load(sruSourceView.getId());
56
            sruSourceView.persistViewInObject(sruSource);
57
            if(sruSourceView.getCollections() != null)
58
            {
59
                if(sruSource.getCollections() == null || sruSource.getCollections().size() == 0)
60
                {
61
                    addNewCollections(sruSource,sruSourceView);
62
                }
63
                else
64
                {
65
                    List<SruSourceCollectionView> toAdd = new ArrayList<SruSourceCollectionView>(sruSourceView.getCollections());
66
                    List<SruSourceCollection> toRemove = new ArrayList<SruSourceCollection>();
67
                    for(SruSourceCollection collection: sruSource.getCollections())
68
                    {
69
                        boolean found = false;
70
                        for(SruSourceCollectionView collectionView: sruSourceView.getCollections())
71
                        {
72
                            if(collectionView.getId() == collection.getId() || collectionView.getCollection().equals(collection.getCollection()))
73
                            {
74
                                collectionView.persistViewInObject(collection);
75
                                toAdd.remove(collectionView);
76
                                found = true;
77
                                break;
78
                            }
79
                        }
80
                        if(!found)
81
                        {
82
                            toRemove.add(collection);
83
                        }
84
                    }
85
                    sruSource.getCollections().removeAll(toRemove);
86
                    DaoFactory.getSruSourceCollectionDaoImpl().delete(toRemove);
87
                    for(SruSourceCollectionView collectionView: toAdd)
88
                    {
89
                        addNewCollection(sruSource,collectionView);
90
                    }
91
                }
92
            }
93
        }
94
        return sruSourceView;
95
    }
96
 
97
    private void addNewCollections(SruSource sruSource, SruSourceView sruSourceView)
98
    {
99
        sruSource.setCollections(new HashSet<SruSourceCollection>());
100
        for(SruSourceCollectionView collectionView: sruSourceView.getCollections())
101
        {
102
            addNewCollection(sruSource,collectionView);
103
        }
104
    }
105
 
106
    private void addNewCollection(SruSource sruSource, SruSourceCollectionView collectionView)
107
    {
108
        SruSourceCollection collection = DomainObjectFactory.createSruSourceCollectionImpl();
109
        collectionView.persistViewInObject(collection);
110
        collection.setSruSource(sruSource);
111
        sruSource.getCollections().add(collection);
112
        DaoFactory.getSruSourceCollectionDaoImpl().save(collection);
113
    }
114
}