package org.openntf.xrest.xsp.model.strategy;

import java.util.ArrayList;
import java.util.List;

import org.openntf.xrest.xsp.exec.Context;
import org.openntf.xrest.xsp.exec.DatabaseProvider;
import org.openntf.xrest.xsp.exec.ExecutorException;

import lotus.domino.Database;
import lotus.domino.Document;
import lotus.domino.View;
import lotus.domino.ViewEntry;
import lotus.domino.ViewNavigator;

public class AllByViewPaged extends AbstractViewDatabaseStrategy implements StrategyModel<List<Document>> {
	private Database dbAccess;
	private View viewAccess;
	private ViewNavigator vnav;
	private ViewEntry entNext;

	private static final int DEFAULT_START = 1;
	private static final int DEFAULT_COUNT = 10;

	@Override
	public List<Document> getModel(final Context context) throws ExecutorException {
		try {
			dbAccess = DatabaseProvider.INSTANCE.getDatabase(getDatabaseNameValue(context), context.getDatabase(),
					context.getSession());
			viewAccess = dbAccess.getView(getViewNameValue(context));
			vnav = viewAccess.createViewNav();
			// TODO: should probably check if dbAccess, viewAccess and vnav are
			// not null before proceeding

			int start = getParam(context.getRequest().getParameter("start"), DEFAULT_START);
			int count = getParam(context.getRequest().getParameter("count"), DEFAULT_COUNT);
			List<Document> docs = new ArrayList<Document>();
			entNext = vnav.getNth(start);
			int i = 0;
			while (entNext != null && i < count) {
				ViewEntry entProcess = entNext;
				entNext = vnav.getNext(entNext);
				docs.add(dbAccess.getDocumentByUNID(entProcess.getUniversalID()));
				i++;
			}
			return docs;
		} catch (Exception ex) {
			throw new ExecutorException(500, ex, "", "getmodel");
		}
	}

	private int getParam(final String param, final int defaultVal) {
		int count = defaultVal;
		try {
			if (null != param) {
				count = Integer.parseInt(param);
				if (count < 1) {
					count = defaultVal;
				}
			}
		} catch (NumberFormatException e) {
		}
		return count;
	}

	@Override
	public void cleanUp() {
		try {
			if (null != vnav) {
				vnav.recycle();
			}
			if (null != viewAccess) {
				viewAccess.recycle();
			}
			if (null != dbAccess) {
				dbAccess.recycle();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}

@@ -607,19 +607,22 @@
 ntry ent
-Nex
+Curren
 t;%0A%0A%09pri
@@ -1163,32 +1163,40 @@
 start = getParam
+IntValue
 (context.getRequ
@@ -1263,16 +1263,24 @@
 getParam
+IntValue
 (context
@@ -1384,34 +1384,46 @@
 ent%3E();%0A
+%0A
 %09%09%09
-entNext
+int skippedEntries
  = vnav.
 getNth(s
@@ -1418,14 +1418,12 @@
 nav.
-getNth
+skip
 (sta
@@ -1435,133 +1435,160 @@
 %09%09%09i
-nt i = 0;%0A%09%09%09while (entNex
+f (skippedEntries == start) %7B%0A%09%09%09%09entCurren
 t 
-!
 = 
-null && i %3C count) %7B%0A%09%09%09%09ViewEntry entProcess = entNext;%0A%09%09%09%09entNex
+vnav.getCurrent();%0A%09%09%09%09int i = 0;%0A%09%09%09%09while (entCurren
 t 
+!
 = 
-vnav.getNext(entNext);%0A
+null && entCurrent.isValid() && i %3C count) %7B%0A%09
 %09%09%09%09
@@ -1630,15 +1630,15 @@
 (ent
-Process
+Current
 .get
@@ -1662,16 +1662,17 @@
 %09%09%09%09
+%09
 i++;%0A%09%09%09
 %7D%0A%09%09
@@ -1671,117 +1671,567 @@
 %0A%09%09%09
-%7D%0A%09%09%09return docs;%0A%09%09%7D catch (Exception ex) %7B%0A%09%09%09throw new ExecutorException(500, ex, %22%22, %22getmodel%22);%0A%09%09%7D%0A%09%7D%0A
+%09%09ViewEntry nextEntry = vnav.getNext();%0A%09%09%09%09%09// recycle!%0A%09%09%09%09%09entCurrent.recycle();%0A%09%09%09%09%09entCurrent = nextEntry;%0A%09%09%09%09%7D%0A%09%09%09%7D else %7B%0A%09%09%09%09// TODO: handle the situation when we did not skipped to desired%0A%09%09%09%09// start: is it because we are already at the end of view?%0A%09%09%09%7D%0A%09%09%09return docs;%0A%09%09%7D catch (Exception ex) %7B%0A%09%09%09throw new ExecutorException(500, ex, %22%22, %22getmodel%22);%0A%09%09%7D%0A%09%7D%0A%0A%09/**%0A%09 * Convert given param to int, assing defaultVal when it is empty or less%0A%09 * than 1%0A%09 * %0A%09 * @param param%0A%09 * @param defaultVal%0A%09 * @return int value of param or defaulVal%0A%09 */
 %0A%09pr
@@ -2248,16 +2248,24 @@
 getParam
+IntValue
 (final S

refactor to use ViewNavigator.skip to get to the desired start position