public void edit(Courses courses) throws IllegalOrphanException, NonexistentEntityException, Exception {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
Courses persistentCourses = em.find(Courses.class, courses.getCourseId());
User createrUserIdOld = persistentCourses.getCreaterUserId();
User createrUserIdNew = courses.getCreaterUserId();
List<Lecture> lectureListOld = persistentCourses.getLectureList();
List<Lecture> lectureListNew = courses.getLectureList();
List<Enrollment> enrollmentListOld = persistentCourses.getEnrollmentList();
List<Enrollment> enrollmentListNew = courses.getEnrollmentList();
List<String> illegalOrphanMessages = null;
for (Enrollment enrollmentListOldEnrollment : enrollmentListOld) {
if (!enrollmentListNew.contains(enrollmentListOldEnrollment)) {
if (illegalOrphanMessages == null) {
illegalOrphanMessages = new ArrayList<String>();
}
illegalOrphanMessages.add("You must retain Enrollment " + enrollmentListOldEnrollment + " since its courseId field is not nullable.");
}
}
if (illegalOrphanMessages != null) {
throw new IllegalOrphanException(illegalOrphanMessages);
}
if (createrUserIdNew != null) {
createrUserIdNew = em.getReference(createrUserIdNew.getClass(), createrUserIdNew.getId());
courses.setCreaterUserId(createrUserIdNew);
}
List<Lecture> attachedLectureListNew = new ArrayList<Lecture>();
for (Lecture lectureListNewLectureToAttach : lectureListNew) {
lectureListNewLectureToAttach = em.getReference(lectureListNewLectureToAttach.getClass(), lectureListNewLectureToAttach.getId());
attachedLectureListNew.add(lectureListNewLectureToAttach);
}
lectureListNew = attachedLectureListNew;
courses.setLectureList(lectureListNew);
List<Enrollment> attachedEnrollmentListNew = new ArrayList<Enrollment>();
for (Enrollment enrollmentListNewEnrollmentToAttach : enrollmentListNew) {
enrollmentListNewEnrollmentToAttach = em.getReference(enrollmentListNewEnrollmentToAttach.getClass(), enrollmentListNewEnrollmentToAttach.getId());
attachedEnrollmentListNew.add(enrollmentListNewEnrollmentToAttach);
}
enrollmentListNew = attachedEnrollmentListNew;
courses.setEnrollmentList(enrollmentListNew);
courses = em.merge(courses);
if (createrUserIdOld != null && !createrUserIdOld.equals(createrUserIdNew)) {
createrUserIdOld.getCoursesList().remove(courses);
createrUserIdOld = em.merge(createrUserIdOld);
}
if (createrUserIdNew != null && !createrUserIdNew.equals(createrUserIdOld)) {
createrUserIdNew.getCoursesList().add(courses);
createrUserIdNew = em.merge(createrUserIdNew);
}
for (Lecture lectureListOldLecture : lectureListOld) {
if (!lectureListNew.contains(lectureListOldLecture)) {
lectureListOldLecture.setCourseId(null);
lectureListOldLecture = em.merge(lectureListOldLecture);
}
}
for (Lecture lectureListNewLecture : lectureListNew) {
if (!lectureListOld.contains(lectureListNewLecture)) {
Courses oldCourseIdOfLectureListNewLecture = lectureListNewLecture.getCourseId();
lectureListNewLecture.setCourseId(courses);
lectureListNewLecture = em.merge(lectureListNewLecture);
if (oldCourseIdOfLectureListNewLecture != null && !oldCourseIdOfLectureListNewLecture.equals(courses)) {
oldCourseIdOfLectureListNewLecture.getLectureList().remove(lectureListNewLecture);
oldCourseIdOfLectureListNewLecture = em.merge(oldCourseIdOfLectureListNewLecture);
}
}
}
for (Enrollment enrollmentListNewEnrollment : enrollmentListNew) {
if (!enrollmentListOld.contains(enrollmentListNewEnrollment)) {
Courses oldCourseIdOfEnrollmentListNewEnrollment = enrollmentListNewEnrollment.getCourseId();
enrollmentListNewEnrollment.setCourseId(courses);
enrollmentListNewEnrollment = em.merge(enrollmentListNewEnrollment);
if (oldCourseIdOfEnrollmentListNewEnrollment != null && !oldCourseIdOfEnrollmentListNewEnrollment.equals(courses)) {
oldCourseIdOfEnrollmentListNewEnrollment.getEnrollmentList().remove(enrollmentListNewEnrollment);
oldCourseIdOfEnrollmentListNewEnrollment = em.merge(oldCourseIdOfEnrollmentListNewEnrollment);
}
}
}
em.getTransaction().commit();