|
|
@ -19,6 +19,7 @@ package sample.data.gemfire.service;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
|
|
import javax.annotation.PostConstruct;
|
|
|
|
import javax.annotation.PostConstruct;
|
|
|
|
|
|
|
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
@ -29,102 +30,106 @@ import org.springframework.util.Assert;
|
|
|
|
import sample.data.gemfire.domain.Gemstone;
|
|
|
|
import sample.data.gemfire.domain.Gemstone;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* The GemstoneServiceImpl class is a Service object implementing the GemstoneService interface containing
|
|
|
|
* The GemstoneServiceImpl class is a Service object implementing the GemstoneService
|
|
|
|
* business logic and rules in addition to data services for processing Gemstones.
|
|
|
|
* interface containing business logic and rules in addition to data services for
|
|
|
|
* <p/>
|
|
|
|
* processing Gemstones.
|
|
|
|
|
|
|
|
*
|
|
|
|
* @author John Blum
|
|
|
|
* @author John Blum
|
|
|
|
* @see org.springframework.stereotype.Service
|
|
|
|
|
|
|
|
* @see org.springframework.transaction.annotation.Transactional
|
|
|
|
|
|
|
|
* @see sample.data.gemfire.domain.Gemstone
|
|
|
|
|
|
|
|
* @see sample.data.gemfire.service.GemstoneRepository
|
|
|
|
|
|
|
|
* @see sample.data.gemfire.service.GemstoneService
|
|
|
|
|
|
|
|
* @since 1.0.0
|
|
|
|
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
@Service("gemstoneService")
|
|
|
|
@Service("gemstoneService")
|
|
|
|
@SuppressWarnings("unused")
|
|
|
|
|
|
|
|
public class GemstoneServiceImpl implements GemstoneService {
|
|
|
|
public class GemstoneServiceImpl implements GemstoneService {
|
|
|
|
|
|
|
|
|
|
|
|
protected static final List<String> APPROVED_GEMS = new ArrayList<String>(Arrays.asList(
|
|
|
|
protected static final List<String> APPROVED_GEMS = new ArrayList<String>(
|
|
|
|
"ALEXANDRITE", "AQUAMARINE", "DIAMOND", "OPAL", "PEARL", "RUBY", "SAPPHIRE", "SPINEL", "TOPAZ"));
|
|
|
|
Arrays.asList("ALEXANDRITE", "AQUAMARINE", "DIAMOND", "OPAL", "PEARL",
|
|
|
|
|
|
|
|
"RUBY", "SAPPHIRE", "SPINEL", "TOPAZ"));
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
@Autowired
|
|
|
|
private GemstoneRepository gemstoneRepo;
|
|
|
|
private GemstoneRepository gemstoneRepo;
|
|
|
|
|
|
|
|
|
|
|
|
@PostConstruct
|
|
|
|
@PostConstruct
|
|
|
|
public void init() {
|
|
|
|
public void init() {
|
|
|
|
Assert.notNull(gemstoneRepo, "A reference to the 'GemstoneRepository' was not properly configured!");
|
|
|
|
Assert.notNull(this.gemstoneRepo,
|
|
|
|
|
|
|
|
"A reference to the 'GemstoneRepository' was not properly configured!");
|
|
|
|
System.out.printf("%1$s initialized!%n", getClass().getSimpleName());
|
|
|
|
System.out.printf("%1$s initialized!%n", getClass().getSimpleName());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Returns a count of the number of Gemstones in the GemFire Cache.
|
|
|
|
* Returns a count of the number of Gemstones in the GemFire Cache.
|
|
|
|
* <p/>
|
|
|
|
* <p/>
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @return a long value indicating the number of Gemstones in the GemFire Cache.
|
|
|
|
* @return a long value indicating the number of Gemstones in the GemFire Cache.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Transactional(readOnly = true)
|
|
|
|
@Transactional(readOnly = true)
|
|
|
|
public long count() {
|
|
|
|
public long count() {
|
|
|
|
return gemstoneRepo.count();
|
|
|
|
return this.gemstoneRepo.count();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Gets a Gemstone by ID.
|
|
|
|
* Gets a Gemstone by ID.
|
|
|
|
* <p/>
|
|
|
|
* <p/>
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param id a long value indicating the identifier of the Gemstone.
|
|
|
|
* @param id a long value indicating the identifier of the Gemstone.
|
|
|
|
* @return a Gemstone with ID, or null if no Gemstone exists with ID.
|
|
|
|
* @return a Gemstone with ID, or null if no Gemstone exists with ID.
|
|
|
|
* @see sample.data.gemfire.domain.Gemstone
|
|
|
|
* @see sample.data.gemfire.domain.Gemstone
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Transactional(readOnly = true)
|
|
|
|
@Transactional(readOnly = true)
|
|
|
|
public Gemstone get(final Long id) {
|
|
|
|
public Gemstone get(final Long id) {
|
|
|
|
return gemstoneRepo.findOne(id);
|
|
|
|
return this.gemstoneRepo.findOne(id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Gets a Gemstone by name.
|
|
|
|
* Gets a Gemstone by name.
|
|
|
|
* <p/>
|
|
|
|
* <p/>
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param name a String value indicating the name of the Gemstone.
|
|
|
|
* @param name a String value indicating the name of the Gemstone.
|
|
|
|
* @return a Gemstone with name, or null if no Gemstone exists with name.
|
|
|
|
* @return a Gemstone with name, or null if no Gemstone exists with name.
|
|
|
|
* @see sample.data.gemfire.domain.Gemstone
|
|
|
|
* @see sample.data.gemfire.domain.Gemstone
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Transactional(readOnly = true)
|
|
|
|
@Transactional(readOnly = true)
|
|
|
|
public Gemstone get(final String name) {
|
|
|
|
public Gemstone get(final String name) {
|
|
|
|
return gemstoneRepo.findByName(name);
|
|
|
|
return this.gemstoneRepo.findByName(name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Return a listing of Gemstones currently stored in the GemFire Cache.
|
|
|
|
* Return a listing of Gemstones currently stored in the GemFire Cache.
|
|
|
|
* <p/>
|
|
|
|
* <p/>
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @return a Iterable object to iterate over the list of Gemstones currently stored in the GemFire Cache.
|
|
|
|
* @return a Iterable object to iterate over the list of Gemstones currently stored in
|
|
|
|
|
|
|
|
* the GemFire Cache.
|
|
|
|
* @see java.lang.Iterable
|
|
|
|
* @see java.lang.Iterable
|
|
|
|
* @see sample.data.gemfire.domain.Gemstone
|
|
|
|
* @see sample.data.gemfire.domain.Gemstone
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Transactional(readOnly = true)
|
|
|
|
@Transactional(readOnly = true)
|
|
|
|
public Iterable<Gemstone> list() {
|
|
|
|
public Iterable<Gemstone> list() {
|
|
|
|
return gemstoneRepo.findAll();
|
|
|
|
return this.gemstoneRepo.findAll();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Saves the specified Gemstone to the GemFire Cache.
|
|
|
|
* Saves the specified Gemstone to the GemFire Cache.
|
|
|
|
* <p/>
|
|
|
|
* <p/>
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param gemstone the Gemstone to save in the GemFire Cache.
|
|
|
|
* @param gemstone the Gemstone to save in the GemFire Cache.
|
|
|
|
* @return the saved Gemstone.
|
|
|
|
* @return the saved Gemstone.
|
|
|
|
* @see sample.data.gemfire.domain.Gemstone
|
|
|
|
* @see sample.data.gemfire.domain.Gemstone
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Transactional
|
|
|
|
@Transactional
|
|
|
|
public Gemstone save(final Gemstone gemstone) {
|
|
|
|
public Gemstone save(final Gemstone gemstone) {
|
|
|
|
Assert.notNull(gemstone, "The Gemstone to save must not be null!");
|
|
|
|
Assert.notNull(gemstone, "The Gemstone to save must not be null!");
|
|
|
|
Assert.notNull(gemstone.getName(), "The name of the Gemstone must be specified!");
|
|
|
|
Assert.notNull(gemstone.getName(), "The name of the Gemstone must be specified!");
|
|
|
|
|
|
|
|
|
|
|
|
// NOTE deliberately (naively) validate the Gemstone after mutating data access in GemFire rather than before
|
|
|
|
// NOTE deliberately (naively) validate the Gemstone after mutating data access in
|
|
|
|
|
|
|
|
// GemFire rather than before
|
|
|
|
// to demonstrate transactions in GemFire.
|
|
|
|
// to demonstrate transactions in GemFire.
|
|
|
|
Gemstone savedGemstone = validate(gemstoneRepo.save(gemstone));
|
|
|
|
Gemstone savedGemstone = validate(this.gemstoneRepo.save(gemstone));
|
|
|
|
|
|
|
|
|
|
|
|
Assert.state(savedGemstone.equals(get(gemstone.getId())),
|
|
|
|
Assert.state(savedGemstone.equals(get(gemstone.getId())), String.format(
|
|
|
|
String.format("Failed to find Gemstone (%1$s) in GemFire's Cache Region 'Gemstones'!", gemstone));
|
|
|
|
"Failed to find Gemstone (%1$s) in GemFire's Cache Region 'Gemstones'!",
|
|
|
|
|
|
|
|
gemstone));
|
|
|
|
|
|
|
|
|
|
|
|
System.out.printf("Saved Gemstone (%1$s)%n", savedGemstone.getName());
|
|
|
|
System.out.printf("Saved Gemstone (%1$s)%n", savedGemstone.getName());
|
|
|
|
|
|
|
|
|
|
|
@ -133,9 +138,11 @@ public class GemstoneServiceImpl implements GemstoneService {
|
|
|
|
|
|
|
|
|
|
|
|
private Gemstone validate(final Gemstone gemstone) {
|
|
|
|
private Gemstone validate(final Gemstone gemstone) {
|
|
|
|
if (!APPROVED_GEMS.contains(gemstone.getName().toUpperCase())) {
|
|
|
|
if (!APPROVED_GEMS.contains(gemstone.getName().toUpperCase())) {
|
|
|
|
// NOTE if the Gemstone is not valid, blow chunks (should cause transaction to rollback in GemFire)!
|
|
|
|
// NOTE if the Gemstone is not valid, blow chunks (should cause transaction to
|
|
|
|
|
|
|
|
// rollback in GemFire)!
|
|
|
|
System.err.printf("Illegal Gemstone (%1$s)!%n", gemstone.getName());
|
|
|
|
System.err.printf("Illegal Gemstone (%1$s)!%n", gemstone.getName());
|
|
|
|
throw new IllegalGemstoneException(String.format("'%1$s' is not a valid Gemstone!", gemstone.getName()));
|
|
|
|
throw new IllegalGemstoneException(String.format(
|
|
|
|
|
|
|
|
"'%1$s' is not a valid Gemstone!", gemstone.getName()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return gemstone;
|
|
|
|
return gemstone;
|
|
|
|