Author: bleny Date: 2014-06-25 12:41:38 +0200 (Wed, 25 Jun 2014) New Revision: 2101 Url: http://forge.codelutin.com/projects/wao/repository/revisions/2101 Log: fixes #5315 deal with wrong contact id in import Added: trunk/wao-services/src/main/java/fr/ifremer/wao/services/service/UnknownContactIdException.java Modified: trunk/wao-services/src/main/java/fr/ifremer/wao/services/AuthenticatedWaoUser.java trunk/wao-services/src/main/java/fr/ifremer/wao/services/service/ContactsService.java trunk/wao-services/src/main/resources/i18n/wao-services_en_GB.properties trunk/wao-services/src/main/resources/i18n/wao-services_fr_FR.properties trunk/wao-web/src/main/java/fr/ifremer/wao/web/action/DeleteContactAction.java trunk/wao-web/src/main/java/fr/ifremer/wao/web/action/EditContactAction.java trunk/wao-web/src/main/java/fr/ifremer/wao/web/action/ValidateContactJsonAction.java trunk/wao-web/src/main/resources/i18n/wao-web_en_GB.properties trunk/wao-web/src/main/resources/i18n/wao-web_fr_FR.properties Modified: trunk/wao-services/src/main/java/fr/ifremer/wao/services/AuthenticatedWaoUser.java =================================================================== --- trunk/wao-services/src/main/java/fr/ifremer/wao/services/AuthenticatedWaoUser.java 2014-06-23 22:11:57 UTC (rev 2100) +++ trunk/wao-services/src/main/java/fr/ifremer/wao/services/AuthenticatedWaoUser.java 2014-06-25 10:41:38 UTC (rev 2101) @@ -206,6 +206,10 @@ return userProfile.isCoordinatorOrObserver() && isCanWrite(); } + public boolean isAuthorizedToUpdateContact() { + return (userProfile.isCoordinatorOrObserver() || userProfile.isAdmin()) && isCanWrite(); + } + public boolean isAuthorizedToViewSamplingPlanReal() { return ! userProfile.isGuest(); } Modified: trunk/wao-services/src/main/java/fr/ifremer/wao/services/service/ContactsService.java =================================================================== --- trunk/wao-services/src/main/java/fr/ifremer/wao/services/service/ContactsService.java 2014-06-23 22:11:57 UTC (rev 2100) +++ trunk/wao-services/src/main/java/fr/ifremer/wao/services/service/ContactsService.java 2014-06-25 10:41:38 UTC (rev 2101) @@ -185,23 +185,60 @@ return result; } - public UpdateContactCommand newUpdateContactCommand(AuthenticatedWaoUser authenticatedWaoUser, Optional<String> optionalContactId) { + public UpdateContactCommand newUpdateContactCommand(AuthenticatedWaoUser authenticatedWaoUser, Optional<String> optionalContactId) throws UnknownContactIdException { - //FIXME Should we apply some security rules (can create, update, import) ? - //Preconditions.checkState(authenticatedWaoUser.isAuthorizedToCreateSampleRow()); + UpdateContactCommand updateContactCommand; + if (optionalContactId.isPresent()) { + + updateContactCommand = newUpdateContactCommand(authenticatedWaoUser, optionalContactId.get()); + + } else { + + updateContactCommand = newUpdateContactCommandForCreation(authenticatedWaoUser); + + } + + return updateContactCommand; + + } + + public UpdateContactCommand newUpdateContactCommandForCreation(AuthenticatedWaoUser authenticatedWaoUser) { + + Preconditions.checkState(authenticatedWaoUser.isAuthorizedToCreateContact()); + + Contact contact = new ContactImpl(); + + contact.setObsProgram(authenticatedWaoUser.getObsProgram()); + + contact.setCreationDate(getNow()); + UpdateContactCommand updateContactCommand = new UpdateContactCommand(); updateContactCommand.setAdmin(authenticatedWaoUser.isAdmin()); - Contact contact; + updateContactCommand.setContact(contact); - if (optionalContactId.isPresent()) { + updateContactCommand.setCreation(true); - String contactId = optionalContactId.get(); + return updateContactCommand; - contact = getContact(contactId); + } + public UpdateContactCommand newUpdateContactCommand(AuthenticatedWaoUser authenticatedWaoUser, String contactId) throws UnknownContactIdException { + + Preconditions.checkState(authenticatedWaoUser.isAuthorizedToUpdateContact()); + + UpdateContactCommand updateContactCommand = new UpdateContactCommand(); + + updateContactCommand.setAdmin(authenticatedWaoUser.isAdmin()); + + Optional<Contact> optionalContact = getContactDao().forTopiaIdEquals(contactId).tryFindUnique(); + + if (optionalContact.isPresent()) { + + Contact contact = optionalContact.get(); + updateContactCommand.setContact(contact); updateContactCommand.setCreation(false); @@ -218,24 +255,13 @@ } else { - contact = new ContactImpl(); + throw new UnknownContactIdException(contactId); - contact.setObsProgram(authenticatedWaoUser.getObsProgram()); - - contact.setCreationDate(getNow()); - - updateContactCommand.setContact(contact); - - updateContactCommand.setCreation(true); } return updateContactCommand; } - public Contact getContact(String contactId) { - return getContactDao().forTopiaIdEquals(contactId).findUnique(); - } - public InputStream exportContacts(ContactsFilter filter) { ContactTopiaDao dao = getContactDao(); @@ -308,8 +334,13 @@ } Optional<String> optionalContactId = Optional.fromNullable(contactId); - UpdateContactCommand updateContactCommand = newUpdateContactCommand(authenticatedWaoUser, - optionalContactId); + UpdateContactCommand updateContactCommand; + try { + updateContactCommand = newUpdateContactCommand(authenticatedWaoUser, optionalContactId); + } catch (UnknownContactIdException e) { + String message = l(l, "wao.import.contact.failure.unknownContactId", lineNumber); + throw new ImportErrorException(message); + } if (updateContactCommand.isCreation()) { @@ -773,11 +804,11 @@ public void delete(String contactId) throws IllegalDeletionException { - Contact contact = getContact(contactId); - // Execute delete ContactTopiaDao dao = getContactDao(); + Contact contact = dao.forTopiaIdEquals(contactId).findUnique(); + Map<Class<? extends TopiaEntity>, List<? extends TopiaEntity>> allUsages = dao.findAllUsages(contact); if (!allUsages.isEmpty()) { @@ -955,7 +986,7 @@ log.info(authenticatedWaoUser.getWaoUser() + " is creating a contact for " + sampleRowId + " and " + boatId); } - UpdateContactCommand updateContactCommand = newUpdateContactCommand(authenticatedWaoUser, Optional.<String>absent()); + UpdateContactCommand updateContactCommand = newUpdateContactCommandForCreation(authenticatedWaoUser); Boat boat = getBoatDao().findByTopiaId(boatId); Preconditions.checkState(boat.isActive(), "boat must be active"); Added: trunk/wao-services/src/main/java/fr/ifremer/wao/services/service/UnknownContactIdException.java =================================================================== --- trunk/wao-services/src/main/java/fr/ifremer/wao/services/service/UnknownContactIdException.java (rev 0) +++ trunk/wao-services/src/main/java/fr/ifremer/wao/services/service/UnknownContactIdException.java 2014-06-25 10:41:38 UTC (rev 2101) @@ -0,0 +1,10 @@ +package fr.ifremer.wao.services.service; + +public class UnknownContactIdException extends Throwable { + + protected String contactId; + + public UnknownContactIdException(String contactId) { + this.contactId = contactId; + } +} Modified: trunk/wao-services/src/main/resources/i18n/wao-services_en_GB.properties =================================================================== --- trunk/wao-services/src/main/resources/i18n/wao-services_en_GB.properties 2014-06-23 22:11:57 UTC (rev 2100) +++ trunk/wao-services/src/main/resources/i18n/wao-services_en_GB.properties 2014-06-25 10:41:38 UTC (rev 2101) @@ -65,7 +65,8 @@ wao.import.contact.failure.observedDataControlToCorrectionAsked=A contact cannot be accepted if observed data control shows that a correction is asked wao.import.contact.failure.sampleRowCodeMissing=The code of the sample row line is missing wao.import.contact.failure.terrestrialLocationMissing=The code of the terrestrial location is missing -wao.import.contact.failure.transmissionDateBeforeDataInputDate=Il faut que la date de transmission de la restitution soit après la date de saisie des données +wao.import.contact.failure.transmissionDateBeforeDataInputDate=Transmission date must be before data input date +wao.import.contact.failure.unknownContactId=Unknown contact id wao.import.contact.failure.unkwonCompany=Company named '%s' does not exist wao.import.contact.failure.unwantedContactStateMotif=You must not give a motif wao.import.contact.failure.wrongBoat=There is no boat with plate number '%s' Modified: trunk/wao-services/src/main/resources/i18n/wao-services_fr_FR.properties =================================================================== --- trunk/wao-services/src/main/resources/i18n/wao-services_fr_FR.properties 2014-06-23 22:11:57 UTC (rev 2100) +++ trunk/wao-services/src/main/resources/i18n/wao-services_fr_FR.properties 2014-06-25 10:41:38 UTC (rev 2101) @@ -63,6 +63,7 @@ wao.import.contact.failure.sampleRowCodeMissing=Il manque le code de la ligne de plan associée wao.import.contact.failure.terrestrialLocationMissing=Il manque le code du lieu wao.import.contact.failure.transmissionDateBeforeDataInputDate=Ligne %s \: La date de transmission de la restitution doit être postérieure à la date de saisie des données +wao.import.contact.failure.unknownContactId=Ligne %s \: l'identifiant du contact ne correspond à aucun contact connu en base wao.import.contact.failure.unkwonCompany=Il n'y a pas de société ayant pour nom '%s' wao.import.contact.failure.unwantedContactStateMotif=Ligne %s \: Il ne faut pas préciser de motif de refus wao.import.contact.failure.wrongBoat=Il n'y a pas de navire avec l'immatriculation '%s' Modified: trunk/wao-web/src/main/java/fr/ifremer/wao/web/action/DeleteContactAction.java =================================================================== --- trunk/wao-web/src/main/java/fr/ifremer/wao/web/action/DeleteContactAction.java 2014-06-23 22:11:57 UTC (rev 2100) +++ trunk/wao-web/src/main/java/fr/ifremer/wao/web/action/DeleteContactAction.java 2014-06-25 10:41:38 UTC (rev 2101) @@ -21,7 +21,6 @@ * #L% */ -import fr.ifremer.wao.entity.Contact; import fr.ifremer.wao.services.service.ContactsService; import fr.ifremer.wao.services.service.IllegalDeletionException; import fr.ifremer.wao.web.WaoJspActionSupport; @@ -61,20 +60,17 @@ @Override public String execute() { - Contact contact = service.getContact(contactId); - String contactCode = contact.getMainObserver().getFullName(); - String result; try { service.delete(contactId); - session.addMessage(t("wao.ui.action.deleteContact.success", contactCode)); + session.addMessage(t("wao.ui.action.deleteContact.success")); result = SUCCESS; } catch (IllegalDeletionException e) { - session.addErrorMessages(t("wao.ui.action.deleteContact.failure", contactCode)); + session.addErrorMessages(t("wao.ui.action.deleteContact.failure")); session.addErrorMessages(e.getExplanation(getLocale())); result = ERROR; Modified: trunk/wao-web/src/main/java/fr/ifremer/wao/web/action/EditContactAction.java =================================================================== --- trunk/wao-web/src/main/java/fr/ifremer/wao/web/action/EditContactAction.java 2014-06-23 22:11:57 UTC (rev 2100) +++ trunk/wao-web/src/main/java/fr/ifremer/wao/web/action/EditContactAction.java 2014-06-25 10:41:38 UTC (rev 2101) @@ -62,6 +62,7 @@ import fr.ifremer.wao.services.service.MissingContactRestitutionException; import fr.ifremer.wao.services.service.MissingContactStateMotifException; import fr.ifremer.wao.services.service.MissingContactTerrestrialLocationException; +import fr.ifremer.wao.services.service.UnknownContactIdException; import fr.ifremer.wao.services.service.UnwantedContactContactStateMotifException; import fr.ifremer.wao.services.service.UpdateContactCommand; import fr.ifremer.wao.services.service.administration.ReferentialService; @@ -204,7 +205,11 @@ @Override public void prepare() { - updateContactCommand = service.newUpdateContactCommand(getAuthenticatedWaoUser(), optionalContactId); + try { + updateContactCommand = service.newUpdateContactCommand(getAuthenticatedWaoUser(), optionalContactId); + } catch (UnknownContactIdException e) { + addActionError(t("wao.ui.error.unknownContactId")); + } List<WaoUser> waoUsers = waoUsersService.getActiveWaoUsers(Optional.of(updateContactCommand.getContact().getSampleRow().getCompany().getTopiaId())); Set<WaoUser> sortedWaoUsers = Sets.newTreeSet(WaoUsers.fullNameComparator()); Modified: trunk/wao-web/src/main/java/fr/ifremer/wao/web/action/ValidateContactJsonAction.java =================================================================== --- trunk/wao-web/src/main/java/fr/ifremer/wao/web/action/ValidateContactJsonAction.java 2014-06-23 22:11:57 UTC (rev 2100) +++ trunk/wao-web/src/main/java/fr/ifremer/wao/web/action/ValidateContactJsonAction.java 2014-06-25 10:41:38 UTC (rev 2101) @@ -21,15 +21,14 @@ * #L% */ -import com.google.common.base.Optional; import com.google.common.base.Preconditions; import com.opensymphony.xwork2.Preparable; import fr.ifremer.wao.WaoTechnicalException; import fr.ifremer.wao.WaoUtils; import fr.ifremer.wao.entity.Contact; import fr.ifremer.wao.services.AuthenticatedWaoUser; +import fr.ifremer.wao.services.service.ContactDataInputDateAfterTodayException; import fr.ifremer.wao.services.service.ContactDataInputDateBeforeObservationEndDateException; -import fr.ifremer.wao.services.service.ContactDataInputDateAfterTodayException; import fr.ifremer.wao.services.service.ContactNotUpdatableException; import fr.ifremer.wao.services.service.ContactObservationEndDateAfterTodayException; import fr.ifremer.wao.services.service.ContactObservationEndDateBeforeBeginDateException; @@ -54,6 +53,7 @@ import fr.ifremer.wao.services.service.MissingContactRestitutionException; import fr.ifremer.wao.services.service.MissingContactStateMotifException; import fr.ifremer.wao.services.service.MissingContactTerrestrialLocationException; +import fr.ifremer.wao.services.service.UnknownContactIdException; import fr.ifremer.wao.services.service.UnwantedContactContactStateMotifException; import fr.ifremer.wao.services.service.UpdateContactCommand; import fr.ifremer.wao.web.WaoJsonActionSupport; @@ -137,9 +137,13 @@ AuthenticatedWaoUser authenticatedWaoUser = session.getAuthenticatedWaoUser(); - updateContactCommand = service.newUpdateContactCommand( - session.getAuthenticatedWaoUser(), - Optional.of(contactId)); + try { + updateContactCommand = service.newUpdateContactCommand( + session.getAuthenticatedWaoUser(), + contactId); + } catch (UnknownContactIdException e) { + addActionError(t("wao.ui.error.unknownContactId")); + } if (authenticatedWaoUser.isAdmin()) { updateContactCommand.setValidationProgram(validationState); Modified: trunk/wao-web/src/main/resources/i18n/wao-web_en_GB.properties =================================================================== --- trunk/wao-web/src/main/resources/i18n/wao-web_en_GB.properties 2014-06-23 22:11:57 UTC (rev 2100) +++ trunk/wao-web/src/main/resources/i18n/wao-web_en_GB.properties 2014-06-25 10:41:38 UTC (rev 2101) @@ -163,6 +163,7 @@ wao.ui.entity.TerrestrialLocation=Place wao.ui.entity.fishingGearDCF=Gear DCF code wao.ui.entity.targetSpeciesDCF=Target species DCF code +wao.ui.error.unknownContactId=This contact doesn't exist wao.ui.field.Boat.boatGroup=Boat group wao.ui.field.Boat.boatLength=Length wao.ui.field.Boat.buildYear=Build year Modified: trunk/wao-web/src/main/resources/i18n/wao-web_fr_FR.properties =================================================================== --- trunk/wao-web/src/main/resources/i18n/wao-web_fr_FR.properties 2014-06-23 22:11:57 UTC (rev 2100) +++ trunk/wao-web/src/main/resources/i18n/wao-web_fr_FR.properties 2014-06-25 10:41:38 UTC (rev 2101) @@ -163,6 +163,7 @@ wao.ui.entity.TerrestrialLocation=Lieu wao.ui.entity.fishingGearDCF=Engin code DCF wao.ui.entity.targetSpeciesDCF=Ensembles d'espèces-cible code DCF +wao.ui.error.unknownContactId=Ce contact n'existe pas wao.ui.field.Boat.boatGroup=Strate wao.ui.field.Boat.boatLength=Longueur wao.ui.field.Boat.buildYear=Année de construction
participants (1)
-
bleny@users.forge.codelutin.com