com.foreach.across.modules.entity.handlers.EntityModuleAdminMenuRegistrar Maven / Gradle / Ivy
/*
* Copyright 2014 the original author or authors
*
* 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 com.foreach.across.modules.entity.handlers;
import com.foreach.across.core.context.info.AcrossModuleInfo;
import com.foreach.across.modules.adminweb.menu.AdminMenuEvent;
import com.foreach.across.modules.bootstrapui.components.builder.NavComponentBuilder;
import com.foreach.across.modules.bootstrapui.styles.AcrossBootstrapStyles;
import com.foreach.across.modules.entity.EntityAttributes;
import com.foreach.across.modules.entity.conditionals.ConditionalOnAdminWeb;
import com.foreach.across.modules.entity.registry.EntityAssociation;
import com.foreach.across.modules.entity.registry.EntityConfiguration;
import com.foreach.across.modules.entity.registry.EntityRegistry;
import com.foreach.across.modules.entity.registry.EntityViewRegistry;
import com.foreach.across.modules.entity.support.EntityMessageCodeResolver;
import com.foreach.across.modules.entity.views.EntityView;
import com.foreach.across.modules.entity.views.EntityViewFactoryAttributes;
import com.foreach.across.modules.entity.views.context.EntityViewContext;
import com.foreach.across.modules.entity.views.menu.EntityAdminMenuEvent;
import com.foreach.across.modules.entity.views.request.EntityViewRequest;
import com.foreach.across.modules.entity.views.support.EntityMessages;
import com.foreach.across.modules.entity.web.links.EntityViewLinkBuilder;
import com.foreach.across.modules.entity.web.links.SingleEntityViewLinkBuilder;
import com.foreach.across.modules.spring.security.actions.AllowableAction;
import com.foreach.across.modules.spring.security.actions.AllowableActions;
import com.foreach.across.modules.web.menu.PathBasedMenuBuilder;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.context.event.EventListener;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import java.util.function.Consumer;
import static com.foreach.across.modules.bootstrapui.components.builder.NavComponentBuilder.customizeViewElement;
import static com.foreach.across.modules.entity.EntityModuleIcons.entityModuleIcons;
@ConditionalOnAdminWeb
@Component
@Slf4j
@RequiredArgsConstructor
class EntityModuleAdminMenuRegistrar
{
private final EntityRegistry entityRegistry;
private final EntityViewRequest entityViewRequest;
@EventListener
public void adminMenu( AdminMenuEvent adminMenuEvent ) {
PathBasedMenuBuilder builder = adminMenuEvent.builder();
builder.group( "/entities", "#{EntityModule.adminMenu=Entity management}" );
for ( EntityConfiguration entityConfiguration : entityRegistry.getEntities() ) {
AllowableActions allowableActions = entityConfiguration.getAllowableActions();
if ( !entityConfiguration.isHidden() && allowableActions.contains( AllowableAction.READ ) ) {
EntityMessageCodeResolver messageCodeResolver = entityConfiguration.getEntityMessageCodeResolver();
Assert.notNull(
messageCodeResolver, () ->
"A visible EntityConfiguration (" + entityConfiguration.getName() + ") requires an EntityMessageCodeResolver"
);
EntityMessages messages = new EntityMessages( messageCodeResolver );
EntityViewLinkBuilder linkBuilder = entityConfiguration.getAttribute( EntityViewLinkBuilder.class );
AcrossModuleInfo moduleInfo = entityConfiguration.getAttribute( AcrossModuleInfo.class );
if ( linkBuilder != null ) {
String group = "/entities";
if ( moduleInfo != null ) {
group = "/entities/" + moduleInfo.getName();
builder.group( group, "#{" + moduleInfo.getName() + ".adminMenu=" + moduleInfo.getName() + "}" );
}
builder.item( group + "/" + entityConfiguration.getName(),
messageCodeResolver.getMessageWithFallback( "adminMenu", messageCodeResolver.getNameSingular() ),
linkBuilder.listView().toString() );
if ( allowableActions.contains( AllowableAction.CREATE ) ) {
builder.item( group + "/" + entityConfiguration.getName() + "/create",
messages.createAction(),
linkBuilder.createView().toString()
);
}
}
else {
LOG.trace( "Not showing entity {} - not hidden but no EntityLinkBuilder", entityConfiguration.getName() );
}
}
}
}
@EventListener
@SuppressWarnings("unchecked")
public void entityMenu( EntityAdminMenuEvent menu ) {
PathBasedMenuBuilder builder = menu.builder();
boolean isAssociation = menu.getViewContext().isForAssociation();
EntityConfiguration
© 2015 - 2024 Weber Informatics LLC | Privacy Policy