package com.foc.web.microservice.loockups;

import com.foc.Globals;
import com.foc.desc.FocDesc;
import com.foc.list.FocList;
import com.foc.shared.json.B01JsonBuilder;

public class WSSingleLookup {
	private String  key        = null;
	private String  json       = null;
	private FocDesc focDesc    = null;
	private long    expiryDuration = 0;
	private long    nextExpiryTime = 0;
	
	public WSSingleLookup(String key, FocDesc focDesc) {
		this(key, focDesc, 0);
	}
	
	public WSSingleLookup(String key, FocDesc focDesc, long expiryDuration) {
		this.key = key;
		this.focDesc = focDesc;
		setExpiryDuration(expiryDuration);
	}

	private void dispose() {
		focDesc = null;
	}
	
	public FocDesc getFocDesc() {
		return focDesc;
	}
	
	protected boolean shouldRefresh() {
		boolean shouldRefresh = false;
		if (nextExpiryTime > 0) {
			long currentTime = System.currentTimeMillis();
			shouldRefresh = currentTime > nextExpiryTime;
			if(shouldRefresh) nextExpiryTime = currentTime + expiryDuration;
		}
		return shouldRefresh;
	}
	
	public synchronized String getJson() {
		if(json == null) {
			jsonRebuild();
		} else if (shouldRefresh()){
			refresh();
		}
		return json;
	}
	
	public String getKey() {
		return key;
	}

	protected synchronized void replaceJson(String newJson) {
		json = newJson;
	}

	protected B01JsonBuilder newJsonBuiler() {
		B01JsonBuilder builder = new B01JsonBuilder();
		builder.setPrintForeignKeyFullObject(true);
		builder.setHideWorkflowFields(true);
		builder.setScanSubList(true);
		return builder;
	}

	public FocList getFocList(boolean load) {
		FocDesc focDesc = getFocDesc();
		FocList list = focDesc != null ? focDesc.getFocList() : null;
		if(list != null && load) {
			list.loadIfNotLoadedFromDB();
		}
		return list; 
	}
		
	public void jsonRebuild() {
		try {
			B01JsonBuilder builder = newJsonBuiler();
			FocList focList = getFocList(true); 
			if (focList != null && builder != null) {
				focList.toJson(builder);
				String newJson = builder.toString();
				if(newJson != null) {
					replaceJson(newJson);
				}
			}
		} catch (Exception e) {
			Globals.logException(e);
		}
	}
	
	public synchronized void refresh(){
		FocList focList = getFocList(true); 
		if (focList != null) focList.reloadFromDB();
		jsonRebuild();
	}

	public long getExpiryTime() {
		return expiryDuration;
	}

	public void setExpiryDuration(long expiryTime) {
		this.expiryDuration = expiryTime;
		if (this.expiryDuration > 0) {
			nextExpiryTime = System.currentTimeMillis();
		}
	}

}

@@ -1753,18 +1753,16 @@
 st; %0A%09%7D%0A
-%09%09
 %0A%09public
@@ -1771,35 +1771,17 @@
 oid 
-jsonRebuild() %7B%0A%09%09try %7B%0A%09%09%09
+fillJson(
 B01J
@@ -1802,31 +1802,14 @@
 lder
- = newJsonBuiler();
+) %7B
 %0A%09%09
-%09
 FocL
@@ -1841,17 +1841,16 @@
 true); %0A
-%09
 %09%09if (fo
@@ -1888,17 +1888,16 @@
 l) %7B%0A%09%09%09
-%09
 focList.
@@ -1913,16 +1913,160 @@
 ilder);%0A
+%09%09%7D%0A%09%7D%0A%09%0A%09public void jsonRebuild() %7B%0A%09%09try %7B%0A%09%09%09B01JsonBuilder builder = newJsonBuiler();%0A%09%09%09if(builder != null) %7B%0A%09%09%09%09fillJson(builder);%0A%09%09%09%09%0A
 %09%09%09%09Stri

Allow to override the fillJson method in WSSingleLookup