Search in sources :

Example 1 with UserEntityProperty

use of fi.otavanopisto.muikku.model.users.UserEntityProperty in project muikku by otavanopisto.

the class UserRESTService method getUserEntityProperty.

@GET
@Path("/property/{KEY}")
@RESTPermit(handling = Handling.INLINE, requireLoggedIn = true)
public Response getUserEntityProperty(@PathParam("KEY") String key) {
    UserEntity loggedUserEntity = sessionController.getLoggedUserEntity();
    UserEntityProperty property = userEntityController.getUserEntityPropertyByKey(loggedUserEntity, key);
    return Response.ok(new fi.otavanopisto.muikku.rest.model.UserEntityProperty(key, property == null ? null : property.getValue())).build();
}
Also used : UserEntityProperty(fi.otavanopisto.muikku.model.users.UserEntityProperty) UserEntity(fi.otavanopisto.muikku.model.users.UserEntity) WorkspaceUserEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceUserEntity) Path(javax.ws.rs.Path) RESTPermit(fi.otavanopisto.security.rest.RESTPermit) GET(javax.ws.rs.GET)

Example 2 with UserEntityProperty

use of fi.otavanopisto.muikku.model.users.UserEntityProperty in project muikku by otavanopisto.

the class UserRESTService method getUserEntityProperties.

@GET
@Path("/properties/{USERENTITYID}")
@RESTPermit(handling = Handling.INLINE, requireLoggedIn = true)
public Response getUserEntityProperties(@PathParam("USERENTITYID") Long userEntityId, @QueryParam("properties") String keys) {
    // TODO Security (maybe via visibility in userEntityProperty?)
    UserEntity userEntity = userEntityController.findUserEntityById(userEntityId);
    if (userEntity == null) {
        return Response.status(Status.NOT_FOUND).build();
    }
    List<UserEntityProperty> storedProperties = new ArrayList<UserEntityProperty>();
    List<fi.otavanopisto.muikku.rest.model.UserEntityProperty> restProperties = new ArrayList<fi.otavanopisto.muikku.rest.model.UserEntityProperty>();
    if (StringUtils.isBlank(keys)) {
        storedProperties = userEntityController.listUserEntityProperties(userEntity);
        for (UserEntityProperty property : storedProperties) {
            restProperties.add(new fi.otavanopisto.muikku.rest.model.UserEntityProperty(property.getKey(), property.getValue()));
        }
    } else {
        UserEntityProperty storedProperty;
        String[] keyArray = keys.split(",");
        for (int i = 0; i < keyArray.length; i++) {
            storedProperty = userEntityController.getUserEntityPropertyByKey(userEntity, keyArray[i]);
            String value = storedProperty == null ? null : storedProperty.getValue();
            restProperties.add(new fi.otavanopisto.muikku.rest.model.UserEntityProperty(keyArray[i], value));
        }
    }
    return Response.ok(restProperties).build();
}
Also used : ArrayList(java.util.ArrayList) UserEntityProperty(fi.otavanopisto.muikku.model.users.UserEntityProperty) UserEntity(fi.otavanopisto.muikku.model.users.UserEntity) WorkspaceUserEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceUserEntity) Path(javax.ws.rs.Path) RESTPermit(fi.otavanopisto.security.rest.RESTPermit) GET(javax.ws.rs.GET)

Example 3 with UserEntityProperty

use of fi.otavanopisto.muikku.model.users.UserEntityProperty in project muikku by otavanopisto.

the class UserEntityPropertyDAO method listByUserEntity.

public List<UserEntityProperty> listByUserEntity(UserEntity userEntity) {
    EntityManager entityManager = getEntityManager();
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<UserEntityProperty> criteria = criteriaBuilder.createQuery(UserEntityProperty.class);
    Root<UserEntityProperty> root = criteria.from(UserEntityProperty.class);
    criteria.select(root);
    criteria.where(criteriaBuilder.equal(root.get(UserEntityProperty_.userEntity), userEntity));
    return entityManager.createQuery(criteria).getResultList();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) UserEntityProperty(fi.otavanopisto.muikku.model.users.UserEntityProperty)

Example 4 with UserEntityProperty

use of fi.otavanopisto.muikku.model.users.UserEntityProperty in project muikku by otavanopisto.

the class UserEntityPropertyDAO method create.

public UserEntityProperty create(UserEntity userEntity, String key, String value) {
    UserEntityProperty userEntityProperty = new UserEntityProperty();
    userEntityProperty.setUserEntity(userEntity);
    userEntityProperty.setKey(key);
    userEntityProperty.setValue(value);
    return persist(userEntityProperty);
}
Also used : UserEntityProperty(fi.otavanopisto.muikku.model.users.UserEntityProperty)

Example 5 with UserEntityProperty

use of fi.otavanopisto.muikku.model.users.UserEntityProperty in project muikku by otavanopisto.

the class UserRESTService method searchStaffMembers.

