Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SAK-50898 Lessons predictable ordering when adding multiple content links #13232

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@
import java.util.function.Predicate;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
* Backing bean for Simple pages
Expand Down Expand Up @@ -6738,6 +6740,28 @@ public void addMultimedia() {
if (!checkCsrf())
return;

// For the sole case of adding multimedia as content links above another item
// (i.e., not the Add Content button at the top of the Lessons page, nor the Add Content '+'
// button at the bottom of a section), the items below the newly inserted content links
// need to have their sequence values revised, especially if there is more than one item to insert.
// First, collect these items in the itemsToFix list before inserting the new content links (which
// affects item sequencing for the page). After inserting the new content links, revise the sequence
// values for itemsToFix.
List<SimplePageItem> itemsToFix = null;
boolean fixItemSequence = StringUtils.isNotEmpty(addBefore) && !StringUtils.startsWith(addBefore, "-");
if (fixItemSequence) {
List<SimplePageItem> items = getItemsOnPage(getCurrentPageId());
items.sort(Comparator.comparing(SimplePageItem::getSequence));
long addBeforeItemId = Long.valueOf(addBefore);
int addBeforeIndex = IntStream.range(0, items.size())
.filter(i -> addBeforeItemId == items.get(i).getId())
.findFirst()
.orElse(-1);
if (addBeforeIndex >= 0) {
itemsToFix = items.stream().skip(addBeforeIndex).collect(Collectors.toList());
}
}

// SAK-41846 - Initialize counters to keep track of files to add and the item sequence values to adjust
totalMultimediaFilesToAdd = multipartMap.size();
remainingMultimediaFilesToAdd = totalMultimediaFilesToAdd;
Expand All @@ -6763,6 +6787,14 @@ public void addMultimedia() {
fileindex++;
}
}

if (fixItemSequence) {
for (SimplePageItem itemToFix : itemsToFix) {
itemToFix.setSequence(itemToFix.getSequence() + totalMultimediaFilesToAdd);
update(itemToFix);
}
}

} catch (Exception exception) {
log.error(exception.getMessage(), exception);
} finally {
Expand All @@ -6772,7 +6804,6 @@ public void addMultimedia() {
totalMultimediaFilesToAdd = 0;
remainingMultimediaFilesToAdd = 0;
}

}

public void addMultimediaFile(MultipartFile file, String name){
Expand Down
Loading