
com.strategicgains.repoexpress.redis.RedisJOhmRepository Maven / Gradle / Ivy
The newest version!
/*
Copyright 2012, Strategic Gains, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.strategicgains.repoexpress.redis;
import redis.clients.johm.JOhm;
import com.strategicgains.repoexpress.AbstractObservableRepository;
import com.strategicgains.repoexpress.domain.Identifiable;
import com.strategicgains.repoexpress.domain.Identifier;
import com.strategicgains.repoexpress.exception.DuplicateItemException;
import com.strategicgains.repoexpress.exception.ItemNotFoundException;
/**
* Persist objects (mainly sub-classes of AbstractRedisJOhmEntity) to a Redis datastore using JOhm.
* The Object must implement Identifiable and the ID must be numeric (e.g. Long, Integer).
*
* @author toddf
* @since Jun 6, 2012
* @see AbstractRedisJOhmEntity
*/
public class RedisJOhmRepository
extends AbstractObservableRepository
{
private Class entityClass;
public RedisJOhmRepository(Class entityClass)
{
super();
this.entityClass = entityClass;
}
@Override
public T doCreate(T object)
{
if (JOhm.isNew(object))
{
return JOhm.save(object);
}
throw new DuplicateItemException(object.getClass().getSimpleName()
+ " ID already exists: " + object.getId());
}
@Override
public void doDelete(T object)
{
JOhm.delete(entityClass, (Long) object.getId().primaryKey());
}
@Override
public T doRead(Identifier id)
{
return JOhm.get(entityClass, (Long) id.primaryKey());
}
@Override
public T doUpdate(T object)
{
if (JOhm.isNew(object))
{
throw new ItemNotFoundException(object.getClass().getSimpleName()
+ " ID not found: " + object.getId());
}
return JOhm.save(object);
}
@Override
public boolean exists(Identifier id)
{
return (JOhm.get(entityClass, (Long) id.primaryKey()) != null);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy