/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package ee.tlu.htk.dippler.managers; import javax.ejb.Stateless; import javax.ejb.EJB; import ee.tlu.htk.dippler.backoffice.StatusCodes; import ee.tlu.htk.dippler.entities.Organization; import ee.tlu.htk.dippler.entities.Profile; import ee.tlu.htk.dippler.entities.User; import java.io.StringReader; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import javax.persistence.Query; import javax.xml.bind.JAXBContext; import javax.xml.bind.Unmarshaller; import ee.tlu.htk.dippler.utils.permissionChecker; import java.util.ArrayList; import java.util.Collection; import java.util.logging.Level; import java.util.logging.Logger; import javax.xml.bind.JAXBException; import org.apache.commons.collections.CollectionUtils; /** * * @author pjotr */ @Stateless public class ProfileManager implements ProfileManagerLocal { @PersistenceContext private EntityManager em; @EJB private UserManagerLocal userManager; public static final Integer LOAD_PROFILE = 1; public static final Integer SAVE_PROFILE = 10; private static Unmarshaller unmarshaller = null; enum ProfileActions { LOAD, SAVE_PROFILE } public ProfileManager() { try { final JAXBContext context = JAXBContext.newInstance(Profile.class); unmarshaller = context.createUnmarshaller(); } catch (JAXBException ex) { Logger.getLogger(ProfileManager.class.getName()).log(Level.SEVERE, null, ex); } } @Override public String manageProfile(String action, String data, User user, Organization org) { switch(ProfileActions.valueOf(action)) { case LOAD: if (userManager.hasPermission(user, LOAD_PROFILE)) { return this.loadProfile(data, user, org); } else { return StatusCodes.respond(StatusCodes.OPERATION_NOT_ALLOWED, "No rights for load profile"); } case SAVE_PROFILE: if (userManager.hasPermission(user, SAVE_PROFILE)) { return this.saveProfile(data, user); } else { return StatusCodes.respond(StatusCodes.OPERATION_NOT_ALLOWED, "No rights for save profile"); } default: return StatusCodes.respond(StatusCodes.OPERATION_NOT_ALLOWED, ""); } } public String loadProfile(String data, User user, Organization org) { Profile profile = findByData(data); if (profile != null) { User owner = em.find(User.class, profile.getUser_id()); // Loading allowed to owner or any of owner organizations if ( owner != null && user != null && user.getId() != null) { //Init empty collection of organizations Collection orgs = new ArrayList(); if ( org != null ) { //If organization is not null, add only that org = client organization orgs.add(org); } else { //Else get user organizations orgs = user.getOrganizations(); } boolean has_org = CollectionUtils.containsAny(owner.getOrganizations(), orgs); if ( permissionChecker.isOwner(user, owner) || has_org ) { return StatusCodes.respondWithData(StatusCodes.SUCCESS, "", marshalProfile(profile)); } } } else { User owner = findUserByData(data); if (owner != null) { // Profile creation and loading is allowed to owner of any of owner organizations if ((user != null && user.getId() != null && permissionChecker.isOwner(user, owner)) || (org != null && owner.getOrganizations().contains(org))) { Profile new_profile = new Profile(); new_profile.setUser_id(owner.getId()); new_profile.setDescription(""); new_profile.setHomepage(""); new_profile.setTwitter(""); new_profile.setDelicious(""); new_profile.setMendeley(""); new_profile.setSkype(""); new_profile.setMsn(""); new_profile.setBlogurl(""); em.persist(new_profile); return StatusCodes.respondWithData(StatusCodes.SUCCESS, "", marshalProfile(new_profile)); } } } return StatusCodes.respond(StatusCodes.PROFILE_DOES_NOT_EXIST, "Profile not found"); } public String saveProfile(String data, User user) { Profile profile = findByData(data); if ( profile != null ) { Profile tmp_profile = unMarshalProfile(data); if ( tmp_profile != null ) { // Check actor is owner User owner = userManager.findById(profile.getUser_id()); if (!permissionChecker.isOwnerOrAdmin(user, owner)) { return StatusCodes.respond(StatusCodes.OPERATION_NOT_ALLOWED, "Not owner or administrator"); } if (!permissionChecker.isOwner(user, owner)) { if (!CollectionUtils.containsAny(owner.getOrganizations(), user.getOrganizations())) { return StatusCodes.respond(StatusCodes.OPERATION_NOT_ALLOWED, "Not the same organization"); } } //Firstname and lastname are updated into user table //Via EntityManager managed entity, which we get from backoffice webservice class owner.setFirstname(tmp_profile.firstname); owner.setLastname(tmp_profile.lastname); //Update profile profile.setDescription(tmp_profile.getDescription()); profile.setHomepage(tmp_profile.getHomepage()); profile.setTwitter(tmp_profile.getTwitter()); profile.setDelicious(tmp_profile.getDelicious()); profile.setMendeley(tmp_profile.getMendeley()); profile.setSkype(tmp_profile.getSkype()); profile.setMsn(tmp_profile.getMsn()); profile.setBlogurl(tmp_profile.getBlogurl()); profile.setDepartment(tmp_profile.getDepartment()); return StatusCodes.respondWithData(StatusCodes.SUCCESS, "", marshalProfile(profile)); } } return StatusCodes.respond(StatusCodes.PROFILE_NOT_SAVED, "Profile could not be saved"); } public Profile findById(Long id) { if ( id > 0 ) { return em.find(Profile.class, id); } return null; } @Override public Profile findByUserId(Long user_id) { if ( user_id > 0 ) { Query find = em.createNamedQuery("Profile.findByUserId"); find.setParameter("id", user_id); try { Profile profile = (Profile) find.getSingleResult(); return profile; } catch(Exception e) { //Could not find profile } } return null; } public Profile findByData(String data) { Profile fakeProfile = unMarshalProfile(data); if (fakeProfile.getId() != null && fakeProfile.getId() > 0) { return findById(fakeProfile.getId()); } else if (fakeProfile.getUser_id() != null && fakeProfile.getUser_id() > 0) { return findByUserId(fakeProfile.getUser_id()); } return null; } public User findUserByData(String data) { Profile fakeProfile = unMarshalProfile(data); if ( fakeProfile != null ) { if (fakeProfile.getUser_id() != null && fakeProfile.getUser_id() > 0) { return userManager.findById(fakeProfile.getUser_id()); } } return null; } public static Profile unMarshalProfile(String data) { if ( unmarshaller != null ) { try { final Profile profileUNM = (Profile) unmarshaller.unmarshal(new StringReader(data)); return profileUNM; } catch(JAXBException e ) { //Something went wrong } } return null; } public String marshalProfile(Profile profile) { User user = userManager.findById(profile.getUser_id()); if ( user != null ) { StringBuilder xml = new StringBuilder(); xml.append(""); xml.append("").append(profile.getId()).append(""); xml.append("").append(user.getId()).append(""); xml.append(""); xml.append(""); xml.append(""); xml.append(""); xml.append(""); xml.append(""); xml.append(""); xml.append(""); xml.append(""); xml.append(""); xml.append(""); xml.append(""); xml.append("").append(user.getApproved()).append(""); xml.append(""); return xml.toString(); } return ""; } // TODO Seems unneeded, to be removed if that is true public String userXML(User user) { StringBuilder xml = new StringBuilder(); xml.append("").append(user.getId()).append(""); xml.append(""); xml.append(""); xml.append(""); xml.append(""); xml.append(""); return xml.toString(); } }