-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ceb2fdc
commit 4f67edd
Showing
27 changed files
with
527 additions
and
143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 6 additions & 6 deletions
12
...itory_Jpa_Commons/src/main/java/org/robbins/flashcards/model/util/EntityAuditingUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,16 @@ | ||
package org.robbins.flashcards.model.util; | ||
|
||
|
||
import java.time.LocalDateTime; | ||
|
||
import org.robbins.flashcards.model.common.AbstractAuditable; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
public class EntityAuditingUtil | ||
{ | ||
|
||
public static void configureCreatedByAndTime(final AbstractAuditable entity, final Long auditingUserId) { | ||
entity.setCreatedBy(auditingUserId); | ||
entity.setCreatedDate(LocalDateTime.now()); | ||
if (entity.isNew()) { | ||
entity.setCreatedBy(auditingUserId); | ||
entity.setCreatedDate(LocalDateTime.now()); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
...java/org/robbins/flashcards/repository/populator/PopulatingFlashCardRepositoryFacade.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
|
||
package org.robbins.flashcards.repository.populator; | ||
|
||
import org.robbins.flashcards.model.FlashCard; | ||
import org.robbins.flashcards.model.util.EntityAuditingUtil; | ||
import org.robbins.flashcards.repository.facade.impl.DefaultFlashCardRepositoryFacade; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component("populatingFlashcardRepositoryFacade") | ||
public class PopulatingFlashCardRepositoryFacade extends DefaultFlashCardRepositoryFacade | ||
{ | ||
@Override | ||
public void configureCreatedByAndTime(FlashCard entity) { | ||
EntityAuditingUtil.configureCreatedByAndTime(entity, 1L); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
.../main/java/org/robbins/flashcards/repository/populator/PopulatingTagRepositoryFacade.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
|
||
package org.robbins.flashcards.repository.populator; | ||
|
||
import org.robbins.flashcards.model.Tag; | ||
import org.robbins.flashcards.model.util.EntityAuditingUtil; | ||
import org.robbins.flashcards.repository.facade.impl.DefaultTagRepositoryFacade; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component("populatingTagRepositoryFacade") | ||
public class PopulatingTagRepositoryFacade extends DefaultTagRepositoryFacade { | ||
|
||
@Override | ||
public void configureCreatedByAndTime(Tag entity) { | ||
EntityAuditingUtil.configureCreatedByAndTime(entity, 1L); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
...main/java/org/robbins/flashcards/repository/populator/PopulatingUserRepositoryFacade.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
|
||
package org.robbins.flashcards.repository.populator; | ||
|
||
import org.robbins.flashcards.model.User; | ||
import org.robbins.flashcards.model.util.EntityAuditingUtil; | ||
import org.robbins.flashcards.repository.facade.impl.DefaultUserRepositoryFacade; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
@Component("populatingUserRepositoryFacade") | ||
public class PopulatingUserRepositoryFacade extends DefaultUserRepositoryFacade { | ||
|
||
@Override | ||
public void configureCreatedByAndTime(User entity) { | ||
EntityAuditingUtil.configureCreatedByAndTime(entity, 1L); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
...ommons/src/main/java/org/robbins/flashcards/repository/populator/RepositoryPopulator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package org.robbins.flashcards.repository.populator; | ||
|
||
|
||
public interface RepositoryPopulator { | ||
void populate(); | ||
} |
38 changes: 38 additions & 0 deletions
38
...rc/main/java/org/robbins/flashcards/repository/populator/RepositoryPopulatorListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package org.robbins.flashcards.repository.populator; | ||
|
||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.context.ApplicationContextAware; | ||
import org.springframework.context.ApplicationListener; | ||
import org.springframework.context.event.ContextRefreshedEvent; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class RepositoryPopulatorListener implements ApplicationListener<ContextRefreshedEvent>, | ||
ApplicationContextAware { | ||
|
||
@Autowired | ||
private RepositoryPopulator populator; | ||
|
||
private ApplicationContext context; | ||
|
||
/* | ||
* (non-Javadoc) | ||
* @see org.springframework.context.ApplicationContextAware#setApplicationContext(org.springframework.context.ApplicationContext) | ||
*/ | ||
public void setApplicationContext(ApplicationContext applicationContext) { | ||
this.context = applicationContext; | ||
} | ||
|
||
/* | ||
* (non-Javadoc) | ||
* @see org.springframework.context.ApplicationListener#onApplicationEvent(org.springframework.context.ApplicationEvent) | ||
*/ | ||
public void onApplicationEvent(ContextRefreshedEvent event) { | ||
|
||
if (event.getApplicationContext().equals(context)) { | ||
populator.populate(); | ||
} | ||
} | ||
} |
99 changes: 99 additions & 0 deletions
99
...n/java/org/robbins/flashcards/repository/populator/ResourceReaderRepositoryPopulator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package org.robbins.flashcards.repository.populator; | ||
|
||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.robbins.flashcards.facade.base.GenericCrudFacade; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.InitializingBean; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.core.io.ClassPathResource; | ||
import org.springframework.core.io.Resource; | ||
import org.springframework.data.repository.init.Jackson2ResourceReader; | ||
import org.springframework.data.repository.init.ResourceReader; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
@Component | ||
public class ResourceReaderRepositoryPopulator implements RepositoryPopulator, InitializingBean { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(ResourceReaderRepositoryPopulator.class); | ||
|
||
@Autowired | ||
private ObjectMapper mapper; | ||
|
||
@javax.annotation.Resource(name = "dtoToFacadeMap") | ||
private Map<String, GenericCrudFacade> dtoToFacadeMap; | ||
|
||
@javax.annotation.Resource(name = "initResourceNames") | ||
@Autowired | ||
private List<String> initResourceNames; | ||
|
||
private ResourceReader reader; | ||
private List<Resource> resources; | ||
|
||
@Override | ||
public void afterPropertiesSet() throws Exception { | ||
reader = new Jackson2ResourceReader(mapper); | ||
resources = getResources(); | ||
} | ||
|
||
public void populate() { | ||
resources.forEach(this::populateResource); | ||
} | ||
|
||
private void populateResource(final Resource resource) { | ||
LOGGER.info(String.format("Reading resource: %s", resource)); | ||
|
||
final Object result = readObjectFrom(resource); | ||
|
||
if (result instanceof Collection) { | ||
((Collection<?>) result).forEach(this::populateElement); | ||
} else { | ||
persist(result); | ||
} | ||
} | ||
|
||
private void populateElement(final Object element) { | ||
if (element != null) { | ||
persist(element); | ||
} else { | ||
LOGGER.info("Skipping null element found in unmarshal result!"); | ||
} | ||
} | ||
|
||
private Object readObjectFrom(Resource resource) { | ||
try { | ||
return getResourceReader().readFrom(resource, null); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
private void persist(Object object) { | ||
|
||
final String dtoName = object.getClass().getSimpleName(); | ||
final GenericCrudFacade facade = dtoToFacadeMap.get(dtoName); | ||
LOGGER.debug(String.format("Persisting %s using repository %s", object, facade)); | ||
facade.save(object); | ||
} | ||
|
||
private List<Resource> getResources() { | ||
if (initResourceNames == null) return new ArrayList<>(); | ||
|
||
return initResourceNames | ||
.stream() | ||
.map(ClassPathResource::new) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
private ResourceReader getResourceReader() { | ||
return reader; | ||
} | ||
|
||
} |
Oops, something went wrong.