restructure of imports to match new api. project participants is now being retrieved as well

This commit is contained in:
Emil Siverhall 2012-02-14 16:32:18 +01:00
parent 29fd30f7bb
commit f525c37300
10 changed files with 207 additions and 79 deletions

@ -150,29 +150,54 @@ public abstract class AbstractDtoResponseHandler implements DtoResponseHandler {
return new UnitDTOCollectionWrapper();
}
protected final SupervisorDTOCollectionWrapper asSupervisorCollection(final String jsonSupervisorCollection){
protected final PersonDTOCollectionWrapper asSupervisorCollection(final String jsonSupervisorCollection){
//Treat as raw array
try{
final Type collectionType = new TypeToken<Collection<SupervisorDTO>>(){}.getType();
final Collection<SupervisorDTO> collection = parseTo(jsonSupervisorCollection,collectionType);
final SupervisorDTOCollectionWrapper wrapper = new SupervisorDTOCollectionWrapper(collection);
final Type collectionType = new TypeToken<Collection<PersonDTO>>(){}.getType();
final Collection<PersonDTO> collection = parseTo(jsonSupervisorCollection,collectionType);
final PersonDTOCollectionWrapper wrapper = new PersonDTOCollectionWrapper(collection);
return wrapper;
}catch(final JsonParseException jpe){
logger.debug("Attempting to treat response '"+jsonSupervisorCollection+"' as a raw array failed.");
}
//If that fails, treat as regular project
{
final SupervisorDTO supervisorDTO = parseTo(jsonSupervisorCollection, SupervisorDTO.class);
final PersonDTO supervisorDTO = parseTo(jsonSupervisorCollection, PersonDTO.class);
if(supervisorDTO != null){
final SupervisorDTOCollectionWrapper wrapper = new SupervisorDTOCollectionWrapper();
wrapper.getSupervisors().add(supervisorDTO);
final PersonDTOCollectionWrapper wrapper = new PersonDTOCollectionWrapper();
wrapper.getPersons().add(supervisorDTO);
return wrapper;
}else{
logger.debug("Attempting to treat response '"+jsonSupervisorCollection+"' as a single project failed.");
}
}
logger.warn("Returning empty supervisor collection wrapper for respose '"+jsonSupervisorCollection+"'");
return new SupervisorDTOCollectionWrapper();
return new PersonDTOCollectionWrapper();
}
protected final ProjectParticipantDTOCollectionWrapper asParticipantCollection(final String jsonParticipantCollection){
//Treat as raw array
try{
final Type collectionType = new TypeToken<Collection<ProjectParticipantDTO>>(){}.getType();
final Collection<ProjectParticipantDTO> collection = parseTo(jsonParticipantCollection,collectionType);
final ProjectParticipantDTOCollectionWrapper wrapper = new ProjectParticipantDTOCollectionWrapper(collection);
return wrapper;
}catch(final JsonParseException jpe){
logger.debug("Attempting to treat response '"+jsonParticipantCollection+"' as a raw array failed.");
}
//If that fails, treat as regular project
{
final ProjectParticipantDTO participantDTO = parseTo(jsonParticipantCollection, ProjectParticipantDTO.class);
if(participantDTO != null){
final ProjectParticipantDTOCollectionWrapper wrapper = new ProjectParticipantDTOCollectionWrapper();
wrapper.getParticipants().add(participantDTO);
return wrapper;
}else{
logger.debug("Attempting to treat response '"+jsonParticipantCollection+"' as a single project failed.");
}
}
logger.warn("Returning empty project participant collection wrapper for response '"+jsonParticipantCollection+"'");
return new ProjectParticipantDTOCollectionWrapper();
}
/**

@ -0,0 +1,27 @@
package se.su.dsv.scipro.io.dto;
public class PersonDTO {
public long id;
public String firstName, lastName, email;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String toString(){
return getFirstName() + " " + getLastName();
}
public PersonDTO() {
}
}

@ -0,0 +1,30 @@
package se.su.dsv.scipro.io.dto;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
//Collection-wrapper for UnitDTO objects
public class PersonDTOCollectionWrapper implements Iterable<PersonDTO>{
private final List<PersonDTO> persons = new ArrayList<PersonDTO>();
public PersonDTOCollectionWrapper(){
}
public PersonDTOCollectionWrapper(Collection<PersonDTO> source){
this.persons.addAll(source);
}
public List<PersonDTO> getPersons() {
return persons;
}
public int size(){
return persons.size();
}
@Override
public String toString(){
return ("listOf "+persons.size()+" persons :" + persons.toString());
}
@Override
public Iterator<PersonDTO> iterator() {
return persons.iterator();
}
}

@ -39,10 +39,11 @@ public class ProjectParticipantDTO {
}};
@Override
public String toString(){
return id+","+role+","+level;
return id+","+role+","+level+","+person;
}
public long id;
public String role;
@SerializedName("type")
public String level;
public PersonDTO person;
}

@ -0,0 +1,30 @@
package se.su.dsv.scipro.io.dto;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
//Collection-wrapper for ResearchAreaDTO objects
public class ProjectParticipantDTOCollectionWrapper implements Iterable<ProjectParticipantDTO>{
private final List<ProjectParticipantDTO> participants = new ArrayList<ProjectParticipantDTO>();
public ProjectParticipantDTOCollectionWrapper(){
}
public ProjectParticipantDTOCollectionWrapper(Collection<ProjectParticipantDTO> source){
this.participants.addAll(source);
}
public List<ProjectParticipantDTO> getParticipants() {
return participants;
}
public int size(){
return participants.size();
}
@Override
public String toString(){
return ("listOf "+participants.size()+" participants :" + participants.toString());
}
@Override
public Iterator<ProjectParticipantDTO> iterator() {
return participants.iterator();
}
}

@ -1,12 +0,0 @@
package se.su.dsv.scipro.io.dto;
public class SupervisorDTO {
public long id;
public String firstName, lastName, email;
public SupervisorDTO() {
}
}

@ -1,30 +0,0 @@
package se.su.dsv.scipro.io.dto;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
//Collection-wrapper for UnitDTO objects
public class SupervisorDTOCollectionWrapper implements Iterable<SupervisorDTO>{
private final List<SupervisorDTO> supervisors = new ArrayList<SupervisorDTO>();
public SupervisorDTOCollectionWrapper(){
}
public SupervisorDTOCollectionWrapper(Collection<SupervisorDTO> source){
this.supervisors.addAll(source);
}
public List<SupervisorDTO> getSupervisors() {
return supervisors;
}
public int size(){
return supervisors.size();
}
@Override
public String toString(){
return ("listOf "+supervisors.size()+" supervisors :" + supervisors.toString());
}
@Override
public Iterator<SupervisorDTO> iterator() {
return supervisors.iterator();
}
}

@ -22,7 +22,7 @@ public class UnitDTO {
this.name = name;
}
public Set<SupervisorDTO> supervisors = new HashSet<SupervisorDTO>();
public Set<PersonDTO> supervisors = new HashSet<PersonDTO>();
public UnitDTO(){}

@ -43,7 +43,7 @@ import se.su.dsv.scipro.io.dto.ProjectParticipantDTO;
import se.su.dsv.scipro.io.dto.ProjectParticipantDTO.EXTERNAL_PROJECT_ROLE;
import se.su.dsv.scipro.io.dto.ProjectParticipantDTO.LOCAL_PROJECT_ROLE;
import se.su.dsv.scipro.io.dto.ResearchAreaDTO;
import se.su.dsv.scipro.io.dto.SupervisorDTO;
import se.su.dsv.scipro.io.dto.PersonDTO;
import se.su.dsv.scipro.io.dto.UnitDTO;
import se.su.dsv.scipro.io.dto.UserDTO;
import se.su.dsv.scipro.io.dto.UserRoleDTO;
@ -205,7 +205,6 @@ public class ImporterFacade {
project = projectDao.save(project);
//Loop over all participants and assign roles
if(mergeLinkedEntities){
System.out.println(projectDTO.participants);
for(ProjectParticipantDTO projectParticipant:projectDTO.participants){
projectParticipantFromDTO(project,projectParticipant);
}
@ -215,18 +214,18 @@ public class ImporterFacade {
//Use existing members to avoid duplicated roles being assigned
final List<Member> currentProjectMembers = project.getMembers();
logger.debug("Linking: "+projectParticipant);
User linkedUser = userDao.getUserByIdentifier(projectParticipant.id);
User linkedUser = userDao.getUserByIdentifier(projectParticipant.person.id);
if(linkedUser == null){ //Attempt to import the user if it's a new one.
externalImporter.importUser(projectParticipant.id, false);
linkedUser = userDao.getUserByIdentifier(projectParticipant.id);
externalImporter.importUser(projectParticipant.person.id, false);
linkedUser = userDao.getUserByIdentifier(projectParticipant.person.id);
if(linkedUser == null){
logger.error("Can't import user with id: " + projectParticipant.id + " skipping assigning him/her to the project: "+ project);
logger.error("Can't import user with id: " + projectParticipant.person.id + " skipping assigning him/her to the project: "+ project);
return;
}
}
final ProjectParticipantDTO.LOCAL_PROJECT_ROLE localRole = extractLocalRole(projectParticipant.role);
if(localRole == null){//this is an unknown project role, warn and continue
logger.warn("Encountered an unknown project role ("+projectParticipant.role+") when importing project participant: "+projectParticipant.id +", the participant will be skipped");
logger.warn("Encountered an unknown project role ("+projectParticipant.role+") when importing project participant: "+projectParticipant.person.id +", the participant will be skipped");
return;
}
//Assign the local role to the participant, this is a lot of boilerplate code and should be made easier.
@ -234,7 +233,7 @@ public class ImporterFacade {
switch(localRole){
case PARTICIPANT:
{
//logger.debug("Assigning Participant role to "+projectParticipant.id);
//logger.debug("Assigning Participant role to "+projectParticipant.person.id);
final Member tmpMember = new Member(linkedUser,Member.Type.AUTHOR);
if(currentProjectMembers.contains(tmpMember)){
logger.debug("Skipping already assigned "+tmpMember.getType() + " user "+tmpMember.getUser());
@ -248,7 +247,7 @@ public class ImporterFacade {
case ACTIVE_PARTICIPANT:
{
final List<FinalSeminar> fsList = assureFinalSeminarAvailable(project);
//logger.debug("Assigning Active participation role to " + fsList.size() + " seminars for "+projectParticipant.id);
//logger.debug("Assigning Active participation role to " + fsList.size() + " seminars for "+projectParticipant.person.id);
for(FinalSeminar fs : fsList){
if(userAlreadyAttachedAsParticipant(linkedUser,fs.getActiveParticipations()))
continue;
@ -264,7 +263,7 @@ public class ImporterFacade {
case EXAMINER:
{
ProjectFollower pf = new ProjectFollower();
//logger.debug("Assigning Examiner role to "+projectParticipant.id);
//logger.debug("Assigning Examiner role to "+projectParticipant.person.id);
final Member tmpMember = new Member(linkedUser,Member.Type.REVIEWER);
if(currentProjectMembers.contains(tmpMember)){
logger.debug("Skipping already assigned "+tmpMember.getType() + " user "+tmpMember.getUser());
@ -283,7 +282,7 @@ public class ImporterFacade {
case OPPONENT:
{
final List<FinalSeminar> fsList = assureFinalSeminarAvailable(project);
//logger.debug("Assigning Opponent role to " + fsList.size() + " seminars for "+projectParticipant.id);
//logger.debug("Assigning Opponent role to " + fsList.size() + " seminars for "+projectParticipant.person.id);
for(FinalSeminar fs : fsList){
if(userAlreadyAttachedAsParticipant(linkedUser,fs.getOppositions()))
continue;
@ -299,7 +298,7 @@ public class ImporterFacade {
}
case SUPERVISOR:
{
//logger.debug("Assigning Supervisor role to "+projectParticipant.id);
//logger.debug("Assigning Supervisor role to "+projectParticipant.person.id);
final Member tmpMember = new Member(linkedUser,Member.Type.SUPERVISOR);
if(currentProjectMembers.contains(tmpMember)){
logger.debug("Skipping already assigned "+tmpMember.getType() + " user "+tmpMember.getUser());
@ -313,7 +312,7 @@ public class ImporterFacade {
case CO_SUPERVISOR:
{
ProjectFollower pf = new ProjectFollower();
//logger.debug("Assigning Co-supervisor role to "+projectParticipant.id);
//logger.debug("Assigning Co-supervisor role to "+projectParticipant.person.id);
final Member tmpMember = new Member(linkedUser,Member.Type.CO_SUPERVISOR);
if(currentProjectMembers.contains(tmpMember)){
logger.debug("Skipping already assigned "+tmpMember.getType() + " user "+tmpMember.getUser());
@ -415,7 +414,7 @@ public class ImporterFacade {
}
@Transactional
public void addUnitToSupervisor(SupervisorDTO supervisorDTO, UnitDTO unitDTO) {
public void addUnitToSupervisor(PersonDTO supervisorDTO, UnitDTO unitDTO) {
User supervisorUser = userDao.getUserByIdentifier(supervisorDTO.id);
Employee supervisor = supervisorDao.getFrom(supervisorUser);
List<Keyword> currentUnits = supervisor.getKeywords().getFiltered(keywordTypeDao.findByType(KeywordTypeDao.TYPE.UNIT));

@ -19,10 +19,12 @@ import se.su.dsv.scipro.io.ExternalImporter;
import se.su.dsv.scipro.io.dto.AbstractDtoResponseHandler;
import se.su.dsv.scipro.io.dto.ProjectDTO;
import se.su.dsv.scipro.io.dto.ProjectDTOCollectionWrapper;
import se.su.dsv.scipro.io.dto.ProjectParticipantDTO;
import se.su.dsv.scipro.io.dto.ProjectParticipantDTOCollectionWrapper;
import se.su.dsv.scipro.io.dto.ResearchAreaDTO;
import se.su.dsv.scipro.io.dto.ResearchAreaDTOCollectionWrapper;
import se.su.dsv.scipro.io.dto.SupervisorDTO;
import se.su.dsv.scipro.io.dto.SupervisorDTOCollectionWrapper;
import se.su.dsv.scipro.io.dto.PersonDTO;
import se.su.dsv.scipro.io.dto.PersonDTOCollectionWrapper;
import se.su.dsv.scipro.io.dto.UnitDTO;
import se.su.dsv.scipro.io.dto.UnitDTOCollectionWrapper;
import se.su.dsv.scipro.io.dto.UserDTO;
@ -101,7 +103,9 @@ public class ExternalImporterDaisyImpl implements ExternalImporter {
return (url+"/thesis");
}
});
for(ProjectDTO projectDTO : externalProjectSet){
final Set<ProjectDTO> projectsWithParticipants = projectDTOWithParticipants(externalProjectSet);
for(ProjectDTO projectDTO : projectsWithParticipants){
final Project project = projectDao.getProjectByIdentifier(projectDTO.id);
if(project == null)
logger.info("Importing new project: '" + projectDTO+"'");
@ -118,7 +122,9 @@ public class ExternalImporterDaisyImpl implements ExternalImporter {
return (url+"/thesis/"+externalIdentifier);
}
});
for(ProjectDTO projectDTO : externalProjectSet){
final Set<ProjectDTO> projectsWithParticipants = projectDTOWithParticipants(externalProjectSet);
for(ProjectDTO projectDTO : projectsWithParticipants){
final Project project = projectDao.getProjectByIdentifier(projectDTO.id);
if(project == null)
logger.info("Importing new project: '" + projectDTO+"'");
@ -141,14 +147,14 @@ public class ExternalImporterDaisyImpl implements ExternalImporter {
importerFacade.addUnexistingUnitsAsKeywords(unitSet);
for (final UnitDTO unitDTO : unitSet){
Set<SupervisorDTO> supervisorSet = fetchSupervisorsOnUnit(new HashMap<String,String>(), HttpRequestSender.REQUEST_TYPE.GET, new UrlProcessor() {
Set<PersonDTO> supervisorSet = fetchSupervisorsOnUnit(new HashMap<String,String>(), HttpRequestSender.REQUEST_TYPE.GET, new UrlProcessor() {
@Override
String process(final String url){
return (url+"/orgunit/"+unitDTO.id+"/supervisors");
}
});
if(!supervisorSet.isEmpty()){
for (SupervisorDTO supervisorDTO : supervisorSet) {
for (PersonDTO supervisorDTO : supervisorSet) {
importerFacade.addUnitToSupervisor(supervisorDTO, unitDTO);
}
} else
@ -173,19 +179,19 @@ public class ExternalImporterDaisyImpl implements ExternalImporter {
}
}
private Set<SupervisorDTO> fetchSupervisorsOnUnit(final Map<String, String> parameters, final HttpRequestSender.REQUEST_TYPE requestType, UrlProcessor urlProcessor) {
private Set<PersonDTO> fetchSupervisorsOnUnit(final Map<String, String> parameters, final HttpRequestSender.REQUEST_TYPE requestType, UrlProcessor urlProcessor) {
final String requestUrl = (urlProcessor==null?getRequestUrl():urlProcessor.process(getRequestUrl()));
final Set<SupervisorDTO> dtoCache = new HashSet<SupervisorDTO>();
final Set<PersonDTO> dtoCache = new HashSet<PersonDTO>();
final HttpsRequestSender senderProxy = new HttpsRequestSender(new AbstractDtoResponseHandler() {
@Override
public void handleResponse(String response) {
logger.info("Response received (from: "+ requestUrl+"): " + response);
final SupervisorDTOCollectionWrapper supervisorCollection = asSupervisorCollection(response);
final PersonDTOCollectionWrapper supervisorCollection = asSupervisorCollection(response);
if(supervisorCollection.size() == 0){
throw new ExternalImportException("No supervisors found in remote");
} else {
dtoCache.addAll(supervisorCollection.getSupervisors());
dtoCache.addAll(supervisorCollection.getPersons());
}
logger.info("Received " + supervisorCollection.size() + " supervisor"+(supervisorCollection.size()==1?"":"s")+" from remote");
}
@ -328,6 +334,30 @@ public class ExternalImporterDaisyImpl implements ExternalImporter {
}
return dtoCache;
}
private Set<ProjectParticipantDTO> fetchParticipants(final Map<String, String> parameters, final HttpRequestSender.REQUEST_TYPE requestType, final UrlProcessor urlProcessor) {
final String requestUrl = (urlProcessor==null?getRequestUrl():urlProcessor.process(getRequestUrl()));
final Set<ProjectParticipantDTO> dtoCache = new HashSet<ProjectParticipantDTO>();
final HttpsRequestSender senderProxy = new HttpsRequestSender(new AbstractDtoResponseHandler(){
@Override
public void handleResponse(String response) {
logger.info("Response recieved (from: "+requestUrl+"): " + response);
final ProjectParticipantDTOCollectionWrapper participantCollection = asParticipantCollection(response);//The query will always returns a collection, regardless of backing data
if(participantCollection.size() == 0){//If at this point we have nothing, we have to throw.
throw new ExternalImportException("No project participants found in remote");
}else{
dtoCache.addAll(participantCollection.getParticipants());
}
logger.info("Recieved " + participantCollection.size() + " project participant"+(participantCollection.size()==1?"":"s")+" from remote");
}
}, requestUrl, getRequestUser(), getRequestPassword(), parameters, requestType);
try{
process(senderProxy);
}catch(final ExternalImportException eie){
logger.warn("No project participants found, this may or may not indicate a problem");
}
return dtoCache;
}
private String getRequestUrl(){
return applicationSettings.getRemoteLookupUrl();
@ -361,13 +391,15 @@ public class ExternalImporterDaisyImpl implements ExternalImporter {
return (url+"/person/"+userDTO.id+"/theses");
}
});
//Fetch authors and supervisors for each project
final Set<ProjectDTO> projectsWithParticipants = projectDTOWithParticipants(fetchedProjects);
final Set<UsernameDTO> fetchedUsernames = fetchRemoteUsernames(new HashMap<String,String>(), HttpRequestSender.REQUEST_TYPE.GET, new UrlProcessor(){
@Override
String process(String url){
return(url+"/person/"+userDTO.id+"/usernames");
}
});
userDTO.projects = fetchedProjects;
userDTO.projects = projectsWithParticipants;
userDTO.usernames = fetchedUsernames;
return userDTO;
}
@ -384,7 +416,8 @@ public class ExternalImporterDaisyImpl implements ExternalImporter {
return (url+"/person/"+userDTO.id+"/theses");
}
});
userDTO.projects = fetchedProjects;
final Set<ProjectDTO> projectsWithParticipants = projectDTOWithParticipants(fetchedProjects);
userDTO.projects = projectsWithParticipants;
}
final Set<UsernameDTO> fetchedUsernames = fetchRemoteUsernames(new HashMap<String,String>(), HttpRequestSender.REQUEST_TYPE.GET, new UrlProcessor(){
@Override
@ -408,10 +441,35 @@ public class ExternalImporterDaisyImpl implements ExternalImporter {
}
return userDTO;
}
};
}
private Set<ProjectDTO> projectDTOWithParticipants(Set<ProjectDTO> fetchedProjects){
Set<ProjectDTO> projects = new HashSet<ProjectDTO>(fetchedProjects);
for (final ProjectDTO projectDTO : projects) {
Set<ProjectParticipantDTO> combinedSet = new HashSet<ProjectParticipantDTO>();
final Set<ProjectParticipantDTO> fetchedAuthors = fetchParticipants(new HashMap<String, String>(), HttpRequestSender.REQUEST_TYPE.GET, new UrlProcessor(){
@Override
String process(String url){
return(url+"/thesis/"+projectDTO.id+"/student");
}
});
combinedSet.addAll(fetchedAuthors);
final Set<ProjectParticipantDTO> fetchedOtherParticipants = fetchParticipants(new HashMap<String, String>(), HttpRequestSender.REQUEST_TYPE.GET, new UrlProcessor(){
@Override
String process(String url){
return(url+"/thesis/"+projectDTO.id+"/supervisor");
}
});
combinedSet.addAll(fetchedOtherParticipants);
projectDTO.participants = combinedSet;
}
return projects;
}
/**
* Small utility interface/default implementation used to preprocess DTO objects, intended for anonymous inner class-usage.
*/