2323import de .learnlib .alex .common .utils .IdsList ;
2424import de .learnlib .alex .common .utils .ValidationExceptionHelper ;
2525import de .learnlib .alex .data .dao .FileDAO ;
26+ import de .learnlib .alex .data .dao .ProjectDAO ;
27+ import de .learnlib .alex .data .entities .Project ;
28+ import de .learnlib .alex .data .repositories .ProjectRepository ;
2629import org .apache .logging .log4j .LogManager ;
2730import org .apache .logging .log4j .Logger ;
2831import org .springframework .dao .DataIntegrityViolationException ;
@@ -45,21 +48,36 @@ public class UserDAOImpl implements UserDAO {
4548 private static final Logger LOGGER = LogManager .getLogger ();
4649
4750 /** The UserRepository to use. Will be injected. */
48- private UserRepository userRepository ;
51+ private final UserRepository userRepository ;
4952
5053 /** The FileDAO to use. Will be injected. */
51- private FileDAO fileDAO ;
54+ private final FileDAO fileDAO ;
55+
56+ /** The DAO for project. */
57+ private final ProjectDAO projectDAO ;
58+
59+ /** The repository for projects. */
60+ private final ProjectRepository projectRepository ;
5261
5362 /**
5463 * Creates a new UserDAO.
5564 *
56- * @param userRepository The UserRepository to use.
57- * @param fileDAO The FileDAO to use.
65+ * @param userRepository
66+ * The UserRepository to use.
67+ * @param fileDAO
68+ * The FileDAO to use.
69+ * @param projectDAO
70+ * The ProjectDAO to use.
71+ * @param projectRepository
72+ * The repository for project.
5873 */
5974 @ Inject
60- public UserDAOImpl (UserRepository userRepository , FileDAO fileDAO ) {
75+ public UserDAOImpl (UserRepository userRepository , FileDAO fileDAO , ProjectDAO projectDAO ,
76+ ProjectRepository projectRepository ) {
6177 this .userRepository = userRepository ;
6278 this .fileDAO = fileDAO ;
79+ this .projectDAO = projectDAO ;
80+ this .projectRepository = projectRepository ;
6381 }
6482
6583 @ Override
@@ -115,8 +133,23 @@ public void update(User user) throws ValidationException {
115133 @ Override
116134 @ Transactional
117135 public void delete (Long id ) throws NotFoundException {
118- User user = getById (id );
136+ delete (getById (id ));
137+ }
138+
139+ @ Override
140+ @ Transactional
141+ public void delete (IdsList ids ) throws NotFoundException {
142+ final List <User > users = userRepository .findAllByIdIn (ids );
143+ if (users .size () != ids .size ()) {
144+ throw new NotFoundException ("At least one user could not be found." );
145+ }
146+
147+ for (User user : users ) {
148+ delete (user );
149+ }
150+ }
119151
152+ private void delete (User user ) throws NotFoundException {
120153 // make sure there is at least one registered admin
121154 if (user .getRole ().equals (UserRole .ADMIN )) {
122155 List <User > admins = userRepository .findByRole (UserRole .ADMIN );
@@ -126,6 +159,9 @@ public void delete(Long id) throws NotFoundException {
126159 }
127160 }
128161
162+ for (Project project : projectRepository .findAllByUser_Id (user .getId ())) {
163+ projectDAO .delete (user , project .getId ());
164+ }
129165 userRepository .delete (user );
130166
131167 // delete the user directory
@@ -136,19 +172,10 @@ public void delete(Long id) throws NotFoundException {
136172 }
137173 }
138174
139- @ Override
140- @ Transactional
141- public void delete (IdsList ids ) throws NotFoundException {
142- for (Long id : ids ) {
143- User user = getById (id );
144- userRepository .delete (user );
145- }
146- }
147-
148175 private void saveUser (User user ) {
149176 try {
150177 userRepository .save (user );
151- // error handling
178+ // error handling
152179 } catch (TransactionSystemException e ) {
153180 LOGGER .info ("Saving a user failed:" , e );
154181 ConstraintViolationException cve = (ConstraintViolationException ) e .getCause ().getCause ();
0 commit comments