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;
|
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
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
);
|
||||||
|
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(
|
.orElseThrow(
|
||||||
() -> new WebApplicationException(
|
() -> new WebApplicationException(
|
||||||
String.format(
|
String.format(
|
||||||
"Manifest of theme %s has no application templates.",
|
"Theme \"%s\" maps view \"%s\" of application \"%s\" "
|
||||||
themeInfo.getName()
|
+ "to template \"%s\" but not template with this "
|
||||||
),
|
+ "name was found in the theme.",
|
||||||
Response.Status.INTERNAL_SERVER_ERROR
|
themeInfo.getName(),
|
||||||
)
|
view,
|
||||||
);
|
application,
|
||||||
final ThemeTemplate themeTemplate;
|
viewTemplateName
|
||||||
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,
|
|
||||||
application
|
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
|
||||||
|
|
||||||
models.put("contextPath", servletContext.getContextPath());
|
models.put("contextPath", servletContext.getContextPath());
|
||||||
models.put("themeName", themeInfo.getName());
|
models.put("themeName", themeInfo.getName());
|
||||||
|
|
|
||||||
|
|
@ -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() {
|
||||||
|
|
|
||||||
|
|
@ -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");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,103 +5,138 @@
|
||||||
"default-template": "category-page.html.ftl",
|
"default-template": "category-page.html.ftl",
|
||||||
|
|
||||||
"mvc-templates": {
|
"mvc-templates": {
|
||||||
"applications": {
|
"category-page": {
|
||||||
"login-form": {
|
"description": {
|
||||||
"description": {
|
"values": {
|
||||||
"values": {
|
"value": [
|
||||||
"value": [
|
{
|
||||||
{
|
"lang": "en",
|
||||||
"lang": "en",
|
"value": "Category Page Template"
|
||||||
"value": "Login Form"
|
}
|
||||||
}
|
]
|
||||||
]
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"name": "Login Form",
|
|
||||||
"path": "login/login-form.html.ftl",
|
|
||||||
"title": {
|
|
||||||
"values": {
|
|
||||||
"value": [
|
|
||||||
{
|
|
||||||
"lang": "en",
|
|
||||||
"value": "Login Form"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"login-recover-password": {
|
"name": "Category Page",
|
||||||
"description": {
|
"path": "categoryPage.html.ftl",
|
||||||
"values": {
|
"title": {
|
||||||
"value": [
|
"values": {
|
||||||
{
|
"value": [
|
||||||
"lang": "en",
|
{
|
||||||
"value": "Recover lost passwords"
|
"lang": "en",
|
||||||
}
|
"value": "Category Page"
|
||||||
]
|
}
|
||||||
}
|
]
|
||||||
},
|
|
||||||
"name": "login-recover-password",
|
|
||||||
"path": "login/login-recover-password.html.ftl",
|
|
||||||
"title": {
|
|
||||||
"values": {
|
|
||||||
"value": [
|
|
||||||
{
|
|
||||||
"lang": "en",
|
|
||||||
"value": "Recover password"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"login-password-recovered": {
|
|
||||||
"description": {
|
|
||||||
"values": {
|
|
||||||
"value": [
|
|
||||||
{
|
|
||||||
"lang": "en",
|
|
||||||
"value": "Password recovered"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"name": "login-password-recovered",
|
|
||||||
"path": "login/login-password-recovered.html.ftl",
|
|
||||||
"title": {
|
|
||||||
"values": {
|
|
||||||
"value": [
|
|
||||||
{
|
|
||||||
"lang": "en",
|
|
||||||
"value": "Password recovered"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"logout": {
|
|
||||||
"description": {
|
|
||||||
"values": {
|
|
||||||
"value": [
|
|
||||||
{
|
|
||||||
"lang": "en",
|
|
||||||
"value": "Logout successful"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"name": "loggedout",
|
|
||||||
"path": "logout/loggedout.html.ftl",
|
|
||||||
"title": {
|
|
||||||
"values": {
|
|
||||||
"value": [
|
|
||||||
{
|
|
||||||
"lang": "en",
|
|
||||||
"value": "Logout succesful"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"login-form": {
|
||||||
|
"description": {
|
||||||
|
"values": {
|
||||||
|
"value": [
|
||||||
|
{
|
||||||
|
"lang": "en",
|
||||||
|
"value": "Login Form"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"name": "Login Form",
|
||||||
|
"path": "login/login-form.html.ftl",
|
||||||
|
"title": {
|
||||||
|
"values": {
|
||||||
|
"value": [
|
||||||
|
{
|
||||||
|
"lang": "en",
|
||||||
|
"value": "Login Form"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"login-recover-password": {
|
||||||
|
"description": {
|
||||||
|
"values": {
|
||||||
|
"value": [
|
||||||
|
{
|
||||||
|
"lang": "en",
|
||||||
|
"value": "Recover lost passwords"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"name": "login-recover-password",
|
||||||
|
"path": "login/login-recover-password.html.ftl",
|
||||||
|
"title": {
|
||||||
|
"values": {
|
||||||
|
"value": [
|
||||||
|
{
|
||||||
|
"lang": "en",
|
||||||
|
"value": "Recover password"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"login-password-recovered": {
|
||||||
|
"description": {
|
||||||
|
"values": {
|
||||||
|
"value": [
|
||||||
|
{
|
||||||
|
"lang": "en",
|
||||||
|
"value": "Password recovered"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"name": "login-password-recovered",
|
||||||
|
"path": "login/login-password-recovered.html.ftl",
|
||||||
|
"title": {
|
||||||
|
"values": {
|
||||||
|
"value": [
|
||||||
|
{
|
||||||
|
"lang": "en",
|
||||||
|
"value": "Password recovered"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"loggedout": {
|
||||||
|
"description": {
|
||||||
|
"values": {
|
||||||
|
"value": [
|
||||||
|
{
|
||||||
|
"lang": "en",
|
||||||
|
"value": "Logout successful"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"name": "loggedout",
|
||||||
|
"path": "logout/loggedout.html.ftl",
|
||||||
|
"title": {
|
||||||
|
"values": {
|
||||||
|
"value": [
|
||||||
|
{
|
||||||
|
"lang": "en",
|
||||||
|
"value": "Logout succesful"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"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