Fix constrain issue.

This commit is contained in:
Paulo Gustavo Veiga 2022-02-17 17:34:22 -08:00
parent 8e080075a0
commit 1eab15f8f3

View File

@ -27,9 +27,11 @@ import com.wisemapping.security.LegacyPasswordEncoder;
import org.hibernate.ObjectNotFoundException; import org.hibernate.ObjectNotFoundException;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport; import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
@ -103,9 +105,10 @@ public class UserManagerImpl
@Override @Override
public User createUser(@NotNull User user, @NotNull Collaborator collaborator) { public User createUser(@NotNull User user, @NotNull Collaborator collaborator) {
this.createUser(user); assert user != null : "Trying to store a null user";
// Migrate from previous temporal collab to new user ... // Migrate from previous temporal collab to new user ...
List<Collaboration> newCollabs = new ArrayList<>();
final Set<Collaboration> collaborations = collaborator.getCollaborations(); final Set<Collaboration> collaborations = collaborator.getCollaborations();
for (Collaboration oldCollab : collaborations) { for (Collaboration oldCollab : collaborations) {
Collaboration newCollab = new Collaboration(); Collaboration newCollab = new Collaboration();
@ -113,16 +116,18 @@ public class UserManagerImpl
newCollab.setMindMap(oldCollab.getMindMap()); newCollab.setMindMap(oldCollab.getMindMap());
newCollab.setCollaborator(user); newCollab.setCollaborator(user);
user.addCollaboration(newCollab); user.addCollaboration(newCollab);
getHibernateTemplate().save(newCollab); newCollabs.add(newCollab);
// Delete collaborations on this collaborator ...
getHibernateTemplate().delete(oldCollab);
} }
// Delete collaboration ... // Delete old collaboration
getHibernateTemplate().delete(collaborator); final HibernateTemplate template = getHibernateTemplate();
getHibernateTemplate().flush(); collaborations.forEach(c -> template.delete(c));
getHibernateTemplate().saveOrUpdate(user); template.delete(collaborator);
// Save all new...
user.setPassword(passwordEncoder.encode(user.getPassword()));
template.save(user);
newCollabs.forEach(c -> template.save(c));
return user; return user;
} }