refactored addUnexistingUnitsAsKeywords method to use new Unit entity

This commit is contained in:
fred-fri 2012-05-29 11:49:20 +09:00
parent 18780cf425
commit 4ee2837c72
4 changed files with 629 additions and 596 deletions
src/main/java/se/su/dsv/scipro

@ -44,6 +44,7 @@ import se.su.dsv.scipro.match.dao.interfaces.KeywordTypeDao;
import se.su.dsv.scipro.match.dao.interfaces.SupervisorDao;
import se.su.dsv.scipro.match.dataobject.Keyword;
import se.su.dsv.scipro.springdata.services.MessageBoardService;
import se.su.dsv.scipro.springdata.services.UnitService;
/**
* Main service facade for all importing operations.
@ -51,6 +52,10 @@ import se.su.dsv.scipro.springdata.services.MessageBoardService;
*/
@Service
public class ImporterFacade {
@Autowired
private UnitService unitService;
@Autowired
private UserDao userDao;
@Autowired
@ -86,6 +91,7 @@ public class ImporterFacade {
/**
* If the supplied user is null, a new one will be created and persisted.
* Does not handle transient User instances, you probably have to do a dao-query for it before running this method.
*
* @param user hte user
* @param dtoUser the dtoUser
*/
@ -96,11 +102,13 @@ public class ImporterFacade {
userToMerge = userDao.save(userToMerge);
userFromDTO(userToMerge, dtoUser, true);
}
@Transactional
public void mergeProject(final Project project, final ProjectDTO dtoProject) {
Project projectToMerge = (project != null ? project : new Project());
projectFromDTO(projectToMerge, dtoProject, true);
}
/**
* Utility method for translating between DTO/DomainObject.
*/
@ -148,6 +156,7 @@ public class ImporterFacade {
user.addUserName(unameToAdd);
}
}
private void mergeLinkedProjects(final Set<ProjectDTO> projects) {
for (final ProjectDTO projectDTO : projects) {
logger.debug("Linking users to project: '" + projectDTO + "'");
@ -159,6 +168,7 @@ public class ImporterFacade {
projectFromDTO(project, projectDTO, true);
}
}
private void mergeLinkedRoles(final User user, final Set<UserRoleDTO> roles) {
for (final UserRoleDTO roleDTO : roles) {
logger.debug("Making role: " + roleDTO.role + " for user: " + user);
@ -173,6 +183,7 @@ public class ImporterFacade {
}
}
}
private void projectFromDTO(Project project, final ProjectDTO projectDTO, boolean mergeLinkedEntities) {
project.setIdentifier(projectDTO.id);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
@ -204,8 +215,7 @@ public class ImporterFacade {
MessageBoard messageBoard = new MessageBoard(project);
messageBoard.setTitle(project.getTitle());
messageBoard = messageBoardService.save(messageBoard);
}
else {
} else {
MessageBoard messageBoard = messageBoardService.getMessageBoard(project);
messageBoard.setTitle(project.getTitle());
messageBoard = messageBoardService.save(messageBoard);
@ -214,9 +224,6 @@ public class ImporterFacade {
}
//Loop over all participants and assign roles
if (mergeLinkedEntities) {
checkForRemovedMembers(project, projectDTO.participants);
@ -225,6 +232,7 @@ public class ImporterFacade {
}
}
}
private void checkForRemovedMembers(final Project project, final Set<ProjectParticipantDTO> participants) {
//Get project team members.
final Set<Member> currentMembers = new HashSet<Member>(project.getMembers());
@ -281,34 +289,32 @@ public class ImporterFacade {
}
private ProjectParticipantDTO participantDtoFromMember(Member m) {
ProjectParticipantDTO dto = new ProjectParticipantDTO();
PersonDTO person = new PersonDTO(m.getUser().getIdentifier());
dto.setPerson(person);
switch (m.getType()) {
case AUTHOR:
{
case AUTHOR: {
dto.setRole(EXTERNAL_PROJECT_ROLE.PARTICIPANT.name());
break;
}
case SUPERVISOR:
{
case SUPERVISOR: {
dto.setRole(EXTERNAL_PROJECT_ROLE.SUPERVISOR.name());
break;
}
case REVIEWER:
{
case REVIEWER: {
dto.setRole(EXTERNAL_PROJECT_ROLE.EXAMINER.name());
break;
}
case CO_SUPERVISOR:
{
case CO_SUPERVISOR: {
dto.setRole(EXTERNAL_PROJECT_ROLE.ASSISTANT_SUPERVISOR.name());
break;
}
}
return dto;
}
private void projectParticipantFromDTO(final Project project, final ProjectParticipantDTO projectParticipant) {
//Use existing members to avoid duplicated roles being assigned
final List<Member> currentProjectMembers = project.getMembers();
@ -330,8 +336,7 @@ public class ImporterFacade {
//Assign the local role to the participant, this is a lot of boilerplate code and should be made easier.
logger.debug("Assigning the role " + localRole + " for " + linkedUser + " on project " + project);
switch (localRole) {
case PARTICIPANT:
{
case PARTICIPANT: {
//logger.debug("Assigning Participant role to "+projectParticipant.person.id);
final Member tmpMember = new Member(linkedUser, Member.Type.AUTHOR);
if (currentProjectMembers.contains(tmpMember)) {
@ -343,8 +348,7 @@ public class ImporterFacade {
project.getProjectParticipants().add(stud);
break;
}
case ACTIVE_PARTICIPANT:
{
case ACTIVE_PARTICIPANT: {
final List<FinalSeminar> fsList = assureFinalSeminarAvailable(project);
//logger.debug("Assigning Active participation role to " + fsList.size() + " seminars for "+projectParticipant.person.id);
for (FinalSeminar fs : fsList) {
@ -359,8 +363,7 @@ public class ImporterFacade {
}
break;
}
case EXAMINER:
{
case EXAMINER: {
ProjectFollower pf = new ProjectFollower();
//logger.debug("Assigning Examiner role to "+projectParticipant.person.id);
final Member tmpMember = new Member(linkedUser, Member.Type.REVIEWER);
@ -378,8 +381,7 @@ public class ImporterFacade {
}
break;
}
case OPPONENT:
{
case OPPONENT: {
final List<FinalSeminar> fsList = assureFinalSeminarAvailable(project);
//logger.debug("Assigning Opponent role to " + fsList.size() + " seminars for "+projectParticipant.person.id);
for (FinalSeminar fs : fsList) {
@ -395,8 +397,7 @@ public class ImporterFacade {
}
break;
}
case SUPERVISOR:
{
case SUPERVISOR: {
//logger.debug("Assigning Supervisor role to "+projectParticipant.person.id);
final Member tmpMember = new Member(linkedUser, Member.Type.SUPERVISOR);
if (currentProjectMembers.contains(tmpMember)) {
@ -408,8 +409,7 @@ public class ImporterFacade {
project.setHeadSupervisor(emp);
break;
}
case CO_SUPERVISOR:
{
case CO_SUPERVISOR: {
ProjectFollower pf = new ProjectFollower();
//logger.debug("Assigning Co-supervisor role to "+projectParticipant.person.id);
final Member tmpMember = new Member(linkedUser, Member.Type.CO_SUPERVISOR);
@ -430,6 +430,7 @@ public class ImporterFacade {
}
}
}
/*
* Translate remote roles to local ones.
*/
@ -442,12 +443,14 @@ public class ImporterFacade {
}
return null;
}
private boolean userAlreadyAttachedAsParticipant(final User user, final List<? extends FinalSeminarParticipation> participantList) {
for (final FinalSeminarParticipation participant : participantList)
if (user.equals(participant.getUser()))
return true;
return false;
}
private List<FinalSeminar> assureFinalSeminarAvailable(final Project project) {
final List<FinalSeminar> fsList = finalSeminarDao.findFinalSeminarsByProject(project);
if (fsList.size() == 0) {
@ -462,10 +465,12 @@ public class ImporterFacade {
}
return fsList;
}
private void userNameFromDTO(final Username username, final UsernameDTO usernameDTO) {
username.setUserName(usernameDTO.getUsername().toLowerCase().trim());
username.setRealm(usernameDTO.getRealm().toUpperCase().trim());
}
private UsernameDTO dtoFromUsername(final Username name) {
UsernameDTO dto = new UsernameDTO();
dto.setUsername(name.getUserName().toLowerCase().trim());
@ -479,6 +484,7 @@ public class ImporterFacade {
pClass = projectClassDao.getProjectClass(extractedType);
return pClass;
}
private String extractProjectType(ProjectDTO projectDTO) {
String extractedType = ProjectClass.UNKNOWN;
if (projectDTO.type != null) {//Find class from project
@ -494,6 +500,7 @@ public class ImporterFacade {
logger.debug("Returning: " + extractedType + " as the extracted type of the project: " + projectDTO);
return extractedType;
}
private String mapToLocalProjectClass(final String input) {
if (input == null)
return null;
@ -503,6 +510,7 @@ public class ImporterFacade {
}
return null;
}
private ProjectStatus toLocalStatus(String externalProjectStatus) {
if (externalProjectStatus.equals("STARTED") || externalProjectStatus.equals("LATE")) {
return ProjectStatus.ACTIVE;
@ -594,35 +602,38 @@ public class ImporterFacade {
}
@Transactional
public void addUnexistingUnitsAsKeywords(final Set<UnitDTO> unitsFromDTO) {
final Set<Keyword> currentUnits = keywordDao.getKeywords(keywordTypeDao.findByType(KeywordTypeDao.TYPE.UNIT), true);
public void addUnexistingUnitsAsUnits(final Set<UnitDTO> unitsFromDTO) {
final Set<Unit> currentUnits = unitService.findAllAsSet();
final Set<UnitDTO> remoteUnits = new HashSet<UnitDTO>(unitsFromDTO);
final Set<Keyword> unitsToRemove = new HashSet<Keyword>();
for (Keyword unit : currentUnits) {
UnitDTO translatedUnit = dtoFromUnitKeyword(unit);
final Set<Unit> unitsToRemove = new HashSet<Unit>();
for (Unit unit : currentUnits) {
UnitDTO translatedUnit = dtoFromUnit(unit);
if (!remoteUnits.contains(translatedUnit)) {
logger.info("Unit " + translatedUnit.getName() + " does not exist on remote, preparing to remove.");
unitsToRemove.add(unit);
}
}
//Delete units that not exist on remote
for (Keyword unitToRemove : unitsToRemove){
keywordDao.delete(unitToRemove);
logger.debug("Deleted unit: " + unitToRemove.getKeyword() + " since it has been removed from remote system");
for (Unit unitToRemove : unitsToRemove){
unitService.delete(unitToRemove.getId());
logger.debug("Deleted unit: " + unitToRemove.getTitle() + " since it has been removed from remote system");
}
//Add units to database
for (final UnitDTO unitDTO : remoteUnits) {
Keyword unit = keywordDao.getKeywordByIdentifierAndType(unitDTO.id, keywordTypeDao.findByType(KeywordTypeDao.TYPE.UNIT));
Unit unit = unitService.findByIdentifier(unitDTO.getId());
if (unit == null){
logger.info("External unit: '" + unitDTO + "' has no local representation, creating");
unit = new Keyword(unitDTO.name, keywordTypeDao.findByType(KeywordTypeDao.TYPE.UNIT));
unit.setIdentifier(unitDTO.id);
unit = keywordDao.save(unit);
} else if (!unit.getKeyword().equals(unitDTO.name)){
logger.info("Unit " + unit.getKeyword() + " has changed name, renaming to: " + unitDTO.name);
unit.setKeyword(unitDTO.name);
unit = new Unit();
unit.setIdentifier(unitDTO.getId());
unit.setTitle(unitDTO.getName());
unit = unitService.save(unit);
} else if (!unit.getTitle().equals(unitDTO.name)) {
logger.info("Unit " + unit.getTitle() + " has changed name, renaming to: " + unitDTO.name);
unit.setTitle(unitDTO.name);
} else {
logger.info("Unit " + unit.getKeyword() + " already exists, skipping");
logger.info("Unit " + unit.getTitle() + " already exists, skipping");
}
}
}
@ -659,6 +670,14 @@ public class ImporterFacade {
return dto;
}
private UnitDTO dtoFromUnit(final Unit unit) {
UnitDTO dto = new UnitDTO();
dto.setName(unit.getTitle());
if (unit.getIdentifier() != null)
dto.setId(unit.getIdentifier());
return dto;
}
private ResearchAreaDTO dtoFromAreaKeyword(final Keyword area) {
ResearchAreaDTO dto = new ResearchAreaDTO();
dto.setName(area.getKeyword());

@ -12,6 +12,6 @@ import se.su.dsv.scipro.data.dataobjects.Unit;
@Transactional(readOnly = true)
public interface UnitRepo extends JpaRepository<Unit, Long>, QueryDslPredicateExecutor<Unit> {
//nothing here yet
public Unit findByIdentifier(Long identifier);
}

@ -9,6 +9,8 @@ import se.su.dsv.scipro.springdata.repos.UnitRepo;
import se.su.dsv.scipro.springdata.services.UnitService;
import javax.annotation.Resource;
import java.util.HashSet;
import java.util.Set;
/**
* @author: fred-fri
@ -29,4 +31,13 @@ public class UnitServiceImpl extends AbstractQueryService<Unit, Long> implements
System.out.println("UnitServiceImpl instantiating...");
}
@Override
public Unit findByIdentifier(Long identifier) {
return unitRepo.findByIdentifier(identifier);
}
@Override
public Set<Unit> findAllAsSet() {
return new HashSet<Unit>(unitRepo.findAll());
}
}

@ -2,12 +2,15 @@ package se.su.dsv.scipro.springdata.services;
import se.su.dsv.scipro.data.dataobjects.Unit;
import java.util.Set;
/**
* @author: fred-fri
* date: 2012 03 26
*/
public interface UnitService extends GenericService<Unit, Long>, QueryService<Unit, Long> {
//nothing here yet
public Unit findByIdentifier(Long identifier);
public Set<Unit> findAllAsSet();
}