@@ -232,7 +232,7 @@ public Project createProject(Project project, int userId) throws Exception {
232232 // TODO: concurrency transaction -> https://www.jooq.org/doc/3.9/manual/sql-execution/transaction-management/
233233 addUserToRole (userId , "ProjectAdmin" , newProject .getId ());
234234
235- // 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.
235+ // 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) .
236236 // Call repository get separately
237237 projectRepository .update (newProject );
238238 return projectRepository .findById (newProject .getId (), userId );
@@ -343,92 +343,112 @@ public Requirement modifyRequirement(Requirement modifiedRequirement, int userId
343343
344344 if (modifiedRequirement .getCategories () != null ) {
345345 List <Category > oldCategories = listCategoriesByRequirementId (modifiedRequirement .getId (), userId );
346+ handleOldCategory (modifiedRequirement , oldCategories );
347+ handleNewCategory (modifiedRequirement , oldCategories );
348+ }
349+ synchronizeTags (modifiedRequirement , oldRequirement );
350+ synchronizeAttachments (modifiedRequirement , userId , oldRequirement );
351+
352+ return getRequirementById (modifiedRequirement .getId (), userId );
353+ }
354+
355+ private void handleNewCategory (Requirement modifiedRequirement , List <Category > oldCategories ) throws BazaarException {
356+ for (Integer newCategory : modifiedRequirement .getCategories ()) {
357+ boolean containCategory = false ;
346358 for (Category oldCategory : oldCategories ) {
347- boolean containCategory = false ;
348- for (Integer newCategory : modifiedRequirement .getCategories ()) {
349- if (oldCategory .getId () == newCategory ) {
350- containCategory = true ;
351- break ;
352- }
353- }
354- if (!containCategory ) {
355- deleteCategoryTag (modifiedRequirement .getId (), oldCategory .getId ());
359+ if (oldCategory .getId () == newCategory ) {
360+ containCategory = true ;
361+ break ;
356362 }
357363 }
364+ if (!containCategory ) {
365+ addCategoryTag (modifiedRequirement .getId (), newCategory );
366+ }
367+ }
368+ }
369+
370+ private void handleOldCategory (Requirement modifiedRequirement , List <Category > oldCategories ) throws BazaarException {
371+ for (Category oldCategory : oldCategories ) {
372+ boolean containCategory = false ;
358373 for (Integer newCategory : modifiedRequirement .getCategories ()) {
359- boolean containCategory = false ;
360- for (Category oldCategory : oldCategories ) {
361- if (oldCategory .getId () == newCategory ) {
362- containCategory = true ;
363- break ;
364- }
365- }
366- if (!containCategory ) {
367- addCategoryTag (modifiedRequirement .getId (), newCategory );
374+ if (oldCategory .getId () == newCategory ) {
375+ containCategory = true ;
376+ break ;
368377 }
369378 }
379+ if (!containCategory ) {
380+ deleteCategoryTag (modifiedRequirement .getId (), oldCategory .getId ());
381+ }
370382 }
383+ }
371384
372- // Synchronize tags
373- if (modifiedRequirement .getTags () != null ) {
385+ private void synchronizeAttachments ( Requirement modifiedRequirement , int userId , Requirement oldRequirement ) {
386+ if (modifiedRequirement .getAttachments () != null ) {
374387 // Check if tags have changed
375- for (Tag tag : modifiedRequirement .getTags ()) {
376- try {
377- Tag internalTag = getTagById (tag .getId ());
378-
379- // Check if tag exists (in project)
380- if (internalTag == null || modifiedRequirement .getProjectId () != internalTag .getProjectId ()) {
381- tag .setProjectId (modifiedRequirement .getProjectId ());
382- tag = createTag (tag );
383- }
384- tagRequirement (tag .getId (), modifiedRequirement .getId ());
385- } catch (Exception e ) {
386- e .printStackTrace ();
387- }
388- }
388+ checkTagChangesAttachments (modifiedRequirement , userId );
389389
390390 // Remove tags no longer present
391- oldRequirement .getTags ().stream ().filter (tag -> modifiedRequirement .getTags ().contains (tag )).forEach (tag -> {
391+ oldRequirement .getAttachments ().stream ().filter (attachment -> modifiedRequirement .getAttachments ().contains (attachment )).forEach (attachment -> {
392392 try {
393- untagRequirement ( tag . getId (), oldRequirement .getId ());
393+ deleteAttachmentById ( attachment .getId ());
394394 } catch (Exception e ) {
395395 e .printStackTrace ();
396396 }
397397 });
398398 }
399+ }
399400
400- // Synchronize attachments
401- if (modifiedRequirement .getAttachments () != null ) {
402- // Check if tags have changed
403- for (Attachment attachment : modifiedRequirement .getAttachments ()) {
404- try {
405- Attachment internalAttachment = null ;
406- if (attachment .getId () != 0 ) {
407- internalAttachment = getAttachmentById (attachment .getId ());
408- }
409-
410- // Check if attachment exists, otherwise create
411- if (internalAttachment == null ) {
412- attachment .setRequirementId (modifiedRequirement .getId ());
413- attachment .setCreator (getUserById (userId ));
414- createAttachment (attachment );
415- }
416- } catch (Exception e ) {
417- e .printStackTrace ();
401+ private void checkTagChangesAttachments (Requirement modifiedRequirement , int userId ) {
402+ for (Attachment attachment : modifiedRequirement .getAttachments ()) {
403+ try {
404+ Attachment internalAttachment = null ;
405+ if (attachment .getId () != 0 ) {
406+ internalAttachment = getAttachmentById (attachment .getId ());
418407 }
408+
409+ // Check if attachment exists, otherwise create
410+ if (internalAttachment == null ) {
411+ attachment .setRequirementId (modifiedRequirement .getId ());
412+ attachment .setCreator (getUserById (userId ));
413+ createAttachment (attachment );
414+ }
415+ } catch (Exception e ) {
416+ e .printStackTrace ();
419417 }
418+ }
419+ }
420+
421+ private void synchronizeTags (Requirement modifiedRequirement , Requirement oldRequirement ) {
422+ if (modifiedRequirement .getTags () != null ) {
423+ // Check if tags have changed
424+ checkTagChanges (modifiedRequirement );
420425
421426 // Remove tags no longer present
422- oldRequirement .getAttachments ().stream ().filter (attachment -> modifiedRequirement .getAttachments ().contains (attachment )).forEach (attachment -> {
427+ oldRequirement .getTags ().stream ().filter (tag -> ! modifiedRequirement .getTags ().contains (tag )).forEach (tag -> {
423428 try {
424- deleteAttachmentById ( attachment .getId ());
429+ untagRequirement ( tag . getId (), oldRequirement .getId ());
425430 } catch (Exception e ) {
426431 e .printStackTrace ();
427432 }
428433 });
429434 }
435+ }
430436
431- return getRequirementById (modifiedRequirement .getId (), userId );
437+ private void checkTagChanges (Requirement modifiedRequirement ) {
438+ for (Tag tag : modifiedRequirement .getTags ()) {
439+ try {
440+ Tag internalTag = getTagById (tag .getId ());
441+
442+ // Check if tag exists (in project)
443+ if (internalTag == null || modifiedRequirement .getProjectId () != internalTag .getProjectId ()) {
444+ tag .setProjectId (modifiedRequirement .getProjectId ());
445+ tag = createTag (tag );
446+ }
447+ tagRequirement (tag .getId (), modifiedRequirement .getId ());
448+ } catch (Exception e ) {
449+ e .printStackTrace ();
450+ }
451+ }
432452 }
433453
434454 @ Override
0 commit comments