Skip to content

Commit

Permalink
Fixed persistence problem with entity manager
Browse files Browse the repository at this point in the history
  • Loading branch information
Vidal322 committed Nov 16, 2024
1 parent 31192c8 commit ae07560
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package pt.up.fe.ni.website.backend.model

import com.fasterxml.jackson.annotation.JsonIgnore
import com.fasterxml.jackson.annotation.JsonProperty
import jakarta.persistence.CascadeType
import jakarta.persistence.Column
import jakarta.persistence.Entity
import jakarta.persistence.FetchType
Expand Down Expand Up @@ -31,7 +32,7 @@ abstract class Activity(
@OneToMany(fetch = FetchType.EAGER)
open val teamMembers: MutableList<Account>,

@OneToMany(/*cascade = [CascadeType.ALL],*/ mappedBy = "activity")
@OneToMany(cascade = [CascadeType.ALL], mappedBy = "activity")
@JsonIgnore // TODO: Decide if we want to return perRoles (or IDs) by default
open val associatedRoles: MutableList<@Valid PerActivityRole> = mutableListOf(),

Expand Down
5 changes: 3 additions & 2 deletions src/main/kotlin/pt/up/fe/ni/website/backend/model/Project.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package pt.up.fe.ni.website.backend.model

import jakarta.persistence.CascadeType
import jakarta.persistence.Entity
import jakarta.persistence.FetchType
import jakarta.persistence.JoinColumn
Expand Down Expand Up @@ -35,14 +36,14 @@ class Project(
var github: String? = null,

@JoinColumn
@OneToMany(/*cascade = [CascadeType.ALL],)*/ fetch = FetchType.EAGER)
@OneToMany(cascade = [CascadeType.ALL], fetch = FetchType.EAGER)
val links: List<@Valid CustomWebsite> = emptyList(),

@JoinColumn
@OneToMany(fetch = FetchType.EAGER)
var hallOfFame: MutableList<Account> = mutableListOf(),

@OneToMany(/*cascade = [CascadeType.ALL],*/fetch = FetchType.EAGER)
@OneToMany(cascade = [CascadeType.ALL], fetch = FetchType.EAGER)
@OrderBy("date")
val timeline: List<@Valid TimelineEvent> = emptyList(),

Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
package pt.up.fe.ni.website.backend.model.seeders

import jakarta.persistence.EntityManager
import jakarta.transaction.Transactional
import java.util.*
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.security.crypto.password.PasswordEncoder
import org.springframework.stereotype.Component
import pt.up.fe.ni.website.backend.config.Logging
import pt.up.fe.ni.website.backend.model.Account
import pt.up.fe.ni.website.backend.repository.AccountRepository
import pt.up.fe.ni.website.backend.repository.CustomWebsiteRepository
import pt.up.fe.ni.website.backend.repository.RoleRepository

@Component
@Component @Transactional
class AccountSeeder(
private val encoder: PasswordEncoder,
@Autowired private val roleRepository: RoleRepository
@Autowired val roleRepository: RoleRepository,
@Autowired val customWebsiteRepository: CustomWebsiteRepository,
@Autowired val entityManager: EntityManager // Inject EntityManager
) : AbstractSeeder<AccountRepository, Account, Long>(), Logging {

override fun createObjects() {
Expand All @@ -24,6 +29,12 @@ class AccountSeeder(
logger.warn("No roles found in the database. Accounts will be created without roles.")
}

val websites = customWebsiteRepository.findAll().toList()
.map { entityManager.merge(it) }.toMutableList()
if (websites.isEmpty()) {
logger.warn("No Custom websites in database. Accounts will be created without websites")
}

val accounts = listOf(
Account(
name = faker.name().fullName(),
Expand All @@ -33,6 +44,7 @@ class AccountSeeder(
birthDate = Date.from(faker.date().birthday(25, 35).toInstant()),
photo = faker.internet().image(),
github = faker.internet().url(),
websites = websites.shuffled().take(1).toMutableList(),
linkedin = faker.internet().url(),
roles = roles.shuffled().take(2).toMutableList()
),
Expand All @@ -58,6 +70,7 @@ class AccountSeeder(
photo = faker.internet().image(),
github = faker.internet().url(),
linkedin = faker.internet().url(),
websites = websites.shuffled().take(3).toMutableList(),
roles = roles.shuffled().take(1).toMutableList()
),
// Account Without Bio or Photo
Expand All @@ -70,6 +83,7 @@ class AccountSeeder(
photo = null,
github = null,
linkedin = faker.internet().url(),
websites = websites.shuffled().take(5).toMutableList(),
roles = roles.shuffled().take(1).toMutableList()
),
// Account with Long Bio
Expand All @@ -94,6 +108,7 @@ class AccountSeeder(
photo = faker.internet().image(),
github = faker.internet().url(),
linkedin = faker.internet().url(),
websites = websites.shuffled().take(2).toMutableList(),
roles = roles.shuffled().take(1).toMutableList()
),
// Account with All Optional Fields Null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class ActivitySeeder(
// Delegate seeding to the EventSeeder and ProjectSeeder
logger.info("Running Activity Seeder")

// Seed Roles per Activity (TODO: needs to add projects and events later)
// Seed Roles per Activity
perActivityRoleSeeder.createObjects()
// Seed Projects
projectSeeder.createObjects()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package pt.up.fe.ni.website.backend.model.seeders

import jakarta.persistence.EntityManager
import jakarta.transaction.Transactional
import java.util.*
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component
Expand All @@ -11,19 +13,22 @@ import pt.up.fe.ni.website.backend.repository.AccountRepository
import pt.up.fe.ni.website.backend.repository.EventRepository
import pt.up.fe.ni.website.backend.repository.PerActivityRoleRepository

@Component
@Component @Transactional
class EventSeeder(
@Autowired val accountRepository: AccountRepository,
@Autowired val perActivityRoleRepository: PerActivityRoleRepository
@Autowired val perActivityRoleRepository: PerActivityRoleRepository,
@Autowired val entityManager: EntityManager
) : AbstractSeeder<EventRepository, Event, Long>(), Logging {

val accounts = accountRepository.findAll().toList()
val perActivityRoles = perActivityRoleRepository.findAll().toList()

override fun createObjects() {
if (repository.count() == 0L) {
logger.info("Running Event seeder...")

val accounts = accountRepository.findAll().toList()
val perActivityRoles = perActivityRoleRepository.findAll().toList()

perActivityRoles.map { entityManager.merge(it) }.toMutableList()

// Helper function to generate a valid DateInterval
fun generateDateInterval(daysFromNow: Int, durationDays: Int): DateInterval {
val startDate = Date(System.currentTimeMillis() + (1000L * 60 * 60 * 24 * daysFromNow))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,36 +1,45 @@
package pt.up.fe.ni.website.backend.model.seeders

import jakarta.persistence.EntityManager
import jakarta.transaction.Transactional
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component
import pt.up.fe.ni.website.backend.config.Logging
import pt.up.fe.ni.website.backend.model.Project
import pt.up.fe.ni.website.backend.model.TimelineEvent
import pt.up.fe.ni.website.backend.repository.AccountRepository
import pt.up.fe.ni.website.backend.repository.CustomWebsiteRepository
import pt.up.fe.ni.website.backend.repository.PerActivityRoleRepository
import pt.up.fe.ni.website.backend.repository.ProjectRepository
import pt.up.fe.ni.website.backend.repository.TimeLineEventRepository

@Component
@Component @Transactional
class ProjectSeeder(
@Autowired val accountRepository: AccountRepository,
@Autowired val perActivityRoleRepository: PerActivityRoleRepository,
@Autowired val customWebsiteRepository: CustomWebsiteRepository,
@Autowired val perActivityRoleRepository: PerActivityRoleRepository,
@Autowired val timelineEventRepository: TimeLineEventRepository,
@Autowired val timelineEventSeeder: TimeLineEventSeeder
@Autowired val timelineEventSeeder: TimeLineEventSeeder,
@Autowired val entityManager: EntityManager
) : AbstractSeeder<ProjectRepository, Project, Long>(), Logging {

val accounts = accountRepository.findAll().toList()
val timelineEvents = timelineEventRepository.findAll().toList()
val perActivityRoles = perActivityRoleRepository.findAll().toList()
val customWebsites = customWebsiteRepository.findAll().toList()

override fun createObjects() {
if (repository.count() == 0L) {
logger.info("Running Project seeder...")

// Ensuring timeline events are created before projects
timelineEventSeeder.createObjects()
val shuffledTimelineEvents = timelineEvents.shuffled().toMutableList()

val accounts = accountRepository.findAll().toList()
val timelineEvents = timelineEventRepository.findAll().toList()
val customWebsites = customWebsiteRepository.findAll().toList()
val perActivityRoles = perActivityRoleRepository.findAll().toList()

val shuffledTimelineEvents = timelineEvents.shuffled()
.map { entityManager.merge(it) }.toMutableList()

customWebsites.map { entityManager.merge(it) }.toMutableList()
perActivityRoles.map { entityManager.merge(it) }.toMutableList()

val projects = (1..6).map {
val title = faker.company().bs()
Expand All @@ -44,17 +53,20 @@ class ProjectSeeder(
.replace(Regex("[^a-z0-9-]"), "")

// Ensure only one TimeLineEvent per project
val projectTimeline = shuffledTimelineEvents.take(1).toMutableList()
shuffledTimelineEvents.removeAt(0)
var projectTimeline = mutableListOf<TimelineEvent>()
if (shuffledTimelineEvents.isNotEmpty()) {
projectTimeline = shuffledTimelineEvents.take(1).toMutableList()
shuffledTimelineEvents.removeAt(0)
}

val hallOfFameMembers = accounts.shuffled().take(2).toMutableList()

Project(
title = title,
description = description,
teamMembers = accounts.shuffled().take(3).toMutableList(),
associatedRoles = perActivityRoles.shuffled().take(1).toMutableList(),
slug = slug,
associatedRoles = perActivityRoles.shuffled().take(2).toMutableList(),
image = "${slug}_image.png",
isArchived = faker.random().nextBoolean(),
technologies = technologies,
Expand Down

0 comments on commit ae07560

Please sign in to comment.