org.appng.api.support.DatasourceInheritanceHelper Maven / Gradle / Ivy
/*
* Copyright 2011-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.appng.api.support;
import java.util.List;
import javax.xml.bind.JAXBException;
import org.apache.commons.lang3.StringUtils;
import org.appng.xml.MarshallService;
import org.appng.xml.platform.BeanOption;
import org.appng.xml.platform.DataConfig;
import org.appng.xml.platform.Datasource;
import org.appng.xml.platform.FieldDef;
import org.appng.xml.platform.FieldType;
import org.appng.xml.platform.Label;
import org.appng.xml.platform.Linkpanel;
import org.appng.xml.platform.Param;
import lombok.extern.slf4j.Slf4j;
/**
* This is a helper class providing some static methods to process the inheritance of datasource definitions found in a
* resource. If a datasource id contains the inheritance separator '::' appNG clones the ancestor datasource definition
* and overrides or adds:
*
* - datasource id - override
* - title - override
* - parameter - add
* - bind class - override
* - fields - add
* - bean id - override
* - bean options - add
* - linkpanel - override
* - config permissions - override
* - config labels - add
*
*
* The simplest usage of this inheritance mechanism can be used to create a clone of an existing datasource definition
* just with another datasource id to be able to place the datasource a second time with other parameters on the same
* page.
*
* @author Claus Stümke, aiticon GmbH, 2016
*
*/
@Slf4j
public class DatasourceInheritanceHelper {
private static String INHERITANCE_SEPARATOR = "::";
/**
* Returns {@code true} if the id contains exactly one inheritance separator string "::"
*
* @param datasourceId
* @return {@code true} if the id contains an inheritance separator string
*/
public static boolean isInheriting(String datasourceId) {
return (StringUtils.countMatches(datasourceId, "::") == 1) && (!StringUtils.startsWith(datasourceId, "::"))
&& (!StringUtils.endsWith(datasourceId, "::"));
}
/**
* returns the part of the datasource id of a datatsource for inheritance which indicates the final id of the
* descendant
*
* @param datasourceId
* @return the descendant's id
*/
public static String getDescendantId(String datasourceId) {
return datasourceId.split(INHERITANCE_SEPARATOR)[0];
}
/**
* returns the part of the datasource id of a datatsource for inheritance which indicates the id of the ancestor
* datasource
*
* @param datasourceId
* @return the ancestor's id
*/
public static String getAncestorId(String datasourceId) {
return datasourceId.split(INHERITANCE_SEPARATOR)[1];
}
/**
* clones the ancestor datasource and adds or overrides id, title, parameter, fields, bean options, bean id, bind
* class and linkpanel. Returns the new datasource.
*
* @param descendantDefinition
* @param ancestor
* @return the cloned {@link Datasource}
*/
public static Datasource inherit(Datasource descendantDefinition, Datasource ancestor,
MarshallService marshallService) {
LOGGER.info("process inheritance for {}", descendantDefinition.getId());
String marshalledAncestor;
Datasource descendant = null;
try {
marshalledAncestor = marshallService.marshallNonRoot(ancestor);
descendant = (Datasource) marshallService.unmarshall(marshalledAncestor);
} catch (JAXBException e) {
LOGGER.error(String.format("error while marshalling/unmarshalling datasource %s", ancestor.getId()), e);
return null;
}
DataConfig descendantDefConfig = descendantDefinition.getConfig();
DataConfig descendantConfig = descendant.getConfig();
// ID
descendant.setId(getDescendantId(descendantDefinition.getId()));
// title
if (null != descendantDefConfig.getTitle()) {
if (StringUtils.isNotBlank(descendantDefConfig.getTitle().getId())
|| StringUtils.isNotBlank(descendantDefConfig.getTitle().getValue())) {
LOGGER.info("override title");
descendantConfig.setTitle(descendantDefConfig.getTitle());
}
}
// description
if (null != descendantDefConfig.getDescription()) {
descendantConfig.setDescription(descendantDefConfig.getDescription());
LOGGER.info("override description");
}
// labels are merged
if (null != descendantDefConfig.getLabels()) {
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy