/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package ee.tlu.htk.dippler.managers;
import ee.tlu.htk.dippler.backoffice.StatusCodes;
import ee.tlu.htk.dippler.entities.Organization;
import ee.tlu.htk.dippler.entities.User;
import java.util.Collection;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
/**
*
* @author metz
*/
@Stateless
public class OrgManager implements OrgManagerLocal {
@PersistenceContext
private EntityManager em;
@EJB
private UserManagerLocal userManager;;
public static final Integer GET_ORG_NAME = 1;
public static final Integer SET_ORG_NAME = 95;
enum CourseActions {
GET_NAME, SET_NAME, UNAPPROVED_USERS_COUNT
}
@Override
public String manageOrganization(String action, String data, User user, Long org_id) {
Organization org = findById(org_id);
if ( org != null ) {
switch (CourseActions.valueOf(action)) {
case GET_NAME:
if (userManager.hasPermission(user, GET_ORG_NAME)) {
return StatusCodes.respondWithData(StatusCodes.SUCCESS, "", org.getName());
} else {
return StatusCodes.respond(StatusCodes.OPERATION_NOT_ALLOWED, "No rights for read org name");
}
case SET_NAME:
if (userManager.hasPermission(user, SET_ORG_NAME)) {
return this.setName(data, org);
} else {
return StatusCodes.respond(StatusCodes.OPERATION_NOT_ALLOWED, "No rights for edit org name");
}
case UNAPPROVED_USERS_COUNT:
// Can not protect with userManager.hasPermission, only allowed to Clients with Organization
if ((org instanceof Organization) && (org.getId() != null)) {
return getUnapprovedUsersCount(org);
} else {
return StatusCodes.respond(StatusCodes.OPERATION_NOT_ALLOWED, "No rights for unapproved users count");
}
default:
return StatusCodes.respond(StatusCodes.OPERATION_NOT_ALLOWED, "");
}
}
return StatusCodes.respond(StatusCodes.OPERATION_NOT_ALLOWED, "");
}
public String setName(String data, Organization org) {
System.out.println("SET NAME");
org.setName(data);
return StatusCodes.respond(StatusCodes.SUCCESS, "Name set");
}
public String getUnapprovedUsersCount(Organization org) {
try {
Query users_count_query = em.createNativeQuery("SELECT COUNT(*) FROM User u LEFT JOIN user_organization uo ON u.id=uo.user WHERE !u.approved AND uo.organization=?1");
users_count_query.setParameter(1, org.getId().toString());
Long users_count = (Long) users_count_query.getSingleResult();
System.out.println(users_count);
StringBuilder xml = new StringBuilder();
xml.append("").append(users_count.toString()).append("");
return StatusCodes.respondWithData(StatusCodes.SUCCESS, "", xml.toString());
} catch (Throwable t) {
t.printStackTrace(System.err);
}
return StatusCodes.respond(StatusCodes.GENERAL_FAIL, "");
}
@Override
public Organization findById(Long id) {
if ( id > 0 ) {
return em.find(Organization.class, id);
}
return null;
}
/*
@Override
public Organization getOrganizationId(String name) {
Query q = em.createNamedQuery("Organization.findByName");
q.setParameter("name", name);
return (Organization) q.getSingleResult();
}*/
/*
@Override
public Boolean checkUser(User user, Long org) {
Collection organizations = user.getOrganizations();
if (organizations.contains(getOrganization(org))) {
return true;
}
return false;
}*/
}