
org.shredzone.commons.view.manager.ViewManager Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of commons-view Show documentation
Show all versions of commons-view Show documentation
Shredzone Commons: Enhanced Spring Views
The newest version!
/*
* Shredzone Commons
*
* Copyright (C) 2012 Richard "Shred" Körber
* http://commons.shredzone.org
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this program. If not, see .
*/
package org.shredzone.commons.view.manager;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import org.shredzone.commons.view.Signature;
import org.shredzone.commons.view.annotation.View;
import org.shredzone.commons.view.annotation.ViewGroup;
import org.shredzone.commons.view.annotation.ViewHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.core.convert.ConversionService;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
/**
* Manages the view handlers.
*
* @author Richard "Shred" Körber
*/
@Component
@ParametersAreNonnullByDefault
public class ViewManager {
private final Logger log = LoggerFactory.getLogger(this.getClass());
@Resource private ApplicationContext applicationContext;
@Resource private ConversionService conversionService;
private Map>> patternMap = new HashMap<>();
private Map> signatureMap = new HashMap<>();
private List patternOrder = new ArrayList<>();
/**
* Returns a collection of all defined {@link ViewPattern}.
*
* @return Collection of matching {@link ViewPattern}
*/
public @Nonnull Collection getViewPatterns() {
return Collections.unmodifiableCollection(patternOrder);
}
/**
* Returns a collection of {@link ViewPattern} that were defined for the given view.
*
* @param view
* View name
* @param qualifier
* Qualifier name, or {@code null}
* @return Collection of matching {@link ViewPattern}, empty if there is no such view
*/
public @Nonnull Collection getViewPatternsForView(String view, @Nullable String qualifier) {
Map> viewMap = patternMap.get(view);
if (viewMap != null) {
List result = viewMap.get(qualifier);
if (result != null) {
return Collections.unmodifiableCollection(result);
}
}
return Collections.emptyList();
}
/**
* Returns the {@link ViewPattern} that handles the given {@link Signature}.
*
* @param signature
* {@link Signature} to find a {@link ViewPattern} for
* @param qualifier
* Qualifier name, or {@code null}
* @return {@link ViewPattern} found, or {@code null} if there is no such
* {@link ViewPattern}
*/
public ViewPattern getViewPatternForSignature(Signature signature, @Nullable String qualifier) {
Map sigMap = signatureMap.get(qualifier);
if (sigMap != null) {
return sigMap.get(signature);
}
return null;
}
/**
* Sets up the view manager. All Spring beans are searched for {@link ViewHandler}
* annotations.
*/
@PostConstruct
protected void setup() {
Collection
© 2015 - 2025 Weber Informatics LLC | Privacy Policy