/gist:434207301d38a132ddee Secret
Created
July 21, 2015 09:18
Star
You must be signed in to star a gist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package info.magnolia.ui.framework.availability; | |
| import info.magnolia.config.registry.DefinitionProvider; | |
| import info.magnolia.config.registry.Registry; | |
| import info.magnolia.config.registry.RegistryFacade; | |
| import info.magnolia.jcr.util.NodeUtil; | |
| import info.magnolia.jcr.util.SessionUtil; | |
| import info.magnolia.ui.api.availability.AbstractAvailabilityRule; | |
| import info.magnolia.ui.vaadin.integration.jcr.JcrItemId; | |
| import info.magnolia.ui.vaadin.integration.jcr.JcrPropertyItemId; | |
| import java.util.Collection; | |
| import javax.inject.Inject; | |
| import javax.jcr.Node; | |
| import org.slf4j.Logger; | |
| import org.slf4j.LoggerFactory; | |
| /** | |
| * This returns {@code true} if the item has {@link DefinitionProvider}. | |
| */ | |
| public class IsDefinitionRule extends AbstractAvailabilityRule { | |
| private static final Logger log = LoggerFactory.getLogger(IsDefinitionRule.class); | |
| private final RegistryFacade registryFacade; | |
| @Inject | |
| public IsDefinitionRule(RegistryFacade registryFacade) { | |
| this.registryFacade = registryFacade; | |
| } | |
| @Override | |
| protected boolean isAvailableForItem(Object itemId) { | |
| if (itemId instanceof JcrItemId && !(itemId instanceof JcrPropertyItemId)) { | |
| JcrItemId jcrItemId = (JcrItemId) itemId; | |
| Node node = SessionUtil.getNodeByIdentifier(jcrItemId.getWorkspace(), jcrItemId.getUuid()); | |
| if (node != null) { | |
| try { | |
| String nodePath = node.getPath(); | |
| return hasDefinitionFor(nodePath); | |
| } catch (Exception e) { | |
| log.warn("Error evaluating availability for node [{}], returning false: {}", NodeUtil.getPathIfPossible(node), e.getMessage()); | |
| } | |
| } | |
| } | |
| return false; | |
| } | |
| @SuppressWarnings("unchecked") | |
| private boolean hasDefinitionFor(String nodePath) { | |
| for (Registry registry : registryFacade.all()) { | |
| Collection<DefinitionProvider> providers = registry.getAllProviders(); | |
| for (DefinitionProvider provider : providers) { | |
| if (provider.getMetadata().getLocation().equals(nodePath)) { | |
| return true; | |
| } | |
| } | |
| } | |
| return false; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment