Search in sources :

Example 1 with Database

use of org.activityinfo.server.database.hibernate.entity.Database 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 Database

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

the class GetUsersHandler method execute.

@Override
public CommandResult execute(GetUsers cmd, User currentUser) {
    Database db = em.getReference(Database.class, cmd.getDatabaseId());
    UserPermission currentUserPermission = PermissionOracle.using(em).getPermissionByUser(db, currentUser);
    assertAuthorized(currentUserPermission);
    String whereClause = "up.database.id = :dbId and " + "up.user.id <> :currentUserId and " + "up.allowView = true";
    if (!currentUserPermission.isAllowManageAllUsers()) {
        whereClause += " and up.partner.id = " + currentUserPermission.getPartner().getId();
    }
    TypedQuery<UserPermission> query = em.createQuery("select up from UserPermission up where " + whereClause + " " + composeOrderByClause(cmd), UserPermission.class).setParameter("dbId", cmd.getDatabaseId()).setParameter("currentUserId", currentUser.getId());
    List<Folder> folders = em.createQuery("select f from Folder f where f.database.id = :dbId", Folder.class).setParameter("dbId", cmd.getDatabaseId()).getResultList();
    Map<ResourceId, Folder> folderMap = new HashMap<>();
    for (Folder folder : folders) {
        folderMap.put(CuidAdapter.folderId(folder.getId()), folder);
    }
    if (cmd.getOffset() > 0) {
        query.setFirstResult(cmd.getOffset());
    }
    if (cmd.getLimit() > 0) {
        query.setMaxResults(cmd.getLimit());
    }
    List<UserPermissionDTO> models = new ArrayList<>();
    for (UserPermission perm : query.getResultList()) {
        UserPermissionDTO dto = new UserPermissionDTO();
        dto.setEmail(perm.getUser().getEmail());
        dto.setName(perm.getUser().getName());
        dto.setOrganization(perm.getUser().getOrganization());
        dto.setJobtitle(perm.getUser().getJobtitle());
        dto.setAllowDesign(perm.isAllowDesign());
        dto.setAllowView(perm.isAllowView());
        dto.setAllowViewAll(perm.isAllowViewAll());
        dto.setAllowEdit(perm.isAllowEdit());
        dto.setAllowEditAll(perm.isAllowEditAll());
        dto.setAllowManageUsers(perm.isAllowManageUsers());
        dto.setAllowManageAllUsers(perm.isAllowManageAllUsers());
        dto.setPartner(new PartnerDTO(perm.getPartner().getId(), perm.getPartner().getName()));
        dto.setFolderLimitation(!Strings.isNullOrEmpty(perm.getModel()));
        dto.setFolders(folderList(folderMap, perm));
        models.add(dto);
    }
    return new UserResult(models, cmd.getOffset(), queryTotalCount(cmd, currentUser, whereClause));
}
Also used : PartnerDTO(org.activityinfo.legacy.shared.model.PartnerDTO) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Folder(org.activityinfo.server.database.hibernate.entity.Folder) UserPermissionDTO(org.activityinfo.legacy.shared.model.UserPermissionDTO) ResourceId(org.activityinfo.model.resource.ResourceId) Database(org.activityinfo.server.database.hibernate.entity.Database) UserResult(org.activityinfo.legacy.shared.command.result.UserResult) UserPermission(org.activityinfo.server.database.hibernate.entity.UserPermission)

Example 3 with Database

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

the class AddProjectHandler method execute.

@Override
public CommandResult execute(AddProject cmd, User user) {
    Database db = em.find(Database.class, cmd.getDatabaseId());
    ProjectDTO from = cmd.getProjectDTO();
    Project project = new Project();
    project.setName(from.getName());
    project.setDescription(from.getDescription());
    project.setDatabase(db);
    db.setLastSchemaUpdate(new Date());
    em.persist(project);
    em.persist(db);
    db.getProjects().add(project);
    return new CreateResult(project.getId());
}
Also used : ProjectDTO(org.activityinfo.legacy.shared.model.ProjectDTO) Project(org.activityinfo.server.database.hibernate.entity.Project) AddProject(org.activityinfo.legacy.shared.command.AddProject) CreateResult(org.activityinfo.legacy.shared.command.result.CreateResult) Database(org.activityinfo.server.database.hibernate.entity.Database) Date(java.util.Date)

Example 4 with Database

use of org.activityinfo.server.database.hibernate.entity.Database 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 5 with Database

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

the class UserDatabasePolicy method update.

@Override
public void update(User user, Object entityId, PropertyMap changes) {
    Database database = em.find(Database.class, entityId);
    PermissionOracle.using(em).assertDesignPrivileges(database, user);
    applyProperties(database, changes);
}
Also used : Database(org.activityinfo.server.database.hibernate.entity.Database)

Aggregations

Database (org.activityinfo.server.database.hibernate.entity.Database)19 User (org.activityinfo.server.database.hibernate.entity.User)6 UserPermission (org.activityinfo.server.database.hibernate.entity.UserPermission)5 Test (org.junit.Test)5 Date (java.util.Date)4 Partner (org.activityinfo.server.database.hibernate.entity.Partner)3 ArrayList (java.util.ArrayList)2 CreateResult (org.activityinfo.legacy.shared.command.result.CreateResult)2 UserPermissionDTO (org.activityinfo.legacy.shared.model.UserPermissionDTO)2 HashMap (java.util.HashMap)1 AddProject (org.activityinfo.legacy.shared.command.AddProject)1 GetSchema (org.activityinfo.legacy.shared.command.GetSchema)1 GetSyncRegions (org.activityinfo.legacy.shared.command.GetSyncRegions)1 RemovePartner (org.activityinfo.legacy.shared.command.RemovePartner)1 UpdatePartner (org.activityinfo.legacy.shared.command.UpdatePartner)1 DuplicateCreateResult (org.activityinfo.legacy.shared.command.result.DuplicateCreateResult)1 RemoveFailedResult (org.activityinfo.legacy.shared.command.result.RemoveFailedResult)1 RemoveResult (org.activityinfo.legacy.shared.command.result.RemoveResult)1 SyncRegion (org.activityinfo.legacy.shared.command.result.SyncRegion)1 SyncRegions (org.activityinfo.legacy.shared.command.result.SyncRegions)1