MenuItemServiceImpl.java
/*
* Copyright 2024 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.genesys.server.service.impl;
import org.apache.commons.lang3.StringUtils;
import org.genesys.server.model.impl.Menu;
import org.genesys.server.model.impl.MenuItem;
import org.genesys.server.model.impl.MenuItemLang;
import org.genesys.server.persistence.MenuItemLangRepository;
import org.genesys.server.persistence.MenuItemRepository;
import org.genesys.server.persistence.MenuRepository;
import org.genesys.server.service.MenuItemService;
import org.genesys.server.service.MenuItemTranslationService;
import org.genesys.server.service.TranslatorService;
import org.genesys.server.service.filter.MenuItemFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
@Service
@Transactional(readOnly = true)
@Validated
public class MenuItemServiceImpl
extends FilteredTranslatedCRUDServiceImpl<MenuItem, MenuItemLang, MenuItemTranslationService.TranslatedMenuItem, MenuItemFilter, MenuItemRepository>
implements MenuItemService {
@Autowired
private MenuRepository menuRepository;
@Component(value = "MenuItemTranslationSupport")
protected static class MenuItemTranslationSupport
extends BaseTranslationSupport<MenuItem, MenuItemLang, MenuItemTranslationService.TranslatedMenuItem, MenuItemFilter, MenuItemLangRepository>
implements MenuItemTranslationService {
@Override
@PreAuthorize("hasRole('ADMINISTRATOR') or hasRole('CONTENTMANAGER')")
public MenuItemLang create(MenuItemLang source) {
return super.create(source);
}
@Override
@PreAuthorize("hasRole('ADMINISTRATOR') or hasRole('CONTENTMANAGER')")
public MenuItemLang update(MenuItemLang updated, MenuItemLang target) {
return super.update(updated, target);
}
@Override
@PreAuthorize("hasRole('ADMINISTRATOR') or hasRole('CONTENTMANAGER')")
public MenuItemLang remove(MenuItemLang entity) {
return super.remove(entity);
}
}
/**
* Ensure the CMS menu item exists in the menu
*
* @return
*/
@Override
@Transactional
@PreAuthorize("hasRole('ADMINISTRATOR') or hasRole('CONTENTMANAGER')")
@CacheEvict(value = "contentcache", allEntries = true)
public MenuItem ensureMenuItem(String menuKey, MenuItem source) {
Menu menu = menuRepository.findByKey(menuKey);
if (menu == null) {
// Create instance
menu = new Menu();
menu.setKey(menuKey);
menu = menuRepository.save(menu);
}
for (MenuItem item : menu.getItems()) {
if (StringUtils.equals(item.getUrl(), source.getUrl())) {
LOG.debug("Found menu item with URL {}", source.getUrl());
return item;
}
}
LOG.info("Creating menu item with URL {}", source.getUrl());
MenuItem item = new MenuItem();
item.setMenu(menu);
item.setUrl(source.getUrl());
item.setText(source.getText());
item.setOrderIndex(source.getOrderIndex());
item.setTarget(source.getTarget());
item.setTitle(source.getTitle());
item.setActive(source.isActive());
item.setOriginalLanguageTag(source.getOriginalLanguageTag());
menu.getItems().add(item);
return repository.save(item);
}
@Override
public Menu getMenu(String key) {
var menu = menuRepository.findByKey(key);
if (menu != null) {
menu.getItems().size();
}
return menu;
}
@Override
@Transactional
@PreAuthorize("hasRole('ADMINISTRATOR') or hasRole('CONTENTMANAGER')")
@CacheEvict(value = "contentcache", allEntries = true)
public MenuItem remove(MenuItem menuItem) {
final MenuItem loaded = get(menuItem.getId());
repository.delete(loaded);
final Menu menu = loaded.getMenu();
if (menu != null && !repository.existsMenuItemsByMenu(menu)) {
LOG.info("Deleting CMS menu {}", menu.getKey());
menuRepository.delete(menu);
}
return loaded;
}
@Override
public MenuItemLang machineTranslate(MenuItem original, String targetLanguage) throws TranslatorService.TranslatorException {
// TODO
return new MenuItemLang(original, targetLanguage, null, null);
}
@Override
@Transactional
@PreAuthorize("hasRole('ADMINISTRATOR') or hasRole('CONTENTMANAGER')")
@CacheEvict(value = "contentcache", allEntries = true)
public MenuItem create(MenuItem source) {
return _lazyLoad(createFast(source));
}
@Override
@Transactional
@PreAuthorize("hasRole('ADMINISTRATOR') or hasRole('CONTENTMANAGER')")
@CacheEvict(value = "contentcache", allEntries = true)
public MenuItem createFast(MenuItem source) {
MenuItem menuItem = new MenuItem();
menuItem.apply(source);
var menu = source.getMenu();
if (menu == null) {
throw new IllegalArgumentException("MenuItem must have menu");
}
menuItem.setMenu(menuRepository.getReferenceById(menu.getId()));
return repository.save(menuItem);
}
@Override
@Transactional
@PreAuthorize("hasRole('ADMINISTRATOR') or hasRole('CONTENTMANAGER')")
@CacheEvict(value = "contentcache", allEntries = true)
public MenuItem update(MenuItem updated, MenuItem target) {
return updateFast(updated, target);
}
@Override
@Transactional
@PreAuthorize("hasRole('ADMINISTRATOR') or hasRole('CONTENTMANAGER')")
@CacheEvict(value = "contentcache", allEntries = true)
public MenuItem updateFast(MenuItem updated, MenuItem target) {
target.apply(updated);
return repository.save(target);
}
}