Skip to content

Commit

Permalink
SAK-23583 When adding announcements from assignments, the ordering is…
Browse files Browse the repository at this point in the history
… wrong

git-svn-id: https://source.sakaiproject.org/svn/announcement/trunk@315518 66ffb92e-73f9-0310-93c1-f5514f145a0a
  • Loading branch information
juanjmerono committed Nov 18, 2014
1 parent ef79b3e commit 0d5971f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
import org.sakaiproject.javax.Filter;
import org.sakaiproject.message.api.Message;
import org.sakaiproject.message.api.MessageChannel;
import org.sakaiproject.message.api.MessageEdit;
import org.sakaiproject.message.api.MessageHeader;
import org.sakaiproject.message.api.MessageHeaderEdit;
import org.sakaiproject.message.util.BaseMessage;
Expand Down Expand Up @@ -1616,6 +1617,25 @@ public AnnouncementMessage addAnnouncementMessage(String subject, boolean draft,
return edit;

} // addAnnouncementMessage

public void commitMessage(MessageEdit edit, int priority, String invokee) {
setMessageOrderMax(edit);
super.commitMessage(edit, priority, invokee);
}

private void setMessageOrderMax(MessageEdit msg) {
try {
List<MessageEdit> msglist = (List<MessageEdit>) this.getMessages(null, false);
int currentMax = 0;
for (MessageEdit me:msglist) {
if (me.getHeaderEdit().getMessage_order()>currentMax)
currentMax = me.getHeaderEdit().getMessage_order();
}
msg.getHeaderEdit().setMessage_order(currentMax+1);
} catch (PermissionException ex) {
M_log.error(ex);
}
}

} // class BaseAnnouncementChannelEdit

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3025,53 +3025,6 @@ else if ("n".equals(notification))
// announcement title changed
titleChanged = true;
}

//set the order of the announcement messages
// for example, if this was MESSAGE_ORDER=5 and now becomes MESSAGE_ORDER=12,
// we should be modifying the database records for 5-12 and decrementing their message order
int oldMessageOrder = header.getMessage_order();
List<Message> channelMessages = channel.getMessages(null, true); // ascending order
//need to clear the message cache otherwise channel.getMessages stores the old (unsaved) message in cache
AnnouncementService.clearMessagesCache(channel.getReference());


// sort the messages by current sort order
SortedIterator messSorted = new SortedIterator(channelMessages.iterator(), new AnnouncementComparator(getCurrentOrder(), true));

int runningCount = 1;
while (messSorted.hasNext()) {
AnnouncementMessageEdit ame = (AnnouncementMessageEdit) messSorted.next();
AnnouncementMessageHeaderEdit amhe = ame.getAnnouncementHeaderEdit();
int currentOrder = amhe.getMessage_order();

// do not attempt to double modify our existing message
// skipping will also make sure our runningCount does *not* increment
if (ame.getId().equals(msg.getId())) {
continue;
}

// only edit the message if we need to modify the order
if (currentOrder != runningCount) {
amhe.setMessage_order(runningCount);
channel.commitMessage_order(ame);

if (M_log.isDebugEnabled()) {
M_log.debug("postOrSaveDraft modifying order: " + ame.getId() + ":"
+ currentOrder + ":" + runningCount + ":" + oldMessageOrder);
}
}

// this max should be correct because we just went through all messages in channel in ascending order
runningCount++;
}

// set the message order for our current message to be max
header.setMessage_order(runningCount);

if (M_log.isDebugEnabled()) {
M_log.debug("postOrSaveDraft set message order for " + msg.getId() + " to running count " + runningCount);
}

// header.setDraft(!post);
// v2.4: Hidden in UI becomes Draft 'behind the scenes'
boolean oDraft = header.getDraft();
Expand Down

0 comments on commit 0d5971f

Please sign in to comment.