Adds the generated code sequence to materials.
Replaces the generated code by the code sequence for each material and to the predefined material categories. FEA :ItEr61S04NavalPlanEntities
This commit is contained in:
parent
a43fe366d3
commit
b1385d0a49
5 changed files with 75 additions and 4 deletions
|
|
@ -30,9 +30,11 @@ import java.util.Set;
|
|||
import org.apache.commons.lang.StringUtils;
|
||||
import org.hibernate.validator.AssertTrue;
|
||||
import org.hibernate.validator.NotEmpty;
|
||||
import org.hibernate.validator.NotNull;
|
||||
import org.hibernate.validator.Valid;
|
||||
import org.navalplanner.business.common.IntegrationEntity;
|
||||
import org.navalplanner.business.common.Registry;
|
||||
import org.navalplanner.business.common.entities.EntitySequence;
|
||||
import org.navalplanner.business.common.exceptions.InstanceNotFoundException;
|
||||
import org.navalplanner.business.materials.daos.IMaterialCategoryDAO;
|
||||
|
||||
|
|
@ -53,6 +55,17 @@ public class MaterialCategory extends IntegrationEntity {
|
|||
return result;
|
||||
}
|
||||
|
||||
public static List<Material> getAllMaterialsWithoutAutogeneratedCodeFrom(
|
||||
Collection<? extends MaterialCategory> categories) {
|
||||
List<Material> result = new ArrayList<Material>();
|
||||
for (MaterialCategory each : categories) {
|
||||
if (!each.isCodeAutogenerated()) {
|
||||
result.addAll(each.getMaterials());
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@NotEmpty
|
||||
private String name;
|
||||
|
||||
|
|
@ -64,6 +77,8 @@ public class MaterialCategory extends IntegrationEntity {
|
|||
@Valid
|
||||
private Set<Material> materials = new HashSet<Material>();
|
||||
|
||||
private Integer lastMaterialSequenceCode = 0;
|
||||
|
||||
// Default constructor, needed by Hibernate
|
||||
protected MaterialCategory() {
|
||||
|
||||
|
|
@ -247,4 +262,32 @@ public class MaterialCategory extends IntegrationEntity {
|
|||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public void generateMaterialCodes(int numberOfDigits) {
|
||||
for (Material material : this.getMaterials()) {
|
||||
if ((material.getCode() == null) || (material.getCode().isEmpty())
|
||||
|| (!material.getCode().startsWith(this.getCode()))) {
|
||||
this.incrementLastMaterialSequenceCode();
|
||||
String materialCode = EntitySequence.formatValue(
|
||||
numberOfDigits, this.getLastMaterialSequenceCode());
|
||||
material
|
||||
.setCode(this.getCode()
|
||||
+ EntitySequence.CODE_SEPARATOR_CHILDREN
|
||||
+ materialCode);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void incrementLastMaterialSequenceCode() {
|
||||
if (this.lastMaterialSequenceCode == null) {
|
||||
this.lastMaterialSequenceCode = 0;
|
||||
}
|
||||
this.lastMaterialSequenceCode++;
|
||||
}
|
||||
|
||||
@NotNull(message = "last material sequence code not specified")
|
||||
public Integer getLastMaterialSequenceCode() {
|
||||
return lastMaterialSequenceCode;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -62,6 +62,8 @@
|
|||
|
||||
<property name="codeAutogenerated" not-null="true" />
|
||||
|
||||
<property name="lastMaterialSequenceCode" access="field" not-null="true"/>
|
||||
|
||||
<!-- Indexed the other side -->
|
||||
<set name="subcategories" inverse="true" cascade="all-delete-orphan">
|
||||
<key column="PARENT_ID"/>
|
||||
|
|
|
|||
|
|
@ -250,6 +250,7 @@ public class MaterialsController extends
|
|||
codeTb.setValue(materialCategory.getCode());
|
||||
codeTb.setDisabled(ce.isChecked());
|
||||
Util.reloadBindings(codeTb);
|
||||
Util.reloadBindings(gridMaterials);
|
||||
}
|
||||
});
|
||||
Treecell generateCodeTc = new Treecell();
|
||||
|
|
|
|||
|
|
@ -85,6 +85,8 @@ public class MaterialsModel extends IntegrationEntityModel implements
|
|||
|
||||
private Map<MaterialCategory, String> oldCodes = new HashMap<MaterialCategory, String>();
|
||||
|
||||
private Map<Material, String> oldMaterialCodes = new HashMap<Material, String>();
|
||||
|
||||
@Override
|
||||
@Transactional(readOnly = true)
|
||||
public MutableTreeModel<MaterialCategory> getMaterialCategories() {
|
||||
|
|
@ -107,10 +109,16 @@ public class MaterialsModel extends IntegrationEntityModel implements
|
|||
initializeMaterials(materialCategory.getMaterials());
|
||||
materialCategories.addToRoot(materialCategory);
|
||||
addCategories(materialCategory, materialCategory.getSubcategories());
|
||||
storeOldCodes(materialCategory);
|
||||
}
|
||||
}
|
||||
|
||||
// it stores temporaly the autogenerated code.
|
||||
if (materialCategory.isCodeAutogenerated()) {
|
||||
oldCodes.put(materialCategory, materialCategory.getCode());
|
||||
private void storeOldCodes(MaterialCategory materialCategory) {
|
||||
// it stores temporaly the autogenerated code.
|
||||
if (materialCategory.isCodeAutogenerated()) {
|
||||
oldCodes.put(materialCategory, materialCategory.getCode());
|
||||
for (Material child : materialCategory.getMaterials()) {
|
||||
oldMaterialCodes.put(child, child.getCode());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -128,6 +136,7 @@ public class MaterialsModel extends IntegrationEntityModel implements
|
|||
for (MaterialCategory category: categories) {
|
||||
initializeMaterials(category.getMaterials());
|
||||
materialCategories.add(materialCategory, category);
|
||||
storeOldCodes(category);
|
||||
final Set<MaterialCategory> subcategories = category.getSubcategories();
|
||||
if (subcategories != null) {
|
||||
addCategories(category, subcategories);
|
||||
|
|
@ -233,15 +242,24 @@ public class MaterialsModel extends IntegrationEntityModel implements
|
|||
public void confirmSave() throws ValidationException {
|
||||
final List<MaterialCategory> categories = materialCategories.asList();
|
||||
checkNoCodeRepeatedAtNewMaterials(categories);
|
||||
Integer numberOfDigits = getNumberOfDigitsCode();
|
||||
for (MaterialCategory each: categories) {
|
||||
generateMaterialCodesIfIsNecessary(each, numberOfDigits);
|
||||
categoryDAO.save(each);
|
||||
}
|
||||
}
|
||||
|
||||
private void generateMaterialCodesIfIsNecessary(MaterialCategory category,
|
||||
Integer numberOfDigits) {
|
||||
if (category.isCodeAutogenerated()) {
|
||||
category.generateMaterialCodes(numberOfDigits);
|
||||
}
|
||||
}
|
||||
|
||||
private void checkNoCodeRepeatedAtNewMaterials(
|
||||
final List<MaterialCategory> categories) throws ValidationException {
|
||||
List<Material> allMaterials = MaterialCategory
|
||||
.getAllMaterialsFrom(categories);
|
||||
.getAllMaterialsWithoutAutogeneratedCodeFrom(categories);
|
||||
Map<String, Material> byCode = new HashMap<String, Material>();
|
||||
for (Material each : allMaterials) {
|
||||
if (byCode.containsKey(each.getCode())) {
|
||||
|
|
@ -307,6 +325,12 @@ public class MaterialsModel extends IntegrationEntityModel implements
|
|||
@Override
|
||||
protected void restoreOldCodes() {
|
||||
getCurrentEntity().setCode(oldCodes.get(getCurrentEntity()));
|
||||
for (Material child : ((MaterialCategory) getCurrentEntity())
|
||||
.getMaterials()) {
|
||||
if (!child.isNewObject()) {
|
||||
child.setCode(oldMaterialCodes.get(child));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void setCodeAutogenerated(boolean codeAutogenerated,
|
||||
|
|
|
|||
|
|
@ -89,6 +89,7 @@
|
|||
<rows>
|
||||
<row self="@{each='material'}" value="@{material}" >
|
||||
<textbox id="code" value="@{material.code}" width="95%"
|
||||
disabled="@{material.category.codeAutogenerated}"
|
||||
constraint="no empty:${i18n:_('cannot be null or empty')}" />
|
||||
<textbox value="@{material.description}" width="95%" />
|
||||
<doublebox value="@{material.defaultUnitPrice}" />
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue