backend-api meta java generator - генерация сырого кода java для упрощения программирования

This commit is contained in:
AKurakin 2023-05-18 18:52:12 +03:00
parent e220d147bb
commit 8e5421900e
2 changed files with 492 additions and 0 deletions

View file

@ -323,6 +323,31 @@
</transformationSets>
</configuration>
</execution>
<execution>
<!-- Generate java_classes.java from META file
command: mvn xml:transform@java -->
<id>java</id>
<goals>
<goal>transform</goal>
</goals>
<configuration>
<transformationSets>
<transformationSet>
<dir>src/main/resources/meta</dir>
<includes>
<include>meta.xml</include>
</includes>
<stylesheet>src/main/resources/meta/xsl/java.xsl</stylesheet>
<fileMappers>
<fileMapper
implementation="org.codehaus.plexus.components.io.filemappers.FileExtensionMapper">
<targetExtension>java_classes.java</targetExtension>
</fileMapper>
</fileMappers>
</transformationSet>
</transformationSets>
</configuration>
</execution>
</executions>
</plugin>
<plugin>

View file

@ -0,0 +1,467 @@
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output version="1.0" method="text" indent="no" encoding="UTF-8"/>
<xsl:variable name="lcletters">abcdefghijklmnopqrstuvwxyz</xsl:variable>
<xsl:variable name="ucletters">ABCDEFGHIJKLMNOPQRSTUVWXYZ</xsl:variable>
<xsl:variable name="avoidletters">_0123456789</xsl:variable>
<xsl:template match="/"><xsl:apply-templates select="*"/></xsl:template>
<xsl:template match="meta">// JAVA classes for DB version: <xsl:value-of select="@version"/><xsl:apply-templates select="*"/></xsl:template>
<xsl:template match="enums">
/* Dictionaries */
<xsl:apply-templates select="*" mode="enums"/>
</xsl:template>
<xsl:template match="objects">
/* Business objects */
<xsl:apply-templates select="*" mode="objects"/>
</xsl:template>
<xsl:template match="types">
// Data types
<xsl:apply-templates select="*" mode="types"/>
</xsl:template>
<xsl:template match="type" mode="types">
// <xsl:value-of select="@id"/>. <xsl:value-of select="name()"/> : <xsl:value-of select="@type"/> - <xsl:value-of select="@name"/>
<xsl:if test="position() != last()">,</xsl:if>
</xsl:template>
<xsl:template match="*" mode="enums">
// ----------------- <xsl:value-of select="name()"/> - <xsl:value-of select="@name"/>
<xsl:variable name="dbTableName"><xsl:call-template name='convertDbStyle'><xsl:with-param name='toconvert' select='concat(name(),"Dictionary")'/></xsl:call-template></xsl:variable>
<xsl:variable name="nameObjFmt"><xsl:call-template name='convertFirstUC_'><xsl:with-param name='toconvert' select='name()'/></xsl:call-template></xsl:variable>
package ru.clearing.platform.dictionary;<!-- todo remove extract last class <xsl:value-of select="@class"/> - only package-->
<!-- import ru.clearing.dictionarys.ConstDictionarySerializable; -->
/**
* <xsl:value-of select="@name"/>
*
* Dictionary DB table: <xsl:value-of select="$dbTableName"/>
**/
public class <xsl:value-of select="$nameObjFmt"/>Dictionary extends AbstractDictionary {
private static final long serialVersionUID = ConstDictionarySerializable.serialVersionUID;
<xsl:apply-templates select="*" mode="field-enums"/> <!-- ignore default AbstractDictionary field -->
<xsl:apply-templates select="*" mode="getter-setter-enums"/>
}
// -- mapstore --
package ru.spcex.clearing.imdg.dictionary;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import ru.clearing.platform.dictionary.<xsl:value-of select="$nameObjFmt"/>Dictionary; <!-- or <xsl:value-of select="@class"/> -->
import ru.spcex.clearing.imdg.base.DictionaryTMapStore;
import ru.spcex.clearing.imdg.IMDGDistributedNames;
@Component
public class <xsl:value-of select="$nameObjFmt"/>DictionaryMapStore extends DictionaryTMapStore&lt;<xsl:value-of select="$nameObjFmt"/>Dictionary&gt; {
public <xsl:value-of select="$nameObjFmt"/>DictionaryMapStore(JdbcTemplate jdbcTemplate) {
super(jdbcTemplate);
}
@Override
public String getMapName() {
return IMDGDistributedNames.Map_<xsl:value-of select="$nameObjFmt"/>Dictionary;
}
@Override
public String getTableName() {
return "<xsl:value-of select="$dbTableName"/>";
}
@Override
public <xsl:value-of select="$nameObjFmt"/>Dictionary getDictionaryObject() {
return new <xsl:value-of select="$nameObjFmt"/>Dictionary();
}
<xsl:apply-templates select="*" mode="mapstore-field-enums"/> <!-- защита от нестандартного Dictionary -->
}
</xsl:template>
<xsl:template match="*" mode="mapstore-field-enums" >
<xsl:choose>
<xsl:when test="name()='id'"></xsl:when>
<xsl:when test="name()='code'"></xsl:when>
<xsl:when test="name()='name'"></xsl:when>
<xsl:otherwise>
!! Нестандартное поле !! <xsl:value-of select="name()"/> // FIXME Нестандартный словарь! Требуется писать код вручную.
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="*" mode="objects">
// ------------------ <xsl:value-of select="name()"/> - <xsl:value-of select="@name"/>
<xsl:variable name="dbTableName"><xsl:call-template name='convertDbStyle'><xsl:with-param name='toconvert' select='name()'/></xsl:call-template></xsl:variable>
<xsl:variable name="nameObjFmt"><xsl:call-template name='convertFirstUC_'><xsl:with-param name='toconvert' select='name()'/></xsl:call-template></xsl:variable>
package <xsl:value-of select="@class"/>;<!-- todo remove extract last class - only package-->
import ru.clearing.classes.ConstSerializable;
import ru.spcex.platform.classes.base.SpcexObjectBase;
/**
* <xsl:value-of select="@name"/>
*
* DB table: <xsl:value-of select="$dbTableName"/>
**/
public class <xsl:value-of select="$nameObjFmt"/> extends SpcexObjectBase {
private static final long serialVersionUID = ConstSerializable.serialVersionUID;
<xsl:apply-templates select="*[@name or @dbname or @type]" mode="field"/>
<xsl:apply-templates select="*[@name or @dbname or @type]" mode="getter-setter-objects"/>
}
//mapstore
// -- mapstore --
package ru.spcex.clearing.imdg.object;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import ru.spcex.clearing.imdg.IMDGDistributedNames;
import <xsl:value-of select="@class"/>; <!-- or ...<xsl:value-of select="$nameObjFmt"/> -->
import ru.spcex.clearing.imdg.base.TemplateMapStore;<!-- ObjectBaseMapStore; -->
import ru.spcex.platform.utils.time.TimeUtil;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
@Component
public class <xsl:value-of select="$nameObjFmt"/>MapStore extends TemplateMapStore&lt;<xsl:value-of select="$nameObjFmt"/>&gt; {
public <xsl:value-of select="$nameObjFmt"/>MapStore(JdbcTemplate jdbcTemplate) {
super(jdbcTemplate);
}
@Override
public String getMapName() {
return IMDGDistributedNames.Map_<xsl:value-of select="$nameObjFmt"/>;
}
@Override
public String getTableName() {
return "<xsl:value-of select="$dbTableName"/>";
}
@Override
public String[] getFields() {
return new String[]{
<xsl:apply-templates select="*[@name or @dbname or @type]" mode="mapstore-field-getFields"/>
};
}
@Override
public <xsl:value-of select="$nameObjFmt"/> objectReader(ResultSet resultSet) throws SQLException {
<xsl:value-of select="$nameObjFmt"/> object = new <xsl:value-of select="$nameObjFmt"/>(); <xsl:apply-templates select="*[@name or @dbname or @type]" mode="mapstore-field-setters"/>
return object;
}
@Override
public Object[] objectToField(<xsl:value-of select="$nameObjFmt"/> object) {
Object[] args = new Object[]{<xsl:apply-templates select="*[@name or @dbname or @type]" mode="mapstore-field-getters"/>
};
return args;
}
}
<xsl:if test="@logUpdates">
// todo добавить класс <xsl:value-of select="$nameObjFmt"/>History
package <xsl:value-of select="@class"/>;<!-- todo remove extract last class - only package-->
import ru.clearing.classes.ConstSerializable;
import ru.clearing.classes.objects.BusinessEvent;
import java.io.Serial;
/**
* Изменение состояния объекта <xsl:value-of select="@name"/>
*
* DB table: <xsl:value-of select="$dbTableName"/>_HISTORY
**/
public class <xsl:value-of select="$nameObjFmt"/>History extends BusinessEvent&lt;<xsl:value-of select="$nameObjFmt"/>&gt; {
@Serial
private static final long serialVersionUID = ConstSerializable.serialVersionUID;
private <xsl:value-of select="$nameObjFmt"/> object;
@Override
public <xsl:value-of select="$nameObjFmt"/> getObject() {
return object;
}
@Override
public void setObject(<xsl:value-of select="$nameObjFmt"/> object) {
this.object = object;
}
}
// -- History mapstore для журналирования --
package ru.spcex.clearing.imdg.businessevent;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import ru.spcex.clearing.imdg.IMDGDistributedNames;
import <xsl:value-of select="@class"/>;
import <xsl:value-of select="@class"/>History; <!-- or ...<xsl:value-of select="$nameObjFmt"/> -->
import ru.spcex.clearing.imdg.base.TemplateEventMapStore;<!-- ObjectBaseMapStore; -->
import ru.spcex.platform.utils.time.TimeUtil;
@Component
public class <xsl:value-of select="$nameObjFmt"/>HistoryMapStore extends TemplateEventMapStore&lt;<xsl:value-of select="$nameObjFmt"/>History&gt; {
public <xsl:value-of select="$nameObjFmt"/>HistoryMapStore(JdbcTemplate jdbcTemplate) {
super(jdbcTemplate);
}
@Override
public String getMapName() {
return IMDGDistributedNames.Map_<xsl:value-of select="$nameObjFmt"/>History;
}
@Override
public String getTableName() {
return "<xsl:value-of select="$dbTableName"/>_HISTORY";
}
@Override
public String[] getFields() {
return new String[]{"ID","EVENT_TIME", "EVENT_USER_ID", "EVENT_TYPE", <!-- todo покрасивее сделать ?_ID, а то код вручную приодится править -->
<xsl:value-of select="$dbTableName"/>_<xsl:apply-templates select="*[@name or @dbname or @type]" mode="mapstore-field-getFields"/>
};
}
@Override
public Object[] objectToField(<xsl:value-of select="$nameObjFmt"/>History historyLog) {
<xsl:value-of select="$nameObjFmt"/> object=historyLog.getObject();
Object[] args = new Object[]{
historyLog.getId(),
TimeUtil.toDateFromInstant(historyLog.getEventTime()),
historyLog.getUserId(),
historyLog.getEventType(),
<xsl:apply-templates select="*[@name or @dbname or @type]" mode="mapstore-field-getters"/>
};
return args;
}
}
</xsl:if>
</xsl:template>
<xsl:template match="*" mode="mapstore-field-getFields" ><xsl:if test="position() != '1'">, </xsl:if> "<xsl:choose>
<xsl:when test="@dbfield"><xsl:value-of select="@dbfield"/></xsl:when>
<xsl:otherwise><xsl:call-template name='convertDbStyle'><xsl:with-param name='toconvert' select='name()'/></xsl:call-template></xsl:otherwise>
</xsl:choose>"</xsl:template>
<xsl:template match="*" mode="mapstore-field-setters" >
<xsl:variable name="tp" select="@type"/>
<xsl:variable name="javatp" select="/meta/types/*[@id=$tp]/@javatype"/> <xsl:choose>
<xsl:when test="$javatp='Instant'">
object.set<xsl:call-template name='convertFirstUC_'><xsl:with-param name='toconvert' select='name()'/></xsl:call-template>(getInstantFromTimestamp(resultSet, "<xsl:choose>
<xsl:when test="@dbfield"><xsl:value-of select="@dbfield"/></xsl:when>
<xsl:otherwise><xsl:call-template name='convertDbStyle'><xsl:with-param name='toconvert' select='name()'/></xsl:call-template></xsl:otherwise>
</xsl:choose>"))</xsl:when>
<xsl:when test="$javatp='LocalDate'">
object.set<xsl:call-template name='convertFirstUC_'><xsl:with-param name='toconvert' select='name()'/></xsl:call-template>(getLocalDateFromSqlDate(resultSet, "<xsl:choose>
<xsl:when test="@dbfield"><xsl:value-of select="@dbfield"/></xsl:when>
<xsl:otherwise><xsl:call-template name='convertDbStyle'><xsl:with-param name='toconvert' select='name()'/></xsl:call-template></xsl:otherwise>
</xsl:choose>"))</xsl:when>
<xsl:when test="$javatp='LocalTime'">
object.set<xsl:call-template name='convertFirstUC_'><xsl:with-param name='toconvert' select='name()'/></xsl:call-template>(getLocalTimeFromSqlTime(resultSet, "<xsl:choose>
<xsl:when test="@dbfield"><xsl:value-of select="@dbfield"/></xsl:when>
<xsl:otherwise><xsl:call-template name='convertDbStyle'><xsl:with-param name='toconvert' select='name()'/></xsl:call-template></xsl:otherwise>
</xsl:choose>"))</xsl:when>
<xsl:otherwise>
object.set<xsl:call-template name='convertFirstUC_'><xsl:with-param name='toconvert' select='name()'/></xsl:call-template>(resultSet.getObject("<xsl:choose>
<xsl:when test="@dbfield"><xsl:value-of select="@dbfield"/></xsl:when>
<xsl:otherwise><xsl:call-template name='convertDbStyle'><xsl:with-param name='toconvert' select='name()'/></xsl:call-template></xsl:otherwise>
</xsl:choose>", <xsl:value-of select="$javatp"/>.class))</xsl:otherwise>
</xsl:choose>;</xsl:template>
<xsl:template match="*" mode="mapstore-field-getters" ><xsl:if test="position() != '1'">, </xsl:if><!-- todo запятую в конце а не в начале, см. last()-->
<xsl:variable name="tp" select="@type"/>
<xsl:variable name="javatp" select="/meta/types/*[@id=$tp]/@javatype"/>
<xsl:choose>
<xsl:when test="$javatp='Instant'">
TimeUtil.toDateFromInstant(object.get<xsl:call-template name='convertFirstUC_'><xsl:with-param name='toconvert' select='name()'/></xsl:call-template>())</xsl:when>
<xsl:when test="$javatp='LocalDate'">
TimeUtil.toDateFromLocalDate(object.get<xsl:call-template name='convertFirstUC_'><xsl:with-param name='toconvert' select='name()'/></xsl:call-template>())</xsl:when>
<xsl:when test="$javatp='LocalTime'">
TimeUtil.toDateFromLocalTime(object.get<xsl:call-template name='convertFirstUC_'><xsl:with-param name='toconvert' select='name()'/></xsl:call-template>())</xsl:when>
<xsl:otherwise>
object.get<xsl:call-template name='convertFirstUC_'><xsl:with-param name='toconvert' select='name()'/></xsl:call-template>()</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="*" mode="field" >
<xsl:variable name="tp" select="@type"/>
<xsl:if test="name()='id'"> // (in parent) </xsl:if> private <xsl:value-of select="/meta/types/*[@id=$tp]/@javatype"/> <xsl:text> <!-- space &#160; --></xsl:text><xsl:value-of select="name()"/>;
<xsl:choose>
<xsl:when test="@dbfield"> // DB field: <xsl:value-of select="@dbfield"/></xsl:when>
<!-- new line -->
<xsl:otherwise> </xsl:otherwise></xsl:choose>
<!-- todo pretty comment: <xsl:if test="@link"> // (linked to <xsl:value-of select="@link"/>)
</xsl:if>-->
<!-- fixme т.к. тут атрибуты перебираются - не работают переносы а ещё знак пробела надо пропатчить, а то NBSP -->
</xsl:template>
<xsl:template match="*" mode="field-enums" >
<xsl:variable name="tp" select="@type"/>
<xsl:choose>
<xsl:when test="name()='id'"></xsl:when><!-- определено в родительском классе AbstractDictionary -->
<xsl:when test="name()='code'"></xsl:when><!-- определено в родительском классе AbstractDictionary -->
<xsl:when test="name()='name'"></xsl:when><!-- определено в родительском классе AbstractDictionary -->
<xsl:otherwise><!-- нестандартное поле -->
private <xsl:value-of select="/meta/types/*[@id=$tp]/@javatype"/> <xsl:text> <!-- space &#160; --> </xsl:text><xsl:value-of select="name()"/>;
<xsl:choose>
<xsl:when test="@dbfield"> // DB field: <xsl:value-of select="@dbfield"/></xsl:when>
<!-- new line -->
<xsl:otherwise> </xsl:otherwise>
</xsl:choose><xsl:if test="@link"> // (linked to <xsl:value-of select="@link"/>)
<!--new line--></xsl:if>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="*" mode="getter-setter-enums">
<xsl:variable name="tp" select="@type"/>
<xsl:choose>
<xsl:when test="name()='id'"></xsl:when><!-- todo other restrictions of parent AbstractDictionary -->
<xsl:when test="name()='code'"></xsl:when><!-- todo other restrictions of parent AbstractDictionary -->
<xsl:when test="name()='name'"></xsl:when><!-- todo other restrictions of parent AbstractDictionary -->
<xsl:otherwise><!-- нестандартное поле -->
<xsl:variable name="NameFmt"><xsl:call-template name='convertFirstUC_'><xsl:with-param name='toconvert' select='name()'/></xsl:call-template></xsl:variable>
public <xsl:value-of select="/meta/types/*[@id=$tp]/@javatype"/> get<xsl:value-of select="$NameFmt"/>() {
return <xsl:value-of select="name()"/>;
}
public void set<xsl:value-of select="$NameFmt"/>(<xsl:value-of select="/meta/types/*[@id=$tp]/@javatype"/> value) {
this.<xsl:value-of select="name()"/>=value;
}
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="*" mode="getter-setter-objects">
<xsl:variable name="tp" select="@type"/>
<xsl:choose>
<xsl:when test="name()='id'"></xsl:when>
<xsl:otherwise>
<xsl:variable name="NameFmt"><xsl:call-template name='convertFirstUC_'><xsl:with-param name='toconvert' select='name()'/></xsl:call-template></xsl:variable>
public <xsl:value-of select="/meta/types/*[@id=$tp]/@javatype"/> get<xsl:value-of select="$NameFmt"/>() {
return <xsl:value-of select="name()"/>;
}
public void set<xsl:value-of select="$NameFmt"/>(<xsl:value-of select="/meta/types/*[@id=$tp]/@javatype"/> value) {
this.<xsl:value-of select="name()"/>=value;
}
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name='convertDbStyle'>
<xsl:param name='toconvert' />
<xsl:variable name="added_">
<xsl:call-template name='add_'>
<xsl:with-param name='toconvert' select='$toconvert' />
</xsl:call-template>
</xsl:variable>
<xsl:call-template name='convertcase'>
<xsl:with-param name='toconvert' select='$added_' />
<xsl:with-param name='conversion' select="'upper'" />
</xsl:call-template>
</xsl:template>
<xsl:template name='convertcase'>
<xsl:param name='toconvert' />
<xsl:param name='conversion' />
<xsl:choose>
<xsl:when test='$conversion="lower"'>
<xsl:value-of select="translate($toconvert,$ucletters,$lcletters)"/>
</xsl:when>
<xsl:when test='$conversion="upper"'>
<xsl:value-of select="translate($toconvert,$lcletters,$ucletters)"/>
</xsl:when>
<xsl:when test='$conversion="proper"'>
<xsl:call-template name='convertpropercase'>
<xsl:with-param name='toconvert'>
<xsl:value-of select="translate($toconvert,$ucletters,$lcletters)"/>
</xsl:with-param>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select='$toconvert' />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name='convertpropercase'>
<xsl:param name='toconvert' />
<xsl:if test="string-length($toconvert) > 0">
<xsl:variable name='f' select='substring($toconvert, 1, 1)' />
<xsl:variable name='s' select='substring($toconvert, 2)' />
<xsl:call-template name='convertcase'>
<xsl:with-param name='toconvert' select='$f' />
<xsl:with-param name='conversion'>upper</xsl:with-param>
</xsl:call-template>
<xsl:choose>
<xsl:when test="contains($s,' ')">
<xsl:value-of select='substring-before($s," ")'/>
<xsl:call-template name='convertpropercase'>
<xsl:with-param name='toconvert' select='substring-after($s," ")' />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select='$s'/>
</xsl:otherwise>
</xsl:choose>
</xsl:if>
</xsl:template>
<xsl:template name='add_'>
<xsl:param name='toconvert' />
<xsl:if test="string-length($toconvert) > 0">
<xsl:variable name='f' select='substring($toconvert, 1, 1)' />
<xsl:variable name='s' select='substring($toconvert, 2)' />
<xsl:choose>
<xsl:when test="$f = translate($f, $lcletters,$ucletters)"><xsl:if test="translate($f, $avoidletters,'')">_</xsl:if><xsl:value-of select='$f'/></xsl:when>
<xsl:otherwise><xsl:value-of select='$f'/></xsl:otherwise>
</xsl:choose>
<xsl:if test="string-length($toconvert) > 1">
<xsl:call-template name='add_'>
<xsl:with-param name='toconvert' select='$s'/>
</xsl:call-template>
</xsl:if>
</xsl:if>
</xsl:template>
<!-- todo должен первую букву прописной сделать -->
<xsl:template name='convertFirstUC_'>
<xsl:param name='toconvert' />
<xsl:if test="string-length($toconvert) > 0">
<xsl:variable name='f' select='translate(substring($toconvert, 1, 1),$lcletters,$ucletters)' />
<xsl:variable name='s' select='substring($toconvert, 2)' />
<xsl:value-of select='$f'/><xsl:value-of select='$s'/>
</xsl:if>
</xsl:template>
</xsl:stylesheet>