if(defect == null) {
issues.add(new ValidationIssue("Defect cannot be null"));
return issues;
}
Defect oldDefect = null;
if(mode == Mode.EDIT) {
oldDefect = getExistingDefect(defect.getId(), session.getProject(), issues, "id");
}
lastExistingDefect = oldDefect;
if(mode == Mode.CREATE) {
// new defects should always have new status
defect.setStatus(DefectStatus.NEW);
} else if(defect.getStatus() == null) {
issues.add(new ValidationIssue("Cannot be null", "status"));
}
// make sure title and description size are within constraints
if(defect.getTitle() == null || defect.getTitle().length() > 150
|| defect.getTitle().length() < 5) {
issues.add(new ValidationIssue("Required, must be 5-150 characters", "title"));
}
if(defect.getDescription() == null) {
// empty descriptions are okay
defect.setDescription("");
} else if(defect.getDescription().length() > 5000) {
issues.add(new ValidationIssue("Cannot be greater than 5000 characters", "description"));
}
// make sure the creator and assignee exist and aren't duplicated
if(mode == Mode.EDIT) {
if(oldDefect != null) {
defect.setCreator(oldDefect.getCreator());
}
} else if(defect.getCreator() == null) {
issues.add(new ValidationIssue("Required", "creator"));
} else {
User creator = getExistingUser(defect.getCreator().getUsername(), issues, "creator");
if(creator != null) {
if(!creator.getUsername().equals(session.getUsername())) {
issues.add(new ValidationIssue("Must match currently logged in user", "creator"));
} else {
defect.setCreator(creator);
}
}
}
if(defect.getAssignee() != null) { // defects can be missing an assignee
User assignee = getExistingUser(defect.getAssignee().getUsername(), issues, "assignee");
if(assignee != null) {
defect.setAssignee(assignee);
}
}
if(defect.getTags() == null) {
defect.setTags(new HashSet<Tag>());
}
final Set<Tag> tags = defect.getTags();
if(tags.size() > 100) {
issues.add(new ValidationIssue("Cannot have more than 100 tags", "tags"));
} else {
// need to make a new set because we can't modify existing set as we iterate over it
final Set<Tag> newTags = new HashSet<Tag>();
// validate each tag
for(Tag tag : tags) {
if(tag == null) {
issues.add(new ValidationIssue("Cannot be null", "tags"));
break;
}
List<Model> existingModels = data.retrieve(Tag.class, "name", tag.getName(),
session.getProject());
if(existingModels.size() > 0 && existingModels.get(0) != null) {
// make sure we don't insert duplicate tags
newTags.add((Tag) existingModels.get(0));
} else if(tag.getName() == null || tag.getName().length() < 1) {
// tags with empty names aren't allowed
// TODO: this validation should probably happen in Tag's EntityManager
issues.add(new ValidationIssue("Names can't be empty", "tags"));
break;
} else { //doesn't already exist, is valid
newTags.add(tag);
}
}
defect.setTags(newTags);
}
// make sure we're not being spoofed with some weird date
final Date now = new Date();
if(oldDefect != null) {
defect.setCreationDate(oldDefect.getCreationDate());
} else {
defect.setCreationDate(now);
}
defect.setLastModifiedDate((Date)now.clone());
if(oldDefect != null) {
defect.setEvents(oldDefect.getEvents());
} else {
// new defects should never have any events
defect.setEvents(new ArrayList<DefectEvent>());
}