From fbe82afad7e78f91d180f540241af5ae023a0510 Mon Sep 17 00:00:00 2001 From: datagear Date: Sun, 20 Jan 2019 15:14:06 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BC=96=E8=BE=91=E8=A1=A8=E6=A0=BC=EF=BC=9A?= =?UTF-8?q?=E5=AE=8C=E5=96=84=E5=8D=95=E5=85=83=E5=B1=9E=E6=80=A7=E5=8D=95?= =?UTF-8?q?=E5=85=83=E6=A0=BC=E7=9A=84=E6=9C=8D=E5=8A=A1=E7=AB=AF=E4=BF=9D?= =?UTF-8?q?=E5=AD=98=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/org/datagear/model/support/MU.java | 36 +++ .../persistence/PersistenceManager.java | 30 +++ .../AbstractModelDataAccessObject.java | 22 +- .../support/DefaultPersistenceManager.java | 27 ++- .../support/UpdatePersistenceOperation.java | 213 ++++++++++-------- .../web/controller/DataController.java | 108 ++++++--- .../web/convert/ClassDataConverter.java | 3 + 7 files changed, 306 insertions(+), 133 deletions(-) diff --git a/datagear-model/src/main/java/org/datagear/model/support/MU.java b/datagear-model/src/main/java/org/datagear/model/support/MU.java index 1b91fe88..99cccb59 100644 --- a/datagear-model/src/main/java/org/datagear/model/support/MU.java +++ b/datagear-model/src/main/java/org/datagear/model/support/MU.java @@ -4,6 +4,7 @@ package org.datagear.model.support; +import org.datagear.model.InstanceCreationException; import org.datagear.model.Model; import org.datagear.model.Property; @@ -684,4 +685,39 @@ public class MU { setPropertyValues(model, obj, model.getProperties(), propertyValues); } + + /** + * 创建{@linkplain Model}实例对象。 + * + * @param model + * @return + * @throws InstanceCreationException + */ + public static Object instance(Model model) throws InstanceCreationException + { + return model.newInstance(); + } + + /** + * 浅拷贝对象。 + * + * @param model + * @param obj + * @return + * @throws InstanceCreationException + */ + public static Object clone(Model model, Object obj) throws InstanceCreationException + { + Object clonedObj = instance(model); + + Property[] properties = model.getProperties(); + + for (Property property : properties) + { + Object pv = property.get(obj); + property.set(clonedObj, pv); + } + + return clonedObj; + } } diff --git a/datagear-persistence/src/main/java/org/datagear/persistence/PersistenceManager.java b/datagear-persistence/src/main/java/org/datagear/persistence/PersistenceManager.java index a26c3305..ef2f4b08 100644 --- a/datagear-persistence/src/main/java/org/datagear/persistence/PersistenceManager.java +++ b/datagear-persistence/src/main/java/org/datagear/persistence/PersistenceManager.java @@ -8,6 +8,7 @@ import java.sql.Connection; import java.util.List; import org.datagear.model.Model; +import org.datagear.model.Property; import org.datagear.model.support.PropertyPathInfo; import org.datagear.persistence.support.ExpressionEvaluationContext; @@ -79,6 +80,20 @@ public interface PersistenceManager int update(Connection cn, Model model, Object originalObj, Object updateObj, boolean updateMultipleProperty) throws PersistenceException; + /** + * 更新数据对象。 + * + * @param cn + * @param model + * @param updateProperties + * @param originalObj + * @param updateObj + * @return + * @throws PersistenceException + */ + int update(Connection cn, Model model, Property[] updateProperties, Object originalObj, Object updateObj) + throws PersistenceException; + /** * 更新数据。 * @@ -164,6 +179,21 @@ public interface PersistenceManager int updateMultiplePropValueElement(Connection cn, Model model, Object obj, PropertyPathInfo propertyPathInfo, Object propertyValueElement) throws PersistenceException; + /** + * 更新多元属性值元素。 + * + * @param cn + * @param model + * @param obj + * @param propertyPathInfo + * @param updatePropertyProperties + * @param propertyValueElement + * @return + * @throws PersistenceException + */ + int updateMultiplePropValueElement(Connection cn, Model model, Object obj, PropertyPathInfo propertyPathInfo, + Property[] updatePropertyProperties, Object propertyValueElement) throws PersistenceException; + /** * 删除对象。 * diff --git a/datagear-persistence/src/main/java/org/datagear/persistence/support/AbstractModelDataAccessObject.java b/datagear-persistence/src/main/java/org/datagear/persistence/support/AbstractModelDataAccessObject.java index d7f20746..e572f011 100644 --- a/datagear-persistence/src/main/java/org/datagear/persistence/support/AbstractModelDataAccessObject.java +++ b/datagear-persistence/src/main/java/org/datagear/persistence/support/AbstractModelDataAccessObject.java @@ -731,7 +731,18 @@ public class AbstractModelDataAccessObject extends AbstractDataAccessObject */ protected RelationMapper[] getRelationMappers(Model model) { - Property[] properties = model.getProperties(); + return getRelationMappers(model, model.getProperties()); + } + + /** + * 获取{@linkplain RelationMapper}。 + * + * @param model + * @param properties + * @return + */ + protected RelationMapper[] getRelationMappers(Model model, Property[] properties) + { RelationMapper[] relationMappers = new RelationMapper[properties.length]; for (int i = 0; i < properties.length; i++) @@ -1443,10 +1454,13 @@ public class AbstractModelDataAccessObject extends AbstractDataAccessObject { List> indexValues = new ArrayList>(); - for (int j = 0; j < objs.length; j++) + if (objs != null) { - if (MU.isModelData(models[i], objs[j])) - indexValues.add(new IndexValue(j, objs[j])); + for (int j = 0; j < objs.length; j++) + { + if (MU.isModelData(models[i], objs[j])) + indexValues.add(new IndexValue(j, objs[j])); + } } indexValuess[i] = indexValues; diff --git a/datagear-persistence/src/main/java/org/datagear/persistence/support/DefaultPersistenceManager.java b/datagear-persistence/src/main/java/org/datagear/persistence/support/DefaultPersistenceManager.java index ee560633..7957342d 100644 --- a/datagear-persistence/src/main/java/org/datagear/persistence/support/DefaultPersistenceManager.java +++ b/datagear-persistence/src/main/java/org/datagear/persistence/support/DefaultPersistenceManager.java @@ -193,14 +193,25 @@ public class DefaultPersistenceManager extends AbstractModelDataAccessObject imp { Dialect dialect = this.dialectSource.getDialect(cn); - return this.updatePersistenceOperation.update(cn, dialect, getTableName(model), model, originalObj, updateObj); + return this.updatePersistenceOperation.update(cn, dialect, getTableName(model), model, null, originalObj, + updateObj); + } + + @Override + public int update(Connection cn, Model model, Property[] updateProperties, Object originalObj, Object updateObj) + throws PersistenceException + { + Dialect dialect = this.dialectSource.getDialect(cn); + + return this.updatePersistenceOperation.update(cn, dialect, getTableName(model), model, updateProperties, + originalObj, updateObj); } @Override public int update(Connection cn, Dialect dialect, String table, Model model, Object originalObj, Object updateObj, ExpressionEvaluationContext expressionEvaluationContext) throws PersistenceException { - return this.updatePersistenceOperation.update(cn, dialect, table, model, originalObj, updateObj, + return this.updatePersistenceOperation.update(cn, dialect, table, model, null, originalObj, updateObj, expressionEvaluationContext); } @@ -241,7 +252,7 @@ public class DefaultPersistenceManager extends AbstractModelDataAccessObject imp SqlBuilder condition = buildRecordCondition(cn, dialect, ownerModel, ownerObj, null); return updatePersistenceOperation.updatePropertyTableData(cn, dialect, getTableName(ownerModel), model, - condition, property, propertyModelMapper, propertyPathInfo.getValueTail(), propValue); + condition, property, propertyModelMapper, null, propertyPathInfo.getValueTail(), propValue); } @Override @@ -280,6 +291,13 @@ public class DefaultPersistenceManager extends AbstractModelDataAccessObject imp @Override public int updateMultiplePropValueElement(Connection cn, Model model, Object obj, PropertyPathInfo propertyPathInfo, Object propertyValueElement) throws PersistenceException + { + return updateMultiplePropValueElement(cn, model, obj, propertyPathInfo, null, propertyValueElement); + } + + @Override + public int updateMultiplePropValueElement(Connection cn, Model model, Object obj, PropertyPathInfo propertyPathInfo, + Property[] updatePropertyProperties, Object propertyValueElement) throws PersistenceException { checkPropertyPathInfoMultipleTail(propertyPathInfo); checkPropertyPathInfoModelTail(propertyPathInfo); @@ -300,7 +318,8 @@ public class DefaultPersistenceManager extends AbstractModelDataAccessObject imp SqlBuilder condition = buildRecordCondition(cn, dialect, ownerModel, ownerObj, null); return updatePersistenceOperation.updatePropertyTableData(cn, dialect, getTableName(ownerModel), ownerModel, - condition, property, propertyModelMapper, oldPropValueElement, propertyValueElement); + condition, property, propertyModelMapper, updatePropertyProperties, oldPropValueElement, + propertyValueElement); } @Override diff --git a/datagear-persistence/src/main/java/org/datagear/persistence/support/UpdatePersistenceOperation.java b/datagear-persistence/src/main/java/org/datagear/persistence/support/UpdatePersistenceOperation.java index cfd47f2a..b583a009 100644 --- a/datagear-persistence/src/main/java/org/datagear/persistence/support/UpdatePersistenceOperation.java +++ b/datagear-persistence/src/main/java/org/datagear/persistence/support/UpdatePersistenceOperation.java @@ -85,98 +85,109 @@ public class UpdatePersistenceOperation extends AbstractExpressionModelPersisten } /** - * 更新。 * * @param cn * @param dialect * @param table * @param model + * @param updateProperties + * 要更新的属性,为{@code null}表示全部更新 * @param originalObj * 原始数据 * @param updateObj * 待更新的数据 * @return */ - public int update(Connection cn, Dialect dialect, String table, Model model, Object originalObj, Object updateObj) + public int update(Connection cn, Dialect dialect, String table, Model model, Property[] updateProperties, + Object originalObj, Object updateObj) { SqlBuilder originalCondition = buildRecordCondition(cn, dialect, model, originalObj, null); - return update(cn, dialect, table, model, originalCondition, originalObj, updateObj, null, null, null, + return update(cn, dialect, table, model, updateProperties, originalCondition, originalObj, updateObj, null, + null, null, new ExpressionEvaluationContext()); + } + + /** + * 更新。 + * + * @param cn + * @param dialect + * @param table + * @param model + * @param updateProperties + * 要更新的属性,为{@code null}表示全部更新 + * @param originalObj + * 原始数据 + * @param updateObj + * 待更新的数据 + * @param expressionEvaluationContext + * @return + */ + public int update(Connection cn, Dialect dialect, String table, Model model, Property[] updateProperties, + Object originalObj, Object updateObj, ExpressionEvaluationContext expressionEvaluationContext) + { + SqlBuilder originalCondition = buildRecordCondition(cn, dialect, model, originalObj, null); + + return update(cn, dialect, table, model, updateProperties, originalCondition, originalObj, updateObj, null, + null, null, expressionEvaluationContext); + } + + /** + * 更新属性表数据。 + * + * @param cn + * @param dialect + * @param table + * @param model + * @param condition + * @param property + * @param propertyModelMapper + * @param updatePropertyProperties + * 要更新属性模型的属性数组,如果为{@code null},则全部更新。 + * @param originalPropertyValue + * 原始属性值 + * @param updatePropertyValue + * 待更新的属性值,允许为{@code null} + * @return + */ + public int updatePropertyTableData(Connection cn, Dialect dialect, String table, Model model, SqlBuilder condition, + Property property, PropertyModelMapper propertyModelMapper, Property[] updatePropertyProperties, + Object originalPropertyValue, Object updatePropertyValue) + { + return updatePropertyTableData(cn, dialect, table, model, condition, property, propertyModelMapper, + updatePropertyProperties, originalPropertyValue, updatePropertyValue, null, true, new ExpressionEvaluationContext()); } /** - * 更新。 + * 更新属性表数据。 * * @param cn * @param dialect * @param table * @param model - * @param originalObj - * 原始数据 - * @param updateObj - * 待更新的数据 + * @param condition + * @param property + * @param propertyModelMapper + * @param updatePropertyProperties + * 要更新属性模型的属性数组,如果为{@code null},则全部更新。 + * @param originalPropertyValue + * 原始属性值 + * @param updatePropertyValue + * 待更新的属性值,允许为{@code null} * @param expressionEvaluationContext * @return */ - public int update(Connection cn, Dialect dialect, String table, Model model, Object originalObj, Object updateObj, + public int updatePropertyTableData(Connection cn, Dialect dialect, String table, Model model, SqlBuilder condition, + Property property, PropertyModelMapper propertyModelMapper, Property[] updatePropertyProperties, + Object originalPropertyValue, Object updatePropertyValue, ExpressionEvaluationContext expressionEvaluationContext) { - SqlBuilder originalCondition = buildRecordCondition(cn, dialect, model, originalObj, null); - - return update(cn, dialect, table, model, originalCondition, originalObj, updateObj, null, null, null, + return updatePropertyTableData(cn, dialect, table, model, condition, property, propertyModelMapper, + updatePropertyProperties, originalPropertyValue, updatePropertyValue, null, true, expressionEvaluationContext); } - /** - * 更新属性表数据。 - * - * @param cn - * @param dialect - * @param table - * @param model - * @param condition - * @param property - * @param propertyModelMapper - * @param originalPropertyValue - * 原始属性值 - * @param updatePropertyValue - * 待更新的属性值,允许为{@code null} - * @return - */ - public int updatePropertyTableData(Connection cn, Dialect dialect, String table, Model model, SqlBuilder condition, - Property property, PropertyModelMapper propertyModelMapper, Object originalPropertyValue, - Object updatePropertyValue) - { - return updatePropertyTableData(cn, dialect, table, model, condition, property, propertyModelMapper, - originalPropertyValue, updatePropertyValue, null, true, new ExpressionEvaluationContext()); - } - - /** - * 更新属性表数据。 - * - * @param cn - * @param dialect - * @param table - * @param model - * @param condition - * @param property - * @param propertyModelMapper - * @param originalPropertyValue - * 原始属性值 - * @param updatePropertyValue - * 待更新的属性值,允许为{@code null} - * @param expressionEvaluationContext - * @return - */ - public int updatePropertyTableData(Connection cn, Dialect dialect, String table, Model model, SqlBuilder condition, - Property property, PropertyModelMapper propertyModelMapper, Object originalPropertyValue, - Object updatePropertyValue, ExpressionEvaluationContext expressionEvaluationContext) - { - return updatePropertyTableData(cn, dialect, table, model, condition, property, propertyModelMapper, - originalPropertyValue, updatePropertyValue, null, true, expressionEvaluationContext); - } - /** * 更新。 * @@ -184,6 +195,8 @@ public class UpdatePersistenceOperation extends AbstractExpressionModelPersisten * @param dialect * @param table * @param model + * @param updateProperties + * 要更新的属性,为{@code null}表示全部更新 * @param originalCondition * 用于确定原始数据记录的模型表条件 * @param originalObj @@ -199,22 +212,25 @@ public class UpdatePersistenceOperation extends AbstractExpressionModelPersisten * @param expressionEvaluationContext * @return */ - protected int update(Connection cn, Dialect dialect, String table, Model model, SqlBuilder originalCondition, - Object originalObj, Object updateObj, String[] extraColumnNames, Object[] extraColumnValues, - String ignorePropertyName, ExpressionEvaluationContext expressionEvaluationContext) + protected int update(Connection cn, Dialect dialect, String table, Model model, Property[] updateProperties, + SqlBuilder originalCondition, Object originalObj, Object updateObj, String[] extraColumnNames, + Object[] extraColumnValues, String ignorePropertyName, + ExpressionEvaluationContext expressionEvaluationContext) { int count = 0; - Property[] properties = model.getProperties(); - RelationMapper[] relationMappers = getRelationMappers(model); + if (updateProperties == null) + updateProperties = model.getProperties(); - Object[] originalPropertyValues = MU.getPropertyValues(model, originalObj); - Object[] updatePropertyValues = MU.getPropertyValues(model, updateObj, properties); + RelationMapper[] relationMappers = getRelationMappers(model, updateProperties); + + Object[] originalPropertyValues = MU.getPropertyValues(model, originalObj, updateProperties); + Object[] updatePropertyValues = MU.getPropertyValues(model, updateObj, updateProperties); // 先求得SQL表达式属性值并赋予obj,因为某些驱动程序并不支持任意设置Statement.getGeneratedKeys() - for (int i = 0; i < properties.length; i++) + for (int i = 0; i < updateProperties.length; i++) { - Property property = properties[i]; + Property property = updateProperties[i]; if (isUpdateIgnoreProperty(model, property, ignorePropertyName, true)) continue; @@ -238,9 +254,9 @@ public class UpdatePersistenceOperation extends AbstractExpressionModelPersisten // 先处理删除属性值,它不会受外键约束的影响; // 先处理KeyRule.isManually()为true的更新属性值操作,它不会受外键约束的影响,并且如果先更新模型表,里更的外键值可能会被更新,那么关联属性值更新则会失效; - for (int i = 0; i < properties.length; i++) + for (int i = 0; i < updateProperties.length; i++) { - Property property = properties[i]; + Property property = updateProperties[i]; if (isUpdateIgnoreProperty(model, property, ignorePropertyName, false)) continue; @@ -288,7 +304,7 @@ public class UpdatePersistenceOperation extends AbstractExpressionModelPersisten if (propertyKeyUpdateRule == null || propertyKeyUpdateRule.isManually()) { int myCount = updatePropertyTableData(cn, dialect, table, model, originalCondition, - property, pmm, originalPropertyValue, updatePropertyValue, updateObj, false, + property, pmm, null, originalPropertyValue, updatePropertyValue, updateObj, false, expressionEvaluationContext); if (myCount == 0) @@ -319,7 +335,7 @@ public class UpdatePersistenceOperation extends AbstractExpressionModelPersisten } // 更新模型表数据 - count = updateModelTableData(cn, dialect, table, model, originalCondition, properties, updateObj, + count = updateModelTableData(cn, dialect, table, model, originalCondition, updateProperties, updateObj, originalPropertyValues, extraColumnNames, extraColumnValues, ignorePropertyName); // 处理KeyRule.isManually()为false的更新属性值操作 @@ -332,7 +348,7 @@ public class UpdatePersistenceOperation extends AbstractExpressionModelPersisten Object updatePropertyValue = updateInfo.getUpdatePropertyValue(); int myCount = updatePropertyTableData(cn, dialect, table, model, updateCondition, - updateInfo.getProperty(), updateInfo.getPropertyModelMapper(), + updateInfo.getProperty(), updateInfo.getPropertyModelMapper(), null, originalPropertyValues[updateInfo.getPropertyIndex()], updatePropertyValue, null, false, expressionEvaluationContext); @@ -453,6 +469,8 @@ public class UpdatePersistenceOperation extends AbstractExpressionModelPersisten * @param condition * @param property * @param propertyModelMapper + * @param updatePropertyProperties + * 要更新属性模型的属性数组,如果为{@code null},则全部更新。 * @param originalPropertyValue * 原始属性值 * @param updatePropertyValue @@ -466,8 +484,8 @@ public class UpdatePersistenceOperation extends AbstractExpressionModelPersisten */ protected int updatePropertyTableData(Connection cn, Dialect dialect, String table, Model model, SqlBuilder condition, Property property, PropertyModelMapper propertyModelMapper, - Object originalPropertyValue, Object updatePropertyValue, Object keyUpdateObj, boolean updateModelTable, - ExpressionEvaluationContext expressionEvaluationContext) + Property[] updatePropertyProperties, Object originalPropertyValue, Object updatePropertyValue, + Object keyUpdateObj, boolean updateModelTable, ExpressionEvaluationContext expressionEvaluationContext) { int count = 0; @@ -476,21 +494,24 @@ public class UpdatePersistenceOperation extends AbstractExpressionModelPersisten PropertyModelMapper mpmm = propertyModelMapper.castModelTableMapperInfo(); count = updatePropertyTableDataForModelTableMapper(cn, dialect, table, model, condition, property, mpmm, - originalPropertyValue, updatePropertyValue, updateModelTable, expressionEvaluationContext); + updatePropertyProperties, originalPropertyValue, updatePropertyValue, updateModelTable, + expressionEvaluationContext); } else if (propertyModelMapper.isPropertyTableMapperInfo()) { PropertyModelMapper ppmm = propertyModelMapper.castPropertyTableMapperInfo(); count = updatePropertyTableDataForPropertyTableMapper(cn, dialect, table, model, condition, property, ppmm, - originalPropertyValue, updatePropertyValue, keyUpdateObj, expressionEvaluationContext); + updatePropertyProperties, originalPropertyValue, updatePropertyValue, keyUpdateObj, + expressionEvaluationContext); } else if (propertyModelMapper.isJoinTableMapperInfo()) { PropertyModelMapper jpmm = propertyModelMapper.castJoinTableMapperInfo(); count = updatePropertyTableDataForJoinTableMapper(cn, dialect, table, model, condition, property, jpmm, - originalPropertyValue, updatePropertyValue, keyUpdateObj, expressionEvaluationContext); + updatePropertyProperties, originalPropertyValue, updatePropertyValue, keyUpdateObj, + expressionEvaluationContext); } else throw new UnsupportedOperationException(); @@ -509,6 +530,8 @@ public class UpdatePersistenceOperation extends AbstractExpressionModelPersisten * 模型表查询条件,允许为{@code null} * @param property * @param propertyModelMapper + * @param updatePropertyProperties + * 要更新属性模型的属性数组,如果为{@code null},则全部更新。 * @param originalPropertyValue * 原始属性值,基本属性值时允许为{@code null} * @param updatePropertyValue @@ -520,8 +543,8 @@ public class UpdatePersistenceOperation extends AbstractExpressionModelPersisten */ protected int updatePropertyTableDataForModelTableMapper(Connection cn, Dialect dialect, String table, Model model, SqlBuilder condition, Property property, PropertyModelMapper propertyModelMapper, - Object originalPropertyValue, Object updatePropertyValue, boolean updateModelTable, - ExpressionEvaluationContext expressionEvaluationContext) + Property[] updatePropertyProperties, Object originalPropertyValue, Object updatePropertyValue, + boolean updateModelTable, ExpressionEvaluationContext expressionEvaluationContext) { int count = 0; @@ -534,7 +557,7 @@ public class UpdatePersistenceOperation extends AbstractExpressionModelPersisten Property[] properties = new Property[] { property }; Object[] originalPropertyValues = new Object[] { originalPropertyValue }; - Object updateObj = model.newInstance(); + Object updateObj = MU.instance(model); property.set(updateObj, updatePropertyValue); count = updateModelTableData(cn, dialect, table, model, condition, properties, updateObj, @@ -549,7 +572,7 @@ public class UpdatePersistenceOperation extends AbstractExpressionModelPersisten if (PMU.isPrivate(model, property, pmodel)) { - count = update(cn, dialect, table, pmodel, + count = update(cn, dialect, table, pmodel, null, buildRecordCondition(cn, dialect, pmodel, originalPropertyValue, null), originalPropertyValue, updatePropertyValue, null, null, getMappedByWith(mapper), expressionEvaluationContext); @@ -596,7 +619,8 @@ public class UpdatePersistenceOperation extends AbstractExpressionModelPersisten * @param condition * @param property * @param propertyModelMapper - * 属性表查询条件,允许为{@code null}。 + * @param updatePropertyProperties + * 要更新属性模型的属性数组,如果为{@code null},则全部更新。 * @param originalPropertyValue * 原始属性值 * @param updatePropertyValue @@ -608,8 +632,9 @@ public class UpdatePersistenceOperation extends AbstractExpressionModelPersisten */ protected int updatePropertyTableDataForPropertyTableMapper(Connection cn, Dialect dialect, String table, Model model, SqlBuilder condition, Property property, - PropertyModelMapper propertyModelMapper, Object originalPropertyValue, - Object updatePropertyValue, Object keyUpdateObj, ExpressionEvaluationContext expressionEvaluationContext) + PropertyModelMapper propertyModelMapper, Property[] updatePropertyProperties, + Object originalPropertyValue, Object updatePropertyValue, Object keyUpdateObj, + ExpressionEvaluationContext expressionEvaluationContext) { int count = 0; @@ -681,8 +706,8 @@ public class UpdatePersistenceOperation extends AbstractExpressionModelPersisten } else { - count = update(cn, dialect, getTableName(propertyModel), propertyModel, ptableCondition, - originalPropertyValue, updatePropertyValue, mkeyColumnNames, mkeyColumnValues, + count = update(cn, dialect, getTableName(propertyModel), propertyModel, updatePropertyProperties, + ptableCondition, originalPropertyValue, updatePropertyValue, mkeyColumnNames, mkeyColumnValues, getMappedByWith(propertyModelMapper.getMapper()), expressionEvaluationContext); } @@ -699,6 +724,8 @@ public class UpdatePersistenceOperation extends AbstractExpressionModelPersisten * @param condition * @param property * @param propertyModelMapper + * @param updatePropertyProperties + * 要更新属性模型的属性数组,如果为{@code null},则全部更新。 * @param originalPropertyValue * 原始属性值 * @param updatePropertyValue @@ -710,8 +737,8 @@ public class UpdatePersistenceOperation extends AbstractExpressionModelPersisten */ protected int updatePropertyTableDataForJoinTableMapper(Connection cn, Dialect dialect, String table, Model model, SqlBuilder condition, Property property, PropertyModelMapper propertyModelMapper, - Object originalPropertyValue, Object updatePropertyValue, Object keyUpdateObj, - ExpressionEvaluationContext expressionEvaluationContext) + Property[] updatePropertyProperties, Object originalPropertyValue, Object updatePropertyValue, + Object keyUpdateObj, ExpressionEvaluationContext expressionEvaluationContext) { int count = 0; @@ -728,8 +755,8 @@ public class UpdatePersistenceOperation extends AbstractExpressionModelPersisten if (PMU.isPrivate(model, property, propertyModel)) { - count = update(cn, dialect, getTableName(propertyModel), propertyModel, ptableCondition, - originalPropertyValue, updatePropertyValue, null, null, + count = update(cn, dialect, getTableName(propertyModel), propertyModel, updatePropertyProperties, + ptableCondition, originalPropertyValue, updatePropertyValue, null, null, getMappedByWith(propertyModelMapper.getMapper()), expressionEvaluationContext); if (keyUpdateObj != null) diff --git a/datagear-web/src/main/java/org/datagear/web/controller/DataController.java b/datagear-web/src/main/java/org/datagear/web/controller/DataController.java index eb5637a3..1eb8fe3c 100644 --- a/datagear-web/src/main/java/org/datagear/web/controller/DataController.java +++ b/datagear-web/src/main/java/org/datagear/web/controller/DataController.java @@ -29,6 +29,7 @@ import org.datagear.management.domain.Schema; import org.datagear.management.service.SchemaService; import org.datagear.model.Model; import org.datagear.model.Property; +import org.datagear.model.support.MU; import org.datagear.model.support.PropertyPath; import org.datagear.model.support.PropertyPathInfo; import org.datagear.persistence.ColumnPropertyPath; @@ -565,6 +566,17 @@ public class DataController extends AbstractSchemaModelController return "/data/data_form"; } + /** + * 混合保存,包括添加、修改、删除。 + * + * @param request + * @param response + * @param springModel + * @param schemaId + * @param tableName + * @return + * @throws Throwable + */ @RequestMapping(value = "/{schemaId}/{tableName}/savess", produces = CONTENT_TYPE_JSON) @ResponseBody public ResponseEntity savess(HttpServletRequest request, HttpServletResponse response, @@ -593,12 +605,13 @@ public class DataController extends AbstractSchemaModelController Connection cn = getConnection(); Object[] updates = modelDataConverter.convertToArray(updatesParam, model); - PropertyPathInfo[][] updatePropertyPathInfoss = null; Object[][] updatePropertyValuess = null; + Object[] adds = modelDataConverter.convertToArray(addsParam, model); + Object[] deletes = modelDataConverter.convertToArray(deletesParam, model); + if (updates != null && updates.length > 0) { - updatePropertyPathInfoss = new PropertyPathInfo[updatePropertyNamess.length][]; updatePropertyValuess = new Object[updatePropertyNamess.length][]; for (int i = 0; i < updates.length; i++) @@ -606,20 +619,19 @@ public class DataController extends AbstractSchemaModelController String[] updatePropertyNames = updatePropertyNamess[i]; Object[] updatePropertyValueParams = updatePropertyValueParamss[i]; - PropertyPathInfo[] updatePropertyPathInfos = new PropertyPathInfo[updatePropertyNames.length]; + Property[] updateProperties = new Property[updatePropertyNames.length]; Object[] updatePropertyValues = convertToPropertyValues(model, updates[i], updatePropertyNames, - updatePropertyValueParams, updatePropertyPathInfos); + updatePropertyValueParams, updateProperties); - updatePropertyPathInfoss[i] = updatePropertyPathInfos; - updatePropertyValuess[i] = updatePropertyValues; + Object updateObj = MU.clone(model, updates[i]); + MU.setPropertyValues(model, updateObj, updateProperties, updatePropertyValues); + + persistenceManager.update(cn, model, updateProperties, updates[i], updateObj); + + updatePropertyValuess[i] = MU.getPropertyValues(model, updateObj, updateProperties); } } - Object[] adds = modelDataConverter.convertToArray(addsParam, model); - Object[] deletes = modelDataConverter.convertToArray(deletesParam, model); - - // TODO 执行更新 - if (adds != null) { for (int i = 0; i < adds.length; i++) @@ -1410,6 +1422,18 @@ public class DataController extends AbstractSchemaModelController return responseEntity; } + /** + * 混合保存多元属性值,包括添加、修改、删除。 + * + * @param request + * @param response + * @param springModel + * @param schemaId + * @param tableName + * @param propertyPathParam + * @return + * @throws Throwable + */ @RequestMapping(value = "/{schemaId}/{tableName}/saveMultiplePropertyValueElementss", produces = CONTENT_TYPE_JSON) @ResponseBody public ResponseEntity saveMultiplePropertyValuess(HttpServletRequest request, @@ -1446,38 +1470,58 @@ public class DataController extends AbstractSchemaModelController Object data = modelDataConverter.convert(dataParam, model); PropertyPathInfo propertyPathInfo = ModelUtils.toPropertyPathInfoConcrete(model, propertyPath, data); - Model propertyModel = propertyPathInfo.getModelTail(); + Property propertyTail = propertyPathInfo.getPropertyTail(); + Model modelTail = propertyPathInfo.getModelTail(); + + Object updates = null; + + if (propertyTail.isArray()) + updates = modelDataConverter.convertToArray(updatesParam, modelTail); + else if (propertyTail.isCollection()) + updates = modelDataConverter.convertToCollection(updatesParam, modelTail, + propertyTail.getCollectionType()); + + propertyPathInfo.setValueTail(updates); - Object[] updates = modelDataConverter.convertToArray(updatesParam, propertyModel); - PropertyPathInfo[][] updatePropertyPathInfoss = null; Object[][] updatePropertyValuess = null; - if (updates != null && updates.length > 0) - { - updatePropertyPathInfoss = new PropertyPathInfo[updatePropertyNamess.length][]; - updatePropertyValuess = new Object[updatePropertyNamess.length][]; + Object[] adds = modelDataConverter.convertToArray(addsParam, modelTail); + Object[] deletes = modelDataConverter.convertToArray(deletesParam, modelTail); - for (int i = 0; i < updates.length; i++) + if (updates != null) + { + updatePropertyValuess = new Object[updatePropertyValueParamss.length][]; + + for (int i = 0; i < updatePropertyValueParamss.length; i++) { + PropertyPath myPropertyPath = PropertyPath + .valueOf(PropertyPath.concatElementIndex(propertyPathParam, i)); + PropertyPathInfo myPropertyPathInfo = ModelUtils.toPropertyPathInfoConcrete(model, + myPropertyPath, data); + String[] updatePropertyNames = updatePropertyNamess[i]; Object[] updatePropertyValueParams = updatePropertyValueParamss[i]; - PropertyPathInfo[] updatePropertyPathInfos = new PropertyPathInfo[updatePropertyNames.length]; - Object[] updatePropertyValues = convertToPropertyValues(propertyModel, updates[i], - updatePropertyNames, updatePropertyValueParams, updatePropertyPathInfos); + Property[] updatePropertyProperties = new Property[updatePropertyNames.length]; + Object[] updatePropertyPropertyValues = convertToPropertyValues(modelTail, + myPropertyPathInfo.getValueTail(), updatePropertyNames, updatePropertyValueParams, + updatePropertyProperties); - updatePropertyPathInfoss[i] = updatePropertyPathInfos; - updatePropertyValuess[i] = updatePropertyValues; + Object updateObj = MU.clone(model, myPropertyPathInfo.getValueTail()); + MU.setPropertyValues(model, updateObj, updatePropertyProperties, updatePropertyPropertyValues); + + persistenceManager.updateMultiplePropValueElement(cn, model, data, myPropertyPathInfo, + updatePropertyProperties, updateObj); + + updatePropertyValuess[i] = MU.getPropertyValues(modelTail, updateObj, updatePropertyProperties); } } - Object[] adds = modelDataConverter.convertToArray(addsParam, propertyModel); - Object[] deletes = modelDataConverter.convertToArray(deletesParam, propertyModel); + if (adds != null && adds.length > 0) + persistenceManager.insertMultiplePropValueElement(cn, model, data, propertyPathInfo, adds); - // TODO 执行更新 - - persistenceManager.insertMultiplePropValueElement(cn, model, data, propertyPathInfo, adds); - persistenceManager.deleteMultiplePropValueElement(cn, model, data, propertyPathInfo, deletes); + if (deletes != null && deletes.length > 0) + persistenceManager.deleteMultiplePropValueElement(cn, model, data, propertyPathInfo, deletes); Map responseDatas = new HashMap(); responseDatas.put("updatePropertyValuess", updatePropertyValuess); @@ -1661,7 +1705,7 @@ public class DataController extends AbstractSchemaModelController } protected Object[] convertToPropertyValues(Model model, Object data, String[] propertyNames, - Object[] propertyValueSources, PropertyPathInfo[] propertyPathInfos) + Object[] propertyValueSources, Property[] properties) { Object[] propertyValues = new Object[propertyNames.length]; @@ -1681,7 +1725,7 @@ public class DataController extends AbstractSchemaModelController else propertyValues[i] = modelDataConverter.convert(propertyValueSources[i], tailModel); - propertyPathInfos[i] = propertyPathInfo; + properties[i] = tailProperty; } return propertyValues; diff --git a/datagear-web/src/main/java/org/datagear/web/convert/ClassDataConverter.java b/datagear-web/src/main/java/org/datagear/web/convert/ClassDataConverter.java index e6fedfbc..21120ade 100644 --- a/datagear-web/src/main/java/org/datagear/web/convert/ClassDataConverter.java +++ b/datagear-web/src/main/java/org/datagear/web/convert/ClassDataConverter.java @@ -132,6 +132,9 @@ public class ClassDataConverter extends AbstractDataConverter if (map == null) return null; + if (type.isInstance(map)) + return (T) map; + if (isRefMap(map)) { String ref = getRefValue(map);