package de.eknori;

import static com.ibm.xsp.extlib.util.ExtLibUtil.getApplicationScope;

import java.util.Date;
import java.util.Map;

import lotus.domino.NotesException;
public class Tools {
	private boolean debugMode = true;

	public Tools() {}

	public boolean refreshCache(String key, long cacheInterval) throws NotesException{
		Map<String, Object> applicationScope = getApplicationScope();
		Long currentTime  = new Date().getTime();
		if (!applicationScope.containsKey(key + "_time")){
			applicationScope.put(key + "_time", currentTime);
			if (this.isDebugMode()) {
				System.out.println("refreshCache(): -- Initializing --");
			}
			return true;
		}
		long diffInSecs  = currentTime - (Long)applicationScope.get(key + "_time")  ;
		if (this.isDebugMode()) {
			System.out.println("refreshCache(): diffInSecs: " + diffInSecs);
		}
		if (diffInSecs < cacheInterval * 1000) {
			if (this.isDebugMode()) {
				System.out.println("refreshCache(): cache still valid");
			}
			return false;
		} else {
			applicationScope.put(key + "_time", currentTime);
			if (this.isDebugMode()) {
				System.out.println("refreshCache(): needs to be refreshed after " + cacheInterval + " seconds");
			}
			return true;
		}
	}

	public boolean isDebugMode() {
		return this.debugMode;
	}

	public void setDebugMode(final boolean debugMode) {
		this.debugMode = debugMode;
	}
}