Add limits for users (Mapping, Entity, DAO, Model, DB).

Refactoring.
Add method for create button of Users to check if users not violating with limits.
This commit is contained in:
Vova Perebykivskiy 2015-12-17 16:02:48 +02:00 committed by Dgray16
parent 28492b477a
commit f48f2ead71
17 changed files with 217 additions and 5 deletions

View file

@ -0,0 +1,20 @@
package org.libreplan.business.common.daos;
import org.libreplan.business.common.entities.Limits;
import java.util.List;
/**
* DAO interface for the <code>Limits</code> entity.
* Contract for {@link LimitsDAO}
*
* Created by
* @author Vova Perebykivskiy <vova@libreplan-enterprise.com>
* on 17.12.2015.
*/
public interface ILimitsDAO extends IGenericDAO<Limits, Long> {
List<Limits> getAll();
Limits getUsersType();
Limits getWorkersType();
}

View file

@ -0,0 +1,40 @@
package org.libreplan.business.common.daos;
import org.libreplan.business.common.entities.Limits;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
* DAO for {@link Limits}
*
* Created by
* @author Vova Perebykivskiy <vova@libreplan-enterprise.com>
* on 24.09.15.
*/
@Repository
public class LimitsDAO extends GenericDAOHibernate<Limits, Long> implements ILimitsDAO {
@Override
public List<Limits> getAll() {
return list(Limits.class);
}
@Override
public Limits getUsersType() {
List<Limits> list = list(Limits.class);
for (Limits item : list)
if (item.getType().equals("users")) return item;
return null;
}
@Override
public Limits getWorkersType() {
List<Limits> list = list(Limits.class);
for (Limits item : list)
if (item.getType().equals("workers")) return item;
return null;
}
}

View file

@ -0,0 +1,34 @@
package org.libreplan.business.common.entities;
import org.libreplan.business.common.BaseEntity;
/**
* Limits entity, represents a limits for any functionality.
* This class is intended to work as a Hibernate component.
* It represents the limit that can be modified only in database.
*
* Created by
* @author Vova Perebykivskiy <vova@libreplan-enterprise.com>
* on 17.12.2015.
*/
public class Limits extends BaseEntity{
private String type;
private Long value;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public Long getValue() {
return value;
}
public void setValue(Long value) {
this.value = value;
}
}

View file

@ -30,7 +30,7 @@ import java.util.List;
*
* Created by
* @author Vova Perebykivskiy <vova@libreplan-enterprise.com>
* on 24.09.15.
* on 24.09.2015.
*/
@Repository
public class EmailTemplateDAO extends GenericDAOHibernate<EmailTemplate, Long> implements IEmailTemplateDAO{

View file

@ -30,7 +30,7 @@ import java.util.List;
*
* Created by
* @author Vova Perebykivskiy <vova@libreplan-enterprise.com>
* on 29.09.15.
* on 29.09.2015.
*/
public interface IEmailTemplateDAO extends IGenericDAO<EmailTemplate, Long>{

View file

@ -29,7 +29,7 @@ import org.libreplan.business.settings.entities.Language;
*
* Created by
* @author Vova Perebykivskiy <vova@libreplan-enterprise.com>
* on 29.09.15.
* on 29.09.2015.
*/
public class EmailTemplate extends BaseEntity {

View file

@ -35,6 +35,7 @@ import org.libreplan.business.users.entities.User;
*
* @author Fernando Bellas Permuy <fbellas@udc.es>
* @author Manuel Rego Casasnovas <rego@igalia.com>
* @author Vova Perebykivskiy <vova@libreplan-enterprise.com>
*/
public interface IUserDAO extends IGenericDAO<User, Long>{
@ -102,4 +103,5 @@ public interface IUserDAO extends IGenericDAO<User, Long>{
List<User> findAll();
Number getRowCount();
}

View file

@ -25,6 +25,7 @@ import java.util.ArrayList;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.libreplan.business.common.daos.GenericDAOHibernate;
import org.libreplan.business.common.exceptions.InstanceNotFoundException;
@ -179,4 +180,8 @@ public class UserDAO extends GenericDAOHibernate<User, Long>
return list(User.class);
}
@Override
public Number getRowCount() {
return (Number) getSession().createCriteria(User.class).setProjection(Projections.rowCount()).uniqueResult();
}
}

View file

@ -100,6 +100,9 @@
<value>
org/libreplan/business/logs/entities/Logs.hbm.xml
</value>
<value>
org/libreplan/business/common/entities/Limits.hbm.xml
</value>
</list>
</property>
</bean>

View file

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="org.libreplan.business.email.entities" default-access="field">
<class name="org.libreplan.business.common.entities.Limits" abstract="true" table="limits">
<id name="id" access="property" type="long">
<generator class="hilo" >
<param name="max_lo">100</param>
</generator>
</id>
<property name="type" column="type"/>
<property name="value" column="value" type="long"/>
</class>
</hibernate-mapping>

View file

@ -0,0 +1,19 @@
package org.libreplan.web.common;
import org.libreplan.business.common.entities.Limits;
import java.util.List;
/**
* Contract for {@link Limits}
*
* Created by
* @author Vova Perebykivskiy <vova@libreplan-enterprise.com>
* on 17.12.2015.
*/
public interface ILimitsModel {
List<Limits> getAll();
Limits getUsersType();
Limits getWorkersType();
}

View file

@ -0,0 +1,45 @@
package org.libreplan.web.common;
import org.libreplan.business.common.daos.ILimitsDAO;
import org.libreplan.business.common.entities.Limits;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
/**
* Model for operations related to {@link Limits}.
*
* Created by
* @author Vova Perebykivskiy <vova@libreplan-enterprise.com>
* on 17.12.15.
*/
@Service
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class LimitsModel implements ILimitsModel {
@Autowired
private ILimitsDAO limitsDAO;
@Override
@Transactional(readOnly = true)
public List<Limits> getAll() {
return limitsDAO.getAll();
}
@Override
@Transactional(readOnly = true)
public Limits getUsersType() {
return limitsDAO.getUsersType();
}
@Override
@Transactional(readOnly = true)
public Limits getWorkersType() {
return limitsDAO.getWorkersType();
}
}

View file

@ -32,7 +32,7 @@ import java.util.List;
*
* Created by
* @author Vova Perebykivskiy <vova@libreplan-enterprise.com>
* on 28.09.15.
* on 28.09.2015.
*/
public interface IEmailTemplateModel {

View file

@ -34,6 +34,7 @@ import org.libreplan.business.users.entities.UserRole;
*
* @author Jacobo Aragunde Perez <jaragunde@igalia.com>
* @author Manuel Rego Casasnovas <rego@igalia.com>
* @author Vova Perebykivskiy <vova@libreplan-enterprise.com>
*/
public interface IUserModel {
@ -134,4 +135,5 @@ public interface IUserModel {
List<Profile> getAllProfiles();
Number getRowCount();
}

View file

@ -32,6 +32,7 @@ import java.util.List;
import javax.annotation.Resource;
import org.apache.commons.logging.LogFactory;
import org.libreplan.business.common.entities.Limits;
import org.libreplan.business.common.exceptions.InstanceNotFoundException;
import org.libreplan.business.common.exceptions.ValidationException;
import org.libreplan.business.resources.entities.Worker;
@ -40,12 +41,14 @@ import org.libreplan.business.users.entities.User;
import org.libreplan.business.users.entities.User.UserAuthenticationType;
import org.libreplan.business.users.entities.UserRole;
import org.libreplan.web.common.BaseCRUDController;
import org.libreplan.web.common.ILimitsModel;
import org.libreplan.web.common.Util;
import org.libreplan.web.common.entrypoints.EntryPointsHandler;
import org.libreplan.web.common.entrypoints.IURLHandlerRegistry;
import org.libreplan.web.resources.worker.IWorkerCRUDControllerEntryPoints;
import org.libreplan.web.security.SecurityUtils;
import org.libreplan.web.users.bootstrap.PredefinedUsers;
import org.springframework.beans.factory.annotation.Autowired;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.WrongValueException;
import org.zkoss.zk.ui.event.Event;
@ -67,6 +70,7 @@ import org.zkoss.zul.api.Groupbox;
* @author Jacobo Aragunde Perez <jaragunde@igalia.com>
* @author Manuel Rego Casasnovas <rego@igalia.com>
* @author Javier Moran Rua <jmoran@igalia.com>
* @author Vova Perebykivskiy <vova@libreplan-enterprise.com>
*/
@SuppressWarnings("serial")
public class UserCRUDController extends BaseCRUDController<User> implements
@ -77,6 +81,10 @@ public class UserCRUDController extends BaseCRUDController<User> implements
@Resource
private IWorkerCRUDControllerEntryPoints workerCRUD;
@Autowired
private ILimitsModel limitsModel;
@Autowired
private IUserModel userModel;
private Textbox passwordBox;
@ -491,4 +499,11 @@ public class UserCRUDController extends BaseCRUDController<User> implements
}
}
public boolean isCreateButtonDisabled(){
Limits usersTypeLimit = limitsModel.getUsersType();
Long usersCount = (Long) userModel.getRowCount();
if (usersTypeLimit != null)
if ( usersCount >= usersTypeLimit.getValue() ) return true;
return false;
}
}

View file

@ -50,6 +50,7 @@ import org.springframework.transaction.annotation.Transactional;
* @author Jacobo Aragunde Perez <jaragunde@igalia.com>
* @author Susana Montes Pedreira <smontes@wirelessgalicia.com>
* @author Manuel Rego Casasnovas <rego@igalia.com>
* @author Vova Perebykivskiy <vova@libreplan-enterprise.com>
*/
@Service
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
@ -275,4 +276,11 @@ public class UserModel implements IUserModel {
return profileDAO.listSorted();
}
@Override
@Transactional(readOnly = true)
public Number getRowCount() {
return userDAO.getRowCount();
}
}

View file

@ -35,6 +35,7 @@
</columns>
</newdatasortablegrid>
<button id="show_create_form" onClick="controller.goToCreateForm();"
label="${i18n:_('Create')}" sclass="create-button global-action" >
label="${i18n:_('Create')}" sclass="create-button global-action"
disabled="@{controller.isCreateButtonDisabled}">
</button>
</window>