Search in sources :

Example 1 with RemovePartner

use of org.activityinfo.shared.command.RemovePartner in project activityinfo by bedatadriven.

the class LocalSiteCreateTest method siteRemovePartnerConflict.

@Test
@OnDataSet("/dbunit/sites-simple1.db.xml")
public void siteRemovePartnerConflict() {
    // FIRST U1 adds a new partner
    int databaseId = 1;
    PartnerDTO iom = new PartnerDTO();
    iom.setName("IOM");
    CreateResult result = executeRemotely(new AddPartner(databaseId, iom));
    iom.setId(result.getNewId());
    // Now U2 synchronizes, and adds a new site with this partner
    synchronizeFirstTime();
    SiteDTO site = new SiteDTO();
    site.setId(3343234);
    site.setActivityId(1);
    site.setPartner(iom);
    site.setDate1(new Date());
    site.setDate2(new Date());
    site.setLocationId(1);
    executeLocally(new CreateSite(site));
    // At T+3, U2 thinks better, removes IOM
    executeRemotely(new RemovePartner(databaseId, iom.getId()));
    // At T+4, U1 synchronizes, and IOM is removed, but site remains
    synchronize();
    // Verify that there is still a label for this partner
    SiteResult sites = executeLocally(GetSites.byId(site.getId()));
    assertThat(sites.getTotalLength(), equalTo(1));
    assertThat(sites.getData().get(0).getName(), equalTo(site.getName()));
}
Also used : PartnerDTO(org.activityinfo.shared.dto.PartnerDTO) CreateResult(org.activityinfo.shared.command.result.CreateResult) SiteResult(org.activityinfo.shared.command.result.SiteResult) RemovePartner(org.activityinfo.shared.command.RemovePartner) AddPartner(org.activityinfo.shared.command.AddPartner) SiteDTO(org.activityinfo.shared.dto.SiteDTO) Date(java.util.Date) CreateSite(org.activityinfo.shared.command.CreateSite) OnDataSet(org.activityinfo.server.database.OnDataSet) Test(org.junit.Test)

Example 2 with RemovePartner

use of org.activityinfo.shared.command.RemovePartner in project activityinfo by bedatadriven.

the class RemovePartnerHandler method execute.

@Override
public CommandResult execute(RemovePartner cmd, User user) throws CommandException {
    // verify the current user has access to this site
    UserDatabase db = em.find(UserDatabase.class, cmd.getDatabaseId());
    if (db.getOwner().getId() != user.getId()) {
        UserPermission perm = db.getPermissionByUser(user);
        if (perm == null || !perm.isAllowDesign()) {
            throw new IllegalAccessCommandException();
        }
    }
    // check to see if there are already sites associated with this partner
    int siteCount = ((Number) em.createQuery("select count(s) " + "from Site s " + "where s.activity.id in (select a.id from Activity a where a.database.id = :dbId) " + "and s.partner.id = :partnerId " + "and s.dateDeleted is null").setParameter("dbId", cmd.getDatabaseId()).setParameter("partnerId", cmd.getPartnerId()).getSingleResult()).intValue();
    if (siteCount > 0) {
        return new RemoveFailedResult();
    }
    db.getPartners().remove(em.getReference(Partner.class, cmd.getPartnerId()));
    db.setLastSchemaUpdate(new Date());
    return new RemoveResult();
}
Also used : RemoveResult(org.activityinfo.shared.command.result.RemoveResult) IllegalAccessCommandException(org.activityinfo.shared.exception.IllegalAccessCommandException) RemoveFailedResult(org.activityinfo.shared.command.result.RemoveFailedResult) UserDatabase(org.activityinfo.server.database.hibernate.entity.UserDatabase) Partner(org.activityinfo.server.database.hibernate.entity.Partner) RemovePartner(org.activityinfo.shared.command.RemovePartner) Date(java.util.Date) UserPermission(org.activityinfo.server.database.hibernate.entity.UserPermission)

Aggregations

Date (java.util.Date)2 RemovePartner (org.activityinfo.shared.command.RemovePartner)2 OnDataSet (org.activityinfo.server.database.OnDataSet)1 Partner (org.activityinfo.server.database.hibernate.entity.Partner)1 UserDatabase (org.activityinfo.server.database.hibernate.entity.UserDatabase)1 UserPermission (org.activityinfo.server.database.hibernate.entity.UserPermission)1 AddPartner (org.activityinfo.shared.command.AddPartner)1 CreateSite (org.activityinfo.shared.command.CreateSite)1 CreateResult (org.activityinfo.shared.command.result.CreateResult)1 RemoveFailedResult (org.activityinfo.shared.command.result.RemoveFailedResult)1 RemoveResult (org.activityinfo.shared.command.result.RemoveResult)1 SiteResult (org.activityinfo.shared.command.result.SiteResult)1 PartnerDTO (org.activityinfo.shared.dto.PartnerDTO)1 SiteDTO (org.activityinfo.shared.dto.SiteDTO)1 IllegalAccessCommandException (org.activityinfo.shared.exception.IllegalAccessCommandException)1 Test (org.junit.Test)1