Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor ItemSetService(updateSet Item) and ConfigChangeContentBuilder a little bit #4515

Merged
merged 1 commit into from
Aug 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -73,20 +73,18 @@ public ItemDTO create(@PathVariable("appId") String appId,
@PathVariable("namespaceName") String namespaceName, @RequestBody ItemDTO dto) {
Item entity = BeanUtils.transform(Item.class, dto);

ConfigChangeContentBuilder builder = new ConfigChangeContentBuilder();
Item managedEntity = itemService.findOne(appId, clusterName, namespaceName, entity.getKey());
if (managedEntity != null) {
throw new BadRequestException("item already exists");
}
entity = itemService.save(entity);
builder.createItem(entity);
dto = BeanUtils.transform(ItemDTO.class, entity);

Commit commit = new Commit();
commit.setAppId(appId);
commit.setClusterName(clusterName);
commit.setNamespaceName(namespaceName);
commit.setChangeSets(builder.build());
commit.setChangeSets(new ConfigChangeContentBuilder().createItem(entity).build());
commit.setDataChangeCreatedBy(dto.getDataChangeLastModifiedBy());
commit.setDataChangeLastModifiedBy(dto.getDataChangeLastModifiedBy());
commitService.save(commit);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.ctrip.framework.apollo.common.exception.BadRequestException;
import com.ctrip.framework.apollo.common.exception.NotFoundException;
import com.ctrip.framework.apollo.common.utils.BeanUtils;
import java.util.List;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
Expand Down Expand Up @@ -68,54 +69,17 @@ public ItemChangeSets updateSet(String appId, String clusterName,
ConfigChangeContentBuilder configChangeContentBuilder = new ConfigChangeContentBuilder();

if (!CollectionUtils.isEmpty(changeSet.getCreateItems())) {
for (ItemDTO item : changeSet.getCreateItems()) {
if (item.getNamespaceId() != namespace.getId()) {
throw new BadRequestException("Invalid request, item and namespace do not match!");
}

Item entity = BeanUtils.transform(Item.class, item);
entity.setDataChangeCreatedBy(operator);
entity.setDataChangeLastModifiedBy(operator);
Item createdItem = itemService.save(entity);
configChangeContentBuilder.createItem(createdItem);
}
this.doCreateItems(changeSet.getCreateItems(), namespace, operator, configChangeContentBuilder);
auditService.audit("ItemSet", null, Audit.OP.INSERT, operator);
}

if (!CollectionUtils.isEmpty(changeSet.getUpdateItems())) {
for (ItemDTO item : changeSet.getUpdateItems()) {
Item entity = BeanUtils.transform(Item.class, item);

Item managedItem = itemService.findOne(entity.getId());
if (managedItem == null) {
throw new NotFoundException(String.format("item not found.(key=%s)", entity.getKey()));
}
if (managedItem.getNamespaceId() != namespace.getId()) {
throw new BadRequestException("Invalid request, item and namespace do not match!");
}
Item beforeUpdateItem = BeanUtils.transform(Item.class, managedItem);

//protect. only value,comment,lastModifiedBy,lineNum can be modified
managedItem.setValue(entity.getValue());
managedItem.setComment(entity.getComment());
managedItem.setLineNum(entity.getLineNum());
managedItem.setDataChangeLastModifiedBy(operator);

Item updatedItem = itemService.update(managedItem);
configChangeContentBuilder.updateItem(beforeUpdateItem, updatedItem);

}
this.doUpdateItems(changeSet.getUpdateItems(), namespace, operator, configChangeContentBuilder);
auditService.audit("ItemSet", null, Audit.OP.UPDATE, operator);
}

if (!CollectionUtils.isEmpty(changeSet.getDeleteItems())) {
for (ItemDTO item : changeSet.getDeleteItems()) {
Item deletedItem = itemService.delete(item.getId(), operator);
if (deletedItem.getNamespaceId() != namespace.getId()) {
throw new BadRequestException("Invalid request, item and namespace do not match!");
}
configChangeContentBuilder.deleteItem(deletedItem);
}
this.doDeleteItems(changeSet.getDeleteItems(), namespace, operator, configChangeContentBuilder);
auditService.audit("ItemSet", null, Audit.OP.DELETE, operator);
}

Expand All @@ -125,7 +89,61 @@ public ItemChangeSets updateSet(String appId, String clusterName,
}

return changeSet;
}

private void doDeleteItems(List<ItemDTO> toDeleteItems, Namespace namespace, String operator,
ConfigChangeContentBuilder configChangeContentBuilder) {

for (ItemDTO item : toDeleteItems) {
Item deletedItem = itemService.delete(item.getId(), operator);
if (deletedItem.getNamespaceId() != namespace.getId()) {
throw new BadRequestException("Invalid request, item and namespace do not match!");
}

configChangeContentBuilder.deleteItem(deletedItem);
}
}

private void doUpdateItems(List<ItemDTO> toUpdateItems, Namespace namespace, String operator,
ConfigChangeContentBuilder configChangeContentBuilder) {

for (ItemDTO item : toUpdateItems) {
Item entity = BeanUtils.transform(Item.class, item);

Item managedItem = itemService.findOne(entity.getId());
if (managedItem == null) {
throw new NotFoundException(String.format("item not found.(key=%s)", entity.getKey()));
}
if (managedItem.getNamespaceId() != namespace.getId()) {
throw new BadRequestException("Invalid request, item and namespace do not match!");
}
Item beforeUpdateItem = BeanUtils.transform(Item.class, managedItem);

//protect. only value,comment,lastModifiedBy,lineNum can be modified
managedItem.setValue(entity.getValue());
managedItem.setComment(entity.getComment());
managedItem.setLineNum(entity.getLineNum());
managedItem.setDataChangeLastModifiedBy(operator);

Item updatedItem = itemService.update(managedItem);
configChangeContentBuilder.updateItem(beforeUpdateItem, updatedItem);
}
}

private void doCreateItems(List<ItemDTO> toCreateItems, Namespace namespace, String operator,
ConfigChangeContentBuilder configChangeContentBuilder) {

for (ItemDTO item : toCreateItems) {
if (item.getNamespaceId() != namespace.getId()) {
throw new BadRequestException("Invalid request, item and namespace do not match!");
}

Item entity = BeanUtils.transform(Item.class, item);
entity.setDataChangeCreatedBy(operator);
entity.setDataChangeLastModifiedBy(operator);
Item createdItem = itemService.save(entity);
configChangeContentBuilder.createItem(createdItem);
}
}

private void createCommit(String appId, String clusterName, String namespaceName, String configChangeContent,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,23 @@
import java.util.List;
import org.springframework.beans.BeanUtils;


public class ConfigChangeContentBuilder {

private static final Gson GSON = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();

private List<Item> createItems = new LinkedList<>();
private List<ItemPair> updateItems = new LinkedList<>();
private List<Item> deleteItems = new LinkedList<>();

private final List<Item> createItems = new LinkedList<>();
private final List<ItemPair> updateItems = new LinkedList<>();
private final List<Item> deleteItems = new LinkedList<>();

public ConfigChangeContentBuilder createItem(Item item) {
if (!StringUtils.isEmpty(item.getKey())){
if (!StringUtils.isEmpty(item.getKey())) {
createItems.add(cloneItem(item));
}
return this;
}

public ConfigChangeContentBuilder updateItem(Item oldItem, Item newItem) {
if (!oldItem.getValue().equals(newItem.getValue())){
if (!oldItem.getValue().equals(newItem.getValue())) {
ItemPair itemPair = new ItemPair(cloneItem(oldItem), cloneItem(newItem));
updateItems.add(itemPair);
}
Expand All @@ -57,12 +55,13 @@ public ConfigChangeContentBuilder deleteItem(Item item) {
return this;
}

public boolean hasContent(){
public boolean hasContent() {
return !createItems.isEmpty() || !updateItems.isEmpty() || !deleteItems.isEmpty();
}

public String build() {
//因为事务第一段提交并没有更新时间,所以build时统一更新
// Because there is no update time for the first commit to the transaction,
// it is updated uniformly during building.
Date now = new Date();

for (Item item : createItems) {
Expand Down