public void edit(User user) throws IllegalOrphanException, NonexistentEntityException, Exception {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
User persistentUser = em.find(User.class, user.getId());
List<Courses> coursesListOld = persistentUser.getCoursesList();
List<Courses> coursesListNew = user.getCoursesList();
List<Enrollment> enrollmentListOld = persistentUser.getEnrollmentList();
List<Enrollment> enrollmentListNew = user.getEnrollmentList();
List<String> illegalOrphanMessages = null;
for (Courses coursesListOldCourses : coursesListOld) {
if (!coursesListNew.contains(coursesListOldCourses)) {
if (illegalOrphanMessages == null) {
illegalOrphanMessages = new ArrayList<String>();
}
illegalOrphanMessages.add("You must retain Courses " + coursesListOldCourses + " since its createrUserId field is not nullable.");
}
}
for (Enrollment enrollmentListOldEnrollment : enrollmentListOld) {
if (!enrollmentListNew.contains(enrollmentListOldEnrollment)) {
if (illegalOrphanMessages == null) {
illegalOrphanMessages = new ArrayList<String>();
}
illegalOrphanMessages.add("You must retain Enrollment " + enrollmentListOldEnrollment + " since its userId field is not nullable.");
}
}
if (illegalOrphanMessages != null) {
throw new IllegalOrphanException(illegalOrphanMessages);
}
List<Courses> attachedCoursesListNew = new ArrayList<Courses>();
for (Courses coursesListNewCoursesToAttach : coursesListNew) {
coursesListNewCoursesToAttach = em.getReference(coursesListNewCoursesToAttach.getClass(), coursesListNewCoursesToAttach.getCourseId());
attachedCoursesListNew.add(coursesListNewCoursesToAttach);
}
coursesListNew = attachedCoursesListNew;
user.setCoursesList(coursesListNew);
List<Enrollment> attachedEnrollmentListNew = new ArrayList<Enrollment>();
for (Enrollment enrollmentListNewEnrollmentToAttach : enrollmentListNew) {
enrollmentListNewEnrollmentToAttach = em.getReference(enrollmentListNewEnrollmentToAttach.getClass(), enrollmentListNewEnrollmentToAttach.getId());
attachedEnrollmentListNew.add(enrollmentListNewEnrollmentToAttach);
}
enrollmentListNew = attachedEnrollmentListNew;
user.setEnrollmentList(enrollmentListNew);
user = em.merge(user);
for (Courses coursesListNewCourses : coursesListNew) {
if (!coursesListOld.contains(coursesListNewCourses)) {
User oldCreaterUserIdOfCoursesListNewCourses = coursesListNewCourses.getCreaterUserId();
coursesListNewCourses.setCreaterUserId(user);
coursesListNewCourses = em.merge(coursesListNewCourses);
if (oldCreaterUserIdOfCoursesListNewCourses != null && !oldCreaterUserIdOfCoursesListNewCourses.equals(user)) {
oldCreaterUserIdOfCoursesListNewCourses.getCoursesList().remove(coursesListNewCourses);
oldCreaterUserIdOfCoursesListNewCourses = em.merge(oldCreaterUserIdOfCoursesListNewCourses);
}
}
}
for (Enrollment enrollmentListNewEnrollment : enrollmentListNew) {
if (!enrollmentListOld.contains(enrollmentListNewEnrollment)) {
User oldUserIdOfEnrollmentListNewEnrollment = enrollmentListNewEnrollment.getUserId();
enrollmentListNewEnrollment.setUserId(user);
enrollmentListNewEnrollment = em.merge(enrollmentListNewEnrollment);
if (oldUserIdOfEnrollmentListNewEnrollment != null && !oldUserIdOfEnrollmentListNewEnrollment.equals(user)) {
oldUserIdOfEnrollmentListNewEnrollment.getEnrollmentList().remove(enrollmentListNewEnrollment);
oldUserIdOfEnrollmentListNewEnrollment = em.merge(oldUserIdOfEnrollmentListNewEnrollment);
}
}
}
em.getTransaction().commit();