ItEr40S22CUProcuraOrganizacionsTraballoItEr32S10: Used generated order code when creating an order.
This commit is contained in:
parent
f59d342a79
commit
e6ae3a66e2
9 changed files with 91 additions and 7 deletions
|
|
@ -40,4 +40,8 @@ public interface IOrderSequenceDAO extends IGenericDAO<OrderSequence, Long> {
|
|||
void remove(OrderSequence orderSequence) throws InstanceNotFoundException,
|
||||
IllegalArgumentException;
|
||||
|
||||
}
|
||||
OrderSequence getActiveOrderSequence();
|
||||
|
||||
String getNextOrderCode();
|
||||
|
||||
}
|
||||
|
|
@ -29,7 +29,10 @@ import org.navalplanner.business.common.exceptions.InstanceNotFoundException;
|
|||
import org.navalplanner.business.i18n.I18nHelper;
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.context.annotation.Scope;
|
||||
import org.springframework.orm.hibernate3.HibernateOptimisticLockingFailureException;
|
||||
import org.springframework.stereotype.Repository;
|
||||
import org.springframework.transaction.annotation.Propagation;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
/**
|
||||
* DAO for {@link OrderSequence}.
|
||||
|
|
@ -72,4 +75,27 @@ public class OrderSequenceDAO extends GenericDAOHibernate<OrderSequence, Long>
|
|||
remove(orderSequence.getId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public OrderSequence getActiveOrderSequence() {
|
||||
return (OrderSequence) getSession().createCriteria(OrderSequence.class)
|
||||
.add(Restrictions.eq("active", true)).uniqueResult();
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(propagation = Propagation.REQUIRES_NEW)
|
||||
public String getNextOrderCode() {
|
||||
for (int i = 0; i < 5; i++) {
|
||||
try {
|
||||
OrderSequence orderSequence = getActiveOrderSequence();
|
||||
orderSequence.incrementLastValue();
|
||||
save(orderSequence);
|
||||
return orderSequence.getCode();
|
||||
} catch (HibernateOptimisticLockingFailureException e) {
|
||||
// Do nothing (optimistic approach 5 attempts)
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -40,6 +40,8 @@ public class OrderSequence extends BaseEntity {
|
|||
public static final Integer MIN_NUMBER_OF_DIGITS = 5;
|
||||
public static final Integer MAX_NUMBER_OF_DIGITS = 9;
|
||||
|
||||
public static final String CODE_SEPARATOR = "-";
|
||||
|
||||
public static OrderSequence create(String prefix) {
|
||||
return create(new OrderSequence(prefix));
|
||||
}
|
||||
|
|
@ -150,4 +152,12 @@ public class OrderSequence extends BaseEntity {
|
|||
return lastValue > 0;
|
||||
}
|
||||
|
||||
public String getCode() {
|
||||
return prefix + CODE_SEPARATOR + formatValue(numberOfDigits, lastValue);
|
||||
}
|
||||
|
||||
public void incrementLastValue() {
|
||||
lastValue++;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -22,6 +22,7 @@ package org.navalplanner.web.common;
|
|||
|
||||
import static org.navalplanner.web.I18nHelper._;
|
||||
|
||||
import java.util.ConcurrentModificationException;
|
||||
import java.util.List;
|
||||
|
||||
import org.navalplanner.business.calendars.entities.BaseCalendar;
|
||||
|
|
@ -104,6 +105,10 @@ public class ConfigurationController extends GenericForwardComposer {
|
|||
reloadWindow();
|
||||
} catch (ValidationException e) {
|
||||
messages.showInvalidValues(e);
|
||||
} catch (ConcurrentModificationException e) {
|
||||
messages.showMessage(Level.ERROR, e.getMessage());
|
||||
configurationModel.init();
|
||||
reloadWindow();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -23,6 +23,7 @@ package org.navalplanner.web.common;
|
|||
import static org.navalplanner.web.I18nHelper._;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.ConcurrentModificationException;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
|
@ -39,6 +40,7 @@ import org.navalplanner.business.i18n.I18nHelper;
|
|||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.context.annotation.Scope;
|
||||
import org.springframework.orm.hibernate3.HibernateOptimisticLockingFailureException;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
|
|
@ -130,8 +132,14 @@ public class ConfigurationModel implements IConfigurationModel {
|
|||
_("Order sequence prefixes can not be repeated"));
|
||||
}
|
||||
|
||||
configurationDAO.save(configuration);
|
||||
storeAndRemoveOrderSequences();
|
||||
try {
|
||||
configurationDAO.save(configuration);
|
||||
storeAndRemoveOrderSequences();
|
||||
} catch (HibernateOptimisticLockingFailureException e) {
|
||||
throw new ConcurrentModificationException(
|
||||
_("Some order was created during the configuration process, it is impossible to update order sequence table. Please, try again later"));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private boolean checkConstraintPrefixNotRepeated() {
|
||||
|
|
|
|||
|
|
@ -20,6 +20,7 @@
|
|||
|
||||
package org.navalplanner.web.orders;
|
||||
|
||||
import java.util.ConcurrentModificationException;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
|
|
@ -80,7 +81,7 @@ public interface IOrderModel {
|
|||
|
||||
void initEdit(Order order);
|
||||
|
||||
void prepareForCreate();
|
||||
void prepareForCreate() throws ConcurrentModificationException;
|
||||
|
||||
void remove(Order order);
|
||||
|
||||
|
|
|
|||
|
|
@ -22,6 +22,7 @@ package org.navalplanner.web.orders;
|
|||
|
||||
import static org.navalplanner.web.I18nHelper._;
|
||||
|
||||
import java.util.ConcurrentModificationException;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
|
@ -378,8 +379,12 @@ public class OrderCRUDController extends GenericForwardComposer {
|
|||
}
|
||||
|
||||
public void goToCreateForm() {
|
||||
orderModel.prepareForCreate();
|
||||
showEditWindow(_("Create order"));
|
||||
try {
|
||||
orderModel.prepareForCreate();
|
||||
showEditWindow(_("Create order"));
|
||||
} catch (ConcurrentModificationException e) {
|
||||
messagesForUser.showMessage(Level.ERROR, e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public void setPlanningControllerEntryPoints(
|
||||
|
|
|
|||
|
|
@ -24,6 +24,7 @@ import static org.navalplanner.web.I18nHelper._;
|
|||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.ConcurrentModificationException;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
|
|
@ -38,6 +39,7 @@ import org.navalplanner.business.advance.entities.IndirectAdvanceAssignment;
|
|||
import org.navalplanner.business.calendars.daos.IBaseCalendarDAO;
|
||||
import org.navalplanner.business.calendars.entities.BaseCalendar;
|
||||
import org.navalplanner.business.common.daos.IConfigurationDAO;
|
||||
import org.navalplanner.business.common.daos.IOrderSequenceDAO;
|
||||
import org.navalplanner.business.common.entities.Configuration;
|
||||
import org.navalplanner.business.common.exceptions.InstanceNotFoundException;
|
||||
import org.navalplanner.business.common.exceptions.ValidationException;
|
||||
|
|
@ -121,6 +123,9 @@ public class OrderModel implements IOrderModel {
|
|||
@Autowired
|
||||
private IConfigurationDAO configurationDAO;
|
||||
|
||||
@Autowired
|
||||
private IOrderSequenceDAO orderSequenceDAO;
|
||||
|
||||
@Override
|
||||
public List<Label> getLabels() {
|
||||
final List<Label> result = new ArrayList<Label>();
|
||||
|
|
@ -280,7 +285,7 @@ public class OrderModel implements IOrderModel {
|
|||
|
||||
@Override
|
||||
@Transactional(readOnly = true)
|
||||
public void prepareForCreate() {
|
||||
public void prepareForCreate() throws ConcurrentModificationException {
|
||||
loadCriterions();
|
||||
initializeCacheLabels();
|
||||
initializeCacheQualityForms();
|
||||
|
|
@ -288,6 +293,17 @@ public class OrderModel implements IOrderModel {
|
|||
this.orderElementTreeModel = new OrderElementTreeModel(this.order);
|
||||
this.order.setInitDate(new Date());
|
||||
this.order.setCalendar(getDefaultCalendar());
|
||||
|
||||
setDefaultOrderCode();
|
||||
}
|
||||
|
||||
private void setDefaultOrderCode() throws ConcurrentModificationException {
|
||||
String code = orderSequenceDAO.getNextOrderCode();
|
||||
if (code == null) {
|
||||
throw new ConcurrentModificationException(
|
||||
_("Could not get order code, please try again later"));
|
||||
}
|
||||
this.order.setCode(code);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -64,6 +64,7 @@ import org.navalplanner.business.resources.entities.ResourceEnum;
|
|||
import org.navalplanner.web.resources.criterion.ICriterionsModel;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.annotation.NotTransactional;
|
||||
import org.springframework.test.annotation.Rollback;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
|
@ -139,6 +140,14 @@ public class OrderModelTest {
|
|||
return order;
|
||||
}
|
||||
|
||||
@Test
|
||||
@Rollback(false)
|
||||
public void testNotRollback() {
|
||||
// Just to do not make rollback in order to have the default
|
||||
// configuration, needed for prepareForCreate in order to autogenerate
|
||||
// the order code
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreation() throws ValidationException {
|
||||
Order order = createValidOrder();
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue