de.dnb.oai.harvester.task.HibernateOaiTaskDao Maven / Gradle / Ivy
/**********************************************************************
* Class HibernateOaiTaskDao
*
* Copyright (c) 2008-2012, German National Library / Deutsche Nationalbibliothek
* Adickesallee 1, D-60322 Frankfurt am Main, Federal Republic of Germany
*
* This program is free software.
* 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.
*
* Kadir Karaca Kocer -- German National Library
*
**********************************************************************/
package de.dnb.oai.harvester.task;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import de.dnb.stm.manager.ManagerConstants;
import de.dnb.stm.task.Task;
import de.dnb.stm.task.TaskDao;
/** ********************************************************************
* Class to save tasks in a persistent way with the help of Hibernate.
*
* @author Kadir Karaca Kocer, German National Library
* @version 20090220
* @see TaskDao
* @see org.springframework.orm.hibernate3.support.HibernateDaoSupport
* @since 20080819
**********************************************************************/
/* ********************************************************************
* CHANGELOG:
* 2012.09.26 Logging changed to apache commons logging
* 2008.11.25 OAIRepository --> oai.repository.HibernateRepositoryDao
* 2008.11.20 OAI Repository added
* Created on 19.08.2008 by Kadir Karaca Kocer, German National Library
********************************************************************/
public class HibernateOaiTaskDao extends HibernateDaoSupport implements OaiTaskDao
{
/**
* Logger for this class
*/
private static final Log LOGGER = LogFactory.getLog(HibernateOaiTaskDao.class);
/**
* {@inheritDoc}
*/
@Override
public void deleteTask(Task task) {
getHibernateTemplate().delete(task);
}
/**
* {@inheritDoc}
*/
@Override
public OaiTask getTask(Long taskId) {
return getHibernateTemplate().get(OaiTask.class, taskId);
}
/**
* @param startdate
* @return List of the tasks
* @author kocer, German National Library
*/
/*
public List getTaskList(Date startdate) {
return null;
}*/
/**
* {@inheritDoc}
*/
@Override
@SuppressWarnings("unchecked")
public List getTaskList() {
List tl = getHibernateTemplate().find("from OaiTask as task order by task.startDate");
return tl;
}
/**
* {@inheritDoc}
*/
@Override
public void saveTask(Task task) {
getHibernateTemplate().saveOrUpdate(task);
}
/**
* {@inheritDoc}
*/
@Override
public int deleteOldRequest(Task task, long RequestId) {
for (int i = 0; i < ((OaiTask)task).getProcessedRequests().size(); i++) {
if (RequestId == ((OaiTask)task).getProcessedRequests().get(i).getRequestId()) {
//found the searched request. remove it
((OaiTask)task).getProcessedRequests().remove(i);
getHibernateTemplate().saveOrUpdate(task);
return 0; // success
}
}
return 1; //no such ID
}
/**
* {@inheritDoc}
*/
@Override
public Request getNextRequest(Task task) {
//if this is not a repeating task there will be no next request!
long runInterval = task.getRunInterval();
if (runInterval == 0) {return null;}
//get the already terminated request
Request request = ((OaiTask)task).getRequest();
if (request == null) {
//actual request is NULL because the task was a one-time task. get the last request instead.
List l = ((OaiTask)task).getProcessedRequests();
request = l.get(l.size() -1);
}
//there will be a subsequent request. calculate the parameters
Request tempRequest = new Request();
//set some important values with the actual values
if (request.getStatus() == 0) {
//Terminated successfully
//TODO: use the OAI-PMH Response Date instead (check if repository supplies correct date!)
if(!((OaiTask)task).getRepository().isHasFineGranularity() && task.getRunInterval() >= 86400){
//does not support fine granularity. calculate in days! 86400000 is a day in milliseconds.
if(request.getResponseDate() != null && request.getUntilDate().after(request.getResponseDate())){
tempRequest.setFromDate(new java.util.Date(request.getResponseDate().getTime() + 86400000)); //Add one day, as it is subtracted when the task is processed anyway
}
else{
tempRequest.setFromDate(new java.util.Date(request.getUntilDate().getTime() + 86400000));
}
}
else{
//--> former until date is now from Date + 1 second
if(request.getResponseDate() != null && request.getUntilDate().after(request.getResponseDate())){
if (LOGGER.isDebugEnabled()) LOGGER.debug("New From-Date adjusted to server ResponseDate!");
//26.09.2011, neubauer: To eliminate holes in the harvesting, we do not add 1s to the last until or responseDate anymore.
//This may lead to duplication-records, but it is as the oai specification recommends
tempRequest.setFromDate(new java.util.Date(request.getResponseDate().getTime()));
}
else{
tempRequest.setFromDate(new java.util.Date(request.getUntilDate().getTime()));
}
}
} else {
//there was an error --> fromDate must be the same
tempRequest.setFromDate(request.getFromDate());
}
if (request.getUntilDate() == null) {
//previous until date is not set. Should not occur! Set it.
request.setUntilDate(Calendar.getInstance(ManagerConstants.LOCALE).getTime());
}
//calculate the until date
java.util.Date untilDate = null;
untilDate = new java.util.Date(request.getUntilDate().getTime() + (runInterval * 1000));
tempRequest.setUntilDate(untilDate);
return tempRequest;
}
/**
* {@inheritDoc}
*/
@Override
public void garbageCollector(long timeLimitTask, long timeLimitRequest) {
long nowMillis = Calendar.getInstance(ManagerConstants.LOCALE).getTimeInMillis();
long limitTask = nowMillis - (timeLimitTask * 60000);
long limitRequest = nowMillis - (timeLimitRequest * 60000);
List tl = getTaskList();
for (Task task : tl) {
boolean taskUpdated = false;
if (LOGGER.isDebugEnabled()) LOGGER.debug("[Garbage Collector] Processing OaiTask: " + task.getTaskId());
if (task.getRunInterval() > 0) {
//repeating task: we can not erase the task! Get its processed requests
//loop & check if they are older than limit
if (timeLimitRequest > 0) {
for (Iterator iterator = ((OaiTask)task).getProcessedRequests().iterator();iterator.hasNext();) {
Request r = iterator.next();
if (( r != null) && (r.getFinishedAt() != null)) {
long finishedAt = r.getFinishedAt().getTime();
if (finishedAt < limitRequest) {
//found the searched request. remove it
iterator.remove();
taskUpdated = true;
}
} else {
//finishedAt of a already finished request can not be NULL. ERROR: remove it!
iterator.remove();
taskUpdated = true;
} // else
} //for
} //if
} else {
//one time task
if ((timeLimitTask > 0) &&
(((OaiTask)task).getRequest().getFinishedAt() != null) &&
(((OaiTask)task).getRequest().getFinishedAt().getTime() < limitTask)){
//its old, erase it!
if (LOGGER.isDebugEnabled()) LOGGER.debug(" Removing OaiTask: " + task.getTaskId());
deleteTask(task);
} //if
} //else
if(taskUpdated){
getHibernateTemplate().saveOrUpdate(task);
}
} //for
} //GarbageCollector
} //Class
© 2015 - 2025 Weber Informatics LLC | Privacy Policy