All Downloads are FREE. Search and download functionalities are using the official Maven repository.

persistence.persistence-example.3.5.1.source-code.ToyShop Maven / Gradle / Ivy

There is a newer version: 3.8.16
Show newest version
import com.distelli.persistence.ConvertMarker;
import com.distelli.persistence.Index;
import com.distelli.persistence.PageIterator;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.List;
import javax.inject.Inject;
import javax.persistence.EntityExistsException;
import javax.persistence.RollbackException;

public class ToyShop {
    /**
     * Declare a POJO to convert that will be persisted. Note that this can be a
     * complex object.
     */
    public static class Toy {
         public long id;
         public String name;
         public String description;
         public long priceInCents;
         public long weightInGrams;
         public String imageUrl;
         public Category category;
         public Dimensions dimensions;
         // Record Version Number (optimistic locking)
         public long rvn;
    }
    public static class Dimensions {
         public int heightInCm;
         public int widthInCm;
    }
    public static enum Category {
         PUZZLES, DOLLS, BUILDING, PARTY, EDUCATIONAL, SPORTS;
    }
    private Index _toysById;
    private Index _toysByCategory;

    @Inject
    protected ToyShop(Index.Factory indexFactory, ConvertMarker.Factory convertMarkerFactory) {
        ObjectMapper om = new ObjectMapper();
        _toysById = indexFactory.create(Toy.class)
            .withTableName("toys")
            .withNoEncrypt("id", "category", "name")
            .withHashKeyName("id")
            .withConvertValue(om::convertValue)
            .withConvertMarker(convertMarkerFactory.create("id"))
            .build();
        _toysByCategory = indexFactory.create(Toy.class)
            .withIndexName("toys", "category-name")
            .withNoEncrypt("id", "category", "name")
            .withHashKeyName("category")
            .withRangeKeyName("name")
            .withConvertValue(om::convertValue)
            .withConvertMarker(convertMarkerFactory.create("category", "name"))
            .build();
    }

    // Create
    public void addToy(Toy toy) throws EntityExistsException {
        toy.rvn = 0; // Start with 0 record version number.
        _toysById.putItemOrThrow(toy);
    }

    // Read
    public Toy getToyById(long id) {
        return _toysById.getItem(id);
    }

    // Query (alternative index)
    public List getToysByCategory(Category category, PageIterator pageIterator) {
        return _toysByCategory.queryItems(category.toString(), pageIterator)
             .list();
    }

    // Scan everything
    public List getAllToys(PageIterator pageIterator) {
        return _toysById.scanItems(pageIterator);
    }

    // Update
    public void updateDescription(long id, long rvn, String description) throws RollbackException {
        _toysById.updateItem(id, null)
             .set("description", description)
             .set("rvn", rvn+1)
             .when((cond) -> cond.eq("rvn", rvn));
    }

    // Delete
    public void removeToy(long id) {
        _toysById.deleteItem(id, null);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy