ItEr16S10ClasificacionTraballoItEr15S13: Retrieve CriterionTypes from DB instead of from PredefinedCriterionTypes

This commit is contained in:
Diego Pino Garcia 2009-07-07 13:30:49 +02:00 committed by Javier Moran Rua
parent 89f294616b
commit 40ca4e6843
13 changed files with 97 additions and 73 deletions

View file

@ -5,7 +5,6 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.navalplanner.business.common.exceptions.ValidationException;
@ -43,14 +42,6 @@ public class CriterionsBootstrap implements ICriterionsBootstrap {
public CriterionsBootstrap() {
}
public List<ICriterionType<?>> getTypes() {
ArrayList<ICriterionType<?>> result = new ArrayList<ICriterionType<?>>();
for (ICriterionTypeProvider provider : providers) {
result.addAll(provider.getRequiredCriterions().keySet());
}
return result;
}
@Override
@Transactional
public void loadRequiredData() {

View file

@ -11,8 +11,6 @@ import org.navalplanner.business.resources.entities.ICriterionType;
*/
public interface ICriterionsBootstrap extends IDataBootstrap {
public abstract List<ICriterionType<?>> getTypes();
public abstract void loadRequiredData();
}

View file

@ -401,8 +401,8 @@ public abstract class Resource {
return criterionSatisfactions.contains(satisfaction);
}
public void checkNotOverlaps(List<ICriterionType<?>> types) {
for (ICriterionType<?> criterionType : types) {
public void checkNotOverlaps(List<CriterionType> types) {
for (CriterionType criterionType : types) {
if (!criterionType.allowSimultaneousCriterionsPerResource()) {
List<CriterionSatisfaction> satisfactions = query().from(
criterionType).sortByStartDate().result();

View file

@ -20,6 +20,8 @@ public interface CriterionTypeService {
CriterionType findUniqueByName(String name);
List<CriterionType> getAll();
void remove(CriterionType criterionType) throws InstanceNotFoundException;
void save(CriterionType entity) throws ValidationException;

View file

@ -1,6 +1,7 @@
package org.navalplanner.business.resources.services.impl;
import java.util.List;
import org.hibernate.validator.InvalidValue;
import org.navalplanner.business.common.exceptions.InstanceNotFoundException;
import org.navalplanner.business.common.exceptions.ValidationException;
@ -49,6 +50,11 @@ public class CriterionTypeServiceImpl implements CriterionTypeService {
}
}
@Override
public List<CriterionType> getAll() {
return criterionTypeDAO.list(CriterionType.class);
}
@Override
public void remove(CriterionType criterionType) throws InstanceNotFoundException {
if (criterionType.getId() != null ) {

View file

@ -7,10 +7,11 @@ import java.util.Set;
import org.navalplanner.business.common.exceptions.InstanceNotFoundException;
import org.navalplanner.business.resources.bootstrap.ICriterionsBootstrap;
import org.navalplanner.business.resources.daos.IResourceDao;
import org.navalplanner.business.resources.entities.CriterionType;
import org.navalplanner.business.resources.entities.ICriterion;
import org.navalplanner.business.resources.entities.ICriterionType;
import org.navalplanner.business.resources.entities.Resource;
import org.navalplanner.business.resources.entities.Worker;
import org.navalplanner.business.resources.services.CriterionTypeService;
import org.navalplanner.business.resources.services.ResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
@ -34,6 +35,10 @@ public class ResourceServiceImpl implements ResourceService {
@Autowired
private ICriterionsBootstrap criterionsBootstrap;
@Autowired
private CriterionTypeService criterionTypeService;
@Transactional
public void saveResource(Resource resource) {
checkResourceIsOk(resource);
@ -46,7 +51,7 @@ public class ResourceServiceImpl implements ResourceService {
}
private void checkResourceIsOk(Resource resource) {
List<ICriterionType<?>> types = criterionsBootstrap.getTypes();
List<CriterionType> types = criterionTypeService.getAll();
resource.checkNotOverlaps(types);
}

View file

@ -1,5 +1,7 @@
package org.navalplanner.business.test.resources.services;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertEquals;
import java.util.UUID;
import org.hibernate.exception.ConstraintViolationException;
@ -13,7 +15,6 @@ import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;
import static org.junit.Assert.assertTrue;
import static org.navalplanner.business.BusinessGlobalNames.BUSINESS_SPRING_CONFIG_FILE;
import static org.navalplanner.business.test.BusinessGlobalNames.BUSINESS_SPRING_CONFIG_TEST_FILE;
@ -30,33 +31,45 @@ public class CriterionTypeServiceTest {
@Autowired
private CriterionTypeService criterionTypeService;
public CriterionType createValidCriterionType() {
String unique = UUID.randomUUID().toString();
return createValidCriterionType(unique);
}
public CriterionType createValidCriterionType(String name) {
return new CriterionType(name);
}
@Test
public void testSaveCriterionType() throws ValidationException {
String unique = UUID.randomUUID().toString();
CriterionType criterionType = createValidCriterionType(unique);
CriterionType criterionType = createValidCriterionType();
criterionTypeService.save(criterionType);
assertTrue(criterionTypeService.exists(criterionType));
}
@Test
public void testSaveCriterionTypeTwice() throws ValidationException {
String unique = UUID.randomUUID().toString();
CriterionType criterionType = createValidCriterionType(unique);
CriterionType criterionType = createValidCriterionType();
criterionTypeService.save(criterionType);
criterionTypeService.save(criterionType);
assertTrue(criterionTypeService.exists(criterionType));
}
@Test(expected=ConstraintViolationException.class)
public void testCannotSaveTwoCriterionTypesWithTheSameName() throws ValidationException {
@Test(expected=ConstraintViolationException.class)
public void testCannotSaveTwoDifferentCriterionTypesWithTheSameName() throws ValidationException {
String unique = UUID.randomUUID().toString();
CriterionType criterionType = createValidCriterionType(unique);
criterionTypeService.save(criterionType);
criterionType = createValidCriterionType(unique);
criterionTypeService.save(criterionType);
}
@Test
public void testGetAll() throws ValidationException {
int previous = criterionTypeService.getAll().size();
CriterionType criterionType = createValidCriterionType();
criterionTypeService.save(criterionType);
int now = criterionTypeService.getAll().size();
assertEquals(now, previous + 1);
}
}

View file

@ -4,7 +4,7 @@ import java.util.List;
import org.navalplanner.business.orders.entities.OrderElement;
import org.navalplanner.business.resources.entities.Criterion;
import org.navalplanner.business.resources.entities.ICriterionType;
import org.navalplanner.business.resources.entities.CriterionType;
public interface IOrderElementModel {
@ -12,10 +12,10 @@ public interface IOrderElementModel {
public void setCurrent(OrderElement orderElement);
public List<ICriterionType<?>> getCriterionTypes();
public List<CriterionType> getCriterionTypes();
public ICriterionType<?> getCriterionTypeByName(String name);
public CriterionType getCriterionTypeByName(String name);
public List<Criterion> getCriterionsFor(ICriterionType<?> type);
public List<Criterion> getCriterionsFor(CriterionType type);
}

View file

@ -15,7 +15,6 @@ import org.navalplanner.business.orders.entities.OrderLine;
import org.navalplanner.business.orders.entities.OrderLineGroup;
import org.navalplanner.business.resources.entities.Criterion;
import org.navalplanner.business.resources.entities.CriterionType;
import org.navalplanner.business.resources.entities.ICriterionType;
import org.navalplanner.web.common.Util;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.WrongValueException;
@ -64,9 +63,9 @@ public class OrderElementController extends GenericForwardComposer {
private Listbox hoursGroupsListbox;
/**
* List of selected {@link ICriterionType} just used in the controller
* List of selected {@link CriterionType} just used in the controller
*/
private Set<ICriterionType<?>> selectedCriterionTypes = new LinkedHashSet<ICriterionType<?>>();
private Set<CriterionType> selectedCriterionTypes = new LinkedHashSet<CriterionType>();
public OrderElement getOrderElement() {
if (model == null) {
@ -102,9 +101,9 @@ public class OrderElementController extends GenericForwardComposer {
return model.getOrderElement().getHoursGroups();
} else {
// If it's an OrderLineGroup
Set<ICriterionType<?>> criterionTypes = getSelectedCriterionTypes();
Set<CriterionType> criterionTypes = getSelectedCriterionTypes();
// If there isn't any ICriterionType selected
// If there isn't any CriterionType selected
if (criterionTypes.isEmpty()) {
return model.getOrderElement().getHoursGroups();
}
@ -118,7 +117,7 @@ public class OrderElementController extends GenericForwardComposer {
for (HoursGroup hoursGroup : hoursGroups) {
String key = "";
for (ICriterionType<?> criterionType : criterionTypes) {
for (CriterionType criterionType : criterionTypes) {
Criterion criterion = hoursGroup
.getCriterionByType(criterionType);
if (criterion != null) {
@ -291,40 +290,40 @@ public class OrderElementController extends GenericForwardComposer {
}
/**
* Gets the list of possible {@link ICriterionType}.
* Gets the list of possible {@link CriterionType}.
*
* @return A {@link List} of {@link ICriterionType}
* @return A {@link List} of {@link CriterionType}
*/
public List<ICriterionType<?>> getCriterionTypes() {
public List<CriterionType> getCriterionTypes() {
if (model == null) {
return new ArrayList<ICriterionType<?>>();
return new ArrayList<CriterionType>();
}
List<ICriterionType<?>> list = model.getCriterionTypes();
List<CriterionType> list = model.getCriterionTypes();
list.removeAll(getSelectedCriterionTypes());
return list;
}
/**
* Returns the selected {@link ICriterionType}.
* Returns the selected {@link CriterionType}.
*
* @return A {@link List} of {@link ICriterionType}
* @return A {@link List} of {@link CriterionType}
*/
public Set<ICriterionType<?>> getSelectedCriterionTypes() {
public Set<CriterionType> getSelectedCriterionTypes() {
return selectedCriterionTypes;
}
/**
* Reloads the selected {@link ICriterionType}, depending on the
* Reloads the selected {@link CriterionType}, depending on the
* {@link Criterion} related with the {@link HoursGroup}
*/
private void reloadSelectedCriterionTypes() {
OrderElement orderElement = getOrderElement();
if (orderElement == null) {
selectedCriterionTypes = new LinkedHashSet<ICriterionType<?>>();
selectedCriterionTypes = new LinkedHashSet<CriterionType>();
} else {
Set<ICriterionType<?>> criterionTypes = new LinkedHashSet<ICriterionType<?>>();
Set<CriterionType> criterionTypes = new LinkedHashSet<CriterionType>();
for (HoursGroup hoursGroup : orderElement.getHoursGroups()) {
Set<Criterion> criterions = hoursGroup.getCriterions();
@ -339,32 +338,32 @@ public class OrderElementController extends GenericForwardComposer {
}
/**
* Adds the selected {@link ICriterionType} to the selectedCriterionTypes
* Adds the selected {@link CriterionType} to the selectedCriterionTypes
* attribute.
*
* @param selectedItems
* {@link Set} of {@link Listitem} with the selected
* {@link ICriterionType}
* {@link CriterionType}
*/
public void assignCriterions(Set<Listitem> selectedItems) {
for (Listitem listitem : selectedItems) {
ICriterionType<?> value = (ICriterionType<?>) listitem.getValue();
CriterionType value = (CriterionType) listitem.getValue();
selectedCriterionTypes.add(value);
}
Util.reloadBindings(popup);
}
/**
* Removes the selected {@link ICriterionType} from the
* Removes the selected {@link CriterionType} from the
* selectedCriterionTypes attribute.
*
* @param selectedItems
* {@link Set} of {@link Listitem} with the selected
* {@link ICriterionType}
* {@link CriterionType}
*/
public void unassignCriterions(Set<Listitem> selectedItems) {
for (Listitem listitem : selectedItems) {
ICriterionType<?> value = (ICriterionType<?>) listitem.getValue();
CriterionType value = (CriterionType) listitem.getValue();
selectedCriterionTypes.remove(value);
removeCriterionsFromHoursGroup(value);
}
@ -378,7 +377,7 @@ public class OrderElementController extends GenericForwardComposer {
* @param type
* The type of the {@link Criterion} that should be removed
*/
private void removeCriterionsFromHoursGroup(ICriterionType<?> type) {
private void removeCriterionsFromHoursGroup(CriterionType type) {
OrderElement orderElement = getOrderElement();
for (HoursGroup hoursGroup : orderElement.getHoursGroups()) {
hoursGroup.removeCriterionByType(type);
@ -440,8 +439,8 @@ public class OrderElementController extends GenericForwardComposer {
}
}));
// For each ICriterionType selected
for (ICriterionType<?> criterionType : getSelectedCriterionTypes()) {
// For each CriterionType selected
for (CriterionType criterionType : getSelectedCriterionTypes()) {
Listcell cellCriterion = new Listcell();
cellCriterion.setParent(item);
@ -450,7 +449,7 @@ public class OrderElementController extends GenericForwardComposer {
headerCriterion.setLabel(criterionType.getName());
headerCriterion.setParent(header);
// Add a new Listbox for each ICriterionType
// Add a new Listbox for each CriterionType
final Listbox criterionListbox = new Listbox();
criterionListbox.setRows(1);
criterionListbox.setMold("select");
@ -582,8 +581,8 @@ public class OrderElementController extends GenericForwardComposer {
cellPercentage.appendChild(percentage);
cellFixedPercentage.appendChild(fixedPercentage);
// For each ICriterionType selected
for (ICriterionType<?> criterionType : getSelectedCriterionTypes()) {
// For each CriterionType selected
for (CriterionType criterionType : getSelectedCriterionTypes()) {
Listcell cellCriterion = new Listcell();
cellCriterion.setParent(item);
@ -592,7 +591,7 @@ public class OrderElementController extends GenericForwardComposer {
headerCriterion.setLabel(criterionType.getName());
headerCriterion.setParent(header);
// Add a new Listbox for each ICriterionType
// Add a new Listbox for each CriterionType
final Listbox criterionListbox = new Listbox();
criterionListbox.setRows(1);
criterionListbox.setMold("select");

View file

@ -10,8 +10,9 @@ import org.navalplanner.business.orders.entities.HoursGroup;
import org.navalplanner.business.orders.entities.OrderElement;
import org.navalplanner.business.resources.bootstrap.ICriterionsBootstrap;
import org.navalplanner.business.resources.entities.Criterion;
import org.navalplanner.business.resources.entities.ICriterionType;
import org.navalplanner.business.resources.entities.CriterionType;
import org.navalplanner.business.resources.services.CriterionService;
import org.navalplanner.business.resources.services.CriterionTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
@ -33,7 +34,10 @@ public class OrderElementModel implements IOrderElementModel {
@Autowired
private CriterionService criterionService;
private Map<String, ICriterionType<?>> mapCriterionTypes = new HashMap<String, ICriterionType<?>>();
@Autowired
private CriterionTypeService criterionTypeService;
private Map<String, CriterionType> mapCriterionTypes = new HashMap<String, CriterionType>();
@Override
public OrderElement getOrderElement() {
@ -59,12 +63,11 @@ public class OrderElementModel implements IOrderElementModel {
}
@Override
public List<ICriterionType<?>> getCriterionTypes() {
List<ICriterionType<?>> criterionTypes = criterionsBootstrap
.getTypes();
public List<CriterionType> getCriterionTypes() {
List<CriterionType> criterionTypes = criterionTypeService.getAll();
if (mapCriterionTypes.isEmpty()) {
for (ICriterionType<?> criterionType : criterionTypes) {
for (CriterionType criterionType : criterionTypes) {
mapCriterionTypes.put(criterionType.getName(), criterionType);
}
}
@ -73,10 +76,10 @@ public class OrderElementModel implements IOrderElementModel {
}
@Override
public ICriterionType<?> getCriterionTypeByName(String name) {
public CriterionType getCriterionTypeByName(String name) {
if (mapCriterionTypes.isEmpty()) {
for (ICriterionType<?> criterionType : criterionsBootstrap
.getTypes()) {
for (CriterionType criterionType : criterionTypeService
.getAll()) {
mapCriterionTypes.put(criterionType.getName(), criterionType);
}
}
@ -85,7 +88,7 @@ public class OrderElementModel implements IOrderElementModel {
}
@Override
public List<Criterion> getCriterionsFor(ICriterionType<?> type) {
public List<Criterion> getCriterionsFor(CriterionType type) {
return (List<Criterion>) criterionService.getCriterionsFor(type);
}

View file

@ -6,6 +6,7 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.navalplanner.business.common.exceptions.ValidationException;
import org.navalplanner.business.resources.entities.Criterion;
import org.navalplanner.business.resources.entities.CriterionType;
import org.navalplanner.business.resources.entities.ICriterionType;
import org.navalplanner.web.common.IMessagesForUser;
import org.navalplanner.web.common.Level;
@ -166,20 +167,20 @@ public class CriterionAdminController extends GenericForwardComposer {
}
private GroupsModel getTypesWithCriterions() {
List<ICriterionType<?>> types = criterionsModel.getTypes();
List<CriterionType> types = criterionsModel.getTypes();
Object[][] groups = new Object[types.size()][];
int i = 0;
for (ICriterionType<?> type : types) {
for (CriterionType type : types) {
groups[i] = criterionsModel.getCriterionsFor(type).toArray();
i++;
}
return new SimpleGroupsModel(groups, asStrings(types), types.toArray());
}
private String[] asStrings(List<ICriterionType<?>> types) {
private String[] asStrings(List<CriterionType> types) {
String[] result = new String[types.size()];
int i = 0;
for (ICriterionType<?> criterionType : types) {
for (CriterionType criterionType : types) {
result[i++] = criterionType.getName();
}
return result;

View file

@ -13,11 +13,13 @@ import org.navalplanner.business.common.exceptions.InstanceNotFoundException;
import org.navalplanner.business.common.exceptions.ValidationException;
import org.navalplanner.business.resources.bootstrap.ICriterionsBootstrap;
import org.navalplanner.business.resources.entities.Criterion;
import org.navalplanner.business.resources.entities.CriterionType;
import org.navalplanner.business.resources.entities.CriterionWithItsType;
import org.navalplanner.business.resources.entities.ICriterionType;
import org.navalplanner.business.resources.entities.Resource;
import org.navalplanner.business.resources.entities.Worker;
import org.navalplanner.business.resources.services.CriterionService;
import org.navalplanner.business.resources.services.CriterionTypeService;
import org.navalplanner.business.resources.services.ResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
@ -44,6 +46,9 @@ public class CriterionsModel implements ICriterionsModel {
@Autowired
private CriterionService criterionService;
@Autowired
private CriterionTypeService criterionTypeService;
@Autowired
private ResourceService resourceService;
@ -53,8 +58,8 @@ public class CriterionsModel implements ICriterionsModel {
@Override
@Transactional(readOnly = true)
public List<ICriterionType<?>> getTypes() {
return criterionsBootstrap.getTypes();
public List<CriterionType> getTypes() {
return criterionTypeService.getAll();
}
@Override

View file

@ -5,6 +5,7 @@ import java.util.List;
import org.navalplanner.business.common.exceptions.ValidationException;
import org.navalplanner.business.resources.entities.Criterion;
import org.navalplanner.business.resources.entities.CriterionType;
import org.navalplanner.business.resources.entities.ICriterionType;
import org.navalplanner.business.resources.entities.Resource;
import org.navalplanner.business.resources.entities.Worker;
@ -15,7 +16,7 @@ import org.navalplanner.business.resources.entities.Worker;
*/
public interface ICriterionsModel {
List<ICriterionType<?>> getTypes();
List<CriterionType> getTypes();
Collection<Criterion> getCriterionsFor(ICriterionType<?> type);