Skip to content

Commit 4e4b6f8

Browse files
committed
GP-126 remove completed solution
1 parent df4a599 commit 4e4b6f8

File tree

1 file changed

+7
-44
lines changed

1 file changed

+7
-44
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
package com.bobocode.dao;
22

3-
import com.bobocode.exception.AccountDaoException;
43
import com.bobocode.model.Account;
4+
import com.bobocode.util.ExerciseNotCompletedException;
55

6-
import javax.persistence.EntityManager;
76
import javax.persistence.EntityManagerFactory;
8-
import javax.persistence.TypedQuery;
97
import java.util.List;
10-
import java.util.function.Consumer;
11-
import java.util.function.Function;
128

139
public class AccountDaoImpl implements AccountDao {
1410
private EntityManagerFactory emf;
@@ -19,65 +15,32 @@ public AccountDaoImpl(EntityManagerFactory emf) {
1915

2016
@Override
2117
public void save(Account account) {
22-
performWithinPersistenceContext(em -> em.persist(account));
18+
throw new ExerciseNotCompletedException(); // todo
2319
}
2420

2521
@Override
2622
public Account findById(Long id) {
27-
return performReturningWithinPersistenceContext(entityManager -> entityManager.find(Account.class, id));
23+
throw new ExerciseNotCompletedException(); // todo
2824
}
2925

3026
@Override
3127
public Account findByEmail(String email) {
32-
return performReturningWithinPersistenceContext(entityManager -> {
33-
TypedQuery<Account> findByEmailQuery
34-
= entityManager.createQuery("select a from Account a where a.email = :email", Account.class);
35-
findByEmailQuery.setParameter("email", email);
36-
return findByEmailQuery.getSingleResult();
37-
});
28+
throw new ExerciseNotCompletedException(); // todo
3829
}
3930

4031
@Override
4132
public List<Account> findAll() {
42-
return performReturningWithinPersistenceContext(entityManager ->
43-
entityManager.createQuery("select a from Account a", Account.class).getResultList());
33+
throw new ExerciseNotCompletedException(); // todo
4434
}
4535

4636
@Override
4737
public void update(Account account) {
48-
performWithinPersistenceContext(em -> em.merge(account));
38+
throw new ExerciseNotCompletedException(); // todo
4939
}
5040

5141
@Override
5242
public void remove(Account account) {
53-
performWithinPersistenceContext(entityManager -> {
54-
Account mergedAccount = entityManager.merge(account);
55-
entityManager.remove(mergedAccount);
56-
});
57-
}
58-
59-
private void performWithinPersistenceContext(Consumer<EntityManager> entityManagerConsumer) {
60-
performReturningWithinPersistenceContext(entityManager -> {
61-
entityManagerConsumer.accept(entityManager);
62-
return null;
63-
});
64-
}
65-
66-
private <T> T performReturningWithinPersistenceContext(Function<EntityManager, T> entityManagerFunction) {
67-
EntityManager entityManager = emf.createEntityManager();
68-
entityManager.getTransaction().begin();
69-
T result;
70-
try {
71-
result = entityManagerFunction.apply(entityManager);
72-
entityManager.getTransaction().commit();
73-
} catch (Exception e) {
74-
entityManager.getTransaction().rollback();
75-
throw new AccountDaoException("Error performing dao operation. Transaction is rolled back!", e);
76-
} finally {
77-
entityManager.close();
78-
}
79-
return result;
43+
throw new ExerciseNotCompletedException(); // todo
8044
}
8145
}
8246

83-

0 commit comments

Comments
 (0)