ItemService.java 7.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * Copyright 2021 Apollo Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
17
package com.ctrip.framework.apollo.biz.service;
Y
Yiming Liu 已提交
18

19

L
lepdou 已提交
20
import com.ctrip.framework.apollo.biz.config.BizConfig;
21 22 23 24
import com.ctrip.framework.apollo.biz.entity.Audit;
import com.ctrip.framework.apollo.biz.entity.Item;
import com.ctrip.framework.apollo.biz.entity.Namespace;
import com.ctrip.framework.apollo.biz.repository.ItemRepository;
L
lepdou 已提交
25 26
import com.ctrip.framework.apollo.common.exception.BadRequestException;
import com.ctrip.framework.apollo.common.exception.NotFoundException;
27
import com.ctrip.framework.apollo.common.utils.BeanUtils;
L
lepdou 已提交
28
import com.ctrip.framework.apollo.core.utils.StringUtils;
29
import org.springframework.context.annotation.Lazy;
L
lepdou 已提交
30 31
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
J
Jason Song 已提交
32 33 34 35
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
L
lepdou 已提交
36
import java.util.Date;
J
Jason Song 已提交
37
import java.util.List;
38
import java.util.Map;
J
Jason Song 已提交
39

Y
Yiming Liu 已提交
40
@Service
L
lepdou 已提交
41
public class ItemService {
Y
Yiming Liu 已提交
42

43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
  private final ItemRepository itemRepository;
  private final NamespaceService namespaceService;
  private final AuditService auditService;
  private final BizConfig bizConfig;

  public ItemService(
      final ItemRepository itemRepository,
      final @Lazy NamespaceService namespaceService,
      final AuditService auditService,
      final BizConfig bizConfig) {
    this.itemRepository = itemRepository;
    this.namespaceService = namespaceService;
    this.auditService = auditService;
    this.bizConfig = bizConfig;
  }
L
lepdou 已提交
58

59

60
  @Transactional
L
lepdou 已提交
61
  public Item delete(long id, String operator) {
62
    Item item = itemRepository.findById(id).orElse(null);
J
Jason Song 已提交
63
    if (item == null) {
L
lepdou 已提交
64
      throw new IllegalArgumentException("item not exist. ID:" + id);
J
Jason Song 已提交
65 66 67 68
    }

    item.setDeleted(true);
    item.setDataChangeLastModifiedBy(operator);
L
lepdou 已提交
69
    Item deletedItem = itemRepository.save(item);
Y
Yiming Liu 已提交
70

J
Jason Song 已提交
71
    auditService.audit(Item.class.getSimpleName(), id, Audit.OP.DELETE, operator);
L
lepdou 已提交
72
    return deletedItem;
Y
Yiming Liu 已提交
73 74
  }

75 76 77 78 79 80
  @Transactional
  public int batchDelete(long namespaceId, String operator) {
    return itemRepository.deleteByNamespaceId(namespaceId, operator);

  }

Y
Yiming Liu 已提交
81
  public Item findOne(String appId, String clusterName, String namespaceName, String key) {
L
abtest  
lepdou 已提交
82
    Namespace namespace = namespaceService.findOne(appId, clusterName, namespaceName);
Y
Yiming Liu 已提交
83 84 85 86
    if (namespace == null) {
      throw new NotFoundException(
          String.format("namespace not found for %s %s %s", appId, clusterName, namespaceName));
    }
J
Jared Tan 已提交
87
    return itemRepository.findByNamespaceIdAndKey(namespace.getId(), key);
Y
Yiming Liu 已提交
88 89
  }

L
lepdou 已提交
90
  public Item findLastOne(String appId, String clusterName, String namespaceName) {
L
abtest  
lepdou 已提交
91
    Namespace namespace = namespaceService.findOne(appId, clusterName, namespaceName);
L
lepdou 已提交
92 93 94 95
    if (namespace == null) {
      throw new NotFoundException(
          String.format("namespace not found for %s %s %s", appId, clusterName, namespaceName));
    }
L
abtest  
lepdou 已提交
96 97 98 99 100
    return findLastOne(namespace.getId());
  }

  public Item findLastOne(long namespaceId) {
    return itemRepository.findFirst1ByNamespaceIdOrderByLineNumDesc(namespaceId);
L
lepdou 已提交
101 102
  }

Y
Yiming Liu 已提交
103
  public Item findOne(long itemId) {
J
Jared Tan 已提交
104
    return itemRepository.findById(itemId).orElse(null);
Y
Yiming Liu 已提交
105 106
  }

L
lepdou 已提交
107 108 109 110 111 112 113 114 115 116 117 118 119
  public List<Item> findItemsWithoutOrdered(Long namespaceId) {
    List<Item> items = itemRepository.findByNamespaceId(namespaceId);
    if (items == null) {
      return Collections.emptyList();
    }
    return items;
  }

