use of org.activityinfo.shared.command.result.DuplicateCreateResult in project activityinfo by bedatadriven.
the class DbPartnerEditor method onAdd.
// public void onSelectionChanged(PartnerDTO selectedItem) {
// this.view.setActionEnabled(UIActions.delete, selectedItem != null);
// }
@Override
protected void onAdd() {
final PartnerDTO newPartner = new PartnerDTO();
this.view.showAddDialog(newPartner, new FormDialogCallback() {
@Override
public void onValidated(final FormDialogTether dlg) {
service.execute(new AddPartner(db.getId(), newPartner), dlg, new AsyncCallback<CreateResult>() {
@Override
public void onFailure(Throwable caught) {
Log.debug("DbPartnerEditor caught exception while executing command AddPartner: ", caught);
}
@Override
public void onSuccess(CreateResult result) {
if (result instanceof DuplicateCreateResult) {
Log.debug("DbPartnerEditor tried to add partner '" + newPartner.getName() + "' to database " + db.getId() + " but it already exists");
MessageBox.alert(I18N.CONSTANTS.newPartner(), I18N.CONSTANTS.duplicatePartner(), null);
} else {
Log.debug("DbPartnerEditor added new partner '" + newPartner.getName() + "' to database " + db.getId());
newPartner.setId(result.getNewId());
store.add(newPartner);
eventBus.fireEvent(AppEvents.SCHEMA_CHANGED);
dlg.hide();
}
}
});
}
});
}
use of org.activityinfo.shared.command.result.DuplicateCreateResult in project activityinfo by bedatadriven.
the class AddPartnerHandler method execute.
@Override
@SuppressWarnings("unchecked")
public CommandResult execute(AddPartner cmd, User user) throws CommandException {
UserDatabase db = em.find(UserDatabase.class, cmd.getDatabaseId());
if (db.getOwner().getId() != user.getId()) {
UserPermission perm = db.getPermissionByUser(user);
if (perm == null || !perm.isAllowManageAllUsers()) {
throw new IllegalAccessCommandException("The user does not have the manageAllUsers permission.");
}
}
// first check to see if an organization by this name is already
// a partner
Set<Partner> dbPartners = db.getPartners();
for (Partner partner : dbPartners) {
if (partner.getName().equals(cmd.getPartner().getName())) {
return new DuplicateCreateResult();
}
}
// now try to match this partner by name
List<Partner> allPartners = em.createQuery("select p from Partner p where p.name = ?1").setParameter(1, cmd.getPartner().getName()).getResultList();
if (allPartners.size() != 0) {
db.getPartners().add(allPartners.get(0));
return new CreateResult(allPartners.get(0).getId());
}
// nope, have to create a new record
Partner newPartner = new Partner();
newPartner.setName(cmd.getPartner().getName());
newPartner.setFullName(cmd.getPartner().getFullName());
em.persist(newPartner);
db.setLastSchemaUpdate(new Date());
em.persist(db);
db.getPartners().add(newPartner);
return new CreateResult(newPartner.getId());
}
use of org.activityinfo.shared.command.result.DuplicateCreateResult in project activityinfo by bedatadriven.
the class PartnerTest method testAddDuplicatePartner.
public void testAddDuplicatePartner() throws Exception {
PartnerDTO newPartner = new PartnerDTO();
newPartner.setName("NRC");
newPartner.setFullName("Norweigen Refugee Committe");
CreateResult cr = execute(new AddPartner(1, newPartner));
Assert.assertTrue(cr instanceof DuplicateCreateResult);
}
Aggregations