Search in sources :

Example 1 with Partner

use of org.activityinfo.server.database.hibernate.entity.Partner 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());
}
Also used : CreateResult(org.activityinfo.legacy.shared.command.result.CreateResult) DuplicateCreateResult(org.activityinfo.legacy.shared.command.result.DuplicateCreateResult) Partner(org.activityinfo.server.database.hibernate.entity.Partner) UpdatePartner(org.activityinfo.legacy.shared.command.UpdatePartner)

Example 2 with Partner

use of org.activityinfo.server.database.hibernate.entity.Partner 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);
    }
}
Also used : CreateResult(org.activityinfo.legacy.shared.command.result.CreateResult) DuplicateCreateResult(org.activityinfo.legacy.shared.command.result.DuplicateCreateResult) Database(org.activityinfo.server.database.hibernate.entity.Database) Partner(org.activityinfo.server.database.hibernate.entity.Partner) UpdatePartner(org.activityinfo.legacy.shared.command.UpdatePartner) Date(java.util.Date)

Example 3 with Partner

use of org.activityinfo.server.database.hibernate.entity.Partner 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());
}
Also used : DuplicateCreateResult(org.activityinfo.legacy.shared.command.result.DuplicateCreateResult) CreateResult(org.activityinfo.legacy.shared.command.result.CreateResult) DuplicateCreateResult(org.activityinfo.legacy.shared.command.result.DuplicateCreateResult) Partner(org.activityinfo.server.database.hibernate.entity.Partner) UpdatePartner(org.activityinfo.legacy.shared.command.UpdatePartner) Date(java.util.Date)

Example 4 with Partner

use of org.activityinfo.server.database.hibernate.entity.Partner in project activityinfo by bedatadriven.

the class ActivityDigestModelBuilder method createDatabaseModel.

private void createDatabaseModel(ActivityDigestModel model, Database database) {
    SiteHistory lastEdit = findLastEdit(database);
    // only include databases that are known to be edited at least once
    if (lastEdit != null) {
        DatabaseModel databaseModel = new DatabaseModel(model, database, lastEdit);
        List<SiteHistory> ownerHistories = findSiteHistory(databaseModel, database.getOwner());
        ActivityMap ownerActivityMap = new ActivityMap(databaseModel, database.getOwner(), ownerHistories);
        databaseModel.setOwnerActivityMap(ownerActivityMap);
        List<Partner> partners = findPartners(databaseModel);
        LOGGER.finest("building user activity digest for user " + model.getUserDigest().getUser().getId() + " and database " + database.getId() + " - found " + partners.size() + " partner(s)");
        if (!partners.isEmpty()) {
            for (Partner partner : partners) {
                PartnerActivityModel partnerModel = new PartnerActivityModel(databaseModel, partner);
                List<User> partnerUsers = findUsers(partnerModel);
                LOGGER.finest("found users " + partnerUsers + " for partner " + partner.getName());
                if (!partnerUsers.isEmpty()) {
                    for (User partnerUser : partnerUsers) {
                        List<SiteHistory> histories = findSiteHistory(databaseModel, partnerUser);
                        ActivityMap activityMap = new ActivityMap(databaseModel, partnerUser, histories);
                        partnerModel.addActivityMap(activityMap);
                    }
                }
            }
        }
    }
}
Also used : ActivityMap(org.activityinfo.server.digest.activity.ActivityDigestModel.ActivityMap) PartnerActivityModel(org.activityinfo.server.digest.activity.ActivityDigestModel.PartnerActivityModel) DatabaseModel(org.activityinfo.server.digest.activity.ActivityDigestModel.DatabaseModel) User(org.activityinfo.server.database.hibernate.entity.User) SiteHistory(org.activityinfo.server.database.hibernate.entity.SiteHistory) Partner(org.activityinfo.server.database.hibernate.entity.Partner)

Example 5 with Partner

use of org.activityinfo.server.database.hibernate.entity.Partner in project activityinfo by bedatadriven.

the class SignUpConfirmationController method addUserToDefaultDatabase.

protected void addUserToDefaultDatabase(User user) {
    Database database = entityManager.find(Database.class, DEFAULT_DATABASE_ID);
    if (database == null) {
        LOGGER.severe("Default database " + DEFAULT_DATABASE_ID + " does not exist, unable to add user " + user.getEmail());
        return;
    }
    Partner partner = entityManager.find(Partner.class, DEFAULT_PARTNER_ID);
    if (partner == null) {
        LOGGER.severe("Default partner " + DEFAULT_PARTNER_ID + " does not exist, unable to add user " + user.getEmail());
        return;
    }
    UserPermission permission = new UserPermission(database, user);
    permission.setPartner(partner);
    permission.setAllowView(true);
    permission.setAllowViewAll(true);
    permission.setLastSchemaUpdate(new Date());
    entityManager.persist(permission);
}
Also used : Database(org.activityinfo.server.database.hibernate.entity.Database) Partner(org.activityinfo.server.database.hibernate.entity.Partner) Date(java.util.Date) UserPermission(org.activityinfo.server.database.hibernate.entity.UserPermission)

Aggregations

Partner (org.activityinfo.server.database.hibernate.entity.Partner)6 Date (java.util.Date)4 UpdatePartner (org.activityinfo.legacy.shared.command.UpdatePartner)3 CreateResult (org.activityinfo.legacy.shared.command.result.CreateResult)3 DuplicateCreateResult (org.activityinfo.legacy.shared.command.result.DuplicateCreateResult)3 Database (org.activityinfo.server.database.hibernate.entity.Database)3 RemovePartner (org.activityinfo.legacy.shared.command.RemovePartner)1 RemoveFailedResult (org.activityinfo.legacy.shared.command.result.RemoveFailedResult)1 RemoveResult (org.activityinfo.legacy.shared.command.result.RemoveResult)1 SiteHistory (org.activityinfo.server.database.hibernate.entity.SiteHistory)1 User (org.activityinfo.server.database.hibernate.entity.User)1 UserPermission (org.activityinfo.server.database.hibernate.entity.UserPermission)1 ActivityMap (org.activityinfo.server.digest.activity.ActivityDigestModel.ActivityMap)1 DatabaseModel (org.activityinfo.server.digest.activity.ActivityDigestModel.DatabaseModel)1 PartnerActivityModel (org.activityinfo.server.digest.activity.ActivityDigestModel.PartnerActivityModel)1