32e0e331989b2eabc3a5252004bfab56405b5c3e
[institute/sw-backend.git] /
1 package com.supwisdom.institute.backend.common.framework.repo;
2
3 import java.util.Calendar;
4 import java.util.Map;
5 import java.util.Optional;
6
7 import javax.persistence.EntityManager;
8 import javax.transaction.Transactional;
9
10 import org.springframework.data.domain.Page;
11 import org.springframework.data.domain.PageRequest;
12 import org.springframework.data.jpa.repository.support.JpaEntityInformation;
13 import org.springframework.data.jpa.repository.support.SimpleJpaRepository;
14 import org.springframework.data.repository.NoRepositoryBean;
15
16 import com.supwisdom.institute.backend.common.framework.entity.ABaseEntity;
17
18 @Transactional
19 @NoRepositoryBean
20 public class ABaseJpaRepositoryImpl<E extends ABaseEntity> extends SimpleJpaRepository<E, String> implements BaseJpaRepository<E> {
21
22   @SuppressWarnings("unused")
23   private final EntityManager em;
24
25   public ABaseJpaRepositoryImpl(Class<E> domainClass, EntityManager em) {
26     super(domainClass, em);
27     this.em = em;
28   }
29
30   public ABaseJpaRepositoryImpl(JpaEntityInformation<E, String> information, EntityManager em) {
31     super(information, em);
32     this.em = em;
33   }
34   
35   public Page<E> selectPageList(int pageIndex, int pageSize, Map<String, Object> mapBean) {
36
37     PageRequest pageRequest = PageRequest.of(pageIndex, pageSize);
38
39     Page<E> page = this.findAll(pageRequest);
40
41     return page;
42   }
43
44   public E selectById(String id) {
45
46     try {
47       Optional<E> entity = this.findById(id);
48
49       if (entity.isPresent()) {
50         return entity.get();
51       }
52     } catch (RuntimeException e) {
53       System.out.println("RuntimeException:" + e.getMessage());
54     } catch (Exception e) {
55       System.out.println("Exception:" + e.getMessage());
56     }
57
58     return null;
59   }
60
61   public E insert(E entity) {
62
63     if (entity.getCompanyId() == null || entity.getCompanyId().isEmpty()) {
64       entity.setCompanyId("1");
65     }
66
67     if (entity.getDeleted() == null) {
68       entity.setDeleted(false);
69     }
70     //entity.setAddAccount(AuthUtil.getRemoteUser()); // FIXME: setAddAccount
71     if (entity.getAddTime() == null) {
72       entity.setAddTime(Calendar.getInstance().getTime());
73     }
74
75     E e = this.save(entity);
76
77     return e;
78   }
79
80   public E update(E entity) {
81
82     //entity.setEditAccount(AuthUtil.getRemoteUser()); // FIXME: setEditAccount
83     if (entity.getEditTime() == null) {
84       entity.setEditTime(Calendar.getInstance().getTime());
85     }
86     
87     E e = this.save(entity);
88
89     return e;
90   }
91
92 }