@GET
@Path("/staffMembers")
@RESTPermit(handling = Handling.INLINE)
public Response searchStaffMembers(@QueryParam("searchString") String searchString, @QueryParam("properties") String properties, @QueryParam("workspaceEntityId") Long workspaceEntityId, @QueryParam("firstResult") @DefaultValue("0") Integer firstResult, @QueryParam("maxResults") @DefaultValue("10") Integer maxResults) {
    if (!sessionController.isLoggedIn()) {
        return Response.status(Status.FORBIDDEN).build();
    }
    List<fi.otavanopisto.muikku.rest.model.StaffMember> staffMembers = new ArrayList<>();
    Set<Long> userGroupFilters = null;
    Set<Long> workspaceFilters = workspaceEntityId == null ? null : Collections.singleton(workspaceEntityId);
    List<SchoolDataIdentifier> userIdentifiers = null;
    SearchProvider elasticSearchProvider = getProvider("elastic-search");
    if (elasticSearchProvider != null) {
        String[] fields;
        if (StringUtils.isNumeric(searchString)) {
            fields = new String[] { "firstName", "lastName", "userEntityId", "email" };
        } else {
            fields = new String[] { "firstName", "lastName", "email" };
        }
        List<EnvironmentRoleArchetype> nonStudentArchetypes = new ArrayList<>(Arrays.asList(EnvironmentRoleArchetype.values()));
        nonStudentArchetypes.remove(EnvironmentRoleArchetype.STUDENT);
        SearchResult result = elasticSearchProvider.searchUsers(searchString, fields, nonStudentArchetypes, userGroupFilters, workspaceFilters, userIdentifiers, false, false, false, firstResult, maxResults);
        List<Map<String, Object>> results = result.getResults();
        if (results != null && !results.isEmpty()) {
            WorkspaceEntity workspaceEntity = workspaceEntityId == null ? null : workspaceEntityController.findWorkspaceEntityById(workspaceEntityId);
            String[] propertyArray = StringUtils.isEmpty(properties) ? new String[0] : properties.split(",");
            for (Map<String, Object> o : results) {
                String studentId = (String) o.get("id");
                if (StringUtils.isBlank(studentId)) {
                    logger.severe("Could not process user found from search index because it had a null id");
                    continue;
                }
                String[] studentIdParts = studentId.split("/", 2);
                SchoolDataIdentifier studentIdentifier = studentIdParts.length == 2 ? new SchoolDataIdentifier(studentIdParts[0], studentIdParts[1]) : null;
                if (studentIdentifier == null) {
                    logger.severe(String.format("Could not process user found from search index with id %s", studentId));
                    continue;
                }
                if (studentIdentifier.getIdentifier().startsWith("STUDENT-")) {
                    // the elasticsearch query returns both. We need to filter them after the fact.
                    continue;
                }
                String email = userEmailEntityController.getUserDefaultEmailAddress(studentIdentifier, false);
                Long userEntityId = new Long((Integer) o.get("userEntityId"));
                UserEntity userEntity = userEntityController.findUserEntityById(userEntityId);
                Map<String, String> propertyMap = new HashMap<String, String>();
                if (userEntity != null) {
                    for (int i = 0; i < propertyArray.length; i++) {
                        UserEntityProperty userEntityProperty = userEntityController.getUserEntityPropertyByKey(userEntity, propertyArray[i]);
                        propertyMap.put(propertyArray[i], userEntityProperty == null ? null : userEntityProperty.getValue());
                    }
                }
                if (workspaceEntity != null) {
                    WorkspaceUserEntity workspaceUserEntity = workspaceUserEntityController.findActiveWorkspaceUserByWorkspaceEntityAndUserEntity(workspaceEntity, userEntity);
                    if (workspaceUserEntity == null || workspaceUserEntity.getWorkspaceUserRole().getArchetype() != WorkspaceRoleArchetype.TEACHER) {
                        continue;
                    }
                }
                staffMembers.add(new fi.otavanopisto.muikku.rest.model.StaffMember(studentIdentifier.toId(), new Long((Integer) o.get("userEntityId")), (String) o.get("firstName"), (String) o.get("lastName"), email, propertyMap));
            }
        }
    }
    return Response.ok(staffMembers).build();
}
Also used : SchoolDataIdentifier(fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier) UserSchoolDataIdentifier(fi.otavanopisto.muikku.model.users.UserSchoolDataIdentifier) EnvironmentRoleArchetype(fi.otavanopisto.muikku.model.users.EnvironmentRoleArchetype) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) UserEntityProperty(fi.otavanopisto.muikku.model.users.UserEntityProperty) WorkspaceUserEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceUserEntity) SearchProvider(fi.otavanopisto.muikku.search.SearchProvider) SearchResult(fi.otavanopisto.muikku.search.SearchResult) UserEntity(fi.otavanopisto.muikku.model.users.UserEntity) WorkspaceUserEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceUserEntity) WorkspaceEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceEntity) Map(java.util.Map) HashMap(java.util.HashMap) Path(javax.ws.rs.Path) RESTPermit(fi.otavanopisto.security.rest.RESTPermit) GET(javax.ws.rs.GET)

Aggregations

UserEntityProperty (fi.otavanopisto.muikku.model.users.UserEntityProperty)6 UserEntity (fi.otavanopisto.muikku.model.users.UserEntity)3 WorkspaceUserEntity (fi.otavanopisto.muikku.model.workspace.WorkspaceUserEntity)3 RESTPermit (fi.otavanopisto.security.rest.RESTPermit)3 GET (javax.ws.rs.GET)3 Path (javax.ws.rs.Path)3 ArrayList (java.util.ArrayList)2 EntityManager (javax.persistence.EntityManager)2 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)2 EnvironmentRoleArchetype (fi.otavanopisto.muikku.model.users.EnvironmentRoleArchetype)1 UserSchoolDataIdentifier (fi.otavanopisto.muikku.model.users.UserSchoolDataIdentifier)1 WorkspaceEntity (fi.otavanopisto.muikku.model.workspace.WorkspaceEntity)1 SchoolDataIdentifier (fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier)1 SearchProvider (fi.otavanopisto.muikku.search.SearchProvider)1 SearchResult (fi.otavanopisto.muikku.search.SearchResult)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1