Yesterday I had posted a question regarding optimizing editMethod here on code-review. Thanks to an awesome answer by Pimgd, I was able to merge conditional checks, checking conditions and few other things.
I am posting this as a follow-up question for the other question for further optimization of the method. All suggestions are welcome!.
Code now :
@Override
public String editGroupNote(GroupNotes noteObjectFromUser, int msectionId) {
if (noteObjectFromUser.isPrivateNoteFlag()) {
createPrivateNote(noteObjectFromUser, msectionId);
return "privacychange";
}
Person person = this.personService.getCurrentlyAuthenticatedUser();
NoteSelection noteSelection = this.noteSelectionService.checkIfSelectionValid(noteObjectFromUser.getMnoticesid(), person.getId());
boolean checkIfEvernote = (noteSelection != null);
String latestText = noteObjectFromUser.getMnotetext();
noteObjectFromUser.setMnotetext(noteObjectFromUser.getMnotetext().replaceAll("\\s*id=\"cke[^\">]*\"", ""));
GroupSection retrievedSection = this.groupSectionService.getGroupSectionById(msectionId);
GroupCanvas ownedCanvas = this.groupCanvasService.getCanvasById(retrievedSection.getCurrentCanvasId());
GroupAccount ownedAccount = this.groupAccountService.getGroupById(ownedCanvas.getGroupAccountId());
GroupNotes databaseNoteObject = this.groupNotesDAO.getGroupNoteById(noteObjectFromUser.getMnoticesid());
GroupMembers loggedInMember = this.groupMembersService.returnMembersMatchingUsernameAccountId(person.getUsername(), ownedAccount.getGroupId());
if (!(loggedInMember.isAccesslevel())) {
return "";
}
String oldText = databaseNoteObject.getMnotetext();
String oldTag = databaseNoteObject.getMnotetag();
databaseNoteObject.setMnotetext(databaseNoteObject.getMnotetext().replaceAll("\\s*id=\"cke[^\">]*\"", ""));
noteObjectFromUser.setCreatorId(databaseNoteObject.getCreatorId());
noteObjectFromUser.setMnotetext(noteObjectFromUser.getMnotetext().replaceAll("\\r?\\n", "<br/>"));
noteObjectFromUser.setMnotetag(noteObjectFromUser.getMnotetag().replaceAll("\\r?\\n", "<br/>"));
noteObjectFromUser.setNoteCreatorEmail(databaseNoteObject.getNoteCreatorEmail());
boolean noEdit = false;
GroupNoteHistory groupNoteHistory = new GroupNoteHistory();
if (!(noteObjectFromUser.getMnotetext().equals(databaseNoteObject.getMnotetext()))) {
String newText = "";
if (isLineDifferent(databaseNoteObject.getMnotetext(), noteObjectFromUser.getMnotetext())) {
diff_match_patch diffMatchPatch = new diff_match_patch();
LinkedList<diff_match_patch.Diff> deltas = diffMatchPatch.diff_main(databaseNoteObject.getMnotetext(), noteObjectFromUser.getMnotetext());
diffMatchPatch.diff_cleanupSemantic(deltas);
newText += diffMatchPatch.diff_prettyHtml(deltas);
groupNoteHistory.setWhatHasChanged("textchange");
noEdit = true;
}
newText = newText.replaceAll("<li>", "<div class=‘list‘>");
newText = newText.replaceAll("</li>", "</div>");
groupNoteHistory.setNewNoteText(newText);
groupNoteHistory.setWhatHasChanged("textchange");
}
if (!(noteObjectFromUser.getMnotecolor().equals(databaseNoteObject.getMnotecolor()))) {
if (noEdit) {
groupNoteHistory.setWhatHasChanged("generalchange");
} else {
groupNoteHistory.setWhatHasChanged("colorchange");
groupNoteHistory.setChangedMessage(databaseNoteObject.getMnotecolor());
}
groupNoteHistory.setChangedMessage(part1 + noteColorSubject + part2 + noteObjectFromUser.getMnotecolor() + part3);
noEdit = true;
}
if (!(noteObjectFromUser.getMnotetag().equals(databaseNoteObject.getMnotetag()))) {
groupNoteHistory.setNewNoteHeadline(noteObjectFromUser.getMnotetag());
if (noEdit) {
groupNoteHistory.setWhatHasChanged("generalchange");
} else {
groupNoteHistory.setWhatHasChanged("tagchange");
groupNoteHistory.setNewNoteHeadline(noteObjectFromUser.getMnotetag());
groupNoteHistory.setOldHeadLine(databaseNoteObject.getMnotetag());
}
noEdit = true;
}
if (!(noteObjectFromUser.getZugwisenPersonId() == databaseNoteObject.getZugwisenPersonId())) {
if ((databaseNoteObject.getZugwisenPersonId() != 0) && (noteObjectFromUser.getZugwisenPersonId() == 0)) {
Person zugweisedPerson = this.personService.getPersonById(databaseNoteObject.getZugwisenPersonId());
if (!noEdit) {
groupNoteHistory.setWhatHasChanged("zugweised");
groupNoteHistory.setChangedMessage("Zuweisung von " + zugweisedPerson.getFirstName() + " entfernt");
} else {
groupNoteHistory.setWhatHasChanged("generalchange");
}
noEdit = true;
updateStatisticsForZugweisen(zugweisedPerson.getId(), false, ownedAccount.getGroupId(),
ownedCanvas.getMcanvasid(), noteObjectFromUser.getMnoticesid());
} else {
Person zugweisedPerson = this.personService.getPersonById(noteObjectFromUser.getZugwisenPersonId());
if (!(zugweisedPerson == null)) {
noteObjectFromUser.setPersonWhoZugweised(person.getId());
if (!noEdit) {
groupNoteHistory.setWhatHasChanged("zugweised");
groupNoteHistory.setChangedMessage("Hat die Note " + zugweisedPerson.getFirstName() + " zugewiesen");
} else {
groupNoteHistory.setWhatHasChanged("generalchange");
}
noEdit = true;
updateStatisticsForZugweisen(zugweisedPerson.getId(), true, ownedAccount.getGroupId(), ownedCanvas.getMcanvasid(), noteObjectFromUser.getMnoticesid());
}
}
}
boolean gantt = false;
if ((!(noteObjectFromUser.getNoteTarget() == null)) || (!(databaseNoteObject.getNoteTarget() == null))) {
if ((noteObjectFromUser.getNoteTarget() != null) && (databaseNoteObject.getNoteTarget() == null)) {
if (!noEdit) {
gantt = true;
}
noEdit = true;
} else if ((noteObjectFromUser.getNoteTarget() == null) && (databaseNoteObject.getNoteTarget() != null)) {
if (!noEdit) {
gantt = true;
}
noEdit = true;
} else if ((!(noteObjectFromUser.getNoteTarget() == null))) {
Date fromUserDate = new Date(noteObjectFromUser.getNoteTarget().getTime());
Date savedDate = new Date(databaseNoteObject.getNoteTarget().getTime());
if (savedDate.after(fromUserDate) || fromUserDate.after(savedDate)) {
if (!noEdit) {
gantt = true;
}
noEdit = true;
}
} else {
Person zugweisedPerson = this.personService.getPersonById(noteObjectFromUser.getZugwisenPersonId());
if (!(zugweisedPerson == null)) {
groupNoteHistory.setWhatHasChanged("zugweised");
groupNoteHistory.setChangedMessage("Hat die Note " + zugweisedPerson.getFirstName() + " zugewiesen");
gantt = false;
noEdit = true;
}
}
}
boolean hasStartFromUser = noteObjectFromUser.getStartDateTimestamp() != null;
boolean hasStartFromDB = databaseNoteObject.getStartDateTimestamp() != null;
// For below line, also check screenshot attached.
if (hasStartFromUser != hasStartFromDB) {
if (!noEdit) {
gantt = true;
}
noEdit = true;
} else if(hasStartFromUser && hasStartFromDB){
Date fromUserDate = new Date(noteObjectFromUser.getStartDateTimestamp().getTime());
Date savedDate = new Date(databaseNoteObject.getStartDateTimestamp().getTime());
if (fromUserDate.after(savedDate) || savedDate.after(fromUserDate)) {
if (!noEdit) {
gantt = true;
}
noEdit = true;
}
}
if (noEdit) {
noteObjectFromUser.setNoteCreationTime(databaseNoteObject.getNoteCreationTime());
noteObjectFromUser.setNoteCreatorEmail(databaseNoteObject.getNoteCreatorEmail());
noteObjectFromUser.setMnotetext(latestText);
databaseNoteObject.setMnotetext(oldText);
this.groupNotesDAO.editGroupNote(noteObjectFromUser, msectionId);
if (checkIfEvernote) {
this.evernoteService.pushNoteToEvernote(noteObjectFromUser.getMnoticesid());
}
groupNoteHistory.setMnoteEditDate(new Timestamp(System.currentTimeMillis()));
groupNoteHistory.setEditorId(person.getId());
if (!gantt) {
this.groupNoteHistoryService.addNoteHistory(groupNoteHistory, noteObjectFromUser.getMnoticesid());
this.noteActivityService.saveGroupNotActivity(noteObjectFromUser.getMnoticesid());
}
noteObjectFromUser.setActiveEdit(false);
noteObjectFromUser.setNoteDate(noteObjectFromUser.getNoteDate());
int canvasId = retrievedSection.getCurrentCanvasId();
GroupCanvas groupCanvas = this.groupCanvasService.getCanvasById(canvasId);
Long groupAccountId = groupCanvas.getGroupAccountId();
this.unreadNotesService.deleteEntriesForAnote(databaseNoteObject.getMnoticesid());
this.notificationService.deleteNotificationsForNote(databaseNoteObject.getMnoticesid());
sendOutNotifications(groupAccountId, groupCanvas, retrievedSection, noteObjectFromUser, groupNoteHistory, person.getId(), person.getUsername(), person.getFirstName(),
groupNoteHistory.getWhatHasChanged(), groupNoteHistory.getChangedMessage(), gantt, oldTag);
return "edit";
} else {
return "nochange";
}
}
sendOutNotifications method :
private void sendOutNotifications(Long groupAccountId, GroupCanvas groupCanvas, GroupSection groupSection,
GroupNotes mnotes, GroupNoteHistory groupNoteHistory, int personid, String email,
String firstName, String subject, String text, boolean gantt, String oldName) {
List<GroupMembers> groupMembersList = this.groupMembersService.returnGroupMembers(groupAccountId);
GroupAccount groupAccount = this.groupAccountService.getGroupById(groupAccountId);
Person editingUser = this.personService.getPersonById(personid);
String type = "note";
if (gantt)
type = "gantt";
for (GroupMembers groupMembers : groupMembersList) {
Notification notification = this.notificationService.createNotification(personid, true, "/section/listing/" + groupCanvas.getMcanvasid() + "?note=" + mnotes.getMnoticesid(), subject, text,
type, groupSection.getMsectionid(), groupCanvas.getMcanvasid(), mnotes.getMnoticesid(), mnotes.getMnotecolor());
if (!(groupNoteHistory.getWhatHasChanged() == null)) {
if (groupNoteHistory.getWhatHasChanged().equals("generalchange")) {
notification.setSubject(groupAccount.getGroupName() + ", " + groupCanvas.getMcanvasname());
notification.setText("Note editiert: <span class='bold'>" + mnotes.getMnotetag() + "</span> ");
} else {
if (groupNoteHistory.getWhatHasChanged().equals("colorchange")) {
notification.setSubject(groupAccount.getGroupName() + ", " + groupCanvas.getMcanvasname());
notification.setText("Note Farbe geändert: <span class='bold'>" + mnotes.getMnotetag() + "</span> ");
}
if (groupNoteHistory.getWhatHasChanged().equals("tagchange")) {
notification.setSubject(groupAccount.getGroupName() + ", " + groupCanvas.getMcanvasname());
notification.setText("Note Überschrift geändert: <span class='bold'>" + oldName + "</span> " + " in <span class='bold'> " + mnotes.getMnotetag() + "</span>");
}
if (groupNoteHistory.getWhatHasChanged().equals("textchange")) {
notification.setSubject(groupAccount.getGroupName() + ", " + groupCanvas.getMcanvasname());
notification.setText("Note Text geändert: <span class='bold'>" + mnotes.getMnotetag() + "</span> ");
}
if (groupNoteHistory.getWhatHasChanged().equals("zugweised")) {
notification.setSubject(groupAccount.getGroupName() + ", " + groupCanvas.getMcanvasname());
notification.setText("Note <span class='bold'>" + mnotes.getMnotetag() + "</span> " + editingUser.getFirstName() + " zugewiesen");
}
}
}
this.chatService.sendNotification(notification, groupMembers.getMemberid());
if (!(groupMembers.getMemberUsername().equals(email)) && (!gantt)) {
this.notificationService.addNotification(notification, groupMembers.getMemberid());
UnreadNotes unreadNotes = this.unreadNotesService.createUnreadNoteEntry(groupAccountId, groupMembers.getMemberUsername(), groupCanvas.getMcanvasid());
this.unreadNotesService.saveUnreadNoteEntry(unreadNotes, mnotes.getMnoticesid());
}
}
this.noteLockService.deleteNoteLockForUser(mnotes.getMnoticesid());
}
Screenshot :