Skip to content

Commit

Permalink
refactor: reduce complexity in DALFacadeImpl
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobasco99 committed Jan 18, 2023
1 parent 99d807b commit 0c7ea8e
Showing 1 changed file with 78 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ public Project createProject(Project project, int userId) throws Exception {
// TODO: concurrency transaction -> https://www.jooq.org/doc/3.9/manual/sql-execution/transaction-management/
addUserToRole(userId, "ProjectAdmin", newProject.getId());

// This is stupid, but the return value of update is inclomplete (dependent objects won't be resolved, since only the top level get by id method is called.
// This is stupid, but the return value of update is incomplete (dependent objects won't be resolved, since only the top level get by id method is called).
// Call repository get separately
projectRepository.update(newProject);
return projectRepository.findById(newProject.getId(), userId);
Expand Down Expand Up @@ -343,92 +343,112 @@ public Requirement modifyRequirement(Requirement modifiedRequirement, int userId

if (modifiedRequirement.getCategories() != null) {
List<Category> oldCategories = listCategoriesByRequirementId(modifiedRequirement.getId(), userId);
handleOldCategory(modifiedRequirement, oldCategories);
handleNewCategory(modifiedRequirement, oldCategories);
}
synchronizeTags(modifiedRequirement, oldRequirement);
synchronizeAttachments(modifiedRequirement, userId, oldRequirement);

return getRequirementById(modifiedRequirement.getId(), userId);
}

private void handleNewCategory(Requirement modifiedRequirement, List<Category> oldCategories) throws BazaarException {
for (Integer newCategory : modifiedRequirement.getCategories()) {
boolean containCategory = false;
for (Category oldCategory : oldCategories) {
boolean containCategory = false;
for (Integer newCategory : modifiedRequirement.getCategories()) {
if (oldCategory.getId() == newCategory) {
containCategory = true;
break;
}
}
if (!containCategory) {
deleteCategoryTag(modifiedRequirement.getId(), oldCategory.getId());
if (oldCategory.getId() == newCategory) {
containCategory = true;
break;
}
}
if (!containCategory) {
addCategoryTag(modifiedRequirement.getId(), newCategory);
}
}
}

private void handleOldCategory(Requirement modifiedRequirement, List<Category> oldCategories) throws BazaarException {
for (Category oldCategory : oldCategories) {
boolean containCategory = false;
for (Integer newCategory : modifiedRequirement.getCategories()) {
boolean containCategory = false;
for (Category oldCategory : oldCategories) {
if (oldCategory.getId() == newCategory) {
containCategory = true;
break;
}
}
if (!containCategory) {
addCategoryTag(modifiedRequirement.getId(), newCategory);
if (oldCategory.getId() == newCategory) {
containCategory = true;
break;
}
}
if (!containCategory) {
deleteCategoryTag(modifiedRequirement.getId(), oldCategory.getId());
}
}
}

// Synchronize tags
if (modifiedRequirement.getTags() != null) {
private void synchronizeAttachments(Requirement modifiedRequirement, int userId, Requirement oldRequirement) {
if (modifiedRequirement.getAttachments() != null) {
// Check if tags have changed
for (Tag tag : modifiedRequirement.getTags()) {
try {
Tag internalTag = getTagById(tag.getId());

// Check if tag exists (in project)
if (internalTag == null || modifiedRequirement.getProjectId() != internalTag.getProjectId()) {
tag.setProjectId(modifiedRequirement.getProjectId());
tag = createTag(tag);
}
tagRequirement(tag.getId(), modifiedRequirement.getId());
} catch (Exception e) {
e.printStackTrace();
}
}
checkTagChangesAttachments(modifiedRequirement, userId);

// Remove tags no longer present
oldRequirement.getTags().stream().filter(tag -> modifiedRequirement.getTags().contains(tag)).forEach(tag -> {
oldRequirement.getAttachments().stream().filter(attachment -> modifiedRequirement.getAttachments().contains(attachment)).forEach(attachment -> {
try {
untagRequirement(tag.getId(), oldRequirement.getId());
deleteAttachmentById(attachment.getId());
} catch (Exception e) {
e.printStackTrace();
}
});
}
}

// Synchronize attachments
if (modifiedRequirement.getAttachments() != null) {
// Check if tags have changed
for (Attachment attachment : modifiedRequirement.getAttachments()) {
try {
Attachment internalAttachment = null;
if (attachment.getId() != 0) {
internalAttachment = getAttachmentById(attachment.getId());
}

// Check if attachment exists, otherwise create
if (internalAttachment == null) {
attachment.setRequirementId(modifiedRequirement.getId());
attachment.setCreator(getUserById(userId));
createAttachment(attachment);
}
} catch (Exception e) {
e.printStackTrace();
private void checkTagChangesAttachments(Requirement modifiedRequirement, int userId) {
for (Attachment attachment : modifiedRequirement.getAttachments()) {
try {
Attachment internalAttachment = null;
if (attachment.getId() != 0) {
internalAttachment = getAttachmentById(attachment.getId());
}

// Check if attachment exists, otherwise create
if (internalAttachment == null) {
attachment.setRequirementId(modifiedRequirement.getId());
attachment.setCreator(getUserById(userId));
createAttachment(attachment);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

private void synchronizeTags(Requirement modifiedRequirement, Requirement oldRequirement) {
if (modifiedRequirement.getTags() != null) {
// Check if tags have changed
checkTagChanges(modifiedRequirement);

// Remove tags no longer present
oldRequirement.getAttachments().stream().filter(attachment -> modifiedRequirement.getAttachments().contains(attachment)).forEach(attachment -> {
oldRequirement.getTags().stream().filter(tag -> modifiedRequirement.getTags().contains(tag)).forEach(tag -> {
try {
deleteAttachmentById(attachment.getId());
untagRequirement(tag.getId(), oldRequirement.getId());
} catch (Exception e) {
e.printStackTrace();
}
});
}
}

return getRequirementById(modifiedRequirement.getId(), userId);
private void checkTagChanges(Requirement modifiedRequirement) {
for (Tag tag : modifiedRequirement.getTags()) {
try {
Tag internalTag = getTagById(tag.getId());

// Check if tag exists (in project)
if (internalTag == null || modifiedRequirement.getProjectId() != internalTag.getProjectId()) {
tag.setProjectId(modifiedRequirement.getProjectId());
tag = createTag(tag);
}
tagRequirement(tag.getId(), modifiedRequirement.getId());
} catch (Exception e) {
e.printStackTrace();
}
}
}

@Override
Expand Down

0 comments on commit 0c7ea8e

Please sign in to comment.