Professor update

This commit is contained in:
2025-11-05 15:27:49 +05:30
parent 0df3fc6ae3
commit 71af6e4268
3 changed files with 101 additions and 85 deletions

View File

@ -4,7 +4,10 @@ public class FileConstant {
public static final String USER_IMAGE_PATH = "/user/image/"; public static final String USER_IMAGE_PATH = "/user/image/";
public static final String JPG_EXTENSION = "jpg"; public static final String JPG_EXTENSION = "jpg";
public static final String USER_FOLDER = System.getProperty("user.home") + "/supportportal/user/";
// ✅ CHANGED: From System.getProperty("user.home") to /app/uploads
public static final String USER_FOLDER = "/app/uploads/user/";
public static final String DIRECTORY_CREATED = "Created directory for: "; public static final String DIRECTORY_CREATED = "Created directory for: ";
public static final String DEFAULT_USER_IMAGE_URI_PATTERN = "/user/%s/profile-image"; public static final String DEFAULT_USER_IMAGE_URI_PATTERN = "/user/%s/profile-image";
public static final String USER_IMAGE_FILENAME = "avatar.jpg"; public static final String USER_IMAGE_FILENAME = "avatar.jpg";
@ -14,9 +17,9 @@ public class FileConstant {
public static final String NOT_AN_IMAGE_FILE = " is not an image file. Please upload an image file"; public static final String NOT_AN_IMAGE_FILE = " is not an image file. Please upload an image file";
public static final String TEMP_PROFILE_IMAGE_BASE_URL = "https://robohash.org/"; public static final String TEMP_PROFILE_IMAGE_BASE_URL = "https://robohash.org/";
// ✅ Professor-specific constants // ✅ CHANGED: Professor-specific constants
public static final String PROFESSOR_IMAGE_PATH = "/professor/image/"; public static final String PROFESSOR_IMAGE_PATH = "/professor/image/";
public static final String PROFESSOR_FOLDER = System.getProperty("user.home") + "/supportportal/professor/"; public static final String PROFESSOR_FOLDER = "/app/uploads/professor/";
public static final String DEFAULT_PROFESSOR_IMAGE_URI_PATTERN = "/professor/%s/profile-image"; public static final String DEFAULT_PROFESSOR_IMAGE_URI_PATTERN = "/professor/%s/profile-image";
public static final String PROFESSOR_IMAGE_FILENAME = "avatar.jpg"; public static final String PROFESSOR_IMAGE_FILENAME = "avatar.jpg";
} }

View File

