
de.tsl2.nano.h5.CMSBean Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of tsl2.nano.h5 Show documentation
Show all versions of tsl2.nano.h5 Show documentation
TSL2 Framework Html5 Extensions (WebServer, Html5Presentation, RuleCover, BeanConfigurator, LogicTable-Sheet, Expression-Descriptors for Actions, Rules, URLs, Queries)
The newest version!
/*
* File: $HeadURL$
* Id : $Id$
*
* created by: Thomas Schneider
* created on: Apr 19, 2025
*
* Copyright: (c) Thomas Schneider 2025, all rights reserved
*/
package de.tsl2.nano.h5;
import static de.tsl2.nano.h5.CMSBean.DefaultAttributes.DESCRIPTION;
import static de.tsl2.nano.h5.CMSBean.DefaultAttributes.IMAGE;
import static de.tsl2.nano.h5.CMSBean.DefaultAttributes.NAME;
import static de.tsl2.nano.h5.CMSBean.DefaultAttributes.VALUE;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import org.apache.commons.logging.Log;
import de.tsl2.nano.action.CommonAction;
import de.tsl2.nano.action.IAction;
import de.tsl2.nano.action.IActivable;
import de.tsl2.nano.bean.BeanUtil;
import de.tsl2.nano.bean.def.AttributeDefinition;
import de.tsl2.nano.bean.def.Bean;
import de.tsl2.nano.bean.def.BeanCollector;
import de.tsl2.nano.bean.def.BeanDefinition;
import de.tsl2.nano.bean.def.IIPresentable;
import de.tsl2.nano.bean.def.IPresentable;
import de.tsl2.nano.bean.def.Value;
import de.tsl2.nano.core.ENV;
import de.tsl2.nano.core.cls.BeanClass;
import de.tsl2.nano.core.exception.Message;
import de.tsl2.nano.core.execution.SystemUtil;
import de.tsl2.nano.core.log.LogFactory;
import de.tsl2.nano.core.util.FilePath;
import de.tsl2.nano.core.util.NetUtil;
import de.tsl2.nano.core.util.StringUtil;
import de.tsl2.nano.core.util.Util;
import de.tsl2.nano.core.util.parser.JSon;
import de.tsl2.nano.execution.IPRunnable;
import de.tsl2.nano.h5.expression.WebClient;
import de.tsl2.nano.specification.Pool;
import de.tsl2.nano.specification.actions.Action;
/**
* Creates Beans from a given content url. Usable like a constraint FlatFile Content Management System or Editorial System.
* This url must point to a README.MD.
* The README.MD must contain a tag "{{NAMES}}" followed by a comma separated list of bean names.
* This bean names have to be found as child folders of the given baseurl (where the README.MD was found)
* So each bean has to have its own folder. If the name (=folder) starts with "-" it is marked as beeing a collection of sub beans.
* Each bean (=folder) has to have its own README.MD containing a tag "{{NAMES}}", specifying the attribute names.
* If a bean has actions, the names of these actions have to be specified after the tag {{ACTIONS}}.
*
* Each attribute is defined by its
* - name (read from README.MD -> {{NAMES}})
* - description (optional) read from {NAME}-description.txt
* - presentable (optional) read from {NAME}-presentable.json
* - image (optional) read from {NAME}-image.jpg
* - value read from {NAME}-value.obj
*
* The value will be interpreted by the content:
* - url : the url will be called
* - json: the json content will be wrapped into an object given by {NAME}-valuetype.txt
* - endswith a standard extension like zip/jpg/tar: it is a download link
* - otherwise: it is a simple string
*
* All Actions (names are defined in README.MD through {{ACTION}} list) have to have the extension ".action"
*
* Each Action will be interpreted by the content:
* - class+method name (parameter: de.tsl2.nano.bean.def.Bean.class (to get all informations through current selected bean/attributes))
* - url (starting with uri scheme expression (see https://en.wikipedia.org/wiki/List_of_URI_schemes))
* - otherwise: shell action
*/
@SuppressWarnings({"rawtypes", "unchecked"})
public class CMSBean {
private static final Log LOG = LogFactory.getLog(CMSBean.class);
private static final String OPEN_TAG = "\\{\\{";
private static final String CLOSE_TAG = "\\}\\}\\: ";
private static final String TAG_NAMES = "NAMES";
private static final String TAG_ACTIONS = "ACTIONS";
private static final String INFO = "/README.MD";
protected static final String PREFIX_COLLECTOR = "-";
enum DefaultAttributes {NAME, DESCRIPTION, IMAGE, VALUE; public String toString() {return name().toLowerCase();} };
public static BeanCollector, BeanDefinition> provideCMSBeans(String definitionUrl) {
return provideCMSBeans(definitionUrl, false);
}
/** if strict=true, all attribute files must be present */
public static BeanCollector, BeanDefinition> provideCMSBeans(String definitionUrl, boolean strict) {
ENV.assignClassloaderToCurrentThread();
definitionUrl = definitionUrl.endsWith(INFO) ? definitionUrl : definitionUrl + (definitionUrl.endsWith("/") ? INFO.substring(1) : INFO);
logFrame("starting from " + definitionUrl + "\n\tall content will be downloaded to: " + getDefinitionDirectory());
List names = readNamesFromUrl(definitionUrl);
String baseUrl = baseUrl(definitionUrl);
List beans = new ArrayList<>(names.size());
names.parallelStream().forEach(n -> beans.add(n.startsWith(PREFIX_COLLECTOR) ? provideCMSBeans(baseUrl + n + INFO, strict) : createBean(baseUrl, n, strict)));
logFrame(beans.size() + " beans created:\n" + beans.stream().map(b -> ((BeanDefinition)b).toValueMap(null).toString() + "\n").toList());
BeanCollector, BeanDefinition> bc = new BeanCollector,BeanDefinition>(beans, 1);
String name = StringUtil.substring(baseUrl.substring(0, baseUrl.length() - 1), "/", null, true);
bc.setName(name);
bc.getPresentable().setIcon("icons/cascade.png");
return bc;
}
static void logMsg(String txt) {
Message.send(txt);
}
static void logFrame(String txt) {
Message.send(txt);
String frame = "\n" + StringUtil.fixString(80, '=') + "\n";
LOG.info(frame + txt + frame);
}
private static String baseUrl(String definitionUrl) {
return StringUtil.substring(definitionUrl, null, "/", true) + "/";
}
private static Bean> createBean(String baseUrl, String name, boolean strict) {
ENV.assignClassloaderToCurrentThread();
Bean
© 2015 - 2025 Weber Informatics LLC | Privacy Policy