com.erudika.para.persistence.MockDAO Maven / Gradle / Ivy
/*
* Copyright 2013-2017 Erudika. https://erudika.com
*
* 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.
*
* For issues and patches go to: https://github.com/erudika
*/
package com.erudika.para.persistence;
import com.erudika.para.annotations.Locked;
import com.erudika.para.core.ParaObject;
import com.erudika.para.core.utils.ParaObjectUtils;
import com.erudika.para.utils.Config;
import com.erudika.para.utils.Pager;
import com.erudika.para.utils.Utils;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import javax.inject.Singleton;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Fake DAO for in-memory persistence.
* Used for testing and development without a database.
* @author Alex Bogdanovski [[email protected]]
*/
@Singleton
public class MockDAO implements DAO {
private static final Logger logger = LoggerFactory.getLogger(MockDAO.class);
private static final Map> MAPS =
new ConcurrentHashMap>();
@Override
public String create(String appid, P so) {
if (so == null) {
return null;
}
if (StringUtils.isBlank(so.getId())) {
so.setId(Utils.getNewId());
}
if (so.getTimestamp() == null) {
so.setTimestamp(Utils.timestamp());
}
so.setAppid(appid);
getMap(appid).put(so.getId(), ParaObjectUtils.setAnnotatedFields(ParaObjectUtils.toObject(so.getType()),
ParaObjectUtils.getAnnotatedFields(so), null));
logger.debug("DAO.create() {}", so.getId());
return so.getId();
}
@Override
@SuppressWarnings("unchecked")
public
P read(String appid, String key) {
if (key == null || StringUtils.isBlank(appid)) {
return null;
}
P so = (P) getMap(appid).get(key);
logger.debug("DAO.read() {} -> {}", key, so);
return so;
}
@Override
public
void update(String appid, P so) {
if (so != null && !StringUtils.isBlank(appid)) {
so.setUpdated(Utils.timestamp());
ParaObject soUpdated = getMap(appid).get(so.getId());
getMap(appid).put(so.getId(), ParaObjectUtils.setAnnotatedFields(soUpdated,
ParaObjectUtils.getAnnotatedFields(so), Locked.class));
logger.debug("DAO.update() {}", so.getId());
}
}
@Override
public
void delete(String appid, P so) {
if (so != null && !StringUtils.isBlank(appid)) {
getMap(appid).remove(so.getId());
logger.debug("DAO.delete() {}", so.getId());
}
}
@Override
public
void createAll(String appid, List
objects) {
if (StringUtils.isBlank(appid) || objects == null) {
return;
}
for (P p : objects) {
create(appid, p);
}
logger.debug("DAO.createAll() {}", objects.size());
}
@Override
@SuppressWarnings("unchecked")
public
Map readAll(String appid, List keys, boolean getAllColumns) {
if (keys == null || StringUtils.isBlank(appid)) {
return Collections.emptyMap();
}
Map results = new LinkedHashMap(keys.size());
for (String key : keys) {
if (getMap(appid).containsKey(key)) {
results.put(key, (P) read(appid, key));
}
}
logger.debug("DAO.readAll() {}", results.size());
return results;
}
@Override
@SuppressWarnings("unchecked")
public List
readPage(String appid, Pager pager) {
List
results = new LinkedList
();
if (StringUtils.isBlank(appid)) {
return results;
}
if (pager == null) {
pager = new Pager();
}
if (pager.getCount() >= getMap(appid).size()) {
return results;
}
String lastKey = pager.getLastKey();
boolean found = false;
int i = 0;
for (String key : getMap(appid).keySet()) {
if (lastKey != null && !found) {
found = key.equals(lastKey);
} else {
results.add((P) getMap(appid).get(key));
i++;
}
if (i >= pager.getLimit()) {
pager.setLastKey(key);
break;
}
}
pager.setCount(pager.getCount() + i);
return results;
}
@Override
public
void updateAll(String appid, List
objects) {
if (!StringUtils.isBlank(appid) && objects != null) {
for (P obj : objects) {
if (obj != null) {
update(appid, obj);
}
}
logger.debug("DAO.updateAll() {}", objects.size());
}
}
@Override
public
void deleteAll(String appid, List
objects) {
if (!StringUtils.isBlank(appid) && objects != null) {
for (P obj : objects) {
if (obj != null && getMap(appid).containsKey(obj.getId())) {
delete(appid, obj);
}
}
logger.debug("DAO.deleteAll() {}", objects.size());
}
}
private Map getMap(String appid) {
if (!MAPS.containsKey(appid)) {
MAPS.put(appid, new ConcurrentHashMap());
}
return MAPS.get(appid);
}
////////////////////////////////////////////////////////////////////
@Override
public String create(P so) {
return create(Config.APP_NAME_NS, so);
}
@Override
public
P read(String key) {
return read(Config.APP_NAME_NS, key);
}
@Override
public
void update(P so) {
update(Config.APP_NAME_NS, so);
}
@Override
public
void delete(P so) {
delete(Config.APP_NAME_NS, so);
}
@Override
public
void createAll(List
objects) {
createAll(Config.APP_NAME_NS, objects);
}
@Override
public
Map readAll(List keys, boolean getAllColumns) {
return readAll(Config.APP_NAME_NS, keys, getAllColumns);
}
@Override
public List
readPage(Pager pager) {
return readPage(Config.APP_NAME_NS, pager);
}
@Override
public
void updateAll(List
objects) {
updateAll(Config.APP_NAME_NS, objects);
}
@Override
public
void deleteAll(List
objects) {
deleteAll(Config.APP_NAME_NS, objects);
}
}