use of org.activityinfo.shared.command.result.RemoveResult 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();
}
Aggregations