Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

closes #223 #224

Merged
merged 1 commit into from
Jan 14, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions FlashCards_Config/src/main/resources/default.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
#Profiles
repository.profile=spring-data
security.profile=security-http
database.profile=embedded
tests.data.profile=test-data

# DataSource properties
hibernate.connection.provider_class=com.zaxxer.hikari.hibernate.HikariConnectionProvider
hibernate.hikari.dataSourceClassName=com.mysql.jdbc.jdbc2.optional.MysqlDataSource
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,58 +60,58 @@ public FlashCardRepository<FlashCard, Tag, Long> getRepository() {

@Override
public FlashCardDto save(final FlashCardDto dto) throws RepositoryException {
FlashCard entity = getConverter().getEntity(dto);
final FlashCard entity = getConverter().getEntity(dto);
EntityAuditingUtil.configureCreatedByAndTime(entity, getAuditingUserId());
entity.setTags(configureTags(dto.getTags()));
FlashCard result = repository.save(entity);
final FlashCard result = repository.save(entity);
return convertAndInitializeEntity(result);
}

@Override
public List<FlashCardDto> findByTagsIn(final Set<TagDto> tagDtos)
throws RepositoryException {
List<FlashCard> results = repository.findByTagsIn(getTags(tagDtos));
final List<FlashCard> results = repository.findByTagsIn(getTags(tagDtos));
return convertAndInitializeEntities(results);
}

@Override
public List<FlashCardDto> findByTagsIn(final Set<TagDto> tagDtos,
final PageRequest page) throws RepositoryException {
List<FlashCard> results = repository.findByTagsIn(getTags(tagDtos), page);
final List<FlashCard> results = repository.findByTagsIn(getTags(tagDtos), page);
return convertAndInitializeEntities(results);
}

@Override
public List<FlashCardDto> findByQuestionLike(final String question)
throws RepositoryException {
List<FlashCard> results = repository.findByQuestionLike(question);
final List<FlashCard> results = repository.findByQuestionLike(question);
return convertAndInitializeEntities(results);
}

@Override
public List<FlashCardDto> findByQuestionLike(final String question,
final PageRequest page)
throws RepositoryException {
List<FlashCard> results = repository.findByQuestionLike(question, page);
final List<FlashCard> results = repository.findByQuestionLike(question, page);
return convertAndInitializeEntities(results);
}

@Override
public FlashCardDto findByQuestion(final String question) throws RepositoryException {
FlashCard result = repository.findByQuestion(question);
final FlashCard result = repository.findByQuestion(question);
return convertAndInitializeEntity(result);
}

@Override
public List<FlashCardDto> findFlashCardsForTag(Long tagId, Set<String> fields) throws FlashCardsException
public List<FlashCardDto> findFlashCardsForTag(final Long tagId, final Set<String> fields) throws FlashCardsException
{
List<FlashCard> results = repository.findByTags_Id(tagId);
final List<FlashCard> results = repository.findByTags_Id(tagId);
return convertAndInitializeEntities(results, fields);
}

private Set<Tag> getTags(final Set<TagDto> tagDtos) {
List<TagDto> tagDtoList = Lists.newArrayList(tagDtos);
List<Tag> tagList = tagConverter.getEntities(tagDtoList);
final List<TagDto> tagDtoList = Lists.newArrayList(tagDtos);
final List<Tag> tagList = tagConverter.getEntities(tagDtoList);
return Sets.newHashSet(tagList);
}

Expand All @@ -121,7 +121,7 @@ private Set<Tag> configureTags(final Set<TagDto> tags) {
.collect(Collectors.toSet());
}

private Tag configureTag(TagDto tagDto) {
private Tag configureTag(final TagDto tagDto) {
if (tagDto.getId() == null) {
final Tag existingTag = tagRepository.findByName(tagDto.getName());
if (existingTag == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public TagRepository<Tag, Long> getRepository() {

@Override
public TagDto findByName(final String name) throws RepositoryException {
Tag result = repository.findByName(name);
final Tag result = repository.findByName(name);

if (result == null) {
return null;
Expand All @@ -51,7 +51,7 @@ public TagDto findByName(final String name) throws RepositoryException {

@Override
public List<TagDto> findTagsForFlashCard(final Long flashCardId, final Set<String> fields) throws RepositoryException {
List<Tag> results = repository.findByFlashCards_Id(flashCardId);
final List<Tag> results = repository.findByFlashCards_Id(flashCardId);
return convertAndInitializeEntities(results, fields);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
* [y] hybris Platform
*
* Copyright (c) 2000-2016 hybris AG
* All rights reserved.
*
* This software is the confidential and proprietary information of hybris
* ("Confidential Information"). You shall not disclose such Confidential
* Information and shall use it only in accordance with the terms of the
* license agreement you entered into with hybris.
*/
package org.robbins.flashcards.context;

import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.ResourcePropertySource;

public class FlashCardsApplicationContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext>
{
private static final Logger LOGGER = LoggerFactory.getLogger(FlashCardsApplicationContextInitializer.class);

private final String REPOSITORY_PROFILE_PROPERTY = "repository.profile";
private final String SECURITY_PROFILE_PROPERTY = "security.profile";
private final String DATABASE_PROFILE_PROPERTY = "database.profile";
private final String TESTDATA_PROFILE_PROPERTY= "tests.data.profile";

private final String DEFAULT_REPOSITORY_PROFILE = "spring-data";
private final String DEFAULT_SECURITY_PROFILE = "security-http";
private final String DEFAULT_DATABASE_PROFILE = "embedded";
private final String DEFAULT_TESTDATA_PROFILE = "test-data";

@Override
public void initialize(final ConfigurableApplicationContext applicationContext) {
final ConfigurableEnvironment environment = applicationContext.getEnvironment();

addPropertiesToEnvironment(environment);
addSpringProfiles(environment);
}

private void addPropertiesToEnvironment(final ConfigurableEnvironment environment)
{
addDefaultPropsToEnvironment(environment);
addOverridePropsToEnvironment(environment);
}

private void addSpringProfiles(final ConfigurableEnvironment environment)
{
addDatabaseProfile(environment);
addRepositoryProfile(environment);
addSecurityProfile(environment);
addTestDataProfile(environment);
}

private void addDefaultPropsToEnvironment(final ConfigurableEnvironment environment)
{
final String DEFAULT_PROPS = "default.properties";
final String LOCATION = "classpath:" + DEFAULT_PROPS;

addPropsToEnvironment(DEFAULT_PROPS, LOCATION, environment);
}

private void addOverridePropsToEnvironment(final ConfigurableEnvironment environment)
{
final String OVERRIDE_PROPS = "override.properties";
final String LOCATION = "classpath:" + OVERRIDE_PROPS;

addPropsToEnvironment(OVERRIDE_PROPS, LOCATION, environment);
}

private void addPropsToEnvironment(final String PROPS, final String LOCATION, final ConfigurableEnvironment environment)
{
try
{
final Resource defaultPropsResource = new DefaultResourceLoader().getResource(LOCATION);
LOGGER.info(PROPS + " loaded from {}", defaultPropsResource.getURI());
final PropertiesPropertySource defaultProps = new ResourcePropertySource(defaultPropsResource);
environment.getPropertySources().addFirst(defaultProps);
}
catch (IOException e)
{
LOGGER.info("Unable to find properties file {}", PROPS);
}
}

private void addRepositoryProfile(final ConfigurableEnvironment environment)
{
addProfile(REPOSITORY_PROFILE_PROPERTY, DEFAULT_REPOSITORY_PROFILE, environment);
}

private void addDatabaseProfile(final ConfigurableEnvironment environment)
{
addProfile(DATABASE_PROFILE_PROPERTY, DEFAULT_DATABASE_PROFILE, environment);
}

private void addSecurityProfile(final ConfigurableEnvironment environment)
{
addProfile(SECURITY_PROFILE_PROPERTY, DEFAULT_SECURITY_PROFILE, environment);
}

private void addTestDataProfile(final ConfigurableEnvironment environment)
{
addProfile(TESTDATA_PROFILE_PROPERTY, DEFAULT_TESTDATA_PROFILE, environment);
}

private void addProfile(final String profileName, final String defaultProfileValue, final ConfigurableEnvironment environment)
{
final String repositoryProfile = environment.getProperty(profileName, defaultProfileValue);
LOGGER.info("Setting Spring Profile: {}={}", profileName, repositoryProfile);
environment.addActiveProfile(repositoryProfile);
}
}
6 changes: 2 additions & 4 deletions FlashCards_WebServices/src/main/webapp/WEB-INF/web.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@
</param-value>
</context-param>

<!-- set the default Spring profile. this can be overridden with an environment variable (spring.profiles.active) -->
<context-param>
<param-name>spring.profiles.default</param-name>
<param-value>spring-data,security-http,embedded,test-data</param-value>
<param-name>contextInitializerClasses</param-name>
<param-value>org.robbins.flashcards.context.FlashCardsApplicationContextInitializer</param-value>
</context-param>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
Expand Down