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

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

import org.openntf.xrest.xsp.exec.DatabaseProvider;
import org.openntf.xrest.xsp.exec.ExecutorException;
import org.openntf.xrest.xsp.exec.convertor.DocumentList2JsonConverter;
import org.openntf.xrest.xsp.exec.datacontainer.DocumentListDataContainer;
import org.openntf.xrest.xsp.model.DataContainer;
import org.openntf.xrest.xsp.model.RouteProcessor;
import org.openntf.xrest.xsp.utils.NotesObjectRecycler;

import com.ibm.commons.util.io.json.JsonJavaArray;

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

import lotus.domino.Database;
import lotus.domino.Document;
import lotus.domino.NotesException;
import lotus.domino.View;

public class AllByView extends AbstractViewDatabaseStrategy implements StrategyModel<DocumentListDataContainer, JsonJavaArray> {

	private Database dbAccess;
	private View viewAccess;

	@Override
	public DocumentListDataContainer buildDataContainer(Context context) throws ExecutorException {
		try {
			dbAccess = DatabaseProvider.INSTANCE.getDatabase(getDatabaseNameValue(context), context.getDatabase(), context.getSession());
			viewAccess = dbAccess.getView(getViewNameValue(context));
			List<Document> docs = new ArrayList<Document>();
			Document docNext = viewAccess.getFirstDocument();
			while (docNext != null) {
				Document docProcess = docNext;
				docNext = viewAccess.getNextDocument(docNext);
				docs.add(docProcess);
			}
			return new DocumentListDataContainer(docs);
		} catch (Exception ex) {
			throw new ExecutorException(500, ex, "", "getmodel");
		}
	}

	@Override
	public void cleanUp() {
		NotesObjectRecycler.recycle(viewAccess, dbAccess);
	}

	@Override
	public JsonJavaArray buildResponse(Context context, RouteProcessor routeProcessor, DataContainer<?> dc) throws NotesException {
		DocumentListDataContainer docListDC = (DocumentListDataContainer) dc;
		DocumentList2JsonConverter d2jc = new DocumentList2JsonConverter(docListDC, routeProcessor, context);
		return d2jc.buildJsonFromDocument();
	}
}

@@ -88,24 +88,67 @@
 util.List;%0A%0A
+import org.openntf.xrest.xsp.exec.Context;%0A
 import org.o
@@ -601,52 +601,8 @@
 y;%0A%0A
-import org.openntf.xrest.xsp.exec.Context;%0A%0A
 impo
@@ -718,16 +718,82 @@
 no.View;
+%0Aimport lotus.domino.ViewEntry;%0Aimport lotus.domino.ViewNavigator;
 %0A%0Apublic
@@ -856,17 +856,19 @@
 plements
- 
+%0A%09%09
 Strategy
@@ -1037,16 +1037,22 @@
 ntainer(
+final 
 Context 
@@ -1200,17 +1200,22 @@
 abase(),
- 
+%0A%09%09%09%09%09
 context.
@@ -1349,91 +1349,160 @@
 %0A%09%09%09
-Document docNext = viewAccess.getFirstDocument();%0A%09%09%09while (docNext != null
+ViewNavigator vnav = viewAccess.createViewNav();%0A%09%09%09ViewEntry entCurrent = vnav.getFirst();%0A%09%09%09while (entCurrent != null && entCurrent.isValid()
 ) %7B%0A%09%09%09%09
 Docu
@@ -1501,114 +1501,185 @@
 %09%09%09%09
-Document docProcess = docNext;%0A%09%09%09%09docNext = viewAccess.getNextDocument(docNext);%0A%09%09%09%09docs.add(docProcess)
+docs.add(dbAccess.getDocumentByUNID(entCurrent.getUniversalID()));%0A%09%09%09%09ViewEntry nextEntry = vnav.getNext();%0A%09%09%09%09// recycle!%0A%09%09%09%09entCurrent.recycle();%0A%09%09%09%09entCurrent = nextEntry
 ;%0A%09%09
@@ -1960,16 +1960,22 @@
 esponse(
+final 
 Context 
@@ -1982,16 +1982,22 @@
 context,
+ final
  RoutePr
@@ -2019,16 +2019,25 @@
 ocessor,
+%0A%09%09%09final
  DataCon

refactor to use ViewNavigator to iterate over view content