  public List<Item> findItemsWithoutOrdered(String appId, String clusterName, String namespaceName) {
    Namespace namespace = namespaceService.findOne(appId, clusterName, namespaceName);
    if (namespace != null) {
      return findItemsWithoutOrdered(namespace.getId());
    }
W
waters 已提交
120
    return Collections.emptyList();
L
lepdou 已提交
121 122 123
  }

  public List<Item> findItemsWithOrdered(Long namespaceId) {
124 125 126 127 128 129
    List<Item> items = itemRepository.findByNamespaceIdOrderByLineNumAsc(namespaceId);
    if (items == null) {
      return Collections.emptyList();
    }
    return items;
  }
L
abtest  
lepdou 已提交
130

L
lepdou 已提交
131
  public List<Item> findItemsWithOrdered(String appId, String clusterName, String namespaceName) {
L
abtest  
lepdou 已提交
132
    Namespace namespace = namespaceService.findOne(appId, clusterName, namespaceName);
L
lepdou 已提交
133
    if (namespace != null) {
L
lepdou 已提交
134
      return findItemsWithOrdered(namespace.getId());
135
    }
W
waters 已提交
136
    return Collections.emptyList();
137
  }
L
abtest  
lepdou 已提交
138

L
lepdou 已提交
139 140
  public List<Item> findItemsModifiedAfterDate(long namespaceId, Date date) {
    return itemRepository.findByNamespaceIdAndDataChangeLastModifiedTimeGreaterThan(namespaceId, date);
L
lepdou 已提交
141 142 143 144
  }

  public Page<Item> findItemsByKey(String key, Pageable pageable) {
    return itemRepository.findByKey(key, pageable);
L
lepdou 已提交
145 146
  }

147
  @Transactional
Y
Yiming Liu 已提交
148
  public Item save(Item entity) {
L
lepdou 已提交
149
    checkItemKeyLength(entity.getKey());
150
    checkItemValueLength(entity.getNamespaceId(), entity.getValue());
L
lepdou 已提交
151

J
Jason Song 已提交
152
    entity.setId(0);//protection
L
abtest  
lepdou 已提交
153 154 155 156 157 158 159

    if (entity.getLineNum() == 0) {
      Item lastItem = findLastOne(entity.getNamespaceId());
      int lineNum = lastItem == null ? 1 : lastItem.getLineNum() + 1;
      entity.setLineNum(lineNum);
    }

Y
Yiming Liu 已提交
160 161 162
    Item item = itemRepository.save(entity);

    auditService.audit(Item.class.getSimpleName(), item.getId(), Audit.OP.INSERT,
L
abtest  
lepdou 已提交
163
                       item.getDataChangeCreatedBy());
Y
Yiming Liu 已提交
164 165

    return item;
Y
Yiming Liu 已提交
166 167
  }

168
  @Transactional
Y
Yiming Liu 已提交
169
  public Item update(Item item) {
170
    checkItemValueLength(item.getNamespaceId(), item.getValue());
171
    Item managedItem = itemRepository.findById(item.getId()).orElse(null);
Y
Yiming Liu 已提交
172
    BeanUtils.copyEntityProperties(item, managedItem);
Y
Yiming Liu 已提交
173 174
    managedItem = itemRepository.save(managedItem);

Y
Yiming Liu 已提交
175
    auditService.audit(Item.class.getSimpleName(), managedItem.getId(), Audit.OP.UPDATE,
L
abtest  
lepdou 已提交
176
                       managedItem.getDataChangeLastModifiedBy());
Y
Yiming Liu 已提交
177

Y
Yiming Liu 已提交
178
    return managedItem;
Y
Yiming Liu 已提交
179 180
  }

181 182 183 184
  private boolean checkItemValueLength(long namespaceId, String value) {
    int limit = getItemValueLengthLimit(namespaceId);
    if (!StringUtils.isEmpty(value) && value.length() > limit) {
      throw new BadRequestException("value too long. length limit:" + limit);
L
lepdou 已提交
185 186 187 188
    }
    return true;
  }

L
abtest  
lepdou 已提交
189
  private boolean checkItemKeyLength(String key) {
L
lepdou 已提交
190 191
    if (!StringUtils.isEmpty(key) && key.length() > bizConfig.itemKeyLengthLimit()) {
      throw new BadRequestException("key too long. length limit:" + bizConfig.itemKeyLengthLimit());
L
lepdou 已提交
192 193 194 195
    }
    return true;
  }

196
  private int getItemValueLengthLimit(long namespaceId) {
L
lepdou 已提交
197 198 199
    Map<Long, Integer> namespaceValueLengthOverride = bizConfig.namespaceValueLengthLimitOverride();
    if (namespaceValueLengthOverride != null && namespaceValueLengthOverride.containsKey(namespaceId)) {
      return namespaceValueLengthOverride.get(namespaceId);
200
    }
L
lepdou 已提交
201
    return bizConfig.itemValueLengthLimit();
202 203
  }

Y
Yiming Liu 已提交
204
}