Skip to content

Commit

Permalink
Merge pull request apolloconfig#286 from lepdou/0620_27
Browse files Browse the repository at this point in the history
commit history & bugfix empty value
  • Loading branch information
nobodyiam authored Jun 23, 2016
2 parents 57ddd52 + 4c0a754 commit d4ed153
Show file tree
Hide file tree
Showing 26 changed files with 568 additions and 157 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.ctrip.framework.apollo.adminservice.controller;

import com.ctrip.framework.apollo.biz.entity.Commit;
import com.ctrip.framework.apollo.biz.service.CommitService;
import com.ctrip.framework.apollo.common.utils.BeanUtils;
import com.ctrip.framework.apollo.core.dto.CommitDTO;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
public class CommitController {

@Autowired
private CommitService commitService;

@RequestMapping(value = "/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/commit")
public List<CommitDTO> find(@PathVariable String appId, @PathVariable String clusterName,
@PathVariable String namespaceName, Pageable pageable){

List<Commit> commits = commitService.find(appId, clusterName, namespaceName, pageable);
return BeanUtils.batchTransform(CommitDTO.class, commits);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,13 @@
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ctrip.framework.apollo.biz.entity.Commit;
import com.ctrip.framework.apollo.biz.entity.Item;
import com.ctrip.framework.apollo.biz.entity.Namespace;
import com.ctrip.framework.apollo.biz.service.CommitService;
import com.ctrip.framework.apollo.biz.service.ItemService;
import com.ctrip.framework.apollo.biz.service.NamespaceService;
import com.ctrip.framework.apollo.biz.utils.ConfigChangeContentBuilder;
import com.ctrip.framework.apollo.common.utils.BeanUtils;
import com.ctrip.framework.apollo.core.dto.ItemDTO;
import com.ctrip.framework.apollo.core.exception.NotFoundException;
Expand All @@ -22,16 +27,24 @@ public class ItemController {

@Autowired
private ItemService itemService;
@Autowired
private NamespaceService namespaceService;
@Autowired
private CommitService commitService;

@RequestMapping(path = "/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/items", method = RequestMethod.POST)
public ItemDTO createOrUpdate(@PathVariable("appId") String appId,
@PathVariable("clusterName") String clusterName,
@PathVariable("namespaceName") String namespaceName, @RequestBody ItemDTO dto) {
Item entity = BeanUtils.transfrom(Item.class, dto);

ConfigChangeContentBuilder builder = new ConfigChangeContentBuilder();
Item managedEntity = itemService.findOne(appId, clusterName, namespaceName, entity.getKey());
if (managedEntity != null) {
Item beforeUpdateItem = BeanUtils.transfrom(Item.class, managedEntity);
BeanUtils.copyEntityProperties(entity, managedEntity);
entity = itemService.update(managedEntity);
builder.updateItem(beforeUpdateItem, entity);
} else {
Item lastItem = itemService.findLastOne(appId, clusterName, namespaceName);
int lineNum = 1;
Expand All @@ -41,9 +54,19 @@ public ItemDTO createOrUpdate(@PathVariable("appId") String appId,
}
entity.setLineNum(lineNum);
entity = itemService.save(entity);
builder.createItem(entity);
}

dto = BeanUtils.transfrom(ItemDTO.class, entity);

Commit commit = new Commit();
commit.setAppId(appId);
commit.setClusterName(clusterName);
commit.setNamespaceName(namespaceName);
commit.setChangeSets(builder.build());
commit.setDataChangeCreatedBy(dto.getDataChangeLastModifiedBy());
commit.setDataChangeLastModifiedBy(dto.getDataChangeLastModifiedBy());
commitService.save(commit);

return dto;
}

Expand All @@ -54,6 +77,17 @@ public void delete(@PathVariable("itemId") long itemId, @RequestParam String ope
throw new NotFoundException("item not found for itemId " + itemId);
}
itemService.delete(entity.getId(), operator);

Namespace namespace = namespaceService.findOne(entity.getNamespaceId());

Commit commit = new Commit();
commit.setAppId(namespace.getAppId());
commit.setClusterName(namespace.getClusterName());
commit.setNamespaceName(namespace.getNamespaceName());
commit.setChangeSets(new ConfigChangeContentBuilder().deleteItem(entity).build());
commit.setDataChangeCreatedBy(operator);
commit.setDataChangeLastModifiedBy(operator);
commitService.save(commit);
}

@RequestMapping("/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/items")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
Expand All @@ -18,8 +19,13 @@ public class ItemSetController {
private ItemSetService itemSetService;

@RequestMapping(path = "/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/itemset", method = RequestMethod.POST)
public ResponseEntity<Void> create(@RequestBody ItemChangeSets changeSet) {
itemSetService.updateSet(changeSet);
public ResponseEntity<Void> create(@PathVariable String appId, @PathVariable String clusterName,
@PathVariable String namespaceName, @RequestBody ItemChangeSets changeSet) {

itemSetService.updateSet(appId, clusterName, namespaceName, changeSet);

return ResponseEntity.status(HttpStatus.OK).build();
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
@Where(clause = "isDeleted = 0")
public class Commit extends BaseEntity {

@Column(name = "ChangeSets", nullable = false)
@Column(name = "ChangeSets", length = 4048, nullable = false)
private String changeSets;

@Column(name = "AppId", nullable = false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

import com.ctrip.framework.apollo.biz.entity.Commit;

import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.PagingAndSortingRepository;

import java.util.List;

public interface CommitRepository extends PagingAndSortingRepository<Commit, Long> {

List<Commit> findByAppIdAndClusterNameAndNamespaceName(String appId, String clusterName,
String namespaceName);
List<Commit> findByAppIdAndClusterNameAndNamespaceNameOrderByIdDesc(String appId, String clusterName,
String namespaceName, Pageable pageable);

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,26 @@
import com.ctrip.framework.apollo.biz.repository.CommitRepository;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class CommitService {

@Autowired
private CommitRepository commitRepository;

public void save(Commit commit, String user){

@Transactional
public Commit save(Commit commit){
commit.setId(0);//protection
commit.setDataChangeCreatedBy(user);
commit.setDataChangeCreatedTime(new Date());
commitRepository.save(commit);
return commitRepository.save(commit);
}

public List<Commit> find(String appId, String clusterName, String namespaceName, Pageable page){
return commitRepository.findByAppIdAndClusterNameAndNamespaceNameOrderByIdDesc(appId, clusterName, namespaceName, page);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@
import org.springframework.util.CollectionUtils;

import com.ctrip.framework.apollo.biz.entity.Audit;
import com.ctrip.framework.apollo.biz.entity.Commit;
import com.ctrip.framework.apollo.biz.entity.Item;
import com.ctrip.framework.apollo.biz.repository.ItemRepository;
import com.ctrip.framework.apollo.biz.utils.ConfigChangeContentBuilder;
import com.ctrip.framework.apollo.common.utils.BeanUtils;
import com.ctrip.framework.apollo.core.dto.ItemChangeSets;
import com.ctrip.framework.apollo.core.dto.ItemDTO;


@Service
public class ItemSetService {

Expand All @@ -21,16 +24,24 @@ public class ItemSetService {
@Autowired
private AuditService auditService;

@Autowired
private CommitService commitService;


@Transactional
public void updateSet(ItemChangeSets changeSet) {
public ItemChangeSets updateSet(String appId, String clusterName,
String namespaceName, ItemChangeSets changeSet) {
String operator = changeSet.getDataChangeLastModifiedBy();
ConfigChangeContentBuilder configChangeContentBuilder = new ConfigChangeContentBuilder();

if (!CollectionUtils.isEmpty(changeSet.getCreateItems())) {
for (ItemDTO item : changeSet.getCreateItems()) {
Item entity = BeanUtils.transfrom(Item.class, item);
entity.setId(0);//protection
entity.setDataChangeCreatedBy(operator);
entity.setDataChangeLastModifiedBy(operator);
itemRepository.save(entity);
Item createdItem = itemRepository.save(entity);
configChangeContentBuilder.createItem(createdItem);
}
auditService.audit("ItemSet", null, Audit.OP.INSERT, operator);
}
Expand All @@ -39,9 +50,12 @@ public void updateSet(ItemChangeSets changeSet) {
for (ItemDTO item : changeSet.getUpdateItems()) {
Item entity = BeanUtils.transfrom(Item.class, item);
Item managedItem = itemRepository.findOne(entity.getId());
Item beforeUpdateItem = BeanUtils.transfrom(Item.class, managedItem);
BeanUtils.copyEntityProperties(entity, managedItem);
managedItem.setDataChangeLastModifiedBy(operator);
itemRepository.save(managedItem);
Item updatedItem = itemRepository.save(managedItem);
configChangeContentBuilder.updateItem(beforeUpdateItem, updatedItem);

}
auditService.audit("ItemSet", null, Audit.OP.UPDATE, operator);
}
Expand All @@ -51,9 +65,27 @@ public void updateSet(ItemChangeSets changeSet) {
Item entity = BeanUtils.transfrom(Item.class, item);
entity.setDeleted(true);
entity.setDataChangeLastModifiedBy(operator);
itemRepository.save(entity);
Item deletedItem = itemRepository.save(entity);
configChangeContentBuilder.deleteItem(deletedItem);
}
auditService.audit("ItemSet", null, Audit.OP.DELETE, operator);
}

createCommit(appId, clusterName, namespaceName, configChangeContentBuilder.build(), changeSet.getDataChangeLastModifiedBy());
return changeSet;

}

private void createCommit(String appId, String clusterName, String namespaceName, String configChangeContent, String operator){

Commit commit = new Commit();
commit.setAppId(appId);
commit.setClusterName(clusterName);
commit.setNamespaceName(namespaceName);
commit.setChangeSets(configChangeContent);
commit.setDataChangeCreatedBy(operator);
commit.setDataChangeLastModifiedBy(operator);
commitService.save(commit);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.ctrip.framework.apollo.biz.utils;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import com.ctrip.framework.apollo.biz.entity.Item;
import com.ctrip.framework.apollo.common.utils.BeanUtils;
import com.ctrip.framework.apollo.core.dto.ItemChangeSets;
import com.ctrip.framework.apollo.core.dto.ItemDTO;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;


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<>();


public ConfigChangeContentBuilder createItem(Item item) {
createItems.add(item);
return this;
}

public ConfigChangeContentBuilder updateItem(Item oldItem, Item newItem) {
ItemPair itemPair = new ItemPair(oldItem, newItem);
updateItems.add(itemPair);
return this;
}

public ConfigChangeContentBuilder deleteItem(Item item) {
deleteItems.add(item);
return this;
}

public String build() {
//因为事务第一段提交并没有更新时间,所以build时统一更新
for (Item item : createItems) {
item.setDataChangeLastModifiedTime(new Date());
}

for (ItemPair item : updateItems) {
item.newItem.setDataChangeLastModifiedTime(new Date());
}

for (Item item : deleteItems) {
item.setDataChangeLastModifiedTime(new Date());
}
return gson.toJson(this);
}

class ItemPair {

Item oldItem;
Item newItem;

public ItemPair(Item oldItem, Item newItem) {
this.oldItem = oldItem;
this.newItem = newItem;
}
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.ctrip.framework.apollo.portal.util;
package com.ctrip.framework.apollo.common.utils;


import com.ctrip.framework.apollo.core.exception.BadRequestException;
Expand All @@ -10,6 +10,8 @@ public class RequestPrecondition {

private static String ILLEGAL_MODEL = "request model is invalid";

private static String ILLEGAL_NUMBER = "number should be positive";

public static void checkArgument(String... args) {
checkArgument(!StringUtils.isContainEmpty(args), CONTAIN_EMPTY_ARGUMENT);
}
Expand All @@ -24,6 +26,14 @@ public static void checkArgument(boolean expression, Object errorMessage) {
}
}

public static void checkNumberPositive(int... args){
for (int num: args){
if (num <= 0){
throw new BadRequestException(ILLEGAL_NUMBER);
}
}
}



}
Loading

0 comments on commit d4ed153

Please sign in to comment.