Skip to content

Commit

Permalink
重命名为OrmManyToManyMappingMeta和OrmMappingTableMeta
Browse files Browse the repository at this point in the history
  • Loading branch information
entropy-cloud committed Aug 13, 2024
1 parent 6673d47 commit 21f6e30
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@
/nop/codegen/xlib/web-gen.xlib
/nop/core/_module
/nop/core/app.module.yaml
/nop/core/model/DictProvider/DictProvider.xbiz
/nop/core/registry/api.register-model.xml
/nop/core/registry/dao-rule.register-model.xml
/nop/core/registry/dao-wf.register-model.xml
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import io.nop.orm.model.IOrmModel;
import io.nop.orm.model.OrmModelConstants;
import io.nop.orm.model.OrmRelationType;
import io.nop.orm.support.OrmManyToManyMappingMeta;
import io.nop.orm.support.OrmMappingTableMeta;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -67,19 +67,19 @@ public void transformModule(IOrmModel ormModel, NopDynModule dynModule) {
entityMetas.put(entityMeta.getEntityName(), entityMeta);
});

List<OrmManyToManyMappingMeta> mappingTables = new ArrayList<>();
List<OrmMappingTableMeta> mappingTables = new ArrayList<>();
ormModel.getEntityModels().forEach(entityModel -> {
// many-to-many的中间表不转换为实体表
if (entityModel.containsTag(OrmModelConstants.TAG_MANY_TO_MANY)) {
mappingTables.add(new OrmManyToManyMappingMeta(entityModel));
mappingTables.add(new OrmMappingTableMeta(entityModel));
return;
}

NopDynEntityMeta entityMeta = makeEntityMeta(entityModel.getName());
transformEntityMeta(entityModel, entityMeta);
});

for (OrmManyToManyMappingMeta mappingMeta : mappingTables) {
for (OrmMappingTableMeta mappingMeta : mappingTables) {
if (mappingMeta.getRefProp2() == null) {
LOG.warn("nop.dyn.ignore-invalid-many-to-many-mapping-table:{}", mappingMeta.getMappingTable());
continue;
Expand All @@ -99,7 +99,7 @@ public void transformModule(IOrmModel ormModel, NopDynModule dynModule) {
relMeta1.setTagsText(TagsHelper.toString(mappingMeta.getMappingTable().getTagSet()));
relMeta1.setRemark(mappingMeta.getMappingTable().getComment());
relMeta1.setRelationType(OrmRelationType.m2m.name());
relMeta1.setRelationName(mappingMeta.getRefSetPropName1());
relMeta1.setRelationName(mappingMeta.getRefPropName1());
relMeta1.setLeftPropName(OrmModelConstants.PROP_ID);
relMeta1.setRightPropName(OrmModelConstants.PROP_ID);
entityMeta1.getRelationMetasForEntity().add(relMeta1);
Expand All @@ -113,7 +113,7 @@ public void transformModule(IOrmModel ormModel, NopDynModule dynModule) {
relMeta2.setTagsText(TagsHelper.toString(mappingMeta.getMappingTable().getTagSet()));
relMeta2.setRemark(mappingMeta.getMappingTable().getComment());
relMeta2.setRelationType(OrmRelationType.m2m.name());
relMeta2.setRelationName(mappingMeta.getRefSetPropName2());
relMeta2.setRelationName(mappingMeta.getRefPropName2());
relMeta2.setLeftPropName(OrmModelConstants.PROP_ID);
relMeta2.setRightPropName(OrmModelConstants.PROP_ID);
entityMeta2.getRelationMetasForEntity().add(relMeta2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ public interface OrmModelConstants {

String TAG_MANY_TO_MANY = "many-to-many";

String TAG_ONE_TO_ONE = "one-to-one";

String TAG_MAPPING = "mapping";

String PROP_NAME_nopRevType = "nopRevType";
String PROP_NAME_nopRevBeginVer = "nopRevBeginVer";
String PROP_NAME_nopRevEndVer = "nopRevEndVer";
Expand Down Expand Up @@ -124,13 +128,13 @@ public interface OrmModelConstants {

String ORM_MANY_TO_MANY_REF_PROP = "orm:manyToManyRefProp";

String ORM_MANY_TO_MANY_REF_SET_NAME1 = "orm:manyToManyRefSetName1";
String ORM_MAPPING_PROP_NAME1 = "orm:mappingPropName1";

String ORM_MANY_TO_MANY_REF_SET_NAME2 = "orm:manyToManyRefSetName2";
String ORM_MAPPING_PROP_NAME2 = "orm:mappingPropName2";

String ORM_MANY_TO_MANY_REF_SET_DISPLAY_NAME1 = "orm:manyToManyRefSetDisplayName1";
String ORM_MAPPING_PROP_DISPLAY_NAME1 = "orm:mappingPropDisplayName1";

String ORM_MANY_TO_MANY_REF_SET_DISPLAY_NAME2 = "orm:manyToManyRefSetDisplayName2";
String ORM_MAPPING_PROP_DISPLAY_NAME2 = "orm:mappingPropDisplayName2";


String VAR_ENTITY = "entity";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,44 @@
package io.nop.orm.support;

import io.nop.api.core.util.Guard;
import io.nop.commons.util.CollectionHelper;
import io.nop.commons.util.StringHelper;
import io.nop.orm.OrmConstants;
import io.nop.orm.model.IEntityModel;
import io.nop.orm.model.IEntityRelationModel;
import io.nop.orm.model.OrmModelConstants;
import io.nop.orm.model.OrmRelationType;

import java.util.List;

import static io.nop.core.reflect.utils.BeanReflectHelper.getValueByFactoryMethod;

public class OrmManyToManyMappingMeta {
/**
* 专用于多对多关联的中间表
*/
public class OrmMappingTableMeta {
private final IEntityModel mappingTable;
private final IEntityRelationModel refProp1;
private final IEntityRelationModel refProp2;

public OrmManyToManyMappingMeta(IEntityModel mappingTable) {
public OrmMappingTableMeta(IEntityModel mappingTable) {
this.mappingTable = mappingTable;
List<? extends IEntityRelationModel> rels = mappingTable.getToOneRelations();
this.refProp1 = CollectionHelper.first(rels);
this.refProp2 = CollectionHelper.last(rels);

Guard.checkArgument(rels.size() == 2, "mappingTable must contains two to-one relations");
}

/**
* 中间表具有mapping标签或者many-to-man标签
*/
public static boolean isMappingTable(IEntityModel entityModel) {
return entityModel.containsTag(OrmModelConstants.TAG_MAPPING) || entityModel.containsTag(OrmModelConstants.TAG_MANY_TO_MANY);
}

public boolean isOneToOne() {
return getRelationType() == OrmRelationType.o2o;
return mappingTable.containsTag(OrmModelConstants.TAG_ONE_TO_ONE);
}

public boolean isManyToMany() {
return mappingTable.containsTag(OrmModelConstants.TAG_MANY_TO_MANY);
}

public IEntityModel getMappingTable() {
Expand All @@ -40,13 +53,30 @@ public IEntityRelationModel getRefProp2() {
return refProp2;
}

public String getRefSetPropName1() {
String refSetName = (String) mappingTable.prop_get(OrmModelConstants.ORM_MANY_TO_MANY_REF_SET_NAME1);
public String getRefPropName1() {
String refSetName = (String) mappingTable.prop_get(OrmModelConstants.ORM_MAPPING_PROP_NAME1);
if (StringHelper.isEmpty(refSetName))
refSetName = "related" + StringHelper.capitalize(refProp2.getName()) + "List";
return refSetName;
}

public String getRefPropName2() {
String refSetName = (String) mappingTable.prop_get(OrmModelConstants.ORM_MAPPING_PROP_NAME2);
if (StringHelper.isEmpty(refSetName))
refSetName = "related" + StringHelper.capitalize(refProp1.getName()) + "List";
return refSetName;
}

public String getRefPropDisplayName1() {
String displayName = (String) mappingTable.prop_get(OrmModelConstants.ORM_MAPPING_PROP_DISPLAY_NAME1);
return displayName;
}

public String getRefPropDisplayName2() {
String displayName = (String) mappingTable.prop_get(OrmModelConstants.ORM_MAPPING_PROP_DISPLAY_NAME2);
return displayName;
}

public String getJoinPropName1() {
if (refProp1.isSingleColumn()) {
return refProp1.getSingleColumnJoin().getLeftProp();
Expand All @@ -61,27 +91,20 @@ public String getJoinPropName2() {
return refProp2.getName();
}

public String getRefSetPropName1_label() {
return getRefSetPropName1() + "_label";
public String getRefPropName1_label() {
return getRefPropName1() + "_label";
}

public String getRefSetPropName1_ids() {
return getRefSetPropName1() + "_ids";
public String getRefPropName1_ids() {
return getRefPropName1() + "_ids";
}

public String getRefSetPropName2() {
String refSetName = (String) mappingTable.prop_get(OrmModelConstants.ORM_MANY_TO_MANY_REF_SET_NAME2);
if (StringHelper.isEmpty(refSetName))
refSetName = "related" + StringHelper.capitalize(refProp1.getName()) + "List";
return refSetName;
}

public String getRefSetPropName2_label() {
return getRefSetPropName2() + "_label";
public String getRefPropName2_label() {
return getRefPropName2() + "_label";
}

public String getRefSetPropName2_ids() {
return getRefSetPropName2() + "_ids";
public String getRefPropName2_ids() {
return getRefPropName2() + "_ids";
}

public String getRefEntityName1() {
Expand All @@ -100,14 +123,14 @@ public String getRefBizObjName2() {
return StringHelper.simpleClassName(getRefEntityName2());
}

public String getRefLabelPropName1() {
public String getRefLabelProp1() {
String labelProp = refProp1.getRefEntityModel().getLabelProp();
if (StringHelper.isEmpty(labelProp))
labelProp = OrmModelConstants.PROP_ID;
return labelProp;
}

public String getRefLabelPropName2() {
public String getRefLabelProp2() {
String labelProp = refProp2.getRefEntityModel().getLabelProp();
if (StringHelper.isEmpty(labelProp))
labelProp = OrmModelConstants.PROP_ID;
Expand All @@ -121,9 +144,4 @@ public String getMappingEntityName() {
public String getMappingTableName() {
return mappingTable.getTableName();
}

public OrmRelationType getRelationType() {
return getValueByFactoryMethod(OrmRelationType.class, mappingTable,
OrmConstants.EXT_ORM_RELATION_TYPE);
}
}
6 changes: 3 additions & 3 deletions nop-xdefs/src/main/resources/_vfs/nop/schema/orm/entity.xdef
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@
maxBatchLoadSize="int" checkVersionWhenLazyLoad="!boolean=true"
labelProp="string" stateProp="var-name"
entityModeEnabled="!boolean=false" dimensionalType="string"
biz:moduleId="string" orm:relationType="string"
orm:manyToManyRefSetName1="string" orm:manyToManyRefSetName2="string"
orm:manyToManyRefSetDisplayName1="string" orm:manyToManyRefSetDisplayName2="string"
biz:moduleId="string"
orm:mappingPropName1="string" orm:mappingPropName2="string"
orm:mappingPropDisplayName1="string" orm:mappingPropDisplayName2="string"
xdef:default-extends="/nop/core/defaults/default.entity.xml"
xdef:name="OrmEntityModel" xdef:bean-package="io.nop.orm.model"
x:schema="/nop/schema/xdef.xdef" xdef:check-ns="ui,biz"
Expand Down

0 comments on commit 21f6e30

Please sign in to comment.