From 759ac5f42c6aff901dbeede4fbf1a1d2e08cad0f Mon Sep 17 00:00:00 2001 From: Thomas Lenz Date: Wed, 4 Dec 2019 19:43:32 +0100 Subject: common EGIZ code-style refactoring --- .../gui/AbstractGuiFormBuilderConfiguration.java | 195 +++++++++++++++++++++ 1 file changed, 195 insertions(+) create mode 100644 eaaf_core/src/main/java/at/gv/egiz/eaaf/core/impl/gui/AbstractGuiFormBuilderConfiguration.java (limited to 'eaaf_core/src/main/java/at/gv/egiz/eaaf/core/impl/gui/AbstractGuiFormBuilderConfiguration.java') diff --git a/eaaf_core/src/main/java/at/gv/egiz/eaaf/core/impl/gui/AbstractGuiFormBuilderConfiguration.java b/eaaf_core/src/main/java/at/gv/egiz/eaaf/core/impl/gui/AbstractGuiFormBuilderConfiguration.java new file mode 100644 index 00000000..efb8c713 --- /dev/null +++ b/eaaf_core/src/main/java/at/gv/egiz/eaaf/core/impl/gui/AbstractGuiFormBuilderConfiguration.java @@ -0,0 +1,195 @@ +/* + * Copyright 2017 Graz University of Technology EAAF-Core Components has been developed in a + * cooperation between EGIZ, A-SIT Plus, A-SIT, and Graz University of Technology. + * + * Licensed under the EUPL, Version 1.2 or - as soon they will be approved by the European + * Commission - subsequent versions of the EUPL (the "Licence"); You may not use this work except in + * compliance with the Licence. You may obtain a copy of the Licence at: + * https://joinup.ec.europa.eu/news/understanding-eupl-v12 + * + * Unless required by applicable law or agreed to in writing, software distributed under the Licence + * is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the Licence for the specific language governing permissions and limitations under + * the Licence. + * + * This product combines work with different licenses. See the "NOTICE" text file for details on the + * various modules and licenses. The "NOTICE" text file is part of the distribution. Any derivative + * works that you distribute must include a readable copy of the "NOTICE" text file. +*/ + +package at.gv.egiz.eaaf.core.impl.gui; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import at.gv.egiz.eaaf.core.api.gui.GroupDefinition; +import at.gv.egiz.eaaf.core.api.gui.GroupDefinition.Type; +import at.gv.egiz.eaaf.core.api.gui.IGuiBuilderConfiguration; +import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Abstract Configuration implementation for GUI Builders. + * + * @author tlenz + * + */ +public abstract class AbstractGuiFormBuilderConfiguration implements IGuiBuilderConfiguration { + private static final Logger log = + LoggerFactory.getLogger(AbstractGuiFormBuilderConfiguration.class); + + public static final GroupDefinition PARAM_GROUP_FORM = + GroupDefinition.getInstance("form", Type.MAP); + public static final GroupDefinition PARAM_GROUP_ACTIONS = + GroupDefinition.getInstance("actions", Type.LIST); + public static final GroupDefinition PARAM_GROUP_PARAMS = + GroupDefinition.getInstance("params", Type.MAP); + public static final GroupDefinition PARAM_GROUP_UIOPTIONS = + GroupDefinition.getInstance("uiOptions", Type.MAP); + public static final GroupDefinition PARAM_GROUP_MSG = + GroupDefinition.getInstance("msg", Type.MAP); + + + public static final String PARAM_VIEWNAME = "viewName"; + public static final String PARAM_AUTHCONTEXT = "contextPath"; + public static final String PARAM_FORMSUBMITENDPOINT = "submitEndpoint"; + @Deprecated + public static final String PARAM_PENDINGREQUESTID_DEPRECATED = "pendingReqID"; + public static final String PARAM_PENDINGREQUESTID = "pendingid"; + + private String authUrl = null; + private String viewName = null; + private String formSubmitEndpoint = null; + + private final Map params = new HashMap<>(); + + /** + * Abstract GUI Builder config. + * + * @param authUrl IDP PublicURL-Prefix which should be used, but never null + * @param viewName Name of the template (with suffix) but never null + * @param formSubmitEndpoint EndPoint on which the form should be submitted, or null if the form + * must not submitted + * + */ + public AbstractGuiFormBuilderConfiguration(final String authUrl, final String viewName, + final String formSubmitEndpoint) { + if (viewName.startsWith("/")) { + this.viewName = viewName.substring(1); + } else { + this.viewName = viewName; + } + + if (authUrl.endsWith("/")) { + this.authUrl = authUrl.substring(0, authUrl.length() - 1); + } else { + this.authUrl = authUrl; + } + + if (StringUtils.isNotEmpty(formSubmitEndpoint)) { + if (formSubmitEndpoint.startsWith("/")) { + this.formSubmitEndpoint = formSubmitEndpoint; + } else { + this.formSubmitEndpoint = "/" + formSubmitEndpoint; + } + } + } + + /* + * (non-Javadoc) + * + * @see at.gv.egovernment.moa.id.auth.frontend.builder.IGUIBuilderConfiguration#getViewName() + */ + @Override + public final String getViewName() { + return this.viewName; + + } + + + /* + * (non-Javadoc) + * + * @see + * at.gv.egovernment.moa.id.auth.frontend.builder.IGUIBuilderConfiguration#getViewParameters() + */ + @Override + public final Map getViewParameters() { + // set generic parameters + setViewParameter(getFromGroup(), PARAM_AUTHCONTEXT, this.authUrl); + setViewParameter(getFromGroup(), PARAM_FORMSUBMITENDPOINT, this.formSubmitEndpoint); + setViewParameter(getFromGroup(), PARAM_VIEWNAME, this.viewName); + + // get parameters from detail implementation + putSpecificViewParameters(); + + try { + log.trace("Full view parameters: {}", StringUtils.join(params, ",")); + } catch (final Exception e) { + log.info("Can NOT trace view parameters. Reason: {}", e.getMessage()); + } + + return params; + + } + + /** + * Define the parameters, which should be evaluated in the template.
+ * IMPORTANT: external HTML escapetion is required, because it is NOT done internally + * during the building process + * + */ + protected abstract void putSpecificViewParameters(); + + /** + * Get the Group for generic form elements. + * + * @return {@link GroupDefinition} or null if no groups are used + */ + protected abstract GroupDefinition getFromGroup(); + + @SuppressWarnings("unchecked") + protected void setViewParameter(final GroupDefinition groupDefinition, final String key, + final Object value) { + + if (groupDefinition != null) { + log.trace("Adding group object ... "); + Object groupMap = params.get(groupDefinition.getName()); + if (groupMap == null) { + if (groupDefinition.getType().equals(Type.MAP)) { + groupMap = new HashMap(); + log.trace("Build new MAP based group element"); + + } else if (groupDefinition.getType().equals(Type.LIST)) { + groupMap = new ArrayList<>(); + log.trace("Build new List based group element"); + + } else { + log.warn("GroupDefinition contains an unknown type: {}", + groupDefinition.getType().name()); + groupMap = StringUtils.EMPTY; + + } + + } + + params.put(groupDefinition.getName(), groupMap); + if (groupMap instanceof Map) { + ((Map) groupMap).put(key, value); + } else if (groupMap instanceof List) { + ((List) groupMap).add(value); + } else { + log.warn("Can NOT add element: {} to group: {}, because group is of type: {}", key, + groupDefinition.getName(), groupMap.getClass().getName()); + } + + } else { + log.trace("Add root object ... "); + params.put(key, value); + + } + + } +} -- cgit v1.2.3