@ -68,17 +68,18 @@ public class Professor implements Serializable {
@Column(name = "work_day") @Column(name = "work_day")
private List<String> workDays; private List<String> workDays;
// Use Set instead of List to avoid MultipleBagFetchException // ✅ CRITICAL FIX: Added orphanRemoval = true
// Sets can be eagerly loaded together without issues // This tells JPA to DELETE skills that are removed from the collection
@OneToMany(mappedBy = "professor", cascade = CascadeType.ALL, fetch = FetchType.EAGER) @OneToMany(mappedBy = "professor", cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true)
private Set<ProfessorSkill> skills; private Set<ProfessorSkill> skills;
// Use Set instead of List to avoid MultipleBagFetchException // ✅ CRITICAL FIX: Added orphanRemoval = true
@OneToMany(mappedBy = "professor", cascade = CascadeType.ALL, fetch = FetchType.EAGER) // This tells JPA to DELETE awards that are removed from the collection
@OneToMany(mappedBy = "professor", cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true)
private Set<ProfessorAward> awards; private Set<ProfessorAward> awards;
@ManyToMany(mappedBy = "professors") @ManyToMany(mappedBy = "professors")
@JsonIgnore // Keep this as @JsonIgnore to avoid circular references @JsonIgnore
private List<Post> posts; private List<Post> posts;
// Convenience method to get full name // Convenience method to get full name

View File

@ -78,15 +78,12 @@ public class ProfessorServiceImpl implements ProfessorService {
return addNewProfessor(professorDto); return addNewProfessor(professorDto);
} }
// ✅ FIXED: Changed from DEFAULT_USER_IMAGE_URI_PATTERN to DEFAULT_PROFESSOR_IMAGE_URI_PATTERN
private String generateDefaultProfileImageUrl(UUID professorId) { private String generateDefaultProfileImageUrl(UUID professorId) {
return ServletUriComponentsBuilder.fromCurrentContextPath() return ServletUriComponentsBuilder.fromCurrentContextPath()
.path(String.format(DEFAULT_PROFESSOR_IMAGE_URI_PATTERN, professorId)) .path(String.format(DEFAULT_PROFESSOR_IMAGE_URI_PATTERN, professorId))
.toUriString(); .toUriString();
} }
// ✅ FIXED: Changed from DEFAULT_USER_IMAGE_URI_PATTERN to DEFAULT_PROFESSOR_IMAGE_URI_PATTERN
// ✅ FIXED: Changed from USER_IMAGE_FILENAME to PROFESSOR_IMAGE_FILENAME
private String generateProfileImageUrl(UUID professorId) { private String generateProfileImageUrl(UUID professorId) {
return ServletUriComponentsBuilder.fromCurrentContextPath() return ServletUriComponentsBuilder.fromCurrentContextPath()
.path(String.format(DEFAULT_PROFESSOR_IMAGE_URI_PATTERN, professorId)) .path(String.format(DEFAULT_PROFESSOR_IMAGE_URI_PATTERN, professorId))
@ -134,7 +131,6 @@ public class ProfessorServiceImpl implements ProfessorService {
.orElseThrow(() -> new ProfessorNotFoundException(PROFESSOR_NOT_FOUND_MSG)); .orElseThrow(() -> new ProfessorNotFoundException(PROFESSOR_NOT_FOUND_MSG));
} }
// ✅ FIXED: Changed from USER_IMAGE_FILENAME to PROFESSOR_IMAGE_FILENAME
private void saveProfileImage(Professor professor, MultipartFile profileImage) { private void saveProfileImage(Professor professor, MultipartFile profileImage) {
if (profileImage == null) return; if (profileImage == null) return;
@ -248,9 +244,18 @@ public Professor updateProfessor(UUID professorId, ProfessorDto professorDto) {
// Create a final reference for lambda expressions // Create a final reference for lambda expressions
final Professor professorRef = professor; final Professor professorRef = professor;
// Update skills - REPLACE the entire collection // ✅ CORRECT FIX: Update skills with orphanRemoval=true
// Initialize collection if null
if (professor.getSkills() == null) {
professor.setSkills(new HashSet<>());
}
// Clear existing skills (orphanRemoval will delete them from DB)
professor.getSkills().clear();
// Add new skills
if (professorDto.getSkills() != null && !professorDto.getSkills().isEmpty()) { if (professorDto.getSkills() != null && !professorDto.getSkills().isEmpty()) {
Set<ProfessorSkill> skills = professorDto.getSkills().stream() Set<ProfessorSkill> newSkills = professorDto.getSkills().stream()
.filter(skillDto -> skillDto.getName() != null && !skillDto.getName().trim().isEmpty()) .filter(skillDto -> skillDto.getName() != null && !skillDto.getName().trim().isEmpty())
.map(skillDto -> ProfessorSkill.builder() .map(skillDto -> ProfessorSkill.builder()
.name(skillDto.getName().trim()) .name(skillDto.getName().trim())
@ -258,14 +263,22 @@ public Professor updateProfessor(UUID professorId, ProfessorDto professorDto) {
.professor(professorRef) .professor(professorRef)
.build()) .build())
.collect(Collectors.toSet()); .collect(Collectors.toSet());
professor.setSkills(skills); // ✅ REPLACE instead of add
} else { professor.getSkills().addAll(newSkills);
professor.setSkills(new HashSet<>()); // Clear if empty
} }
// Update awards - REPLACE the entire collection // ✅ CORRECT FIX: Update awards with orphanRemoval=true
// Initialize collection if null
if (professor.getAwards() == null) {
professor.setAwards(new HashSet<>());
}
// Clear existing awards (orphanRemoval will delete them from DB)
professor.getAwards().clear();
// Add new awards
if (professorDto.getAwards() != null && !professorDto.getAwards().isEmpty()) { if (professorDto.getAwards() != null && !professorDto.getAwards().isEmpty()) {
Set<ProfessorAward> awards = professorDto.getAwards().stream() Set<ProfessorAward> newAwards = professorDto.getAwards().stream()
.filter(awardDto -> awardDto.getTitle() != null && !awardDto.getTitle().trim().isEmpty()) .filter(awardDto -> awardDto.getTitle() != null && !awardDto.getTitle().trim().isEmpty())
.map(awardDto -> ProfessorAward.builder() .map(awardDto -> ProfessorAward.builder()
.title(awardDto.getTitle().trim()) .title(awardDto.getTitle().trim())
@ -275,9 +288,8 @@ public Professor updateProfessor(UUID professorId, ProfessorDto professorDto) {
.professor(professorRef) .professor(professorRef)
.build()) .build())
.collect(Collectors.toSet()); .collect(Collectors.toSet());
professor.setAwards(awards); // ✅ REPLACE instead of add
} else { professor.getAwards().addAll(newAwards);
professor.setAwards(new HashSet<>()); // Clear if empty
} }
Professor savedProfessor = professorRepository.save(professor); Professor savedProfessor = professorRepository.save(professor);