More user friendly methods for getting template for ThemesMvc
parent
4a5b38f42e
commit
32d5eed14d
|
|
@ -92,11 +92,15 @@ public class ThemeManifest implements Serializable {
|
|||
private String defaultTemplate;
|
||||
|
||||
@XmlElement(name = "mvc-templates", namespace = THEMES_XML_NS)
|
||||
private Map<String, Map<String, ThemeTemplate>> mvcTemplates;
|
||||
private Map<String, ThemeTemplate> mvcTemplates;
|
||||
|
||||
@XmlElement(name = "views", namespace = THEMES_XML_NS)
|
||||
private Map<String, Map<String, String>> views;
|
||||
|
||||
public ThemeManifest() {
|
||||
templates = new ArrayList<>();
|
||||
mvcTemplates = new HashMap<>();
|
||||
views = new HashMap<>();
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
|
|
@ -163,65 +167,62 @@ public class ThemeManifest implements Serializable {
|
|||
this.defaultTemplate = defaultTemplate;
|
||||
}
|
||||
|
||||
public Map<String, Map<String, ThemeTemplate>> getMvcTemplates() {
|
||||
public Map<String, ThemeTemplate> getMvcTemplates() {
|
||||
return Collections.unmodifiableMap(mvcTemplates);
|
||||
}
|
||||
|
||||
public Optional<Map<String, ThemeTemplate>> getMvcTemplatesOfCategory(
|
||||
final String category
|
||||
) {
|
||||
return Optional.ofNullable(mvcTemplates.get(category));
|
||||
}
|
||||
|
||||
public void addMvcTemplatesCategory(final String category) {
|
||||
mvcTemplates.put(category, new HashMap<>());
|
||||
}
|
||||
|
||||
public void addMvcTemplatesCategory(
|
||||
final String category, final Map<String, ThemeTemplate> templates
|
||||
) {
|
||||
mvcTemplates.put(category, templates);
|
||||
}
|
||||
|
||||
public Optional<ThemeTemplate> getMvcTemplate(
|
||||
final String category, final String objectType
|
||||
) {
|
||||
final Optional<Map<String, ThemeTemplate>> templatesInCat
|
||||
= getMvcTemplatesOfCategory(category);
|
||||
|
||||
if (templatesInCat.isPresent()) {
|
||||
return Optional.ofNullable(templatesInCat.get().get(objectType));
|
||||
} else {
|
||||
return Optional.empty();
|
||||
}
|
||||
public Optional<ThemeTemplate> getMvcTemplate(final String name) {
|
||||
return Optional.ofNullable(mvcTemplates.get(name));
|
||||
}
|
||||
|
||||
public void addMvcTemplate(
|
||||
final String category,
|
||||
final String objectType,
|
||||
final ThemeTemplate template
|
||||
final String name, final ThemeTemplate template
|
||||
) {
|
||||
if (!mvcTemplates.containsKey(category)) {
|
||||
addMvcTemplatesCategory(category);
|
||||
}
|
||||
|
||||
mvcTemplates
|
||||
.get(category)
|
||||
.put(
|
||||
objectType,
|
||||
Objects.requireNonNull(
|
||||
template,
|
||||
"Template can't be null."
|
||||
)
|
||||
);
|
||||
mvcTemplates.put(name, template);
|
||||
}
|
||||
|
||||
protected void setMvcTemplates(
|
||||
final Map<String, Map<String, ThemeTemplate>> mvcTemplates
|
||||
final Map<String, ThemeTemplate> mvcTemplates
|
||||
) {
|
||||
this.mvcTemplates = mvcTemplates;
|
||||
}
|
||||
|
||||
public Map<String, Map<String, String>> getViews() {
|
||||
return Collections.unmodifiableMap(views);
|
||||
}
|
||||
|
||||
public Map<String, String> getViewsOfApplication(final String application) {
|
||||
if (views.containsKey(application)) {
|
||||
return views.get(application);
|
||||
} else {
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
}
|
||||
|
||||
public void addViewsOfApplication(
|
||||
final String application, final Map<String, String> viewsOfApplication
|
||||
) {
|
||||
views.put(application, viewsOfApplication);
|
||||
}
|
||||
|
||||
public void addViewToApplication(
|
||||
final String application, final String view, final String templateName
|
||||
) {
|
||||
final Map<String, String> applicationViews;
|
||||
if (views.containsKey(application)) {
|
||||
applicationViews = views.get(application);
|
||||
} else {
|
||||
applicationViews = new HashMap<>();
|
||||
views.put(application, applicationViews);
|
||||
}
|
||||
|
||||
applicationViews.put(view, templateName);
|
||||
}
|
||||
|
||||
protected void setViews(final Map<String, Map<String, String>> views) {
|
||||
this.views = new HashMap<>(views);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int hash = 7;
|
||||
|
|
@ -233,6 +234,7 @@ public class ThemeManifest implements Serializable {
|
|||
hash = 83 * hash + Objects.hashCode(templates);
|
||||
hash = 83 * hash + Objects.hashCode(defaultTemplate);
|
||||
hash = 83 * hash + Objects.hashCode(mvcTemplates);
|
||||
hash = 83 * hash + Objects.hashCode(views);
|
||||
return hash;
|
||||
}
|
||||
|
||||
|
|
@ -272,7 +274,11 @@ public class ThemeManifest implements Serializable {
|
|||
if (!Objects.equals(defaultTemplate, other.getDefaultTemplate())) {
|
||||
return false;
|
||||
}
|
||||
return mvcTemplates.equals(other.getMvcTemplates());
|
||||
if (!Objects.equals(mvcTemplates, other.getMvcTemplates())) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return Objects.equals(views, other.getViews());
|
||||
}
|
||||
|
||||
public boolean canEqual(final Object obj) {
|
||||
|
|
@ -295,7 +301,8 @@ public class ThemeManifest implements Serializable {
|
|||
+ "description = \"%s\", "
|
||||
+ "templates = %s, "
|
||||
+ "defaultTemplate, "
|
||||
+ "mvcTemplates = %s%s"
|
||||
+ "mvcTemplates = %s,"
|
||||
+ "views = %s%s"
|
||||
+ " }",
|
||||
super.toString(),
|
||||
name,
|
||||
|
|
@ -306,6 +313,7 @@ public class ThemeManifest implements Serializable {
|
|||
Objects.toString(templates),
|
||||
defaultTemplate,
|
||||
Objects.toString(mvcTemplates),
|
||||
Objects.toString(views),
|
||||
data
|
||||
);
|
||||
|
||||
|
|
|
|||
|
|
@ -18,6 +18,7 @@
|
|||
*/
|
||||
package org.libreccm.theming.mvc;
|
||||
|
||||
import org.libreccm.core.UnexpectedErrorException;
|
||||
import org.libreccm.sites.Site;
|
||||
import org.libreccm.sites.SiteRepository;
|
||||
import org.libreccm.theming.ThemeInfo;
|
||||
|
|
@ -61,7 +62,8 @@ public class ThemesMvc {
|
|||
|
||||
public String getMvcTemplate(
|
||||
final UriInfo uriInfo,
|
||||
final String application
|
||||
final String application,
|
||||
final String view
|
||||
) {
|
||||
final Site site = getSite(uriInfo);
|
||||
final String theme = parseThemeParam(uriInfo);
|
||||
|
|
@ -72,35 +74,46 @@ public class ThemesMvc {
|
|||
themeVersion
|
||||
);
|
||||
final ThemeManifest manifest = themeInfo.getManifest();
|
||||
final Map<String, ThemeTemplate> applicationTemplates = manifest
|
||||
.getMvcTemplatesOfCategory("applications")
|
||||
.orElseThrow(
|
||||
() -> new WebApplicationException(
|
||||
String.format(
|
||||
"Manifest of theme %s has no application templates.",
|
||||
themeInfo.getName()
|
||||
),
|
||||
Response.Status.INTERNAL_SERVER_ERROR
|
||||
)
|
||||
);
|
||||
final ThemeTemplate themeTemplate;
|
||||
if (applicationTemplates.containsKey(application)) {
|
||||
themeTemplate = applicationTemplates.get(application);
|
||||
} else {
|
||||
themeTemplate = Optional.ofNullable(
|
||||
applicationTemplates.get("@default")
|
||||
).orElseThrow(
|
||||
() -> new WebApplicationException(
|
||||
String.format(
|
||||
"Theme %s does not provide a template for application "
|
||||
+ "%s and has not default template for "
|
||||
+ "applications.",
|
||||
theme,
|
||||
final Map<String, String> views = manifest.getViewsOfApplication(
|
||||
application
|
||||
);
|
||||
final String viewTemplateName;
|
||||
if (views.containsKey(view)) {
|
||||
viewTemplateName = views.get(view);
|
||||
} else {
|
||||
final Map<String, String> defaultAppViews = manifest
|
||||
.getViewsOfApplication(application);
|
||||
if (defaultAppViews.containsKey("default")) {
|
||||
viewTemplateName = defaultAppViews.get("default");
|
||||
} else {
|
||||
throw new WebApplicationException(
|
||||
String.format(
|
||||
"Theme \"%s\" does not provide a template for view "
|
||||
+ "\"%s\" of application \"%s\", and there is no "
|
||||
+ "default template configured.",
|
||||
themeInfo.getName(),
|
||||
view,
|
||||
application
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
final ThemeTemplate themeTemplate = manifest
|
||||
.getMvcTemplate(viewTemplateName)
|
||||
.orElseThrow(
|
||||
() -> new WebApplicationException(
|
||||
String.format(
|
||||
"Theme \"%s\" maps view \"%s\" of application \"%s\" "
|
||||
+ "to template \"%s\" but not template with this "
|
||||
+ "name was found in the theme.",
|
||||
themeInfo.getName(),
|
||||
view,
|
||||
application,
|
||||
viewTemplateName
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
models.put("contextPath", servletContext.getContextPath());
|
||||
models.put("themeName", themeInfo.getName());
|
||||
|
|
|
|||
|
|
@ -96,7 +96,7 @@ public class LoginController {
|
|||
models.put("loginFailed", false);
|
||||
}
|
||||
models.put("returnUrl", returnUrl);
|
||||
return themesMvc.getMvcTemplate(uriInfo, "login-form");
|
||||
return themesMvc.getMvcTemplate(uriInfo, "login", "loginForm");
|
||||
}
|
||||
|
||||
@POST
|
||||
|
|
@ -141,7 +141,7 @@ public class LoginController {
|
|||
@GET
|
||||
@Path("/recover-password")
|
||||
public String getRecoverPasswordForm(@Context final UriInfo uriInfo) {
|
||||
return themesMvc.getMvcTemplate(uriInfo, "login-recover-password");
|
||||
return themesMvc.getMvcTemplate(uriInfo, "login", "recoverPassword");
|
||||
}
|
||||
|
||||
@POST
|
||||
|
|
@ -160,7 +160,7 @@ public class LoginController {
|
|||
}
|
||||
}
|
||||
|
||||
return themesMvc.getMvcTemplate(uriInfo, "login-password-recovered");
|
||||
return themesMvc.getMvcTemplate(uriInfo, "login", "passwordRecovered");
|
||||
}
|
||||
|
||||
private boolean isEmailPrimaryIdentifier() {
|
||||
|
|
|
|||
|
|
@ -49,7 +49,7 @@ public class LogoutController {
|
|||
public String logout(@Context final UriInfo uriInfo) {
|
||||
subject.logout();
|
||||
|
||||
return themesMvc.getMvcTemplate(uriInfo, "logout");
|
||||
return themesMvc.getMvcTemplate(uriInfo, "logout", "loggedout");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,7 +5,30 @@
|
|||
"default-template": "category-page.html.ftl",
|
||||
|
||||
"mvc-templates": {
|
||||
"applications": {
|
||||
"category-page": {
|
||||
"description": {
|
||||
"values": {
|
||||
"value": [
|
||||
{
|
||||
"lang": "en",
|
||||
"value": "Category Page Template"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"name": "Category Page",
|
||||
"path": "categoryPage.html.ftl",
|
||||
"title": {
|
||||
"values": {
|
||||
"value": [
|
||||
{
|
||||
"lang": "en",
|
||||
"value": "Category Page"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
"login-form": {
|
||||
"description": {
|
||||
"values": {
|
||||
|
|
@ -78,7 +101,7 @@
|
|||
}
|
||||
}
|
||||
},
|
||||
"logout": {
|
||||
"loggedout": {
|
||||
"description": {
|
||||
"values": {
|
||||
"value": [
|
||||
|
|
@ -102,6 +125,18 @@
|
|||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"views": {
|
||||
"default": {
|
||||
"default": "category-page"
|
||||
},
|
||||
"login": {
|
||||
"loginForm": "login-form",
|
||||
"passwordRecovered": "login-password-recovered",
|
||||
"recoverPassword": "login-recover-password"
|
||||
},
|
||||
"logout": {
|
||||
"loggedout": "loggedout"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue