Search in sources :

Example 1 with GetSyncRegions

use of org.activityinfo.legacy.shared.command.GetSyncRegions in project activityinfo by bedatadriven.

the class GetSyncRegionsHandler method execute.

@Override
public SyncRegions execute(GetSyncRegions cmd, User user) {
    Set<Integer> countryIds = Sets.newHashSet();
    Set<Integer> visibleDatabaseIds = Sets.newHashSet();
    List<SyncRegion> databaseRegions = new ArrayList<>();
    List<Database> ownedDatabases = entityManager.createQuery("SELECT db FROM Database db WHERE db.owner = :user", Database.class).setParameter("user", user).getResultList();
    for (Database database : ownedDatabases) {
        if (!database.isDeleted()) {
            visibleDatabaseIds.add(database.getId());
            countryIds.add(database.getCountry().getId());
        }
        databaseRegions.add(new SyncRegion("db/" + database.getId(), database.getVersion()));
    }
    List<UserPermission> sharedDatabases = entityManager.createQuery("SELECT p FROM UserPermission p LEFT JOIN FETCH p.database WHERE p.user = :user", UserPermission.class).setParameter("user", user).getResultList();
    for (UserPermission permission : sharedDatabases) {
        if (permission.isAllowView() && !permission.getDatabase().isDeleted()) {
            visibleDatabaseIds.add(permission.getDatabase().getId());
            countryIds.add(permission.getDatabase().getCountry().getId());
        }
        long version = Math.max(permission.getVersion(), permission.getDatabase().getVersion());
        databaseRegions.add(new SyncRegion("db/" + permission.getDatabase().getId(), version));
    }
    List<SyncRegion> regions = Lists.newArrayList();
    regions.add(new SyncRegion("site-tables", TableDefinitionUpdateBuilder.CURRENT_VERSION));
    for (Integer countryId : countryIds) {
        regions.add(new SyncRegion("country/" + countryId, "1"));
    }
    regions.addAll(databaseRegions);
    regions.addAll(listAdminRegions(countryIds));
    regions.addAll(listLocations(countryIds));
    regions.addAll(listSiteRegions(visibleDatabaseIds));
    return new SyncRegions(regions);
}
Also used : SyncRegion(org.activityinfo.legacy.shared.command.result.SyncRegion) ArrayList(java.util.ArrayList) Database(org.activityinfo.server.database.hibernate.entity.Database) SyncRegions(org.activityinfo.legacy.shared.command.result.SyncRegions) GetSyncRegions(org.activityinfo.legacy.shared.command.GetSyncRegions) UserPermission(org.activityinfo.server.database.hibernate.entity.UserPermission)

Example 2 with GetSyncRegions

use of org.activityinfo.legacy.shared.command.GetSyncRegions in project activityinfo by bedatadriven.

the class LocationBuilderTest method locationTypeChange.

@Test
@OnDataSet("/dbunit/sites-simple1.db.xml")
public void locationTypeChange() throws Exception {
    EntityManager em = emf.createEntityManager();
    User user = em.find(User.class, 1);
    // Update the location type 1
    Map<String, Object> changes = new HashMap<>();
    changes.put("name", "Ishamael");
    em.getTransaction().begin();
    LocationTypePolicy locationTypePolicy = new LocationTypePolicy(em);
    locationTypePolicy.update(user, 1, new PropertyMap(changes));
    em.getTransaction().commit();
    // First update should include this change
    String regionId = "location/" + 1;
    LocationUpdateBuilder builder = new LocationUpdateBuilder(em);
    GetSyncRegionUpdates request = new GetSyncRegionUpdates(regionId, null);
    SyncRegionUpdate update = builder.build(user, request);
    assertThat(update.isComplete(), equalTo(true));
    assertThat(update.getSql(), containsString("Ishamael"));
    // We should be up to date now...
    GetSyncRegionsHandler getSyncRegionsHandler = new GetSyncRegionsHandler(em);
    SyncRegions syncRegions = getSyncRegionsHandler.execute(new GetSyncRegions(), user);
    System.out.println(syncRegions.getList());
    assertThat(syncRegions, hasItem(new SyncRegion(regionId, update.getVersion())));
}
Also used : GetSyncRegionUpdates(org.activityinfo.legacy.shared.command.GetSyncRegionUpdates) User(org.activityinfo.server.database.hibernate.entity.User) HashMap(java.util.HashMap) LocationTypePolicy(org.activityinfo.server.command.handler.crud.LocationTypePolicy) SyncRegions(org.activityinfo.legacy.shared.command.result.SyncRegions) GetSyncRegions(org.activityinfo.legacy.shared.command.GetSyncRegions) GetSyncRegions(org.activityinfo.legacy.shared.command.GetSyncRegions) SyncRegion(org.activityinfo.legacy.shared.command.result.SyncRegion) EntityManager(javax.persistence.EntityManager) PropertyMap(org.activityinfo.server.command.handler.crud.PropertyMap) SyncRegionUpdate(org.activityinfo.legacy.shared.command.result.SyncRegionUpdate) GetSyncRegionsHandler(org.activityinfo.server.command.handler.GetSyncRegionsHandler) OnDataSet(org.activityinfo.server.database.OnDataSet) Test(org.junit.Test)

Aggregations

GetSyncRegions (org.activityinfo.legacy.shared.command.GetSyncRegions)2 SyncRegion (org.activityinfo.legacy.shared.command.result.SyncRegion)2 SyncRegions (org.activityinfo.legacy.shared.command.result.SyncRegions)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 EntityManager (javax.persistence.EntityManager)1 GetSyncRegionUpdates (org.activityinfo.legacy.shared.command.GetSyncRegionUpdates)1 SyncRegionUpdate (org.activityinfo.legacy.shared.command.result.SyncRegionUpdate)1 GetSyncRegionsHandler (org.activityinfo.server.command.handler.GetSyncRegionsHandler)1 LocationTypePolicy (org.activityinfo.server.command.handler.crud.LocationTypePolicy)1 PropertyMap (org.activityinfo.server.command.handler.crud.PropertyMap)1 OnDataSet (org.activityinfo.server.database.OnDataSet)1 Database (org.activityinfo.server.database.hibernate.entity.Database)1 User (org.activityinfo.server.database.hibernate.entity.User)1 UserPermission (org.activityinfo.server.database.hibernate.entity.UserPermission)1 Test (org.junit.Test)1