Now, when I attach a new project to the contractor, I have to edit it so that I can enter the award date and the contract sum. I would like to have the a pre-commit condition (or alternative way) that checks if there is a contract on the list that doesn’t have these two fields filled out and prevent you from saving.
If I have only one project, it works as desired. But if there is one project with the fields already filled out, my code doesn’t work. How can I have it check the individual projects to identify one that doesn’t have?
public class ContractorEdit extends AbstractEditor {
@Inject
private CollectionDatasource<Projects, UUID> projectsDs;
@Override
protected boolean preCommit() {
if (projectsDs.getItems().iterator().next().getContractSum() ==null || projectsDs.getItems().iterator().next().getAwardDate() ==null)
{
showMessageDialog("Validation Error", "Contract Sum & Award Date Cannot Be Null. Edit Project and Fill in the details.", MessageType.WARNING);
return false;
}
}
return super.preCommit();
}
At first sight, the code in your preCommit() method checks only the first element of the collection, and you need to iterate through all the elements in datasource, for example:
@Override
protected boolean preCommit() {
while (projectsDs.getItems().iterator().hasNext()) {
if (projectsDs.getItems().iterator().next().getContractSum() == null || projectsDs.getItems().iterator().next().getAwardDate() == null) {
showMessageDialog("Validation Error", "Contract Sum & Award Date Cannot Be Null. Edit Project and Fill in the details.", MessageType.WARNING);
return false;
}
}
return super.preCommit();
}
By the way, why not just making these attributes required or using the bean validation mechanism?
The reason why I can’t make them required is that they are keyed in at a different stage in the process i.e. after the project has already been created and undergone several stages.
It’s at this point that they are assigned to a contractor and that’s when the user is expected to key in the contract sum and the award date.
This one gives a continuous loop and freezes the application.
This alternative worked!
@Override
protected boolean preCommit() {
for (Projects p : projectsDs.getItems()){
if (p.getContractSum() == null) {
showMessageDialog("Validation Error", String.format("Contract Sum & Award Date Cannot Be Null. Edit Project %s and Fill in the details.", p.getName()), MessageType.WARNING);
return false;
}
}
return super.preCommit();
}