use of org.activityinfo.server.database.hibernate.entity.UserDatabase in project activityinfo by bedatadriven.
the class AddProjectHandler method execute.
@Override
public CommandResult execute(AddProject cmd, User user) throws CommandException {
UserDatabase db = em.find(UserDatabase.class, cmd.getDatabaseId());
ProjectDTO from = cmd.getProjectDTO();
Project project = new Project();
project.setName(from.getName());
project.setDescription(from.getDescription());
project.setUserDatabase(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.UserDatabase in project activityinfo by bedatadriven.
the class AddPartnerHandler method execute.
@Override
@SuppressWarnings("unchecked")
public CommandResult execute(AddPartner cmd, User user) throws CommandException {
UserDatabase db = em.find(UserDatabase.class, cmd.getDatabaseId());
if (db.getOwner().getId() != user.getId()) {
UserPermission perm = db.getPermissionByUser(user);
if (perm == null || !perm.isAllowManageAllUsers()) {
throw new IllegalAccessCommandException("The user does not have the manageAllUsers permission.");
}
}
// first check to see if an organization by this name is already
// a partner
Set<Partner> dbPartners = db.getPartners();
for (Partner partner : dbPartners) {
if (partner.getName().equals(cmd.getPartner().getName())) {
return new DuplicateCreateResult();
}
}
// now try to match this partner by name
List<Partner> allPartners = em.createQuery("select p from Partner p where p.name = ?1").setParameter(1, cmd.getPartner().getName()).getResultList();
if (allPartners.size() != 0) {
db.getPartners().add(allPartners.get(0));
return new CreateResult(allPartners.get(0).getId());
}
// nope, have to create a new record
Partner newPartner = new Partner();
newPartner.setName(cmd.getPartner().getName());
newPartner.setFullName(cmd.getPartner().getFullName());
em.persist(newPartner);
db.setLastSchemaUpdate(new Date());
em.persist(db);
db.getPartners().add(newPartner);
return new CreateResult(newPartner.getId());
}
use of org.activityinfo.server.database.hibernate.entity.UserDatabase in project activityinfo by bedatadriven.
the class AddTargetHandler method execute.
@Override
public CommandResult execute(AddTarget cmd, User user) throws CommandException {
TargetDTO form = cmd.getTargetDTO();
UserDatabase db = em.find(UserDatabase.class, cmd.getDatabaseId());
Partner partner = null;
if (form.get("partnerId") != null) {
partner = em.find(Partner.class, form.get("partnerId"));
}
Project project = null;
if (form.get("projectId") != null) {
project = em.find(Project.class, form.get("projectId"));
}
AdminEntity adminEntity = null;
// if(form.getAdminEntity() != null){
// adminEntity = em.find(AdminEntity.class,
// form.getAdminEntity().getId());
// }
Target target = new Target();
target.setName(form.getName());
target.setUserDatabase(db);
target.setAdminEntity(adminEntity);
target.setPartner(partner);
target.setProject(project);
target.setDate1(form.getDate1());
target.setDate2(form.getDate2());
db.setLastSchemaUpdate(new Date());
em.persist(target);
em.persist(db);
db.getTargets().add(target);
// }
if (project != null) {
project.getTargets().add(target);
}
if (partner != null) {
partner.getTargets().add(target);
}
return new CreateResult(target.getId());
}
use of org.activityinfo.server.database.hibernate.entity.UserDatabase in project activityinfo by bedatadriven.
the class SchemaUpdateBuilder method createAndSyncAttributeGroupInActivity.
private void createAndSyncAttributeGroupInActivity(JpaUpdateBuilder builder) throws JSONException {
builder.executeStatement("create table if not exists AttributeGroupInActivity (ActivityId integer, AttributeGroupId integer)");
builder.executeStatement("delete from AttributeGroupInActivity");
if (anyAttributes()) {
builder.beginPreparedStatement("insert into AttributeGroupInActivity (ActivityId, AttributeGroupId) values (?,?)");
for (UserDatabase db : databases) {
for (Activity activity : db.getActivities()) {
for (AttributeGroup group : activity.getAttributeGroups()) {
builder.addExecution(activity.getId(), group.getId());
}
}
}
builder.finishPreparedStatement();
}
}
use of org.activityinfo.server.database.hibernate.entity.UserDatabase in project activityinfo by bedatadriven.
the class DesignAuthorizationHandler method isAuthorized.
@Override
public boolean isAuthorized(AuthenticatedUser requestingUser, SchemaElement entity) {
Preconditions.checkNotNull(requestingUser, "requestingUser");
UserDatabase database = entity.findOwningDatabase();
if (database.getOwner().getId() == requestingUser.getId()) {
return true;
}
for (UserPermission permission : database.getUserPermissions()) {
if (permission.getUser().getId() == requestingUser.getId() && permission.isAllowDesign()) {
return true;
}
}
return false;
}
Aggregations