最近在探索 mendix 时,我注意到他们有一个 platform sdk,允许您通过 api 与 mendix 应用程序模型进行交互。
这给了我一个想法,探索它是否可以用于创建我们的领域模型。具体来说,是基于现有的传统应用程序创建领域模型。
如果进一步推广,这可用于将任何现有应用程序转换为 mendix 并从那里继续开发。
将 java/spring web 应用程序转换为 mendix
因此,我创建了一个带有简单 api 和数据库层的小型 java/spring web 应用程序。为了简单起见,它使用嵌入式 h2 数据库。
在这篇文章中,我们将仅转换 jpa 实体。让我们来看看它们:
@entity@table(name = "cat")class cat { @id @generatedvalue(strategy = generationtype.auto) private long id; private string name; private int age; private string color; @onetoone private human humanpuppet; ... constructor ... ... getters ...}@entity@table(name = "human")public class human { @id @generatedvalue(strategy = generationtype.auto) private long id; private string name; ... constructor ... ... getters ...}
登录后复制
如您所见,它们非常简单:一只有名字、年龄、颜色的猫和它的人类傀儡,因为正如我们所知,猫统治着世界。
它们都有一个自动生成的 id 字段。猫与人类有一对一的联系,这样它就可以随时呼唤人类。 (如果它不是 jpa 实体,我会放置一个 meow() 方法,但让我们将其留到将来)。
应用程序功能齐全,但现在我们只对数据层感兴趣。
提取 json 中的实体元数据
这可以通过几种不同的方式来完成:
通过静态分析包中的实体。通过使用反射在运行时读取这些实体。
我选择了选项 2,因为它更快,而且我无法轻松找到可以执行选项 1 的库。
接下来,我们需要决定构建后如何公开 json。为了简单起见,我们只需将其写入文件即可。一些替代方法可能是:
通过 api 公开它。这更加复杂,因为您还需要确保端点受到很好的保护,因为我们不能公开暴露我们的元数据。通过一些管理工具公开它,例如 spring boot actuator 或 jmx。它更安全,但仍然需要时间来设置。
现在让我们看看实际的代码:
public class mendixexporter { public static void exportentitiesto(string filepath) throws ioexception { annotatedtypescanner typescanner = new annotatedtypescanner(false, entity.class); set<class> entityclasses = typescanner.findtypes(javatomendixapplication.class.getpackagename()); log.info("entity classes are: {}", entityclasses); list mendixentities = new arraylist(); for (class entityclass : entityclasses) { list attributes = new arraylist(); for (field field : entityclass.getdeclaredfields()) { attributetype attributetype = determineattributetype(field); associationtype associationtype = determineassociationtype(field, attributetype); string associationentitytype = determineassociationentitytype(field, attributetype); attributes.add( new mendixattribute(field.getname(), attributetype, associationtype, associationentitytype)); } mendixentity newentity = new mendixentity(entityclass.getsimplename(), attributes); mendixentities.add(newentity); } writetojsonfile(filepath, mendixentities); } ...}
登录后复制
我们首先查找应用程序中标有 jpa 的 @entity 注释的所有类。
然后,对于每堂课,我们:
使用entityclass.getdeclaredfields()获取声明的字段。循环该类的每个字段。
对于每个字段,我们:
确定属性的类型:
private static final map, attributetype> java_to_mendix_type = map.ofentries( map.entry(string.class, attributetype.string), map.entry(integer.class, attributetype.integer), ... );// we return attributetype.entity if we cannot map to anything else
登录后复制
本质上,我们只是通过在 java_to_mendix_type 映射中查找 java 类型与我们的自定义枚举值进行匹配。
接下来,我们检查这个属性是否实际上是一个关联(指向另一个@entity)。如果是这样,我们确定关联的类型:一对一、一对多、多对多:
private static associationtype determineassociationtype(field field, attributetype attributetype) { if (!attributetype.equals(attributetype.entity)) return null; if (field.gettype().equals(list.class)) { return associationtype.one_to_many; } else { return associationtype.one_to_one; }}
登录后复制
为此,我们只需检查之前映射的属性类型。如果它是 entity,这仅意味着在之前的步骤中我们无法将其映射到任何原始 java 类型、string 或 enum。
然后我们还需要决定它是什么类型的关联。检查很简单:如果是 list 类型,则它是一对多,否则是一对一(尚未实现“多对多”)。
然后我们为找到的每个字段创建一个 mendixattribute 对象。
完成后,我们只需为实体创建一个 mendixentity 对象并分配属性列表。
mendixentity 和 mendixattribute 是我们稍后将用来映射到 json 的类:
public record mendixentity( string name, list attributes) {}public record mendixattribute( string name, attributetype type, associationtype associationtype, string entitytype) { public enum attributetype { string, integer, decimal, auto_number, boolean, enum, entity; } public enum associationtype { one_to_one, one_to_many }}
登录后复制
最后,我们使用 jackson 将 list 保存到 json 文件中。
将实体导入 mendix
有趣的部分来了,我们如何读取上面生成的 json 文件并从中创建 mendix 实体?
mendix 的 platform sdk 有一个 typescript api 可以与之交互。
首先,我们将创建对象来表示我们的实体和属性,以及关联和属性类型的枚举:
interface importedentity { name: string; generalization: string; attributes: importedattribute[];}interface importedattribute { name: string; type: importedattributetype; entitytype: string; associationtype: importedassociationtype;}enum importedassociationtype { one_to_one = "one_to_one", one_to_many = "one_to_many"}enum importedattributetype { integer = "integer", string = "string", decimal = "decimal", auto_number = "auto_number", boolean = "boolean", enum = "enum", entity = "entity"}
登录后复制
接下来,我们需要使用 appid 获取我们的应用程序,创建临时工作副本,打开模型,并找到我们感兴趣的域模型:
const client = new mendixplatformclient();const app = await client.getapp(appid);const workingcopy = await app.createtemporaryworkingcopy("main");const model = await workingcopy.openmodel();const domainmodelinterface = model.alldomainmodels().filter(dm => dm.containerasmodule.name === myfirstmodule")[0];const domainmodel = await domainmodelinterface.load();
登录后复制
sdk 实际上会从 git 中提取我们的 mendix 应用程序并进行处理。
读取 json 文件后,我们将循环实体:
function createmendixentities(domainmodel: domainmodels.domainmodel, entitiesinjson: any) { const importedentities: importedentity[] = json.parse(entitiesinjson); importedentities.foreach((importedentity, i) => { const mendixentity = domainmodels.entity.createin(domainmodel); mendixentity.name = importedentity.name; processattributes(importedentity, mendixentity); }); importedentities.foreach(importedentity => { const mendixparententity = domainmodel.entities.find(e => e.name === importedentity.name) as domainmodels.entity; processassociations(importedentity, domainmodel, mendixparententity); });}
登录后复制
这里我们使用domainmodels.entity.createin(domainmodel);在我们的域模型中创建一个新实体并为其分配一个名称。我们可以分配更多属性,例如文档、索引,甚至实体在域模型中呈现的位置。
我们在单独的函数中处理属性:
function processattributes(importedentity: importedentity, mendixentity: domainmodels.entity) { importedentity.attributes.filter(a => a.type !== importedattributetype.entity).foreach(a => { const mendixattribute = domainmodels.attribute.createin(mendixentity); mendixattribute.name = capitalize(getattributename(a.name, importedentity)); mendixattribute.type = assignattributetype(a.type, mendixattribute); });}
登录后复制
这里我们唯一需要付出一些努力的就是将属性类型映射到有效的 mendix 类型。
接下来我们处理关联。首先,由于在我们的java实体中关联是通过字段声明的,因此我们需要区分哪些字段是简单属性,哪些字段是关联。为此,我们只需要检查它是实体类型还是原始类型:
importedentity.attributes.filter(a => a.type === importedattributetype.entity) ...
登录后复制
让我们创建关联:
const mendixassociation = domainmodels.association.createin(domainmodel);const mendixchildentity = domainmodel.entities.find(e => e.name === a.entitytype) as domainmodelsentity;mendixassociation.name = `${mendixparententity?.name}_${mendixchildentity?.name}`;mendixassociation.parent = mendixparententity;mendixassociation.child = mendixchildentity;mendixassociation.type = a.associationtype === importedassociationtype.one_to_one || a.associationtype === importedassociationtype.one_to_many ? domainmodels.associationtype.reference : domainmodels.associationtype.referenceset;mendixassocation.owner = a.associationtype === importedassociationtype.one_to_one ? domainmodelsassociationowner.both : domainmodels.associationowner.default;
登录后复制
除了名称之外,我们还有 4 个重要的属性需要设置:
父实体。这是当前实体。
子实体。在最后一步中,我们为每个 java 实体创建了 mendix 实体。现在我们只需要根据实体中java字段的类型找到匹配的实体:
domainmodel.entities.find(e => e.name === a.entitytype) as domainmodelsentity;
登录后复制
关联类型。如果是一对一的,它会映射到一个引用。如果是一对多,则映射到参考集。我们现在将跳过多对多。
协会所有者。一对一和多对多关联都具有相同的所有者类型:两者。对于一对一,所有者类型必须为默认。
mendix platform sdk 将在我们的 mendix 应用程序的本地工作副本中创建实体。现在我们只需要告诉它提交更改:
async function commitChanges(model: IModel, workingCopy: OnlineWorkingCopy, entitiesFile: string) { await model.flushChanges(); await workingCopy.commitToRepository("main", { commitMessage: `Imported DB entities from ${entitiesFile}` });}
登录后复制
几秒钟后,您可以在 mendix studio pro 中打开应用程序并验证结果:
现在你已经看到了:猫和人的实体,它们之间存在一对一的关联。
如果您想亲自尝试或查看完整代码,请访问此存储库。
对未来的想法
在这个示例中,我使用了 java/spring 应用程序进行转换,因为我最精通它,但任何应用程序都可以使用。 只需能够读取类型数据(静态或运行时)来提取类和字段名称就足够了。我很好奇尝试读取一些 java 逻辑并将其导出到 mendix 微流程。我们可能无法真正转换业务逻辑本身,但我们应该能够获得它的结构(至少是业务方法签名?)。本文中的代码可以推广并制作成一个库:json 格式可以保持不变,并且可以有一个库用于导出 java 类型,另一个库用于导入 mendix 实体。我们可以使用相同的方法进行相反的操作:将 mendix 转换为另一种语言。
结论
mendix platform sdk 是一项强大的功能,允许以编程方式与 mendix 应用程序进行交互。他们列出了一些示例用例,例如导入/导出代码、分析应用程序复杂性。
如果您有兴趣,请看一下。
对于本文,您可以在此处找到完整代码。
以上就是将 JPA 实体转换为 Mendix的详细内容,更多请关注【创想鸟】其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。
发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2607376.html