More user friendly methods for getting template for ThemesMvc

ccm-docs
Jens Pelzetter 2021-01-06 19:27:44 +01:00
parent 4a5b38f42e
commit 32d5eed14d
5 changed files with 225 additions and 169 deletions

View File

@ -92,11 +92,15 @@ public class ThemeManifest implements Serializable {
private String defaultTemplate; private String defaultTemplate;
@XmlElement(name = "mvc-templates", namespace = THEMES_XML_NS) @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() { public ThemeManifest() {
templates = new ArrayList<>(); templates = new ArrayList<>();
mvcTemplates = new HashMap<>(); mvcTemplates = new HashMap<>();
views = new HashMap<>();
} }
public String getName() { public String getName() {
@ -163,65 +167,62 @@ public class ThemeManifest implements Serializable {
this.defaultTemplate = defaultTemplate; this.defaultTemplate = defaultTemplate;
} }
public Map<String, Map<String, ThemeTemplate>> getMvcTemplates() { public Map<String, ThemeTemplate> getMvcTemplates() {
return Collections.unmodifiableMap(mvcTemplates); return Collections.unmodifiableMap(mvcTemplates);
} }
public Optional<Map<String, ThemeTemplate>> getMvcTemplatesOfCategory( public Optional<ThemeTemplate> getMvcTemplate(final String name) {
final String category return Optional.ofNullable(mvcTemplates.get(name));
) {
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 void addMvcTemplate( public void addMvcTemplate(
final String category, final String name, final ThemeTemplate template
final String objectType,
final ThemeTemplate template
) { ) {
if (!mvcTemplates.containsKey(category)) { mvcTemplates.put(name, template);
addMvcTemplatesCategory(category);
}
mvcTemplates
.get(category)
.put(
objectType,
Objects.requireNonNull(
template,
"Template can't be null."
)
);
} }
protected void setMvcTemplates( protected void setMvcTemplates(
final Map<String, Map<String, ThemeTemplate>> mvcTemplates final Map<String, ThemeTemplate> mvcTemplates
) { ) {
this.mvcTemplates = 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 @Override
public int hashCode() { public int hashCode() {
int hash = 7; int hash = 7;
@ -233,6 +234,7 @@ public class ThemeManifest implements Serializable {
hash = 83 * hash + Objects.hashCode(templates); hash = 83 * hash + Objects.hashCode(templates);
hash = 83 * hash + Objects.hashCode(defaultTemplate); hash = 83 * hash + Objects.hashCode(defaultTemplate);
hash = 83 * hash + Objects.hashCode(mvcTemplates); hash = 83 * hash + Objects.hashCode(mvcTemplates);
hash = 83 * hash + Objects.hashCode(views);
return hash; return hash;
} }
@ -272,7 +274,11 @@ public class ThemeManifest implements Serializable {
if (!Objects.equals(defaultTemplate, other.getDefaultTemplate())) { if (!Objects.equals(defaultTemplate, other.getDefaultTemplate())) {
return false; 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) { public boolean canEqual(final Object obj) {
@ -295,7 +301,8 @@ public class ThemeManifest implements Serializable {
+ "description = \"%s\", " + "description = \"%s\", "
+ "templates = %s, " + "templates = %s, "
+ "defaultTemplate, " + "defaultTemplate, "
+ "mvcTemplates = %s%s" + "mvcTemplates = %s,"
+ "views = %s%s"
+ " }", + " }",
super.toString(), super.toString(),
name, name,
@ -306,6 +313,7 @@ public class ThemeManifest implements Serializable {
Objects.toString(templates), Objects.toString(templates),
defaultTemplate, defaultTemplate,
Objects.toString(mvcTemplates), Objects.toString(mvcTemplates),
Objects.toString(views),
data data
); );

View File

@ -18,6 +18,7 @@
*/ */
package org.libreccm.theming.mvc; package org.libreccm.theming.mvc;
import org.libreccm.core.UnexpectedErrorException;
import org.libreccm.sites.Site; import org.libreccm.sites.Site;
import org.libreccm.sites.SiteRepository; import org.libreccm.sites.SiteRepository;
import org.libreccm.theming.ThemeInfo; import org.libreccm.theming.ThemeInfo;
@ -61,7 +62,8 @@ public class ThemesMvc {
public String getMvcTemplate( public String getMvcTemplate(
final UriInfo uriInfo, final UriInfo uriInfo,
final String application final String application,
final String view
) { ) {
final Site site = getSite(uriInfo); final Site site = getSite(uriInfo);
final String theme = parseThemeParam(uriInfo); final String theme = parseThemeParam(uriInfo);
@ -72,35 +74,46 @@ public class ThemesMvc {
themeVersion themeVersion
); );
final ThemeManifest manifest = themeInfo.getManifest(); final ThemeManifest manifest = themeInfo.getManifest();
final Map<String, ThemeTemplate> applicationTemplates = manifest final Map<String, String> views = manifest.getViewsOfApplication(
.getMvcTemplatesOfCategory("applications") application
.orElseThrow( );
() -> new WebApplicationException( final String viewTemplateName;
String.format( if (views.containsKey(view)) {
"Manifest of theme %s has no application templates.", viewTemplateName = views.get(view);
themeInfo.getName() } else {
), final Map<String, String> defaultAppViews = manifest
Response.Status.INTERNAL_SERVER_ERROR .getViewsOfApplication(application);
) if (defaultAppViews.containsKey("default")) {
); viewTemplateName = defaultAppViews.get("default");
final ThemeTemplate themeTemplate; } else {
if (applicationTemplates.containsKey(application)) { throw new WebApplicationException(
themeTemplate = applicationTemplates.get(application); String.format(
} else { "Theme \"%s\" does not provide a template for view "
themeTemplate = Optional.ofNullable( + "\"%s\" of application \"%s\", and there is no "
applicationTemplates.get("@default") + "default template configured.",
).orElseThrow( themeInfo.getName(),
() -> new WebApplicationException( view,
String.format(
"Theme %s does not provide a template for application "
+ "%s and has not default template for "
+ "applications.",
theme,
application 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("contextPath", servletContext.getContextPath());
models.put("themeName", themeInfo.getName()); models.put("themeName", themeInfo.getName());

View File

@ -96,7 +96,7 @@ public class LoginController {
models.put("loginFailed", false); models.put("loginFailed", false);
} }
models.put("returnUrl", returnUrl); models.put("returnUrl", returnUrl);
return themesMvc.getMvcTemplate(uriInfo, "login-form"); return themesMvc.getMvcTemplate(uriInfo, "login", "loginForm");
} }
@POST @POST
@ -141,7 +141,7 @@ public class LoginController {
@GET @GET
@Path("/recover-password") @Path("/recover-password")
public String getRecoverPasswordForm(@Context final UriInfo uriInfo) { public String getRecoverPasswordForm(@Context final UriInfo uriInfo) {
return themesMvc.getMvcTemplate(uriInfo, "login-recover-password"); return themesMvc.getMvcTemplate(uriInfo, "login", "recoverPassword");
} }
@POST @POST
@ -160,7 +160,7 @@ public class LoginController {
} }
} }
return themesMvc.getMvcTemplate(uriInfo, "login-password-recovered"); return themesMvc.getMvcTemplate(uriInfo, "login", "passwordRecovered");
} }
private boolean isEmailPrimaryIdentifier() { private boolean isEmailPrimaryIdentifier() {

View File

@ -49,7 +49,7 @@ public class LogoutController {
public String logout(@Context final UriInfo uriInfo) { public String logout(@Context final UriInfo uriInfo) {
subject.logout(); subject.logout();
return themesMvc.getMvcTemplate(uriInfo, "logout"); return themesMvc.getMvcTemplate(uriInfo, "logout", "loggedout");
} }
} }

View File

@ -5,7 +5,30 @@
"default-template": "category-page.html.ftl", "default-template": "category-page.html.ftl",
"mvc-templates": { "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": { "login-form": {
"description": { "description": {
"values": { "values": {
@ -78,7 +101,7 @@
} }
} }
}, },
"logout": { "loggedout": {
"description": { "description": {
"values": { "values": {
"value": [ "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"
} }
} }
} }