package pt.estgp.estgweb.services.srusources;
import pt.utl.ist.berserk.logic.serviceManager.IService;
import pt.estgp.estgweb.services.data.RepositoryService;
import pt.estgp.estgweb.services.expceptions.ServiceException;
import pt.estgp.estgweb.domain.views.CourseView;
import pt.estgp.estgweb.domain.views.SruSourceView;
import pt.estgp.estgweb.domain.views.SruSourceCollectionView;
import pt.estgp.estgweb.domain.*;
import pt.estgp.estgweb.domain.dao.DaoFactory;
import org.apache.log4j.Logger;
import java.util.HashSet;
import java.util.List;
import java.util.ArrayList;
/*
* @author Goncalo Luiz gedl [AT] rnl [DOT] ist [DOT] utl [DOT] pt
*
*
* Created at 17/Out/2003 , 23:45:24
*
*/
/**
* @author Jorge Machado
*
*
* Created at 17/Out/2003 , 23:45:24
*
*/
public class SruSourceService
implements IService
{
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);
public SruSourceView submit
(SruSourceView sruSourceView, UserSession userSession
) throws ServiceException
{
if(sruSourceView.
getId() <=
0)
{
logger.
info("Creating new SRU Source with URL: " + sruSourceView.
getUrl());
SruSource sruSource = DomainObjectFactory.
createSruSourceImpl();
sruSourceView.
persistViewInObject(sruSource
);
sruSource.
setCreator(userSession.
getUser());
DaoFactory.
getSruSourceDaoImpl().
save(sruSource
);
if(sruSourceView.
getCollections() !=
null)
{
addNewCollections
(sruSource,sruSourceView
);
}
}
else
{
SruSource sruSource = DaoFactory.
getSruSourceDaoImpl().
load(sruSourceView.
getId());
sruSourceView.
persistViewInObject(sruSource
);
if(sruSourceView.
getCollections() !=
null)
{
if(sruSource.
getCollections() ==
null || sruSource.
getCollections().
size() == 0
)
{
addNewCollections
(sruSource,sruSourceView
);
}
else
{
List
<SruSourceCollectionView
> toAdd =
new ArrayList
<SruSourceCollectionView
>(sruSourceView.
getCollections());
List
<SruSourceCollection
> toRemove =
new ArrayList
<SruSourceCollection
>();
for(SruSourceCollection collection: sruSource.
getCollections())
{
boolean found =
false;
for(SruSourceCollectionView collectionView: sruSourceView.
getCollections())
{
if(collectionView.
getId() == collection.
getId() || collectionView.
getCollection().
equals(collection.
getCollection()))
{
collectionView.
persistViewInObject(collection
);
toAdd.
remove(collectionView
);
found =
true;
break;
}
}
if(!found
)
{
toRemove.
add(collection
);
}
}
sruSource.
getCollections().
removeAll(toRemove
);
DaoFactory.
getSruSourceCollectionDaoImpl().
delete(toRemove
);
for(SruSourceCollectionView collectionView: toAdd
)
{
addNewCollection
(sruSource,collectionView
);
}
}
}
}
return sruSourceView
;
}
private void addNewCollections
(SruSource sruSource, SruSourceView sruSourceView
)
{
sruSource.
setCollections(new HashSet
<SruSourceCollection
>());
for(SruSourceCollectionView collectionView: sruSourceView.
getCollections())
{
addNewCollection
(sruSource,collectionView
);
}
}
private void addNewCollection
(SruSource sruSource, SruSourceCollectionView collectionView
)
{
SruSourceCollection collection = DomainObjectFactory.
createSruSourceCollectionImpl();
collectionView.
persistViewInObject(collection
);
collection.
setSruSource(sruSource
);
sruSource.
getCollections().
add(collection
);
DaoFactory.
getSruSourceCollectionDaoImpl().
save(collection
);
}
}