| 1 | |
package com.ctrip.apollo.biz.service; |
| 2 | |
|
| 3 | |
import java.util.List; |
| 4 | |
import java.util.Objects; |
| 5 | |
|
| 6 | |
import org.springframework.beans.factory.annotation.Autowired; |
| 7 | |
import org.springframework.data.domain.Page; |
| 8 | |
import org.springframework.data.domain.Pageable; |
| 9 | |
import org.springframework.stereotype.Service; |
| 10 | |
import org.springframework.transaction.annotation.Transactional; |
| 11 | |
|
| 12 | |
import com.ctrip.apollo.biz.entity.App; |
| 13 | |
import com.ctrip.apollo.biz.entity.Audit; |
| 14 | |
import com.ctrip.apollo.biz.repository.AppRepository; |
| 15 | |
import com.ctrip.apollo.common.utils.BeanUtils; |
| 16 | |
import com.ctrip.apollo.core.exception.ServiceException; |
| 17 | |
|
| 18 | |
@Service |
| 19 | 1 | public class AppService { |
| 20 | |
|
| 21 | |
@Autowired |
| 22 | |
private AppRepository appRepository; |
| 23 | |
|
| 24 | |
@Autowired |
| 25 | |
private AuditService auditService; |
| 26 | |
|
| 27 | |
public boolean isAppIdUnique(String appId) { |
| 28 | 5 | Objects.requireNonNull(appId, "AppId must not be null"); |
| 29 | 5 | return Objects.isNull(appRepository.findByAppId(appId)); |
| 30 | |
} |
| 31 | |
|
| 32 | |
@Transactional |
| 33 | |
public void delete(long id, String owner) { |
| 34 | 0 | appRepository.delete(id); |
| 35 | |
|
| 36 | 0 | auditService.audit(App.class.getSimpleName(), id, Audit.OP.DELETE, owner); |
| 37 | 0 | } |
| 38 | |
|
| 39 | |
public List<App> findAll(Pageable pageable) { |
| 40 | 0 | Page<App> page = appRepository.findAll(pageable); |
| 41 | 0 | return page.getContent(); |
| 42 | |
} |
| 43 | |
|
| 44 | |
public List<App> findByName(String name) { |
| 45 | 0 | return appRepository.findByName(name); |
| 46 | |
} |
| 47 | |
|
| 48 | |
public App findOne(String appId) { |
| 49 | 0 | return appRepository.findByAppId(appId); |
| 50 | |
} |
| 51 | |
|
| 52 | |
@Transactional |
| 53 | |
public App save(App entity) { |
| 54 | 5 | if (!isAppIdUnique(entity.getAppId())) { |
| 55 | 1 | throw new ServiceException("appId not unique"); |
| 56 | |
} |
| 57 | 4 | App app = appRepository.save(entity); |
| 58 | |
|
| 59 | 8 | auditService.audit(App.class.getSimpleName(), app.getId(), Audit.OP.INSERT, |
| 60 | 4 | app.getDataChangeCreatedBy()); |
| 61 | |
|
| 62 | 4 | return app; |
| 63 | |
} |
| 64 | |
|
| 65 | |
@Transactional |
| 66 | |
public App update(App app) { |
| 67 | 0 | App managedApp = appRepository.findByAppId(app.getAppId()); |
| 68 | 0 | BeanUtils.copyEntityProperties(app, managedApp); |
| 69 | 0 | managedApp = appRepository.save(managedApp); |
| 70 | |
|
| 71 | 0 | auditService.audit(App.class.getSimpleName(), managedApp.getId(), Audit.OP.UPDATE, |
| 72 | 0 | managedApp.getDataChangeLastModifiedBy()); |
| 73 | |
|
| 74 | 0 | return managedApp; |
| 75 | |
} |
| 76 | |
} |