for registerCompetitors in the dialog callback assemble the results from the existing CompetitorDTOs and the server-provided new CompetitorDTOs

Change-Id: I119e1bf0c3ef0a3a559fc72ef623e985f02c42dc
This commit is contained in:
Axel Uhl committed 2016-12-15 21:35:27 +01:00
1 parent f48ca78eaa
commit aa4f72731d
5 files changed
+43 -17

No files matched your search

@@ -1,6 +1,7 @@
package com.sap.sailing.gwt.ui.adminconsole; package com.sap.sailing.gwt.ui.adminconsole;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
@@ -12,10 +13,12 @@ import com.sap.sse.gwt.client.ErrorReporter;
import com.sap.sse.gwt.client.dialog.DataEntryDialog.DialogCallback; import com.sap.sse.gwt.client.dialog.DataEntryDialog.DialogCallback;
/** /**
* Ñallback is responsible for saving competitors in store and registering them if necessary. Logic for registering * Callback is responsible for saving competitors that have a {@code null} {@link CompetitorDTO#getIdAsString() ID} to
* should be implemented in child's classes. * the competitor store using {@link SailingServiceAsync#addCompetitors(Iterable, AsyncCallback)} and registering them
* if necessary. Logic for registering should be implemented in subclasses by overriding the {@link #registerCompetitors(Set)}
* method which does nothing in this class.
* *
* @author Alexander_Tatarinovich * @author Alexander Tatarinovich
* *
*/ */
public class ImportCompetitorCallback implements DialogCallback<Set<CompetitorDTO>> { public class ImportCompetitorCallback implements DialogCallback<Set<CompetitorDTO>> {
@@ -52,14 +55,20 @@ public class ImportCompetitorCallback implements DialogCallback<Set<CompetitorDT
private void registerCompetitorsAfterSaving(final List<CompetitorDTO> competitorsForSaving, private void registerCompetitorsAfterSaving(final List<CompetitorDTO> competitorsForSaving,
final Set<CompetitorDTO> competitorsForRegistration) { final Set<CompetitorDTO> competitorsForRegistration) {
sailingService.addCompetitors(competitorsForSaving, new AsyncCallback<Void>() { sailingService.addCompetitors(competitorsForSaving, new AsyncCallback<List<CompetitorDTO>>() {
@Override @Override
public void onFailure(Throwable caught) { public void onFailure(Throwable caught) {
errorReporter.reportError(caught.getMessage());
} }
@Override @Override
public void onSuccess(Void result) { public void onSuccess(List<CompetitorDTO> result) {
registerCompetitors(competitorsForRegistration); final Set<CompetitorDTO> competitorsToAddWithNewOnesReplacedBySavedOnesWithId = new HashSet<>(competitorsForRegistration);
// remove the locally constructed CompetitorDTOs that had null as their ID...
competitorsToAddWithNewOnesReplacedBySavedOnesWithId.removeAll(competitorsForSaving);
// ...and replace by those returned by the server after saving to the competitor store where they received an ID:
competitorsToAddWithNewOnesReplacedBySavedOnesWithId.addAll(result);
registerCompetitors(competitorsToAddWithNewOnesReplacedBySavedOnesWithId);
} }
}); });
} }
@@ -29,7 +29,23 @@ import com.sap.sse.gwt.client.celltable.RefreshableSingleSelectionModel;
import com.sap.sse.gwt.client.dialog.DataEntryDialog; import com.sap.sse.gwt.client.dialog.DataEntryDialog;
/** /**
* Defines dialog where we can match and choose imported competitors. * Defines dialog where we can match and choose imported competitors. It mainly consists of two tables: one showing
* competitor records coming from an source for import, such as an external regatta management system, and another table
* that---upon selecting a single record in the table with the importable records--- displays potentially matching
* existing competitors from the server's competitor base. The user can then assemble a set of competitors by first
* optionally defining mappings from the importable competitors to existing competitors where instead of creating a new
* competitor by means of import the existing one shall be used. Then, the user can make a selection in the table with
* the importable competitors, including those for which a mapping to an existing competitor was defined. Pressing OK
* will produce a set of {@link CompetitorDTO}s where the ones coming from the set of already existing competitors have
* a non-{@code null} {@link CompetitorDTO#getIdAsString() ID} whereas the ones to import have all <em>but</em> an
* {@link CompetitorDTO#getIdAsString() ID}.
* <p>
*
* Use an {@link ImportCompetitorCallback} or subclass thereof as the {@link DialogCallback} passed to the constructor
* for conveniently saving imported competitors to the store using
* {@link SailingServiceAsync#addCompetitors(Iterable, com.google.gwt.user.client.rpc.AsyncCallback)}. Define a subclass
* thereof and override {@link ImportCompetitorCallback#registerCompetitors(Set)} to determine what should happen with
* the set of competitors the user has assembled through this dialog.
* *
* @author Alexander Tatarinovich * @author Alexander Tatarinovich
* *
@@ -445,7 +445,7 @@ public interface SailingService extends RemoteService, FileStorageManagementGwtS
CompetitorDTO addOrUpdateCompetitor(CompetitorDTO competitor) throws Exception; CompetitorDTO addOrUpdateCompetitor(CompetitorDTO competitor) throws Exception;
void addCompetitors(Iterable<CompetitorDTO> competitorDTOs) throws Exception; List<CompetitorDTO> addCompetitors(Iterable<CompetitorDTO> competitorDTOs) throws Exception;
void allowCompetitorResetToDefaults(Iterable<CompetitorDTO> competitors); void allowCompetitorResetToDefaults(Iterable<CompetitorDTO> competitors);
@@ -597,12 +597,7 @@ public interface SailingServiceAsync extends ServerInfoRetriever, FileStorageMan
void addOrUpdateCompetitor(CompetitorDTO competitor, AsyncCallback<CompetitorDTO> asyncCallback); void addOrUpdateCompetitor(CompetitorDTO competitor, AsyncCallback<CompetitorDTO> asyncCallback);
/** void addCompetitors(Iterable<CompetitorDTO> competitorDTOs, AsyncCallback<List<CompetitorDTO>> asyncCallback);
* The {@link CompetitorDTO#getIdAsString() ID} of the {@code competitorDTOs} (should they contain any) are
* ignored here, and new {@code Competitor} objects with new, random UUIDs will be created on the server that
* otherwise obtain their properties from the {@code competitorDTOs} passed here.
*/
void addCompetitors(Iterable<CompetitorDTO> competitorDTOs, AsyncCallback<Void> asyncCallback);
void allowCompetitorResetToDefaults(Iterable<CompetitorDTO> competitors, AsyncCallback<Void> asyncCallback); void allowCompetitorResetToDefaults(Iterable<CompetitorDTO> competitors, AsyncCallback<Void> asyncCallback);
@@ -4867,24 +4867,30 @@ public class SailingServiceImpl extends ProxiedRemoteServiceServlet implements S
} }
@Override @Override
public void addCompetitors(Iterable<CompetitorDTO> competitorDTOs) throws URISyntaxException { public List<CompetitorDTO> addCompetitors(Iterable<CompetitorDTO> competitorDTOs) throws URISyntaxException {
List<Competitor> competitorsForSaving = new ArrayList<>(); List<Competitor> competitorsForSaving = new ArrayList<>();
for (CompetitorDTO competitorDTO : competitorDTOs) { for (CompetitorDTO competitorDTO : competitorDTOs) {
Competitor competitor = convertCompetitorDTOToCompetitor(competitorDTO); Competitor competitor = convertCompetitorDTOToCompetitor(competitorDTO);
competitorsForSaving.add(competitor); competitorsForSaving.add(competitor);
} }
getBaseDomainFactory().getCompetitorStore().addCompetitors(competitorsForSaving); getBaseDomainFactory().getCompetitorStore().addCompetitors(competitorsForSaving);
return convertToCompetitorDTOs(competitorsForSaving);
} }
/**
* Creates a new {@link Competitor} object from a {@link CompetitorDTO}, assuming that the DTO does not have
* a valid {@link CompetitorDTO#getIdAsString() ID} set.
*/
private Competitor convertCompetitorDTOToCompetitor(CompetitorDTO competitorDTO) throws URISyntaxException { private Competitor convertCompetitorDTOToCompetitor(CompetitorDTO competitorDTO) throws URISyntaxException {
assert competitorDTO.getIdAsString() == null;
Nationality nationality = (competitorDTO.getThreeLetterIocCountryCode() == null Nationality nationality = (competitorDTO.getThreeLetterIocCountryCode() == null
|| competitorDTO.getThreeLetterIocCountryCode().isEmpty()) ? null || competitorDTO.getThreeLetterIocCountryCode().isEmpty()) ? null
: getBaseDomainFactory() : getBaseDomainFactory()
.getOrCreateNationality(competitorDTO.getThreeLetterIocCountryCode()); .getOrCreateNationality(competitorDTO.getThreeLetterIocCountryCode());
BoatClass boatClass = getBaseDomainFactory().getOrCreateBoatClass(competitorDTO.getBoatClass().getName()); BoatClass boatClass = getBaseDomainFactory().getOrCreateBoatClass(competitorDTO.getBoatClass().getName());
DynamicPerson sailor = new PersonImpl(competitorDTO.getName(), nationality, null, null); DynamicPerson sailor = new PersonImpl(competitorDTO.getName(), nationality, null, null);
DynamicTeam team = new TeamImpl(competitorDTO.getName() + " team", Collections.singleton(sailor), null); DynamicTeam team = new TeamImpl(competitorDTO.getName(), Collections.singleton(sailor), null);
DynamicBoat boat = new BoatImpl(competitorDTO.getName() + " boat", boatClass, competitorDTO.getSailID()); DynamicBoat boat = new BoatImpl(competitorDTO.getSailID(), boatClass, competitorDTO.getSailID());
Competitor competitor = new CompetitorImpl(UUID.randomUUID(), competitorDTO.getName(), Competitor competitor = new CompetitorImpl(UUID.randomUUID(), competitorDTO.getName(),
competitorDTO.getColor(), competitorDTO.getEmail(), competitorDTO.getColor(), competitorDTO.getEmail(),
competitorDTO.getFlagImageURL() == null ? null : new URI(competitorDTO.getFlagImageURL()), team, competitorDTO.getFlagImageURL() == null ? null : new URI(competitorDTO.getFlagImageURL()), team,