Commit 8fa2fc19 authored by michael.simon's avatar michael.simon
Browse files

Add job to delete old login infos from database

parent 0498147f
......@@ -22,5 +22,7 @@ public interface UserLoginInfoDao extends BaseDao<UserLoginInfoEntity, Long> {
List<UserLoginInfoEntity> findByRegistry(Long registryId);
UserLoginInfoEntity findLastByRegistryAndMethod(Long registryId, UserLoginMethod method);
void deleteLoginInfo(long millis);
}
......@@ -10,10 +10,12 @@
******************************************************************************/
package edu.kit.scc.webreg.dao.jpa;
import java.util.Date;
import java.util.List;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Named;
import javax.persistence.Query;
import edu.kit.scc.webreg.dao.UserLoginInfoDao;
import edu.kit.scc.webreg.entity.UserLoginInfoEntity;
......@@ -55,6 +57,13 @@ public class JpaUserLoginInfoDao extends JpaBaseDao<UserLoginInfoEntity, Long> i
}
}
@Override
public void deleteLoginInfo(long millis) {
Query query = em.createQuery("delete from UserLoginInfoEntity where loginDate <= :loginDate");
query.setParameter("loginDate", new Date(System.currentTimeMillis() - millis));
query.executeUpdate();
}
@Override
public Class<UserLoginInfoEntity> getEntityClass() {
return UserLoginInfoEntity.class;
......
/*******************************************************************************
* Copyright (c) 2014 Michael Simon.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Public License v3.0
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/gpl.html
*
* Contributors:
* Michael Simon - initial
******************************************************************************/
package edu.kit.scc.webreg.job;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import edu.kit.scc.webreg.service.UserLoginInfoService;
public class ClearExpiredLoginInfo extends AbstractExecutableJob {
private static final long serialVersionUID = 1L;
@Override
public void execute() {
Logger logger = LoggerFactory.getLogger(ClearExpiredLoginInfo.class);
try {
logger.debug("Delete expired Login Infos");
long purgeMillis;
if (! getJobStore().containsKey("purge_millis")) {
purgeMillis = 7776000000L; // 90 days default
}
else {
purgeMillis = Long.parseLong(getJobStore().get("purge_millis"));
}
InitialContext ic = new InitialContext();
UserLoginInfoService service = (UserLoginInfoService) ic.lookup("global/bwreg/bwreg-service/UserLoginInfoServiceImpl!edu.kit.scc.webreg.service.UserLoginInfoService");
service.deleteLoginInfo(purgeMillis);
logger.debug("Deletion done");
} catch (NamingException e) {
logger.warn("Could not delete Login Infos: {}", e);
}
}
}
/*******************************************************************************
* Copyright (c) 2014 Michael Simon.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Public License v3.0
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/gpl.html
*
* Contributors:
* Michael Simon - initial
******************************************************************************/
package edu.kit.scc.webreg.service;
import edu.kit.scc.webreg.entity.UserLoginInfoEntity;
public interface UserLoginInfoService extends BaseService<UserLoginInfoEntity, Long> {
void deleteLoginInfo(long millis);
}
package edu.kit.scc.webreg.service.impl;
import javax.ejb.Stateless;
import javax.inject.Inject;
import org.slf4j.Logger;
import edu.kit.scc.webreg.dao.BaseDao;
import edu.kit.scc.webreg.dao.UserLoginInfoDao;
import edu.kit.scc.webreg.entity.UserLoginInfoEntity;
import edu.kit.scc.webreg.service.UserLoginInfoService;
@Stateless
public class UserLoginInfoServiceImpl extends BaseServiceImpl<UserLoginInfoEntity, Long> implements UserLoginInfoService {
private static final long serialVersionUID = 1L;
@Inject
private Logger logger;
@Inject
private UserLoginInfoDao dao;
@Override
public void deleteLoginInfo(long millis) {
Long start = System.currentTimeMillis();
dao.deleteLoginInfo(millis);
logger.info("Deleting old User Login Infos took {} ms", (System.currentTimeMillis() - start));
}
@Override
protected BaseDao<UserLoginInfoEntity, Long> getDao() {
return dao;
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment