Skip to content

Instantly share code, notes, and snippets.

@pedrosan7os
Created December 1, 2014 17:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pedrosan7os/ab26aae84cf7b59c165e to your computer and use it in GitHub Desktop.
Save pedrosan7os/ab26aae84cf7b59c165e to your computer and use it in GitHub Desktop.
CleanupDeletedMenus
package pt.ist.fenix.webapp;
import java.util.Optional;
import org.fenixedu.bennu.portal.domain.MenuContainer;
import org.fenixedu.bennu.portal.domain.MenuFunctionality;
import org.fenixedu.bennu.portal.domain.MenuItem;
import org.fenixedu.bennu.portal.domain.PortalConfiguration;
import org.fenixedu.bennu.portal.model.Application;
import org.fenixedu.bennu.portal.model.ApplicationRegistry;
import org.fenixedu.bennu.portal.model.Functionality;
import org.fenixedu.bennu.scheduler.custom.CustomTask;
public class CleanupDeletedMenus extends CustomTask {
@Override
public void runTask() throws Exception {
MenuContainer menu = PortalConfiguration.getInstance().getMenu();
cleanup(menu);
}
private void cleanup(MenuContainer menu) {
for (MenuItem item : menu.getChildSet()) {
if (item instanceof MenuContainer) {
cleanup(item.getAsMenuContainer());
} else {
cleanup(item.getAsMenuFunctionality());
}
}
if (menu.getChildSet().isEmpty()) {
menu.delete();
taskLog("Deleted empty container: %s", menu.getFullPath());
}
}
private void cleanup(MenuFunctionality item) {
Functionality found = null;
for (Application application : ApplicationRegistry.availableApplications()) {
Optional<Functionality> functionality =
application.getFunctionalities().stream()
.filter(f -> f.getProvider().equals(item.getProvider()) && f.getKey().equals(item.getItemKey()))
.findAny();
if (functionality.isPresent()) {
found = functionality.get();
}
}
if (found == null) {
if (item.getProvider().equals("old-cms") || item.getProvider().equals("struts")) {
taskLog("Deleted %s: %s (%s)\n", item.getFullPath(), item.getItemKey(), item.getProvider());
item.delete();
} else {
taskLog("Not found %s: %s (%s)\n", item.getFullPath(), item.getItemKey(), item.getProvider());
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment