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);
}
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));
}
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());
}
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);
}
}
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);
}
Aggregations