Index: lams_common/src/java/org/lamsfoundation/lams/dao/IBaseDAO.java =================================================================== diff -u -r48f344a789c9af4dcca102b6b5914333e9cbd17a -rf4051aee3b6cf552e0e4e3d4afa5ab4961c344af --- lams_common/src/java/org/lamsfoundation/lams/dao/IBaseDAO.java (.../IBaseDAO.java) (revision 48f344a789c9af4dcca102b6b5914333e9cbd17a) +++ lams_common/src/java/org/lamsfoundation/lams/dao/IBaseDAO.java (.../IBaseDAO.java) (revision f4051aee3b6cf552e0e4e3d4afa5ab4961c344af) @@ -1,302 +1,304 @@ -/**************************************************************** - * Copyright (C) 2005 LAMS Foundation (http://lamsfoundation.org) - * ============================================================= - * License Information: http://lamsfoundation.org/licensing/lams/2.0/ - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License version 2.0 - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 - * USA - * - * http://www.gnu.org/licenses/gpl.txt - * **************************************************************** - */ - -package org.lamsfoundation.lams.dao; - -import java.io.Serializable; -import java.util.Collection; -import java.util.List; -import java.util.Map; - -import org.hibernate.HibernateException; - -/** - * @version - * - *

- * View Source - *

- * - * @author Fei Yang - * - * Created at 22:50:25 on 16/06/2006 - */ -public interface IBaseDAO { - - /** - * Insert an object into the database. Should only be used if the object has not - * been persisted previously. - * - * @param object - * The object to be inserted - */ - public void insert(Object object); - - /** - * Update a previously inserted object into the database. - * - * @param object - * The object to be updated - */ - public void update(Object object); - - /** - * Insert or update an object into the database. It is up to the persistence - * engine to decide whether to insert or update. - * - * @param object - * The object to be inserted/updated - */ - public void insertOrUpdate(Object object); - - /** - * @param objects - * @return void - */ - public void insertOrUpdateAll(Collection objects); - - public void update(String queryString); - - public void update(String queryString, Object value); - - public void update(String queryString, Object[] values); - - /** - * @param clazz - * @param propertyToChange - * @param newValue - * @param conditionProperty - * @param conditionValue - * @return void - */ - public void update(Class clazz, String propertyToChange, Object newValue, String conditionProperty, - Object conditionValue); - - /** - * @param clazz - * @param propertyToChange - * @param newValue - * @param conditions - * in a map - * @return void - */ - public void update(Class clazz, String propertyToChange, Object newValue, Map conditions); - - /** - * @param clazz - * @param newValues - * in a map - * @param conditionProperty - * @param conditionValue - * @return void - */ - public void update(Class clazz, Map newValues, String conditionProperty, Object conditionValue); - - /** - * @param clazz - * @param newValues - * in a map - * @param conditions - * in a map - * @return void - */ - public void update(Class clazz, Map newValues, Map conditions); - - /** - * These 2 objects have to be instances of the Class - * - * @param clazz - * @param newValues - * in a object - * @param conditions - * in a object - * @return void - */ - public void updateAnythingLike(Class clazz, Object newValues, Object conditions); - - /** - * Force this session to flush. Must be called at the end of a unit of work, before commiting the transaction and - * closing the session (depending on flush-mode, Transaction.commit() calls this method). - * - * @throws HibernateException - * - Indicates problems flushing the session or talking to the database. - */ - void flush(); - - /** - * Remove an object from the database. - * - * @param object - * The object to be deleted - */ - public void delete(Object object); - - /** - * Be careful to use this method. It will clean up the whole table for the Class - * - * @param clazz - * @return void - */ - public void deleteAll(Class clazz); - - /** - * @param objects - * to delete - * @return void - */ - public void deleteAll(Collection objects); - - /** - * @param clazz - * java Class - * @param id - * identifier - * @return void - */ - public void deleteById(Class clazz, Serializable id); - - /** - * @param clazz - * @param name - * @param value - * @return void - */ - public void deleteByProperty(Class clazz, String name, Object value); - - /** - * @param properties - * a map of property names and values - * @return void - */ - public void deleteByProperties(Class clazz, Map properties); - - /** - * Delete any object which has the same non-null property values as the object - * - * @param object - * @return void - */ - public void deleteAnythingLike(Object object); - - /** - * Find an object. If the object is not found - * then it will return null - * - * @param clazz - * @param id - */ - public T find(Class clazz, Serializable id); - - /** - * @param clazz - * @return all of rows in the table for the Class as objects - */ - public List findAll(Class clazz); - - /** - * @param clazz - * @param name - * @param value - * @return a list of objects which have the same propery value - */ - public List findByProperty(Class clazz, String name, Object value); - - /** - * @param properties - * a map of property names and values - * @return a list of objects which have the same property values - */ - public List findByProperties(Class clazz, Map properties); - - public List find(String queryString); - - public List find(String queryString, Object value); - - public List find(String queryString, Object[] values); - - public List findByNamedQuery(String queryName); - - public List findByNamedQuery(String queryName, Object value); - - public List findByNamedQuery(String queryName, Object[] values); - - /** - * @param clazz - * @param name - * of the property - * @param pattern - * to match - * @return a list of objects - */ - public List searchByStringProperty(Class clazz, String name, String pattern); - - /** - * @param clazz - * @param name - * of the property - * @param pattern - * to match - * @return a list of objects - */ - public List searchByStringProperties(Class clazz, Map properties); - - /** - * @param clazz - * @param name - * of the property - * @param min - * @param minIncluded - * @param max - * @param maxIncluded - * @return a list of objects - */ - public List searchByNumberSpan(Class clazz, String name, Integer min, Boolean minIncluded, Integer max, - Boolean maxIncluded); - - /** - * Force initialization of a Hibernate proxy or persistent collection - * - * @param proxy - * of persistent object or a collection - */ - public void initialize(Object proxy); - - /** - * Count all rows in a table for a hibernate-mapped class - * - * @param clazz - * @return - */ - public long countAll(Class clazz); - - /** - * Create a query based on the properties, and count the result - * - * @param properties - * a map of property names and values - * @return a list of objects which have the same property values - */ - public long countByProperties(Class clazz, Map properties); - - public void releaseFromCache(Object o); +/**************************************************************** + * Copyright (C) 2005 LAMS Foundation (http://lamsfoundation.org) + * ============================================================= + * License Information: http://lamsfoundation.org/licensing/lams/2.0/ + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2.0 + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * http://www.gnu.org/licenses/gpl.txt + * **************************************************************** + */ + +package org.lamsfoundation.lams.dao; + +import java.io.Serializable; +import java.util.Collection; +import java.util.List; +import java.util.Map; + +import org.hibernate.HibernateException; + +/** + * @version + * + *

+ * View Source + *

+ * + * @author Fei Yang + * + * Created at 22:50:25 on 16/06/2006 + */ +public interface IBaseDAO { + + /** + * Insert an object into the database. Should only be used if the object has not + * been persisted previously. + * + * @param object + * The object to be inserted + */ + public void insert(Object object); + + /** + * Update a previously inserted object into the database. + * + * @param object + * The object to be updated + */ + public void update(Object object); + + /** + * Insert or update an object into the database. It is up to the persistence + * engine to decide whether to insert or update. + * + * @param object + * The object to be inserted/updated + */ + public void insertOrUpdate(Object object); + + /** + * @param objects + * @return void + */ + public void insertOrUpdateAll(Collection objects); + + public void update(String queryString); + + public void update(String queryString, Object value); + + public void update(String queryString, Object[] values); + + /** + * @param clazz + * @param propertyToChange + * @param newValue + * @param conditionProperty + * @param conditionValue + * @return void + */ + public void update(Class clazz, String propertyToChange, Object newValue, String conditionProperty, + Object conditionValue); + + /** + * @param clazz + * @param propertyToChange + * @param newValue + * @param conditions + * in a map + * @return void + */ + public void update(Class clazz, String propertyToChange, Object newValue, Map conditions); + + /** + * @param clazz + * @param newValues + * in a map + * @param conditionProperty + * @param conditionValue + * @return void + */ + public void update(Class clazz, Map newValues, String conditionProperty, Object conditionValue); + + /** + * @param clazz + * @param newValues + * in a map + * @param conditions + * in a map + * @return void + */ + public void update(Class clazz, Map newValues, Map conditions); + + /** + * These 2 objects have to be instances of the Class + * + * @param clazz + * @param newValues + * in a object + * @param conditions + * in a object + * @return void + */ + public void updateAnythingLike(Class clazz, Object newValues, Object conditions); + + /** + * Force this session to flush. Must be called at the end of a unit of work, before commiting the transaction and + * closing the session (depending on flush-mode, Transaction.commit() calls this method). + * + * @throws HibernateException + * - Indicates problems flushing the session or talking to the database. + */ + void flush(); + + /** + * Remove an object from the database. + * + * @param object + * The object to be deleted + */ + public void delete(Object object); + + /** + * Be careful to use this method. It will clean up the whole table for the Class + * + * @param clazz + * @return void + */ + public void deleteAll(Class clazz); + + /** + * @param objects + * to delete + * @return void + */ + public void deleteAll(Collection objects); + + /** + * @param clazz + * java Class + * @param id + * identifier + * @return void + */ + public void deleteById(Class clazz, Serializable id); + + /** + * @param clazz + * @param name + * @param value + * @return void + */ + public void deleteByProperty(Class clazz, String name, Object value); + + /** + * @param properties + * a map of property names and values + * @return void + */ + public void deleteByProperties(Class clazz, Map properties); + + /** + * Delete any object which has the same non-null property values as the object + * + * @param object + * @return void + */ + public void deleteAnythingLike(Object object); + + /** + * Find an object. If the object is not found + * then it will return null + * + * @param clazz + * @param id + */ + public T find(Class clazz, Serializable id); + + /** + * @param clazz + * @return all of rows in the table for the Class as objects + */ + public List findAll(Class clazz); + + /** + * @param clazz + * @param name + * @param value + * @return a list of objects which have the same propery value + */ + public List findByProperty(Class clazz, String name, Object value); + + public List findByPropertyValues(Class clazz, String name, Collection values); + + /** + * @param properties + * a map of property names and values + * @return a list of objects which have the same property values + */ + public List findByProperties(Class clazz, Map properties); + + public List find(String queryString); + + public List find(String queryString, Object value); + + public List find(String queryString, Object[] values); + + public List findByNamedQuery(String queryName); + + public List findByNamedQuery(String queryName, Object value); + + public List findByNamedQuery(String queryName, Object[] values); + + /** + * @param clazz + * @param name + * of the property + * @param pattern + * to match + * @return a list of objects + */ + public List searchByStringProperty(Class clazz, String name, String pattern); + + /** + * @param clazz + * @param name + * of the property + * @param pattern + * to match + * @return a list of objects + */ + public List searchByStringProperties(Class clazz, Map properties); + + /** + * @param clazz + * @param name + * of the property + * @param min + * @param minIncluded + * @param max + * @param maxIncluded + * @return a list of objects + */ + public List searchByNumberSpan(Class clazz, String name, Integer min, Boolean minIncluded, Integer max, + Boolean maxIncluded); + + /** + * Force initialization of a Hibernate proxy or persistent collection + * + * @param proxy + * of persistent object or a collection + */ + public void initialize(Object proxy); + + /** + * Count all rows in a table for a hibernate-mapped class + * + * @param clazz + * @return + */ + public long countAll(Class clazz); + + /** + * Create a query based on the properties, and count the result + * + * @param properties + * a map of property names and values + * @return a list of objects which have the same property values + */ + public long countByProperties(Class clazz, Map properties); + + public void releaseFromCache(Object o); } \ No newline at end of file Index: lams_common/src/java/org/lamsfoundation/lams/dao/hibernate/LAMSBaseDAO.java =================================================================== diff -u -r67db3e1b2ada0d59ff98c807a54282783797c5e6 -rf4051aee3b6cf552e0e4e3d4afa5ab4961c344af --- lams_common/src/java/org/lamsfoundation/lams/dao/hibernate/LAMSBaseDAO.java (.../LAMSBaseDAO.java) (revision 67db3e1b2ada0d59ff98c807a54282783797c5e6) +++ lams_common/src/java/org/lamsfoundation/lams/dao/hibernate/LAMSBaseDAO.java (.../LAMSBaseDAO.java) (revision f4051aee3b6cf552e0e4e3d4afa5ab4961c344af) @@ -266,6 +266,15 @@ return doFind(queryString, value); } + @Override + public List findByPropertyValues(Class clazz, String name, Collection values) { + if (values == null || values.isEmpty()) { + return new ArrayList<>(); + } + String queryString = "FROM " + clazz.getCanonicalName() + " WHERE " + name + " IN (:param)"; + return getSession().createQuery(queryString, clazz).setParameterList("param", values).getResultList(); + } + /* * (non-Javadoc) * Index: lams_common/src/java/org/lamsfoundation/lams/usermanagement/service/IUserManagementService.java =================================================================== diff -u -r47f01daecb191d1ca81eaf131b1d063b2bc640b5 -rf4051aee3b6cf552e0e4e3d4afa5ab4961c344af --- lams_common/src/java/org/lamsfoundation/lams/usermanagement/service/IUserManagementService.java (.../IUserManagementService.java) (revision 47f01daecb191d1ca81eaf131b1d063b2bc640b5) +++ lams_common/src/java/org/lamsfoundation/lams/usermanagement/service/IUserManagementService.java (.../IUserManagementService.java) (revision f4051aee3b6cf552e0e4e3d4afa5ab4961c344af) @@ -111,6 +111,8 @@ */ List findByProperty(Class clazz, String name, Object value); + List findByPropertyValues(Class clazz, String name, Collection values); + /** * @param properties * a map of property names and values @@ -173,7 +175,7 @@ * @return true or false */ boolean isUserInRole(Integer userId, Integer orgId, String roleName); - + /** * @param organisationId * @return organisation by its id @@ -224,10 +226,10 @@ * @return a list of UserOrganisationRoles */ List getUserOrganisationRoles(Integer orgId, String login); - + /** * Returns list of UserOrganisationCollapsed, indicating which sub-courses was collapsed by the given user. - * + * * @param parentOrganisationId * @param userId * @return @@ -281,7 +283,7 @@ * @return UserOrganisation */ UserOrganisation getUserOrganisation(Integer userId, Integer orgId); - + /** * @param userId * @param orgId @@ -341,6 +343,18 @@ */ void setRolesForUserOrganisation(User user, Integer organisationId, List rolesList); + /** + * Set the roles for the specified user and organisation using the roleIds in rolesList. If userOrganisation + * exists, + * will also remove roles that are not in rolesList. + * + * @param checkGroupManagerRoles + * whether check if user is a group manager, he should also have other roles in organisation; can be + * false to avoid extra check if we know that user can not be a group manager + */ + void setRolesForUserOrganisation(User user, Organisation org, List rolesList, + boolean checkGroupManagerRoles); + void setRolesForUserOrganisation(Integer userId, Integer organisationId, Set roleIDList); /** @@ -412,6 +426,7 @@ void logPasswordChanged(User user, User modifiedBy); void logUserCreated(User user, User createdBy); + void logUserCreated(User user, UserDTO createdBy); Integer getCountUsers(); Index: lams_common/src/java/org/lamsfoundation/lams/usermanagement/service/UserManagementService.java =================================================================== diff -u -r0e7d403e91b0916fd3842d8d3098b1c466d28ece -rf4051aee3b6cf552e0e4e3d4afa5ab4961c344af --- lams_common/src/java/org/lamsfoundation/lams/usermanagement/service/UserManagementService.java (.../UserManagementService.java) (revision 0e7d403e91b0916fd3842d8d3098b1c466d28ece) +++ lams_common/src/java/org/lamsfoundation/lams/usermanagement/service/UserManagementService.java (.../UserManagementService.java) (revision f4051aee3b6cf552e0e4e3d4afa5ab4961c344af) @@ -224,6 +224,11 @@ } @Override + public List findByPropertyValues(Class clazz, String name, Collection values) { + return baseDAO.findByPropertyValues(clazz, name, values); + } + + @Override public List findByProperties(Class clazz, Map properties) { return baseDAO.findByProperties(clazz, properties); } @@ -343,8 +348,8 @@ return baseDAO.findByProperties(UserOrganisationRole.class, properties); } - @Override @SuppressWarnings("unchecked") + @Override public Map> getRolesForUser(Integer userId) { return ((List) findByProperty(UserOrganisation.class, "user.userId", userId)).stream() .collect(Collectors.toMap(userOrganisation -> userOrganisation.getOrganisation().getOrganisationId(), @@ -656,10 +661,12 @@ // access the org.UserOrganisations set // if org has come from the web layer. Organisation org = (Organisation) findById(Organisation.class, organisationId); - setRolesForUserOrganisation(user, org, rolesList); + setRolesForUserOrganisation(user, org, rolesList, true); } - private void setRolesForUserOrganisation(User user, Organisation org, List rolesList) { + @Override + public void setRolesForUserOrganisation(User user, Organisation org, List rolesList, + boolean checkGroupManagerRoles) { // The private version of setRolesForUserOrganisation can pass around // the org safely as we are within @@ -681,7 +688,7 @@ // course also if not already if (org.getOrganisationType().getOrganisationTypeId().equals(OrganisationType.CLASS_TYPE) && (getUserOrganisation(user.getUserId(), org.getParentOrganisation().getOrganisationId()) == null)) { - setRolesForUserOrganisation(user, org.getParentOrganisation(), rolesList); + setRolesForUserOrganisation(user, org.getParentOrganisation(), rolesList, checkGroupManagerRoles); } List rolesCopy = new ArrayList<>(); @@ -729,8 +736,11 @@ uo.setUserOrganisationRoles(uors); save(uo); } - // make sure group managers have monitor and learner in each subgroup - checkGroupManager(user, org); + + if (checkGroupManagerRoles) { + // make sure group managers have monitor and learner in each subgroup + checkGroupManager(user, org); + } } private void checkGroupManager(User user, Organisation org) {