use of org.activityinfo.legacy.shared.command.UpdatePartner in project activityinfo by bedatadriven.
the class CloneDatabaseHandler method createDefaultPartner.
private void createDefaultPartner(User user) {
Preconditions.checkNotNull(targetDb);
Preconditions.checkState(targetDb.getId() > 0);
PartnerDTO partner = new PartnerDTO();
partner.setName("Default");
new UpdatePartnerHandler(em).execute(new UpdatePartner(targetDb.getId(), partner), user);
}
use of org.activityinfo.legacy.shared.command.UpdatePartner in project activityinfo by bedatadriven.
the class UpdatePartnerHandler method copyAndReplace.
/**
* Originally, partner objects were shared between databases, which meant that it was not possible
* for a single database owner to change the partner's name or description.
* <p>
* <p>To move forward, we apply a "copy-on-write" strategy. If a user wants to change a partner, and it
* is shared with another database, then create a new copy of the partner with the desired changes,
* and update all references in this database.</p>
*/
private CreateResult copyAndReplace(Partner sharedPartner, UpdatePartner cmd) {
LOGGER.info("Copying and replacing " + sharedPartner + " with " + cmd.getPartner());
Partner newPartner = new Partner();
newPartner.setName(cmd.getPartner().getName());
newPartner.setFullName(cmd.getPartner().getFullName());
em.persist(newPartner);
assert newPartner.getId() != 0;
em.createNativeQuery("UPDATE site SET partnerId = ? WHERE partnerId = ? AND activityId IN " + "(SELECT activityid FROM activity WHERE databaseId = ?)").setParameter(1, newPartner.getId()).setParameter(2, sharedPartner.getId()).setParameter(3, cmd.getDatabaseId()).executeUpdate();
em.createNativeQuery("UPDATE activity SET version = version+1, siteVersion = siteVersion+1, schemaVersion=schemaVersion+1 WHERE databaseId = ? ").setParameter(1, cmd.getDatabaseId()).executeUpdate();
em.createNativeQuery("UPDATE userpermission SET partnerId = ? where partnerId = ? and databaseId = ?").setParameter(1, newPartner.getId()).setParameter(2, sharedPartner.getId()).setParameter(3, cmd.getDatabaseId()).executeUpdate();
em.createNativeQuery("UPDATE partnerindatabase SET partnerId = ? WHERE databaseId = ? AND partnerId = ?").setParameter(1, newPartner.getId()).setParameter(2, cmd.getDatabaseId()).setParameter(3, sharedPartner.getId()).executeUpdate();
return new CreateResult(newPartner.getId());
}
use of org.activityinfo.legacy.shared.command.UpdatePartner in project activityinfo by bedatadriven.
the class UpdatePartnerHandler method updatePartner.
private CreateResult updatePartner(Database db, UpdatePartner cmd) {
Partner partner = em.find(Partner.class, cmd.getPartner().getId());
if (partner == null) {
LOGGER.severe("Partner " + cmd.getPartner() + " is not ");
throw new IllegalArgumentException("No such partner");
}
boolean ownedByThisDatabase = false;
boolean shared = false;
LOGGER.info("Partner is shared between " + partner.getDatabases().size() + " database(s)");
for (Database database : partner.getDatabases()) {
if (database.getId() == cmd.getDatabaseId()) {
ownedByThisDatabase = true;
} else {
shared = true;
}
}
if (!ownedByThisDatabase) {
LOGGER.info("Partner " + cmd.getPartner() + " is not associated with database " + cmd.getDatabaseId());
throw new IllegalArgumentException("Partner not owned by the database");
}
// Are there any changes to actually make?
if (Objects.equals(partner.getName(), cmd.getPartner().getName()) && Objects.equals(partner.getFullName(), cmd.getPartner().getFullName())) {
LOGGER.info("No changes to make, stopping.");
return new CreateResult(cmd.getPartner().getId());
}
// Increment the database's version number
db.setLastSchemaUpdate(new Date());
// If this partner is shared, then duplicate and update all references.
if (shared) {
return copyAndReplace(partner, cmd);
} else {
return simpleUpdate(partner, cmd);
}
}
use of org.activityinfo.legacy.shared.command.UpdatePartner in project activityinfo by bedatadriven.
the class UpdatePartnerHandler method addNewPartner.
private CreateResult addNewPartner(UpdatePartner cmd, Database db) {
// first check to see if an organization by this name is already
// a partner in the same database
Set<Partner> dbPartners = db.getPartners();
for (Partner partner : dbPartners) {
if (partner.getName().equals(cmd.getPartner().getName())) {
return new DuplicateCreateResult();
}
}
// Add a new partner
Partner partner = new Partner();
partner.setName(cmd.getPartner().getName());
partner.setFullName(cmd.getPartner().getFullName());
em.persist(partner);
db.setLastSchemaUpdate(new Date());
em.persist(db);
db.getPartners().add(partner);
return new CreateResult(partner.getId());
}
use of org.activityinfo.legacy.shared.command.UpdatePartner in project activityinfo by bedatadriven.
the class UpdatePartnerHandlerTest method modifyShared.
@Test
public void modifyShared() {
SchemaDTO schema = execute(new GetSchema());
UserDatabaseDTO nfiDatabase = schema.getDatabaseById(NFI_DATABASE);
UserDatabaseDTO healthDatabase = schema.getDatabaseById(HEALTH_DATABASE);
// The "Default" partner starts out being shared between the two database.
PartnerDTO nfiDefaultPartner = nfiDatabase.getDefaultPartner().get();
PartnerDTO healthDefaultPartner = healthDatabase.getDefaultPartner().get();
assertThat(nfiDefaultPartner.getId(), equalTo(healthDefaultPartner.getId()));
// If I update the name of "Default" partner for my database, a copy should
// be created.
PartnerDTO updatedPartner = new PartnerDTO(nfiDefaultPartner.getId(), "Solidarites");
updatedPartner.setFullName("Solidarites International");
execute(new UpdatePartner(1, updatedPartner));
// Now we should have two distinct partner objects
schema = execute(new GetSchema());
nfiDatabase = schema.getDatabaseById(NFI_DATABASE);
healthDatabase = schema.getDatabaseById(HEALTH_DATABASE);
assertThat(nfiDatabase.getPartners(), containsInAnyOrder(hasProperty("name", equalTo("Solidarites")), hasProperty("name", equalTo("NRC"))));
assertThat(healthDatabase.getPartners(), contains(hasProperty("name", equalTo("Default"))));
// Users in the NFI database should be reassigned to the new NRC partner object
UserResult nfiUsers = execute(new GetUsers(NFI_DATABASE));
Optional<UserPermissionDTO> bavon = nfiUsers.getData().stream().filter(u -> u.getEmail().equals("bavon@nrc.org")).findAny();
Optional<UserPermissionDTO> lisa = nfiUsers.getData().stream().filter(u -> u.getEmail().equals("lisa@solidarites")).findAny();
// Bavon should stay as NRC
assertThat(bavon.get().getPartner(), hasProperty("name", equalTo("NRC")));
// Lisa should be moved to the new Solidarites partner
assertThat(lisa.get().getPartner(), hasProperty("name", equalTo("Solidarites")));
// Users in the Health database should be unaffected
UserResult healthUsers = execute(new GetUsers(HEALTH_DATABASE));
Optional<UserPermissionDTO> bavonInHealth = healthUsers.getData().stream().filter(u -> u.getEmail().equals("bavon@nrc.org")).findAny();
assertThat(bavonInHealth.get().getPartner(), hasProperty("name", equalTo("Default")));
// Sites in the NFI database should be update to point to the new partner
SiteDTO nfiSite1 = execute(GetSites.byId(1)).getData().get(0);
SiteDTO nfiSite3 = execute(GetSites.byId(3)).getData().get(0);
assertThat(nfiSite1.getPartner(), hasProperty("name", equalTo("Solidarites")));
assertThat(nfiSite3.getPartner(), hasProperty("name", equalTo("NRC")));
// Sites in the Health database should be unaffected
SiteDTO healthSite4 = execute(GetSites.byId(4)).getData().get(0);
assertThat(healthSite4.getPartner(), hasProperty("name", equalTo("Default")));
}
Aggregations